A platform for community discussion. Free, open, simple.
FIX: AI bot submission in safari during IME composition (#40276)
David Battersby368551809838
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjsindex 5c5f12816a7..2f5960f5144 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs@@ -47,6 +47,9 @@ export default class AiBotConversations extends Component {@tracked creditStatus = null;@tracked selectedLlmId = null;@tracked uploads = trackedArray();++ shiftHeldOnEnter = false;+// Don't track this directly - we'll get it from uppyUploadtextarea = null;@@ -223,11 +226,24 @@ export default class AiBotConversations extends Component {if (event.target.tagName !== "TEXTAREA") {return;}- if (event.key === "Enter" && !event.shiftKey && !event.isComposing) {- event.preventDefault();- event.stopPropagation();- this.prepareAndSubmitToBot();+ if (event.key === "Enter") {+ this.shiftHeldOnEnter = event.shiftKey;+ }+ }++ @action+ handleBeforeInput(event) {+ // Real Enter on a textarea inserts a line break; IME-confirming Enter+ // inserts composition text. Reading inputType is deterministic across+ // browsers regardless of compositionend/keydown ordering quirks.+ const shiftHeld = this.shiftHeldOnEnter;+ this.shiftHeldOnEnter = false;+ if (event.inputType !== "insertLineBreak" || shiftHeld) {+ return;}+ event.preventDefault();+ event.stopPropagation();+ this.prepareAndSubmitToBot();}@action@@ -412,6 +428,7 @@ export default class AiBotConversations extends Component {{{didInsert this.setTextArea}}{{on "input" this.updateInputValue}}{{on "keydown" this.handleKeyDown}}+ {{on "beforeinput" this.handleBeforeInput}}id="ai-bot-conversations-input"autofocus={{unless this.isSubmitDisabled "true"}}placeholder={{i18n "discourse_ai.ai_bot.conversations.placeholder"}}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-helper-custom-prompt.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-helper-custom-prompt.gjsindex a660fc93458..4396c7dd204 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-helper-custom-prompt.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-helper-custom-prompt.gjs@@ -9,19 +9,19 @@ import { i18n } from "discourse-i18n";export default class AiHelperCustomPrompt extends Component {@action- sendInput(event) {- if (event.key !== "Enter" || event.isComposing) {+ handleSubmit(event) {+ event.preventDefault();+ if (!this.args.value?.length) {return;}- return this.args.submit(this.args.promptArgs);+ this.args.submit(this.args.promptArgs);}<template>- <div class="ai-custom-prompt">+ <form class="ai-custom-prompt" {{on "submit" this.handleSubmit}}><input{{on "input" (withEventValue (fn (mut @value)))}}- {{on "keydown" this.sendInput}}value={{@value}}placeholder={{i18n"discourse_ai.ai_helper.context_menu.custom_prompt.placeholder"@@ -37,6 +37,6 @@ export default class AiHelperCustomPrompt extends Component {@disabled={{not @value.length}}class="ai-custom-prompt__submit btn-primary"/>- </div>+ </form></template>}diff --git a/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-conversations-test.js b/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-conversations-test.jsindex bde68818ae6..6afac86c5ce 100644--- a/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-conversations-test.js+++ b/plugins/discourse-ai/test/javascripts/acceptance/ai-bot-conversations-test.js@@ -1,7 +1,9 @@-import { fillIn, triggerKeyEvent, visit } from "@ember/test-helpers";+import { fillIn, triggerEvent, visit } from "@ember/test-helpers";import { test } from "qunit";import { acceptance } from "discourse/tests/helpers/qunit-helpers";+const INPUT = "#ai-bot-conversations-input";+acceptance("AI Bot - Conversations IME handling", function (needs) {let postRequests = 0;@@ -24,12 +26,12 @@ acceptance("AI Bot - Conversations IME handling", function (needs) {});needs.pretender((server, helper) => {- server.get("/discourse-ai/ai-bot/conversations.json", () => {- return helper.response({+ server.get("/discourse-ai/ai-bot/conversations.json", () =>+ helper.response({conversations: [],meta: { has_more: false },- });- });+ })+ );server.post("/posts.json", () => {postRequests += 1;@@ -40,21 +42,43 @@ acceptance("AI Bot - Conversations IME handling", function (needs) {post_url: "/t/ai-conversation/1/1",});});++ server.get("/t/:slug/:id.json", () => helper.response({}));});- test("does not submit when Enter is pressed during IME composition", async function (assert) {- postRequests = 0;+ needs.hooks.beforeEach(() => (postRequests = 0));+ async function prepareDraft() {await visit("/discourse-ai/ai-bot/conversations");- await fillIn(- "#ai-bot-conversations-input",- "これはテスト入力として十分長い文章です"- );+ await fillIn(INPUT, "これはテスト入力として十分長い文章です");+ }++ test("Enter submits the message", async function (assert) {+ await prepareDraft();++ await triggerEvent(INPUT, "keydown", { key: "Enter" });+ await triggerEvent(INPUT, "beforeinput", { inputType: "insertLineBreak" });++ assert.strictEqual(postRequests, 1, "submitted once");+ });++ test("Shift+Enter does not submit", async function (assert) {+ await prepareDraft();++ await triggerEvent(INPUT, "keydown", { key: "Enter", shiftKey: true });+ await triggerEvent(INPUT, "beforeinput", { inputType: "insertLineBreak" });++ assert.strictEqual(postRequests, 0, "did not submit");+ });++ test("IME-confirming Enter does not submit", async function (assert) {+ await prepareDraft();- await triggerKeyEvent("#ai-bot-conversations-input", "keydown", "Enter", {- isComposing: true,+ await triggerEvent(INPUT, "keydown", { key: "Enter" });+ await triggerEvent(INPUT, "beforeinput", {+ inputType: "insertCompositionText",});- assert.strictEqual(postRequests, 0, "does not request /posts.json");+ assert.strictEqual(postRequests, 0, "did not submit");});});diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-helper-custom-prompt-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-helper-custom-prompt-test.gjsindex 4f414442245..1c83d2ef76c 100644--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-helper-custom-prompt-test.gjs+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-helper-custom-prompt-test.gjs@@ -1,5 +1,5 @@import { tracked } from "@glimmer/tracking";-import { render, triggerKeyEvent } from "@ember/test-helpers";+import { render, triggerEvent } from "@ember/test-helpers";import { module, test } from "qunit";import { setupRenderingTest } from "discourse/tests/helpers/component-test";import AiHelperCustomPrompt from "discourse/plugins/discourse-ai/discourse/components/ai-helper-custom-prompt";@@ -11,37 +11,31 @@ class TestState {module("Integration | Component | ai-helper-custom-prompt", function (hooks) {setupRenderingTest(hooks);- test("does not submit when Enter is pressed during IME composition", async function (assert) {- let submitted = false;- const submit = () => (submitted = true);+ async function renderPrompt(submit, initialValue = "test") {const state = new TestState();-+ state.value = initialValue;await render(<template><AiHelperCustomPrompt @value={{state.value}} @submit={{submit}} /></template>);+ }- await triggerKeyEvent(".ai-custom-prompt__input", "keydown", "Enter", {- isComposing: true,- });+ test("submitting the form calls @submit", async function (assert) {+ let submitted = 0;+ await renderPrompt(() => (submitted += 1));- assert.false(submitted, "does not submit during IME composition");- });+ await triggerEvent(".ai-custom-prompt", "submit");- test("submits when Enter is pressed outside of IME composition", async function (assert) {- let submitted = false;- const submit = () => (submitted = true);- const state = new TestState();+ assert.strictEqual(submitted, 1, "called @submit once");+ });- await render(- <template>- <AiHelperCustomPrompt @value={{state.value}} @submit={{submit}} />- </template>- );+ test("submit is skipped when the input is empty", async function (assert) {+ let submitted = 0;+ await renderPrompt(() => (submitted += 1), "");- await triggerKeyEvent(".ai-custom-prompt__input", "keydown", "Enter");+ await triggerEvent(".ai-custom-prompt", "submit");- assert.true(submitted, "submits on Enter without IME composition");+ assert.strictEqual(submitted, 0, "did not call @submit");});});