From 2829b670f988a281e41ce6f1aeabf24629592dd0 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Fri, 23 Aug 2024 11:44:27 +0200 Subject: [PATCH] DEV: adds support for replaceText in toolbarEvent (#28512) This function was available in textarea manipulation mixin, but not exposed as other functions like addText, applySurround, ... --- .../discourse/app/components/d-editor.js | 2 ++ .../integration/components/d-editor-test.js | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/app/assets/javascripts/discourse/app/components/d-editor.js b/app/assets/javascripts/discourse/app/components/d-editor.js index c1fc8d1f6f4..7e7eac68917 100644 --- a/app/assets/javascripts/discourse/app/components/d-editor.js +++ b/app/assets/javascripts/discourse/app/components/d-editor.js @@ -812,6 +812,8 @@ export default Component.extend(TextareaTextManipulation, { addText: (text) => this.addText(selected, text), getText: () => this.value, toggleDirection: () => this._toggleDirection(), + replaceText: (oldVal, newVal, opts) => + this.replaceText(oldVal, newVal, opts), }; }, diff --git a/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js b/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js index 7cb36b83451..418571ba9d4 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js @@ -646,6 +646,29 @@ third line` assert.strictEqual(textarea.getAttribute("dir"), "rtl"); }); + test("toolbar event supports replaceText", async function (assert) { + withPluginApi("0.1", (api) => { + api.onToolbarCreate((toolbar) => { + toolbar.addButton({ + id: "replace-text", + icon: "times", + group: "extras", + action: () => { + toolbar.context.newToolbarEvent().replaceText("hello", "goodbye"); + }, + condition: () => true, + }); + }); + }); + + this.value = "hello"; + + await render(hbs``); + await click("button.replace-text"); + + assert.strictEqual(this.value, "goodbye"); + }); + testCase( `doesn't jump to bottom with long text`, async function (assert, textarea) {