A platform for community discussion. Free, open, simple.
FIX: improvements to keyboard shortcuts from toolbar API (#33290)
Keegan George38cf7cdbeae8
diff --git a/app/assets/javascripts/discourse/app/components/d-editor.gjs b/app/assets/javascripts/discourse/app/components/d-editor.gjsindex 86d4d1201e4..27d8da2f371 100644--- a/app/assets/javascripts/discourse/app/components/d-editor.gjs+++ b/app/assets/javascripts/discourse/app/components/d-editor.gjs@@ -134,7 +134,9 @@ export default class DEditor extends Component {if (customAction) {const toolbarEvent = this.newToolbarEvent();- customAction(toolbarEvent);+ if (!button.condition || button.condition(toolbarEvent)) {+ customAction(toolbarEvent);+ }} else {button.action(button);}@@ -159,8 +161,10 @@ export default class DEditor extends Component {}});- keymap["tab"] = () => this.textManipulation.indentSelection("right");- keymap["shift+tab"] = () => this.textManipulation.indentSelection("left");+ // indentSelection returns true if the selection was indented+ // itsatrap expects the return value to be false to prevent default+ keymap["tab"] = () => !this.textManipulation.indentSelection("right");+ keymap["shift+tab"] = () => !this.textManipulation.indentSelection("left");if (this.siteSettings.rich_editor) {keymap["ctrl+m"] = () => this.toggleRichEditor();}diff --git a/app/assets/javascripts/discourse/app/lib/textarea-text-manipulation.js b/app/assets/javascripts/discourse/app/lib/textarea-text-manipulation.jsindex 900382d2189..b66aa49bd8c 100644--- a/app/assets/javascripts/discourse/app/lib/textarea-text-manipulation.js+++ b/app/assets/javascripts/discourse/app/lib/textarea-text-manipulation.js@@ -727,6 +727,8 @@ export default class TextareaTextManipulation {this.replaceText(value, newValue, { skipNewSelection: true });this.selectText(this.value.indexOf(newValue), newValue.length);}++ return true;}@binddiff --git a/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs b/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjsindex 411348f5b90..3b7371d4971 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs+++ b/app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs@@ -121,7 +121,7 @@ export default class ProsemirrorEditor extends Component {.split("+").map((word) => replacements[word] ?? word).join("-");- result[pmKey] = value;+ result[pmKey] = () => !(value() ?? false);}return result;}diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-test.jsindex 85912e489ed..2b3ffb6c24b 100644--- a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js@@ -1584,6 +1584,62 @@ import { i18n } from "discourse-i18n";assert.dom(editor).hasValue(":smile: from keyboard");});+ test("buttons with conditions should not trigger shortcut actions when condition is false", async function (assert) {+ withPluginApi((api) => {+ api.onToolbarCreate((toolbar) => {+ toolbar.addButton({+ id: "smile",+ group: "extras",+ icon: "far-face-smile",+ shortcut: "ALT+S",+ shortcutAction: (toolbarEvent) => {+ toolbarEvent.addText(":smile: from keyboard");+ },+ condition: () => false,+ });+ });+ });++ await visit("/t/internationalization-localization/280");+ await click(".post-controls button.reply");++ const editor = find(".d-editor-input");+ await triggerKeyEvent(".d-editor-input", "keydown", "S", {+ altKey: true,+ ...metaModifier,+ });++ assert.dom(editor).hasValue("");+ });++ test("buttons with conditions should trigger shortcut actions when condition is true", async function (assert) {+ withPluginApi((api) => {+ api.onToolbarCreate((toolbar) => {+ toolbar.addButton({+ id: "smile",+ group: "extras",+ icon: "far-face-smile",+ shortcut: "ALT+S",+ shortcutAction: (toolbarEvent) => {+ toolbarEvent.addText(":smile: from keyboard");+ },+ condition: () => true,+ });+ });+ });++ await visit("/t/internationalization-localization/280");+ await click(".post-controls button.reply");++ const editor = find(".d-editor-input");+ await triggerKeyEvent(".d-editor-input", "keydown", "S", {+ altKey: true,+ ...metaModifier,+ });++ assert.dom(editor).hasValue(":smile: from keyboard");+ });+test("buttons can be added conditionally", async function (assert) {withPluginApi("0", (api) => {api.addComposerToolbarPopupMenuOption({