Skip to content

A platform for community discussion. Free, open, simple.

FIX: uses text selection when using hide details (#33049)

Joffrey JAFFEUX8873a33b7ad5
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/lib/text-manipulation.js b/app/assets/javascripts/discourse/app/static/prosemirror/lib/text-manipulation.js
index d05fffd1045..5010f3fae95 100644
--- a/app/assets/javascripts/discourse/app/static/prosemirror/lib/text-manipulation.js
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/lib/text-manipulation.js
@@ -105,8 +105,18 @@ export default class ProsemirrorTextManipulation {
return;
}
- const text = head + i18n(`composer.${exampleKey}`) + tail;
- const doc = this.convertFromMarkdown(text);
+ const { state } = this.view;
+ const { from, to, empty } = state.selection;
+
+ let text;
+ if (empty) {
+ text = i18n(`composer.${exampleKey}`);
+ } else {
+ const selectedFragment = state.doc.slice(from, to).content;
+ text = this.convertToMarkdown(selectedFragment);
+ }
+
+ const doc = this.convertFromMarkdown(head + text + tail);
this.view.dispatch(
this.view.state.tr.replaceWith(sel.start, sel.end, doc.content.firstChild)
diff --git a/plugins/discourse-details/spec/system/details_spec.rb b/plugins/discourse-details/spec/system/details_spec.rb
new file mode 100644
index 00000000000..c21ccaadeb8
--- /dev/null
+++ b/plugins/discourse-details/spec/system/details_spec.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+describe "Details button", type: :system do
+ fab!(:admin)
+
+ let(:composer) { PageObjects::Components::Composer.new }
+ let(:rich) { composer.rich_editor }
+
+ context "with rich editor" do
+ before do
+ SiteSetting.rich_editor = true
+ sign_in(admin)
+ end
+
+ it "uses the text selection for content" do
+ visit("/new-topic")
+ composer.fill_content("test :+1:").toggle_rich_editor.select_all
+ find(".toolbar-popup-menu-options").click
+ find(".select-kit-row[data-name='Hide Details']").click
+ rich.click(x: 22, y: 30) # hack for pseudo element
+
+ expect(rich).to have_css(
+ "details img.emoji[src=\"/images/emoji/twitter/+1.png?v=#{Emoji::EMOJI_VERSION}\"]",
+ )
+ end
+ end
+end
diff --git a/spec/system/page_objects/components/composer.rb b/spec/system/page_objects/components/composer.rb
index cee4a14c6ba..774eaac24ac 100644
--- a/spec/system/page_objects/components/composer.rb
+++ b/spec/system/page_objects/components/composer.rb
@@ -274,11 +274,7 @@ module PageObjects
end
def select_all
- execute_script(<<~JS, text)
- const composer = document.querySelector("#{COMPOSER_ID} .d-editor-input");
- composer.focus();
- composer.setSelectionRange(0, composer.value.length);
- JS
+ find(COMPOSER_INPUT_SELECTOR).send_keys([PLATFORM_KEY_MODIFIER, "a"])
end
def select_range(start_index, length)