Skip to content

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

FIX: preserve selection formatting in link-insertion modal (#39374)

Régis Hanol7bb363b4cb87
diff --git a/frontend/discourse/app/components/modal/upsert-hyperlink.gjs b/frontend/discourse/app/components/modal/upsert-hyperlink.gjs
index 58fa2206adc..a6852af8481 100644
--- a/frontend/discourse/app/components/modal/upsert-hyperlink.gjs
+++ b/frontend/discourse/app/components/modal/upsert-hyperlink.gjs
@@ -29,6 +29,14 @@ export default class UpsertHyperlink extends Component {
cancel(this.#debounced);
}
+ get wrapsSelection() {
+ return this.args.model.hasSelection && !this.args.model.editing;
+ }
+
+ get showLinkTextField() {
+ return !this.wrapsSelection;
+ }
+
@cached
get data() {
return {
@@ -63,7 +71,7 @@ export default class UpsertHyperlink extends Component {
});
this.selectedRow = -1;
- document.querySelector("input.link-text").focus();
+ document.querySelector("input.link-text")?.focus();
}
async triggerSearch(linkUrl) {
@@ -172,9 +180,13 @@ export default class UpsertHyperlink extends Component {
return;
}
- const sel = this.args.model.toolbarEvent.selected;
- const linkText = data.linkText || sel.value || origLink || "";
- this.args.model.toolbarEvent.addText(`[${linkText.trim()}](${linkUrl})`);
+ if (this.wrapsSelection) {
+ this.args.model.toolbarEvent.applyLink(linkUrl);
+ } else {
+ const sel = this.args.model.toolbarEvent.selected;
+ const linkText = data.linkText || sel?.value || origLink || "";
+ this.args.model.toolbarEvent.addText(`[${linkText.trim()}](${linkUrl})`);
+ }
this.args.closeModal();
}
@@ -264,18 +276,20 @@ export default class UpsertHyperlink extends Component {
</div>
{{/if}}
- <form.Field
- @name="linkText"
- @type="input"
- @title={{i18n "composer.link_text_label"}}
- @format="full"
- as |field|
- >
- <field.Control
- placeholder={{i18n "composer.link_optional_text"}}
- class="link-text"
- />
- </form.Field>
+ {{#if this.showLinkTextField}}
+ <form.Field
+ @name="linkText"
+ @type="input"
+ @title={{i18n "composer.link_text_label"}}
+ @format="full"
+ as |field|
+ >
+ <field.Control
+ placeholder={{i18n "composer.link_optional_text"}}
+ class="link-text"
+ />
+ </form.Field>
+ {{/if}}
</Form>
</div>
</:body>
diff --git a/frontend/discourse/app/lib/textarea-text-manipulation.js b/frontend/discourse/app/lib/textarea-text-manipulation.js
index 37406e482d4..8a0aa905b64 100644
--- a/frontend/discourse/app/lib/textarea-text-manipulation.js
+++ b/frontend/discourse/app/lib/textarea-text-manipulation.js
@@ -349,6 +349,15 @@ export default class TextareaTextManipulation {
schedule("afterRender", this, this.blurAndFocus);
}
+ applyLink(url) {
+ const sel = this.getSelected();
+ if (sel.start === sel.end) {
+ return;
+ }
+ this._insertAt(sel.start, sel.end, `[${sel.value}](${url})`);
+ this.blurAndFocus();
+ }
+
addText(sel, text, options) {
if (options && options.ensureSpace) {
if ((sel.pre + "").length > 0) {
diff --git a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js
index 21c8177a3a5..a82b1d35199 100644
--- a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js
+++ b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js
@@ -181,6 +181,18 @@ export default class ProsemirrorTextManipulation {
);
}
+ applyLink(url) {
+ const { state, dispatch } = this.view;
+ const { from, to, empty } = state.selection;
+ if (empty) {
+ return;
+ }
+ dispatch(
+ state.tr.addMark(from, to, state.schema.marks.link.create({ href: url }))
+ );
+ this.focus();
+ }
+
addText(sel, text) {
const doc = this.convertFromMarkdown(text);
diff --git a/frontend/discourse/app/ui-kit/d-editor.gjs b/frontend/discourse/app/ui-kit/d-editor.gjs
index 1a733a75198..f26e8be70ac 100644
--- a/frontend/discourse/app/ui-kit/d-editor.gjs
+++ b/frontend/discourse/app/ui-kit/d-editor.gjs
@@ -552,6 +552,7 @@ export default class DEditor extends Component {
this.textManipulation.applyHeading(selected, level, exampleKey),
formatCode: () => this.textManipulation.formatCode(),
addText: (text) => this.textManipulation.addText(selected, text),
+ applyLink: (url) => this.textManipulation.applyLink(url),
getText: () => this.value,
toggleDirection: () => this.textManipulation.toggleDirection(),
replaceText: (oldVal, newVal, opts) =>
@@ -565,16 +566,13 @@ export default class DEditor extends Component {
return;
}
- let linkText = "";
this._lastSel = toolbarEvent.selected;
-
- if (this._lastSel) {
- linkText = this._lastSel.value;
- }
+ const hasSelection =
+ !!this._lastSel && this._lastSel.start !== this._lastSel.end;
this.modal.show(UpsertHyperlink, {
model: {
- linkText,
+ hasSelection,
toolbarEvent,
},
});
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-composer.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-composer.gjs
index a12d1a37bf4..4d8a9418f4f 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-composer.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-composer.gjs
@@ -425,12 +425,14 @@ export default class ChatComposer extends Component {
}
const selected = this.composer.textarea.getSelected("", { lineVal: true });
- const linkText = selected?.value;
+ const hasSelection = !!selected && selected.start !== selected.end;
this.modal.show(UpsertHyperlink, {
model: {
- linkText,
+ hasSelection,
toolbarEvent: {
+ selected,
addText: (text) => this.composer.textarea.addText(selected, text),
+ applyLink: (url) => this.composer.textarea.applyLink(url),
},
},
});
diff --git a/plugins/chat/assets/javascripts/discourse/lib/textarea-interactor.js b/plugins/chat/assets/javascripts/discourse/lib/textarea-interactor.js
index 7d8d03af8d1..4883a1bd6b4 100644
--- a/plugins/chat/assets/javascripts/discourse/lib/textarea-interactor.js
+++ b/plugins/chat/assets/javascripts/discourse/lib/textarea-interactor.js
@@ -116,6 +116,11 @@ export default class TextareaInteractor extends EmberObject {
return this.textManipulation.addText(...arguments);
}
+ @bind
+ applyLink() {
+ return this.textManipulation.applyLink(...arguments);
+ }
+
@bind
isInside() {
return this.textManipulation.isInside(...arguments);
diff --git a/spec/system/composer/prosemirror_links_spec.rb b/spec/system/composer/prosemirror_links_spec.rb
index 5de6b14ceb2..8b65bbf993e 100644
--- a/spec/system/composer/prosemirror_links_spec.rb
+++ b/spec/system/composer/prosemirror_links_spec.rb
@@ -204,6 +204,82 @@ describe "Composer - ProseMirror - Links" do
)
end
+ context "when inserting a link over an existing selection" do
+ it "only shows the URL field in the modal" do
+ open_composer
+ composer.type_content("some text")
+ composer.select_all
+ composer.click_toolbar_button("link")
+
+ expect(upsert_hyperlink_modal).to be_open
+ expect(upsert_hyperlink_modal).to have_no_link_text_field
+ end
+
+ it "wraps the selection with the link while preserving inline formatting" do
+ open_composer
+ composer.type_content("Example")
+ composer.select_all
+ composer.click_toolbar_button("bold")
+ heading_menu = composer.heading_menu
+ heading_menu.expand
+ heading_menu.option("[data-name='heading-2']").click
+ expect(rich).to have_css("h2 strong", text: "Example")
+
+ composer.select_all
+ composer.click_toolbar_button("link")
+ upsert_hyperlink_modal.fill_in_link_url("https://example.com")
+ upsert_hyperlink_modal.click_primary_button
+
+ expect(rich).to have_css("h2 strong a[href='https://example.com']", text: "Example")
+ composer.toggle_rich_editor
+ expect(composer).to have_value("## **[Example](https://example.com)**")
+ end
+
+ it "does not leak heading markdown into the resulting link" do
+ open_composer
+ composer.type_content("Example")
+ composer.select_all
+ heading_menu = composer.heading_menu
+ heading_menu.expand
+ heading_menu.option("[data-name='heading-2']").click
+ expect(rich).to have_css("h2", text: "Example")
+
+ composer.select_all
+ composer.click_toolbar_button("link")
+ upsert_hyperlink_modal.fill_in_link_url("https://example.com")
+ upsert_hyperlink_modal.click_primary_button
+
+ expect(rich).to have_css("h2 a[href='https://example.com']", text: "Example")
+ composer.toggle_rich_editor
+ expect(composer).to have_value("## [Example](https://example.com)")
+ end
+ end
+
+ context "in markdown mode, when inserting a link over an existing selection" do
+ it "only shows the URL field and wraps the selection as-is" do
+ open_composer
+ composer.toggle_rich_editor
+ composer.type_content("`code word`")
+ composer.select_all
+ composer.click_toolbar_button("link")
+
+ expect(upsert_hyperlink_modal).to be_open
+ expect(upsert_hyperlink_modal).to have_no_link_text_field
+ upsert_hyperlink_modal.fill_in_link_url("https://example.com")
+ upsert_hyperlink_modal.click_primary_button
+
+ expect(composer).to have_value("[`code word`](https://example.com)")
+ end
+ end
+
+ it "shows both fields when inserting a link without a selection" do
+ open_composer
+ composer.click_toolbar_button("link")
+
+ expect(upsert_hyperlink_modal).to be_open
+ expect(upsert_hyperlink_modal).to have_link_text_field
+ end
+
it "does not infinite loop on link rewrite" do
with_logs do |logger|
open_composer
diff --git a/spec/system/page_objects/modals/upsert_hyperlink.rb b/spec/system/page_objects/modals/upsert_hyperlink.rb
index 28e641e5e0c..0121bb3ca09 100644
--- a/spec/system/page_objects/modals/upsert_hyperlink.rb
+++ b/spec/system/page_objects/modals/upsert_hyperlink.rb
@@ -26,6 +26,14 @@ module PageObjects
def link_url_value
find(LINK_URL_SELECTOR).value
end
+
+ def has_link_text_field?
+ has_css?(LINK_TEXT_SELECTOR)
+ end
+
+ def has_no_link_text_field?
+ has_no_css?(LINK_TEXT_SELECTOR)
+ end
end
end
end