add TrailingNode extension

This commit is contained in:
Philipp Kühn 2019-07-23 18:30:59 +02:00
parent 6049b5a318
commit 48cc408a97
3 changed files with 64 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import {
Bold, Bold,
Italic, Italic,
History, History,
TrailingNode,
} from 'tiptap-extensions' } from 'tiptap-extensions'
import Iframe from './Iframe.js' import Iframe from './Iframe.js'
@ -28,6 +29,7 @@ export default {
new Bold(), new Bold(),
new Italic(), new Italic(),
new History(), new History(),
new TrailingNode(),
// custom extension // custom extension
new Iframe(), new Iframe(),
], ],

View File

@ -0,0 +1,61 @@
import { Extension, Plugin, PluginKey } from 'tiptap'
import { nodeEqualsType } from 'tiptap-utils'
export default class TrailingNode extends Extension {
get name() {
return 'trailing_node'
}
get defaultOptions() {
return {
node: 'paragraph',
notAfter: [
'paragraph',
'heading',
],
}
}
get plugins() {
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))
return [
new Plugin({
key: plugin,
view: () => ({
update: view => {
const { state } = view
const { doc } = state
const insertNodeAtEnd = plugin.getState(state)
if (!insertNodeAtEnd) {
return
}
const type = state.schema.nodes[this.options.node]
view.dispatch(view.state.tr.insert(doc.content.size, type.create()))
},
}),
state: {
init: (_, state) => {
const lastNode = state.tr.doc.lastChild
return !nodeEqualsType({ node: lastNode, types: disabledNodes })
},
apply: (tr, oldState) => {
if (!tr.docChanged) {
return oldState
}
const lastNode = tr.doc.lastChild
return !nodeEqualsType({ node: lastNode, types: disabledNodes })
},
},
}),
]
}
}

View File

@ -27,6 +27,7 @@ export { default as Collaboration } from './extensions/Collaboration'
export { default as History } from './extensions/History' export { default as History } from './extensions/History'
export { default as Placeholder } from './extensions/Placeholder' export { default as Placeholder } from './extensions/Placeholder'
export { default as Search } from './extensions/Search' export { default as Search } from './extensions/Search'
export { default as TrailingNode } from './extensions/TrailingNode'
export { default as Suggestions } from './plugins/Suggestions' export { default as Suggestions } from './plugins/Suggestions'
export { default as Highlight } from './plugins/Highlight' export { default as Highlight } from './plugins/Highlight'