From c6fc458ae3b403b5ffa5f406c7c6befe0426d9b9 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Mon, 13 Jun 2022 17:47:04 +0800 Subject: [PATCH] tiptap: fix paste without title extension --- .../client/src/tiptap/core/extensions/paste.ts | 18 ++++++++++++++++-- .../client/src/tiptap/core/extensions/title.ts | 6 +++++- .../markdown/markdown-to-prosemirror/index.tsx | 4 ++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/client/src/tiptap/core/extensions/paste.ts b/packages/client/src/tiptap/core/extensions/paste.ts index 7456e651..f44b97e0 100644 --- a/packages/client/src/tiptap/core/extensions/paste.ts +++ b/packages/client/src/tiptap/core/extensions/paste.ts @@ -15,6 +15,8 @@ import { normalizeMarkdown, } from 'tiptap/prose-utils'; +import { TitleExtensionName } from './title'; + interface IPasteOptions { /** * @@ -26,7 +28,7 @@ interface IPasteOptions { * 将 markdown 转换为 prosemirror 节点 * FIXME: prosemirror 节点的类型是什么? */ - markdownToProsemirror: (arg: { schema: Schema; content: string; hasTitle: boolean }) => unknown; + markdownToProsemirror: (arg: { schema: Schema; content: string; needTitle: boolean }) => unknown; /** * 将 prosemirror 转换为 markdown @@ -100,6 +102,13 @@ export const Paste = Extension.create({ const { markdownToProsemirror } = extensionThis.options; + console.log('p', { + text, + html, + node, + markdownText, + }); + // 直接复制节点 if (node) { const json = safeJSONParse(node); @@ -159,13 +168,18 @@ export const Paste = Extension.create({ if (markdownText || isMarkdown(text) || html.length === 0 || pasteCodeLanguage === 'markdown') { event.preventDefault(); const firstNode = view.props.state.doc.content.firstChild; + const hasTitleExtension = !!editor.extensionManager.extensions.find( + (extension) => extension.name === TitleExtensionName + ); const hasTitle = isTitleNode(firstNode) && firstNode.content.size > 0; const schema = view.props.state.schema; const doc = markdownToProsemirror({ schema, content: normalizeMarkdown(markdownText || text), - hasTitle, + needTitle: hasTitleExtension && !hasTitle, }); + console.log('p', markdownText, text); + let tr = view.state.tr; const selection = tr.selection; view.state.doc.nodesBetween(selection.from, selection.to, (node, position) => { diff --git a/packages/client/src/tiptap/core/extensions/title.ts b/packages/client/src/tiptap/core/extensions/title.ts index 386764b1..8603f5c9 100644 --- a/packages/client/src/tiptap/core/extensions/title.ts +++ b/packages/client/src/tiptap/core/extensions/title.ts @@ -18,9 +18,13 @@ declare module '@tiptap/core' { } } +export const TitleExtensionName = 'title'; + export const Title = Node.create({ - name: 'title', + name: TitleExtensionName, content: 'inline*', + group: 'block', + selectable: true, addOptions() { return { diff --git a/packages/client/src/tiptap/markdown/markdown-to-prosemirror/index.tsx b/packages/client/src/tiptap/markdown/markdown-to-prosemirror/index.tsx index 986d04df..68991ef1 100644 --- a/packages/client/src/tiptap/markdown/markdown-to-prosemirror/index.tsx +++ b/packages/client/src/tiptap/markdown/markdown-to-prosemirror/index.tsx @@ -25,7 +25,7 @@ const extractImage = (html) => { }; // 将 markdown 字符串转换为 ProseMirror JSONDocument -export const markdownToProsemirror = ({ schema, content, hasTitle }) => { +export const markdownToProsemirror = ({ schema, content, needTitle }) => { const html = markdownToHTML(content); if (!html) return null; @@ -34,7 +34,7 @@ export const markdownToProsemirror = ({ schema, content, hasTitle }) => { const { body } = parser.parseFromString(extractImage(html), 'text/html'); body.append(document.createComment(content)); - const node = htmlToPromsemirror(body, !hasTitle); + const node = htmlToPromsemirror(body, needTitle); return node; };