A platform for community discussion. Free, open, simple.
UX: keep existing marks when pasting a url to make a link in rich editor (#31814)
Renato Atiliob1b2b8e4c8ae
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.jsindex ffc1523f535..b7e26789ecf 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.js@@ -112,7 +112,7 @@ const extension = {return { href: match[2], title: match[3] };}),- plugins: ({ pmState: { Plugin }, pmModel: { Slice, Fragment }, utils }) =>+ plugins: ({ pmState: { Plugin }, utils }) =>new Plugin({props: {// Auto-linkify plain-text pasted URLs@@ -121,59 +121,60 @@ const extension = {return;}- const marks = $context.marks();- const selectedText = view.state.doc.textBetween(- view.state.selection.from,- view.state.selection.to- );- const textNode = view.state.schema.text(selectedText, [- ...marks,- view.state.schema.marks.link.create({ href: text }),- ]);- return new Slice(Fragment.from(textNode), 0, 0);+ return addLinkMark(view, text);},// Auto-linkify rich content with a single text node that is a URLtransformPasted(paste, view) {+ let textContent = null;++ if (paste.content.childCount === 1) {+ if (paste.content.firstChild.isText) {+ // Direct text node+ textContent = paste.content.firstChild.text;+ } else if (+ paste.content.firstChild.type.name === "paragraph" &&+ paste.content.firstChild.childCount === 1 &&+ paste.content.firstChild.firstChild.isText+ ) {+ // Text inside paragraph+ textContent = paste.content.firstChild.firstChild.text;+ }+ }+if (- paste.content.childCount === 1 &&- paste.content.firstChild.isText &&- !paste.content.firstChild.marks.some(+ !textContent ||+ paste.content.firstChild.firstChild.marks.some((mark) => mark.type.name === "link")) {- const matches = utils- .getLinkify()- .match(paste.content.firstChild.text);- const isFullMatch =- matches &&- matches.length === 1 &&- matches[0].raw === paste.content.firstChild.text;-- if (!isFullMatch) {- return paste;- }+ return paste;+ }- const marks = view.state.selection.$head.marks();- const originalText = view.state.doc.textBetween(- view.state.selection.from,- view.state.selection.to- );-- const textNode = view.state.schema.text(originalText, [- ...marks,- view.state.schema.marks.link.create({- href: paste.content.firstChild.text,- }),- ]);- paste = new Slice(Fragment.from(textNode), 0, 0);+ const matches = utils.getLinkify().match(textContent);+ const isFullMatch =+ matches && matches.length === 1 && matches[0].raw === textContent;++ if (!isFullMatch) {+ return paste;}- return paste;++ return addLinkMark(view, textContent);},},}),};+function addLinkMark(view, href) {+ const { from, to } = view.state.selection;+ const linkMark = view.state.schema.marks.link.create({ href });++ const tr = view.state.tr;+ tr.addMark(from, to, linkMark);++ return tr.doc.slice(from, to);+}+function isPlainURL(link, parent, index) {if (link.attrs.title || !/^\w+:/.test(link.attrs.href)) {return false;diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rbindex a0370cdee92..460fbaef18a 100644--- a/spec/system/composer/prosemirror_editor_spec.rb+++ b/spec/system/composer/prosemirror_editor_spec.rb@@ -464,6 +464,24 @@ describe "Composer - ProseMirror editor", type: :system do"img[src$='image.png'][alt='alt text'][data-orig-src='upload://1234567890']",)end++ it "respects existing marks when pasting a url to make a link" do+ cdp.allow_clipboard+ open_composer_and_toggle_rich_editor+ cdp.write_clipboard("`code` **bold** *italic*")+ page.send_keys([PLATFORM_KEY_MODIFIER, "v"])+ page.execute_script("document.execCommand('selectAll',false,null)")+ cdp.write_clipboard("www.google.com")+ page.send_keys([PLATFORM_KEY_MODIFIER, "v"])++ expect(rich).to have_css("code", text: "code")+ expect(rich).to have_css("strong", text: "bold")+ expect(rich).to have_css("em", text: "italic")++ composer.toggle_rich_editor++ expect(composer).to have_value("[`code` **bold** *italic*](www.google.com)")+ endenddescribe "trailing paragraph" do