diff --git a/app/editor/extensions/PasteHandler.tsx b/app/editor/extensions/PasteHandler.tsx index 312a5ec6d2..754edb15fb 100644 --- a/app/editor/extensions/PasteHandler.tsx +++ b/app/editor/extensions/PasteHandler.tsx @@ -8,6 +8,7 @@ import { Plugin, PluginKey, TextSelection } from "prosemirror-state"; import { Decoration, DecorationSet } from "prosemirror-view"; import type { WidgetProps } from "@shared/editor/lib/Extension"; import Extension from "@shared/editor/lib/Extension"; +import { pasteIntoListItem } from "@shared/editor/commands/pasteIntoListItem"; import { codeLanguages } from "@shared/editor/lib/code"; import isMarkdown from "@shared/editor/lib/isMarkdown"; import normalizePastedMarkdown from "@shared/editor/lib/markdown/normalize"; @@ -63,7 +64,7 @@ export default class PasteHandler extends Extension { return false; }, }, - handlePaste: (view, event: ClipboardEvent) => { + handlePaste: (view, event: ClipboardEvent, slice: Slice) => { // Do nothing if the document isn't currently editable or there is no clipboard data if (!view.editable || !event.clipboardData) { return false; @@ -101,7 +102,7 @@ export default class PasteHandler extends Extension { // compatibility is to just use the HTML parser, regardless of // whether it "looks" like Markdown, see: outline/outline#2416 if (html?.includes("data-pm-slice")) { - return false; + return pasteIntoListItem(slice)(state, dispatch); } // If the HTML on the clipboard is from Claude then the best diff --git a/shared/editor/commands/pasteIntoListItem.test.ts b/shared/editor/commands/pasteIntoListItem.test.ts new file mode 100644 index 0000000000..f7ae1faf3d --- /dev/null +++ b/shared/editor/commands/pasteIntoListItem.test.ts @@ -0,0 +1,168 @@ +import type { Node } from "prosemirror-model"; +import { EditorState, TextSelection } from "prosemirror-state"; +import { bulletList, doc, p, schema } from "@shared/test/editor"; +import { pasteIntoListItem } from "./pasteIntoListItem"; + +const { checkbox_list, checkbox_item } = schema.nodes; + +/** + * Returns the position at the start of the first text node matching the text. + * + * @throws if no matching text node exists in the document. + */ +function posOfText(node: Node, text: string) { + let found = -1; + node.descendants((child, pos) => { + if (found === -1 && child.isText && child.text === text) { + found = pos; + } + return found === -1; + }); + if (found === -1) { + throw new Error(`Text "${text}" not found in document`); + } + return found; +} + +/** + * Copies the range from the start of one text node to the end of another, as + * the editor does when the range is selected and copied. + */ +function copy(node: Node, fromText: string, toText: string) { + return node.slice( + posOfText(node, fromText), + posOfText(node, toText) + toText.length, + true + ); +} + +/** + * Pastes the slice over the given range and returns the resulting state, or + * undefined if the paste was not handled. + */ +function paste( + node: Node, + pos: number, + slice: ReturnType, + to = pos +) { + const state = EditorState.create({ + doc: node, + selection: TextSelection.create(node, pos, to), + }); + let result: EditorState | undefined; + pasteIntoListItem(slice)(state, (tr) => { + result = state.apply(tr); + }); + return result; +} + +describe("pasteIntoListItem", () => { + it("merges a copied item into the item at the cursor", () => { + const before = doc(bulletList(["one", "two", "three"])); + const pos = posOfText(before, "two"); + const after = paste(before, pos, copy(before, "three", "three")); + + expect(after?.doc.toJSON()).toEqual( + doc(bulletList(["one", "threetwo", "three"])).toJSON() + ); + expect(after?.selection.from).toEqual(pos + "three".length); + }); + + it("merges several copied items into the list", () => { + const before = doc(bulletList(["one", "two", "three"])); + const after = paste( + before, + posOfText(before, "two"), + copy(before, "one", "two") + ); + + expect(after?.doc.toJSON()).toEqual( + doc(bulletList(["one", "one", "twotwo", "three"])).toJSON() + ); + }); + + it("does not split the list when the copy ran past the end of it", () => { + const before = doc([bulletList(["one", "two"]), p("three")]); + const after = paste( + before, + posOfText(before, "one"), + copy(before, "two", "three") + ); + + expect(after?.doc.toJSON()).toEqual( + doc([bulletList(["two", "threeone", "two"]), p("three")]).toJSON() + ); + }); + + it("merges a copied checklist item into the item at the cursor", () => { + const checklist = (items: string[]) => + doc( + checkbox_list.create( + null, + items.map((text) => checkbox_item.create(null, p(text))) + ) + ); + const before = checklist(["one", "two", "three"]); + const after = paste( + before, + posOfText(before, "two"), + copy(before, "three", "three") + ); + + expect(after?.doc.toJSON()).toEqual( + checklist(["one", "threetwo", "three"]).toJSON() + ); + }); + + it("merges a copied item into the middle of the item at the cursor", () => { + const before = doc(bulletList(["one", "two", "three"])); + const after = paste( + before, + posOfText(before, "two") + 1, + copy(before, "three", "three") + ); + + expect(after?.doc.toJSON()).toEqual( + doc(bulletList(["one", "tthreewo", "three"])).toJSON() + ); + }); + + it("replaces a selection that spans list items", () => { + const before = doc(bulletList(["one", "two", "three"])); + const from = posOfText(before, "two") + 1; + const after = paste( + before, + from, + copy(before, "one", "one"), + posOfText(before, "three") + 2 + ); + + expect(after?.doc.toJSON()).toEqual( + doc(bulletList(["one", "toneree"])).toJSON() + ); + expect(after?.selection.empty).toBe(true); + expect(after?.selection.from).toEqual(from + "one".length); + }); + + it("replaces a selection with several copied items", () => { + const before = doc(bulletList(["one", "two", "three"])); + const after = paste( + before, + posOfText(before, "two"), + copy(before, "one", "two"), + posOfText(before, "two") + "two".length + ); + + expect(after?.doc.toJSON()).toEqual( + doc(bulletList(["one", "one", "two", "three"])).toJSON() + ); + }); + + it("does nothing outside of a list", () => { + const before = doc([bulletList(["one"]), p("two")]); + const slice = copy(before, "one", "one"); + + expect(paste(before, posOfText(before, "two"), slice)).toBeUndefined(); + }); +}); diff --git a/shared/editor/commands/pasteIntoListItem.ts b/shared/editor/commands/pasteIntoListItem.ts new file mode 100644 index 0000000000..bc4bac85e7 --- /dev/null +++ b/shared/editor/commands/pasteIntoListItem.ts @@ -0,0 +1,87 @@ +import type { Node } from "prosemirror-model"; +import { Fragment, Slice } from "prosemirror-model"; +import type { Command } from "prosemirror-state"; +import { TextSelection } from "prosemirror-state"; +import { isList } from "../queries/isList"; + +/** + * Replaces the selection in a list item with a slice copied from inside a list. + * The copied items are added to the surrounding list at the cursor, rather + * than nested in a new list inside the item or splitting the list. + * + * @param slice The pasted slice. + * @returns A prosemirror command. + */ +export const pasteIntoListItem = + (slice: Slice): Command => + (state, dispatch) => { + const { selection } = state; + if (!(selection instanceof TextSelection)) { + return false; + } + + const { $from, from, to } = selection; + const listItem = $from.depth > 1 ? $from.node(-1) : undefined; + if ( + !listItem || + !["list_item", "checkbox_item"].includes(listItem.type.name) || + !$from.parent.isTextblock + ) { + return false; + } + + // The copied list and its first item must both be open in the slice. + const list = slice.content.firstChild; + if (slice.openStart < 2 || !list || !isList(list, state.schema)) { + return false; + } + + if (dispatch) { + // Unlike replaceSelection this does not keep the defining list item from + // the slice, so the open nodes are fitted into the list item at the cursor. + const tr = state.tr.replace(from, to, appendTrailingBlocks(slice, list)); + dispatch( + tr + .setSelection(TextSelection.near(tr.doc.resolve(tr.mapping.map(to)))) + .scrollIntoView() + .setMeta("paste", true) + .setMeta("uiEvent", "paste") + ); + } + return true; + }; + +/** + * Moves the blocks that follow the list in the slice into new items at the end + * of the list, so that they stay in the list they are pasted into. + * + * @param slice The pasted slice. + * @param list The list at the start of the slice. + * @returns The slice with the trailing blocks moved into the list. + */ +function appendTrailingBlocks(slice: Slice, list: Node): Slice { + const { content, openStart, openEnd } = slice; + const itemType = list.lastChild?.type; + if (content.childCount === 1 || openEnd === 0 || !itemType) { + return slice; + } + + const items: Node[] = []; + for (let i = 1; i < content.childCount; i++) { + const block = content.child(i); + if ( + isList(block, list.type.schema) || + !itemType.validContent(Fragment.from(block)) + ) { + return slice; + } + items.push(itemType.create(null, block)); + } + + // The list and the last new item are now open around the last block. + return new Slice( + Fragment.from(list.copy(list.content.append(Fragment.from(items)))), + openStart, + openEnd + 2 + ); +}