diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js b/assets/javascripts/discourse/initializers/custom-wizard-edits.js index f944fe777..92e19a333 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js @@ -1,4 +1,5 @@ import { action } from "@ember/object"; +import TextareaEditor from "discourse/components/composer/textarea-editor"; import { ajax } from "discourse/lib/ajax"; import { popupAjaxError } from "discourse/lib/ajax-error"; import getUrl from "discourse/lib/get-url"; @@ -42,6 +43,34 @@ export default { } ); + // Core treats any editor other than its own TextareaEditor as the rich + // editor, so the wizard textarea made the MD/rich toggle start in the + // wrong state and need two clicks to reach the rich editor. + api.modifyClass( + "component:d-editor", + (Superclass) => + class extends Superclass { + get isRichEditorEnabled() { + if (this.editorComponent === CustomWizardTextareaEditor) { + return false; + } + return super.isRichEditorEnabled; + } + + @action + async toggleRichEditor() { + await super.toggleRichEditor(...arguments); + + if ( + this.wizardComposer && + this.editorComponent === TextareaEditor + ) { + this.editorComponent = CustomWizardTextareaEditor; + } + } + } + ); + api.modifyClass("component:d-editor", { pluginId: "custom-wizard", diff --git a/plugin.rb b/plugin.rb index b113e3548..4c00af195 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.18.6 +# version: 2.18.7 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/test/javascripts/acceptance/field-test.js b/test/javascripts/acceptance/field-test.js index 918d33f0d..05bbf634b 100644 --- a/test/javascripts/acceptance/field-test.js +++ b/test/javascripts/acceptance/field-test.js @@ -336,3 +336,24 @@ acceptance("Field | Tag submission", function (needs) { ); }); }); + +acceptance("Field | Composer editor mode", function (needs) { + needs.user(); + needs.pretender((server, helper) => { + server.get("/w/wizard.json", () => helper.response(allFieldsWizard)); + }); + + test("toggles to the rich editor in one click", async function (assert) { + const toggle = ".wizard-field.composer-field .composer-toggle-switch"; + await visit("/w/wizard"); + assert.dom(toggle).hasAria("checked", "false", "starts in markdown"); + + await click(toggle); + assert.dom(toggle).hasAria("checked", "true"); + assert.dom(".wizard-field.composer-field .ProseMirror").exists(); + + await click(toggle); + assert.dom(toggle).hasAria("checked", "false"); + assert.dom(".wizard-field.composer-field textarea.d-editor-input").exists(); + }); +});