Open platform to collect and prioritize feedback
fix(editor): images on their own line, and Select All block toggles
Matt Roberts448802e1eb20
diff --git a/package-lock.json b/package-lock.jsonindex 021da0b08..7afe838c2 100644--- a/package-lock.json+++ b/package-lock.json@@ -13,6 +13,7 @@"@tiptap/extension-image": "3.31.3","@tiptap/extension-link": "3.31.3","@tiptap/extension-mention": "3.31.3",+ "@tiptap/extension-paragraph": "3.31.3","@tiptap/extensions": "3.31.3","@tiptap/markdown": "3.31.3","@tiptap/pm": "3.31.3",diff --git a/package.json b/package.jsonindex 2ded5f706..994a9731a 100644--- a/package.json+++ b/package.json@@ -9,6 +9,7 @@"@tiptap/extension-image": "3.31.3","@tiptap/extension-link": "3.31.3","@tiptap/extension-mention": "3.31.3",+ "@tiptap/extension-paragraph": "3.31.3","@tiptap/extensions": "3.31.3","@tiptap/markdown": "3.31.3","@tiptap/pm": "3.31.3",diff --git a/public/components/common/form/CommentEditor.markdown.spec.ts b/public/components/common/form/CommentEditor.markdown.spec.tsindex dfc0ec12d..b69635b81 100644--- a/public/components/common/form/CommentEditor.markdown.spec.ts+++ b/public/components/common/form/CommentEditor.markdown.spec.ts@@ -1,10 +1,10 @@import { Editor, JSONContent } from "@tiptap/core"-import StarterKit from "@tiptap/starter-kit"import Link from "@tiptap/extension-link"import { CustomImage } from "./CustomImage"import { CustomMention } from "./CustomMention"import { fiderMarkdown } from "./FiderMarkdown"import { RawMarkdown } from "./RawMarkdown"+import { fiderStarterKit } from "./FiderStarterKit"import * as markdown from "@fider/services/markdown"import { fiderAllowedSchemes } from "@fider/hooks"@@ -17,7 +17,7 @@ fiderAllowedSchemes.get = () => ""const makeEditor = (imageOptions: Parameters<typeof CustomImage.configure>[0] = {}) =>new Editor({extensions: [- StarterKit.configure({ link: false, underline: false }),+ ...fiderStarterKit(),Link.configure({ openOnClick: true, autolink: true, defaultProtocol: "https" }),fiderMarkdown(),RawMarkdown,@@ -200,3 +200,34 @@ describe("CommentEditor and renderer agree on mentions", () => {})})})++describe("block toggles over the whole document", () => {+ // tiptap v3's trailing empty paragraph used to be swept into Select All toggles, leaving empty+ // list items, quotes and code blocks behind.+ const commands = ["toggleBulletList", "toggleOrderedList", "toggleBlockquote", "toggleCodeBlock"] as const++ commands.forEach((command) => {+ test(`Select All + ${command} twice leaves no empty blocks`, () => {+ const editor = load("sample text")+ for (let i = 0; i < 2; i++) {+ editor.commands.selectAll()+ editor.commands[command]()+ }++ const emptyTextblocks: string[] = []+ editor.state.doc.descendants((node) => {+ if (node.isTextblock && node.content.size === 0) emptyTextblocks.push(node.type.name)+ })+ expect(emptyTextblocks).toEqual([])+ expect(editor.state.doc.textContent).toEqual("sample text")+ editor.destroy()+ })+ })++ test("a raw-markdown block at the end still gets a paragraph after it to type into", () => {+ const editor = load("| a | b |\n| --- | --- |\n| 1 | 2 |")+ expect(editor.state.doc.lastChild?.type.name).toEqual("paragraph")+ expect(editor.getMarkdown().trim()).toEqual("| a | b |\n| --- | --- |\n| 1 | 2 |")+ editor.destroy()+ })+})diff --git a/public/components/common/form/CommentEditor.spec.tsx b/public/components/common/form/CommentEditor.spec.tsxindex 0e89d62ac..06f089474 100644--- a/public/components/common/form/CommentEditor.spec.tsx+++ b/public/components/common/form/CommentEditor.spec.tsx@@ -49,3 +49,27 @@ describe("<CommentEditor /> unsaved images", () => {expect(editorImages(container)[0].getAttribute("src")).toEqual(dataUrl)})})++describe("<CommentEditor /> loading images on their own line", () => {+ // tiptap's Paragraph unwraps a paragraph that only holds an image; Fider's image is inline, so it+ // must stay inside the paragraph or the document is invalid and every later edit fails.+ const cases = {+ "attachment appended after text": "Some text\n\n",+ "only an attachment": "",+ "two appended attachments": "Text\n\n\n\n",+ "URL image on its own line": "Some text\n\n",+ }++ Object.entries(cases).forEach(([name, markdown]) => {+ test(`keeps the image inside a paragraph: ${name}`, async () => {+ const { container } = renderEditor({ initialValue: markdown })+ await waitFor(() => expect(editorImages(container).length).toBeGreaterThan(0))++ const pm = container.querySelector(".ProseMirror") as HTMLElement+ expect(Array.from(pm.children).map((child) => child.tagName.toLowerCase())).not.toContain("img")++ fireEvent.click(screen.getByTitle("Markdown Mode"))+ expect(screen.getByTestId<HTMLTextAreaElement>("markdown-textarea").value).toEqual(markdown)+ })+ })+})diff --git a/public/components/common/form/CommentEditor.tsx b/public/components/common/form/CommentEditor.tsxindex 15a3a73d9..d1f51ca71 100644--- a/public/components/common/form/CommentEditor.tsx+++ b/public/components/common/form/CommentEditor.tsx@@ -1,5 +1,4 @@import { Editor } from "@tiptap/react"-import StarterKit from "@tiptap/starter-kit"import Link from "@tiptap/extension-link"import React, { useState, useRef, useEffect } from "react"import { EditorContent, useEditor } from "@tiptap/react"@@ -27,6 +26,7 @@ import { fileToBase64 } from "@fider/services"import { generateBkey } from "@fider/services/bkey"import { ImageUpload } from "@fider/models"import { CustomImage } from "./CustomImage"+import { fiderStarterKit } from "./FiderStarterKit"import { fiderMarkdown } from "./FiderMarkdown"import { RawMarkdown } from "./RawMarkdown"@@ -496,9 +496,8 @@ const Tiptap: React.FunctionComponent<CommentEditorProps> = (props) => {// Only use extensions for rich text modeconst extensions = [- // tiptap v3 StarterKit bundles Link (custom Link.configure below owns the mark) and- // Underline (no markdown representation with the marked renderer); disable both.- StarterKit.configure({ link: false, underline: false }),+ // StarterKit without its Link (configured below) or Underline (no markdown form)+ ...fiderStarterKit(),Link.configure({openOnClick: true,autolink: true,diff --git a/public/components/common/form/CustomImage.ts b/public/components/common/form/CustomImage.tsindex 0ac3f9b90..6f21fa648 100644--- a/public/components/common/form/CustomImage.ts+++ b/public/components/common/form/CustomImage.ts@@ -1,4 +1,5 @@import Image from "@tiptap/extension-image"+import Paragraph from "@tiptap/extension-paragraph"import { JSONContent, mergeAttributes } from "@tiptap/core"import { fiderImageBkey, fiderImageMarkdown } from "@fider/services/markdownSyntax"@@ -13,6 +14,22 @@ export interface CustomImageOptions {// marked image token shape (the parts we read)type ImageToken = { href?: string; text?: string; title?: string | null }+// tiptap's Paragraph unwraps a paragraph holding only an image, expecting a block image node.+// Fider's image is inline, so an unwrapped image would sit directly in the document: invalid+// content that makes every later edit fail. Posts often end with such paragraphs (the server+// appends unreferenced attachments as ""), so keep them as paragraphs.+// Use in place of StarterKit's paragraph.+const parseParagraph = Paragraph.config.parseMarkdown+export const ImageParagraph = Paragraph.extend({+ parseMarkdown: (token, helpers) => {+ const tokens = token.tokens || []+ if (tokens.length === 1 && tokens[0].type === "image") {+ return helpers.createNode("paragraph", undefined, helpers.parseInline(tokens))+ }+ return parseParagraph ? parseParagraph(token, helpers) : helpers.createNode("paragraph", undefined, helpers.parseInline(tokens))+ },+})+export const CustomImage = Image.extend<CustomImageOptions>({name: "customImage",diff --git a/public/components/common/form/FiderStarterKit.ts b/public/components/common/form/FiderStarterKit.tsnew file mode 100644index 000000000..2f066212c--- /dev/null+++ b/public/components/common/form/FiderStarterKit.ts@@ -0,0 +1,20 @@+import StarterKit from "@tiptap/starter-kit"+import { ImageParagraph } from "./CustomImage"++// StarterKit as configured for Fider's comment editor, shared with the markdown specs so they+// exercise the same schema. Link is configured by CommentEditor, underline has no markdown+// form, and paragraph is replaced by ImageParagraph (see CustomImage).+//+// TrailingNode (new in tiptap v3) keeps an empty paragraph after the last block. Select All then+// sweeps it into block toggles, leaving stray empty list items, quotes or code blocks in the+// markdown. Headings, lists, quotes and code blocks can already be left with Enter or the arrow+// keys (as in tiptap v2), so only keep the trailing paragraph after a raw-markdown block.+export const fiderStarterKit = () => [+ StarterKit.configure({+ link: false,+ underline: false,+ paragraph: false,+ trailingNode: { notAfter: ["heading", "bulletList", "orderedList", "blockquote", "codeBlock", "horizontalRule"] },+ }),+ ImageParagraph,+]