From 75a7c6952d16ae62797c111384a5bdd51b68ccc3 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Fri, 11 Mar 2022 19:37:33 +0800 Subject: [PATCH] fix: fix trailing-node and title --- packages/client/package.json | 1 - .../client/src/components/tiptap/base-kit.tsx | 2 +- .../components/tiptap/extensions/title.tsx | 6 +- .../tiptap/extensions/trailing-node.tsx | 60 +++++++++---------- pnpm-lock.yaml | 11 ---- 5 files changed, 34 insertions(+), 46 deletions(-) diff --git a/packages/client/package.json b/packages/client/package.json index 82bfe9e3..0faaae85 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -33,7 +33,6 @@ "@tiptap/extension-hard-break": "^2.0.0-beta.30", "@tiptap/extension-heading": "^2.0.0-beta.26", "@tiptap/extension-highlight": "^2.0.0-beta.33", - "@tiptap/extension-horizontal-rule": "^2.0.0-beta.31", "@tiptap/extension-image": "^2.0.0-beta.25", "@tiptap/extension-italic": "^2.0.0-beta.25", "@tiptap/extension-link": "^2.0.0-beta.36", diff --git a/packages/client/src/components/tiptap/base-kit.tsx b/packages/client/src/components/tiptap/base-kit.tsx index 91d9de23..2a89ad2f 100644 --- a/packages/client/src/components/tiptap/base-kit.tsx +++ b/packages/client/src/components/tiptap/base-kit.tsx @@ -102,7 +102,7 @@ export const BaseExtension = [ TableCell, TableHeader, Toc, - // TrailingNode, + TrailingNode, Attachment, Katex, DocumentReference, diff --git a/packages/client/src/components/tiptap/extensions/title.tsx b/packages/client/src/components/tiptap/extensions/title.tsx index 3ace6570..80c8ae0f 100644 --- a/packages/client/src/components/tiptap/extensions/title.tsx +++ b/packages/client/src/components/tiptap/extensions/title.tsx @@ -17,14 +17,14 @@ const Title = Node.create({ parseHTML() { return [ { - tag: "div[class=title]", + tag: "h1[class=title]", }, ]; }, renderHTML({ HTMLAttributes }) { return [ - "div", + "h1", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0, ]; @@ -32,7 +32,7 @@ const Title = Node.create({ }); const TitledDocument = Document.extend({ - content: "title block*", + content: "title block+", }); export { Document, Title, TitledDocument }; diff --git a/packages/client/src/components/tiptap/extensions/trailing-node.tsx b/packages/client/src/components/tiptap/extensions/trailing-node.tsx index a010b0b9..17f7fc27 100644 --- a/packages/client/src/components/tiptap/extensions/trailing-node.tsx +++ b/packages/client/src/components/tiptap/extensions/trailing-node.tsx @@ -1,67 +1,67 @@ -import { Extension } from "@tiptap/core"; -import { PluginKey, Plugin } from "prosemirror-state"; - -export interface TrailingNodeOptions { - node: string; - notAfter: string[]; -} +import { Extension } from '@tiptap/core' +import { PluginKey, Plugin } from 'prosemirror-state' // @ts-ignore function nodeEqualsType({ types, node }) { - if (!node) return false; - return ( - (Array.isArray(types) && types.includes(node.type)) || node.type === types - ); + return (Array.isArray(types) && types.includes(node.type)) || node.type === types +} + + +export interface TrailingNodeOptions { + node: string, + notAfter: string[], } export const TrailingNode = Extension.create({ - name: "trailingNode", + name: 'trailingNode', addOptions() { return { - node: "paragraph", - notAfter: ["paragraph"], - }; + node: 'paragraph', + notAfter: [ + 'paragraph', + ], + } }, addProseMirrorPlugins() { - const plugin = new PluginKey(this.name); + const plugin = new PluginKey(this.name) const disabledNodes = Object.entries(this.editor.schema.nodes) .map(([, value]) => value) - .filter((node) => this.options.notAfter.includes(node.name)); + .filter(node => this.options.notAfter.includes(node.name)) return [ new Plugin({ key: plugin, appendTransaction: (_, __, state) => { - const { doc, tr, schema } = state; - const shouldInsertNodeAtEnd = plugin.getState(state); - const endPosition = doc.content.size; - const type = schema.nodes[this.options.node]; + const { doc, tr, schema } = state + const shouldInsertNodeAtEnd = plugin.getState(state) + const endPosition = doc.content.size + const type = schema.nodes[this.options.node] if (!shouldInsertNodeAtEnd) { - return; + return } - return tr.insert(endPosition, type.create()); + return tr.insert(endPosition, type.create()) }, state: { init: (_, state) => { - const lastNode = state.tr.doc.lastChild; + const lastNode = state.tr.doc.lastChild - return !nodeEqualsType({ node: lastNode, types: disabledNodes }); + return !nodeEqualsType({ node: lastNode, types: disabledNodes }) }, apply: (tr, value) => { if (!tr.docChanged) { - return value; + return value } - const lastNode = tr.doc.lastChild; + const lastNode = tr.doc.lastChild - return !nodeEqualsType({ node: lastNode, types: disabledNodes }); + return !nodeEqualsType({ node: lastNode, types: disabledNodes }) }, }, }), - ]; + ] }, -}); +}) \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index af2d2d11..ca55ecb6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -42,7 +42,6 @@ importers: '@tiptap/extension-hard-break': ^2.0.0-beta.30 '@tiptap/extension-heading': ^2.0.0-beta.26 '@tiptap/extension-highlight': ^2.0.0-beta.33 - '@tiptap/extension-horizontal-rule': ^2.0.0-beta.31 '@tiptap/extension-image': ^2.0.0-beta.25 '@tiptap/extension-italic': ^2.0.0-beta.25 '@tiptap/extension-link': ^2.0.0-beta.36 @@ -110,7 +109,6 @@ importers: '@tiptap/extension-hard-break': 2.0.0-beta.30_@tiptap+core@2.0.0-beta.171 '@tiptap/extension-heading': 2.0.0-beta.26_@tiptap+core@2.0.0-beta.171 '@tiptap/extension-highlight': 2.0.0-beta.33_@tiptap+core@2.0.0-beta.171 - '@tiptap/extension-horizontal-rule': 2.0.0-beta.31_@tiptap+core@2.0.0-beta.171 '@tiptap/extension-image': 2.0.0-beta.25_@tiptap+core@2.0.0-beta.171 '@tiptap/extension-italic': 2.0.0-beta.25_@tiptap+core@2.0.0-beta.171 '@tiptap/extension-link': 2.0.0-beta.36_@tiptap+core@2.0.0-beta.171 @@ -1725,15 +1723,6 @@ packages: '@tiptap/core': 2.0.0-beta.171 dev: false - /@tiptap/extension-horizontal-rule/2.0.0-beta.31_@tiptap+core@2.0.0-beta.171: - resolution: {integrity: sha512-MNc4retfjRgkv3qxqGya0+/BEd1Kmn+oMsCRvE+8x3sXyKIse+vdqMuG5qUcA6np0ZD/9hh1riiQ1GQdgc23Ng==} - peerDependencies: - '@tiptap/core': ^2.0.0-beta.1 - dependencies: - '@tiptap/core': 2.0.0-beta.171 - prosemirror-state: 1.3.4 - dev: false - /@tiptap/extension-image/2.0.0-beta.25_@tiptap+core@2.0.0-beta.171: resolution: {integrity: sha512-RgW5jFVS2QNDvFhBOz7H1hY6LjYcbVAa/mE4F4c3RPg3o7GJZXNoL9s+k0QkEM2GXAvY6fX+OICMBn8TSENXKA==} peerDependencies: