fix: fix trailing-node and title

This commit is contained in:
fantasticit 2022-03-11 19:37:33 +08:00
parent 6a83038c3b
commit 75a7c6952d
5 changed files with 34 additions and 46 deletions

View File

@ -33,7 +33,6 @@
"@tiptap/extension-hard-break": "^2.0.0-beta.30", "@tiptap/extension-hard-break": "^2.0.0-beta.30",
"@tiptap/extension-heading": "^2.0.0-beta.26", "@tiptap/extension-heading": "^2.0.0-beta.26",
"@tiptap/extension-highlight": "^2.0.0-beta.33", "@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-image": "^2.0.0-beta.25",
"@tiptap/extension-italic": "^2.0.0-beta.25", "@tiptap/extension-italic": "^2.0.0-beta.25",
"@tiptap/extension-link": "^2.0.0-beta.36", "@tiptap/extension-link": "^2.0.0-beta.36",

View File

@ -102,7 +102,7 @@ export const BaseExtension = [
TableCell, TableCell,
TableHeader, TableHeader,
Toc, Toc,
// TrailingNode, TrailingNode,
Attachment, Attachment,
Katex, Katex,
DocumentReference, DocumentReference,

View File

@ -17,14 +17,14 @@ const Title = Node.create({
parseHTML() { parseHTML() {
return [ return [
{ {
tag: "div[class=title]", tag: "h1[class=title]",
}, },
]; ];
}, },
renderHTML({ HTMLAttributes }) { renderHTML({ HTMLAttributes }) {
return [ return [
"div", "h1",
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
0, 0,
]; ];
@ -32,7 +32,7 @@ const Title = Node.create({
}); });
const TitledDocument = Document.extend({ const TitledDocument = Document.extend({
content: "title block*", content: "title block+",
}); });
export { Document, Title, TitledDocument }; export { Document, Title, TitledDocument };

View File

@ -1,67 +1,67 @@
import { Extension } from "@tiptap/core"; import { Extension } from '@tiptap/core'
import { PluginKey, Plugin } from "prosemirror-state"; import { PluginKey, Plugin } from 'prosemirror-state'
export interface TrailingNodeOptions {
node: string;
notAfter: string[];
}
// @ts-ignore // @ts-ignore
function nodeEqualsType({ types, node }) { 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<TrailingNodeOptions>({ export const TrailingNode = Extension.create<TrailingNodeOptions>({
name: "trailingNode", name: 'trailingNode',
addOptions() { addOptions() {
return { return {
node: "paragraph", node: 'paragraph',
notAfter: ["paragraph"], notAfter: [
}; 'paragraph',
],
}
}, },
addProseMirrorPlugins() { addProseMirrorPlugins() {
const plugin = new PluginKey(this.name); const plugin = new PluginKey(this.name)
const disabledNodes = Object.entries(this.editor.schema.nodes) const disabledNodes = Object.entries(this.editor.schema.nodes)
.map(([, value]) => value) .map(([, value]) => value)
.filter((node) => this.options.notAfter.includes(node.name)); .filter(node => this.options.notAfter.includes(node.name))
return [ return [
new Plugin({ new Plugin({
key: plugin, key: plugin,
appendTransaction: (_, __, state) => { appendTransaction: (_, __, state) => {
const { doc, tr, schema } = state; const { doc, tr, schema } = state
const shouldInsertNodeAtEnd = plugin.getState(state); const shouldInsertNodeAtEnd = plugin.getState(state)
const endPosition = doc.content.size; const endPosition = doc.content.size
const type = schema.nodes[this.options.node]; const type = schema.nodes[this.options.node]
if (!shouldInsertNodeAtEnd) { if (!shouldInsertNodeAtEnd) {
return; return
} }
return tr.insert(endPosition, type.create()); return tr.insert(endPosition, type.create())
}, },
state: { state: {
init: (_, 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) => { apply: (tr, value) => {
if (!tr.docChanged) { 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 })
}, },
}, },
}), }),
]; ]
}, },
}); })

View File

@ -42,7 +42,6 @@ importers:
'@tiptap/extension-hard-break': ^2.0.0-beta.30 '@tiptap/extension-hard-break': ^2.0.0-beta.30
'@tiptap/extension-heading': ^2.0.0-beta.26 '@tiptap/extension-heading': ^2.0.0-beta.26
'@tiptap/extension-highlight': ^2.0.0-beta.33 '@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-image': ^2.0.0-beta.25
'@tiptap/extension-italic': ^2.0.0-beta.25 '@tiptap/extension-italic': ^2.0.0-beta.25
'@tiptap/extension-link': ^2.0.0-beta.36 '@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-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-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-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-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-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 '@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 '@tiptap/core': 2.0.0-beta.171
dev: false 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: /@tiptap/extension-image/2.0.0-beta.25_@tiptap+core@2.0.0-beta.171:
resolution: {integrity: sha512-RgW5jFVS2QNDvFhBOz7H1hY6LjYcbVAa/mE4F4c3RPg3o7GJZXNoL9s+k0QkEM2GXAvY6fX+OICMBn8TSENXKA==} resolution: {integrity: sha512-RgW5jFVS2QNDvFhBOz7H1hY6LjYcbVAa/mE4F4c3RPg3o7GJZXNoL9s+k0QkEM2GXAvY6fX+OICMBn8TSENXKA==}
peerDependencies: peerDependencies: