A platform for community discussion. Free, open, simple.
FEATURE: Improve ProseMirror list handling for multi-line text (#38812)
Martin Brennan85043a4b26f8
diff --git a/frontend/discourse/app/lib/composer/rich-editor-extensions.js b/frontend/discourse/app/lib/composer/rich-editor-extensions.jsindex 9c1006dc55d..21af9c81031 100644--- a/frontend/discourse/app/lib/composer/rich-editor-extensions.js+++ b/frontend/discourse/app/lib/composer/rich-editor-extensions.js@@ -23,6 +23,8 @@ import { waitForPromise } from "@ember/test-waiters";* @typedef {Object} EditorInstanceUtils* @property {(markdown: string) => import("prosemirror-model").Node} convertFromMarkdown* @property {(doc: import("prosemirror-model").Node) => string} convertToMarkdown+ * @property {(text: string) => string[]} splitNonEmptyLines+ * @property {(schema: import("prosemirror-model").Schema, listType: import("prosemirror-model").NodeType, lines: string[]) => import("prosemirror-model").Node} buildListNode* @property {() => void} toggleRichEditor*/diff --git a/frontend/discourse/app/static/prosemirror/components/prosemirror-editor.gjs b/frontend/discourse/app/static/prosemirror/components/prosemirror-editor.gjsindex 5fd2cd7aa15..98e0fc14bf7 100644--- a/frontend/discourse/app/static/prosemirror/components/prosemirror-editor.gjs+++ b/frontend/discourse/app/static/prosemirror/components/prosemirror-editor.gjs@@ -99,6 +99,8 @@ export default class ProsemirrorEditor extends Component {...utils,convertFromMarkdown: this.convertFromMarkdown,convertToMarkdown: this.convertToMarkdown,+ splitNonEmptyLines: this.splitNonEmptyLines,+ buildListNode: this.buildListNode,toggleRichEditor: this.args.toggleRichEditor,},schema: this.schema,@@ -262,6 +264,8 @@ export default class ProsemirrorEditor extends Component {view: this.view,convertFromMarkdown: this.convertFromMarkdown,convertToMarkdown: this.convertToMarkdown,+ splitNonEmptyLines: this.splitNonEmptyLines,+ buildListNode: this.buildListNode,commands: buildCommands(this.extensions, params, this.view),customState: buildCustomState(this.extensions, params),});@@ -303,6 +307,29 @@ export default class ProsemirrorEditor extends Component {}}+ @bind+ splitNonEmptyLines(text) {+ return text.split(/\r?\n/).filter((line) => line.trim().length > 0);+ }++ @bind+ buildListNode(schema, listType, lines) {+ const listItems = lines.map((line) =>+ schema.nodes.list_item.create(null, [+ schema.nodes.paragraph.create(+ null,+ line.length > 0 ? schema.text(line) : undefined+ ),+ ])+ );++ if (typeof listType === "string") {+ listType = schema.nodes[listType];+ }++ return listType.create(null, listItems);+ }+@bindconvertFromValue() {const value = this.args.value ?? "";diff --git a/frontend/discourse/app/static/prosemirror/extensions/markdown-paste.js b/frontend/discourse/app/static/prosemirror/extensions/markdown-paste.jsindex 36ee21411d0..67c4304f126 100644--- a/frontend/discourse/app/static/prosemirror/extensions/markdown-paste.js+++ b/frontend/discourse/app/static/prosemirror/extensions/markdown-paste.js@@ -3,11 +3,65 @@ const extension = {plugins({pmState: { Plugin },pmModel: { Fragment, Slice },- utils: { convertFromMarkdown },+ utils: { convertFromMarkdown, splitNonEmptyLines, buildListNode },}) {+ function buildListSlice(schema, listType, lines) {+ const listNode = buildListNode(schema, listType, lines);+ return Slice.maxOpen(Fragment.from(listNode));+ }++ function getEmptyListPasteContext(view) {+ const { selection, schema } = view.state;++ if (!selection.empty) {+ return null;+ }++ const { $from } = selection;++ for (let depth = $from.depth; depth > 1; depth--) {+ const node = $from.node(depth);++ if (node.type !== schema.nodes.list_item) {+ continue;+ }++ const parentList = $from.node(depth - 1);+ const isSupportedList =+ parentList.type === schema.nodes.bullet_list ||+ parentList.type === schema.nodes.ordered_list;+ const isEmptyParagraph =+ node.childCount === 1 &&+ node.firstChild?.type === schema.nodes.paragraph &&+ node.firstChild.content.size === 0;++ if (isSupportedList && isEmptyParagraph && $from.parentOffset === 0) {+ return { listType: parentList.type.name };+ }++ return null;+ }++ return null;+ }+return new Plugin({props: {- clipboardTextParser(text) {+ clipboardTextParser(text, $context, plain, view) {+ const listContext = view && getEmptyListPasteContext(view);++ if (listContext) {+ const lines = splitNonEmptyLines(text);++ if (lines.length > 1) {+ return buildListSlice(+ view.state.schema,+ listContext.listType,+ lines+ );+ }+ }+const doc = convertFromMarkdown(text);return Slice.maxOpen(Fragment.from(doc.content));},diff --git a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.jsindex caeea30da7f..6ad08aa8179 100644--- a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js+++ b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js@@ -17,6 +17,24 @@ import DAutocompleteModifier from "discourse/modifiers/d-autocomplete";import { i18n } from "discourse-i18n";import { hasMark, inNode, isNodeActive } from "./plugin-utils";+function isPlainTextFragment(fragment, schema) {+ return fragment.content.every((node) => {+ if (node.isText) {+ return node.marks.length === 0;+ }++ if (node.type === schema.nodes.hard_break) {+ return true;+ }++ if (node.type === schema.nodes.paragraph) {+ return isPlainTextFragment(node.content, schema);+ }++ return false;+ });+}+/*** @typedef {import("discourse/lib/composer/text-manipulation").TextManipulation} TextManipulation* @typedef {import("discourse/lib/composer/text-manipulation").AutocompleteHandler} AutocompleteHandler@@ -40,6 +58,8 @@ export default class ProsemirrorTextManipulation {state = trackedObject({});convertFromMarkdown;convertToMarkdown;+ splitNonEmptyLines;+ buildListNode;constructor(owner,@@ -48,6 +68,8 @@ export default class ProsemirrorTextManipulation {view,convertFromMarkdown,convertToMarkdown,+ splitNonEmptyLines,+ buildListNode,commands,customState,}@@ -57,6 +79,8 @@ export default class ProsemirrorTextManipulation {this.view = view;this.convertFromMarkdown = convertFromMarkdown;this.convertToMarkdown = convertToMarkdown;+ this.splitNonEmptyLines = splitNonEmptyLines;+ this.buildListNode = buildListNode;this.commands = commands;this.customState = customState;@@ -208,6 +232,35 @@ export default class ProsemirrorTextManipulation {return null;};+ const replaceSelectionWithList = (targetType) => {+ const { state } = this.view;+ const selectedContent = state.selection.content().content;++ if (!isPlainTextFragment(selectedContent, this.schema)) {+ return false;+ }++ const selectedText = state.doc.textBetween(+ state.selection.from,+ state.selection.to,+ "\n",+ "\n"+ );+ const lines = this.splitNonEmptyLines(selectedText);++ if (lines.length <= 1) {+ return false;+ }++ const listNode = this.buildListNode(this.schema, targetType, lines);++ this.view.dispatch(+ state.tr.replaceSelectionWith(listNode).scrollIntoView()+ );++ return true;+ };+if (exampleKey === "list_item") {const targetType =head === "* "@@ -242,6 +295,11 @@ export default class ProsemirrorTextManipulation {};}} else {+ if (replaceSelectionWithList(targetType)) {+ this.focus();+ return;+ }+// Not in a list - wrap in the target typecommand = wrapInList(targetType);}diff --git a/spec/system/composer/prosemirror_pasting_spec.rb b/spec/system/composer/prosemirror_pasting_spec.rbindex b78f03d6211..07f2b96c4fd 100644--- a/spec/system/composer/prosemirror_pasting_spec.rb+++ b/spec/system/composer/prosemirror_pasting_spec.rb@@ -195,6 +195,57 @@ describe "Composer - ProseMirror - Pasting content" doexpect(rich).to have_css("a", text: "lin")end+ it "continues a bullet list when pasting multi-line text into an empty list item" do+ cdp.allow_clipboard+ open_composer++ composer.type_content("* ")+ cdp.copy_paste("apple\nbanana\ncoconut")++ expect(rich).to have_css("ul li", count: 3)+ expect(rich).to have_css("ul li", text: "apple")+ expect(rich).to have_css("ul li", text: "banana")+ expect(rich).to have_css("ul li", text: "coconut")+ end++ it "continues an ordered list when pasting multi-line text into an empty list item" do+ cdp.allow_clipboard+ open_composer++ composer.type_content("1. ")+ cdp.copy_paste("apple\nbanana\ncoconut")++ expect(rich).to have_css("ol li", count: 3)+ expect(rich).to have_css("ol li", text: "apple")+ expect(rich).to have_css("ol li", text: "banana")+ expect(rich).to have_css("ol li", text: "coconut")+ end++ it "skips blank lines when continuing a list from pasted multi-line text" do+ cdp.allow_clipboard+ open_composer++ composer.type_content("* ")+ cdp.copy_paste("apple\n\nbanana")++ expect(rich).to have_css("ul li", count: 2)+ expect(rich).to have_css("ul li", text: "apple")+ expect(rich).to have_css("ul li", text: "banana")+ end++ it "keeps default paste behavior in a non-empty list item" do+ cdp.allow_clipboard+ open_composer++ composer.type_content("* existing")+ cdp.copy_paste("\napple\nbanana")++ expect(rich).to have_css("ul li", count: 1)+ expect(rich).to have_css("ul li", text: "existing")+ expect(rich).to have_css("ul li", text: "apple")+ expect(rich).to have_css("ul li", text: "banana")+ end+it "clears closed marks from stored marks when using markInputRule" doopen_composercomposer.type_content("[`something`](link) word")diff --git a/spec/system/composer/prosemirror_toolbar_spec.rb b/spec/system/composer/prosemirror_toolbar_spec.rbindex c1aab830402..3a302fb9969 100644--- a/spec/system/composer/prosemirror_toolbar_spec.rb+++ b/spec/system/composer/prosemirror_toolbar_spec.rb@@ -203,6 +203,40 @@ describe "Composer - ProseMirror - Toolbar" doexpect(rich).to have_css("ol li", text: "A list item")end++ it "splits multi-line plain text into bullet list items" do+ cdp.allow_clipboard+ open_composer++ cdp.copy_paste("apple\nbanana\ncoconut")+ composer.select_all++ list_menu = composer.list_menu+ list_menu.expand+ list_menu.option("[data-name='list-bullet']").click++ expect(rich).to have_css("ul li", count: 3)+ expect(rich).to have_css("ul li", text: "apple")+ expect(rich).to have_css("ul li", text: "banana")+ expect(rich).to have_css("ul li", text: "coconut")+ end++ it "splits multi-line plain text into ordered list items" do+ cdp.allow_clipboard+ open_composer++ cdp.copy_paste("apple\nbanana\ncoconut")+ composer.select_all++ list_menu = composer.list_menu+ list_menu.expand+ list_menu.option("[data-name='list-ordered']").click++ expect(rich).to have_css("ol li", count: 3)+ expect(rich).to have_css("ol li", text: "apple")+ expect(rich).to have_css("ol li", text: "banana")+ expect(rich).to have_css("ol li", text: "coconut")+ endenddescribe "heading toolbar" do