mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-14 18:49:02 +08:00
add TrailingNode extension
This commit is contained in:
parent
6049b5a318
commit
48cc408a97
@ -12,6 +12,7 @@ import {
|
||||
Bold,
|
||||
Italic,
|
||||
History,
|
||||
TrailingNode,
|
||||
} from 'tiptap-extensions'
|
||||
import Iframe from './Iframe.js'
|
||||
|
||||
@ -28,6 +29,7 @@ export default {
|
||||
new Bold(),
|
||||
new Italic(),
|
||||
new History(),
|
||||
new TrailingNode(),
|
||||
// custom extension
|
||||
new Iframe(),
|
||||
],
|
||||
|
61
packages/tiptap-extensions/src/extensions/TrailingNode.js
Normal file
61
packages/tiptap-extensions/src/extensions/TrailingNode.js
Normal 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 })
|
||||
},
|
||||
},
|
||||
}),
|
||||
]
|
||||
}
|
||||
|
||||
}
|
@ -27,6 +27,7 @@ export { default as Collaboration } from './extensions/Collaboration'
|
||||
export { default as History } from './extensions/History'
|
||||
export { default as Placeholder } from './extensions/Placeholder'
|
||||
export { default as Search } from './extensions/Search'
|
||||
export { default as TrailingNode } from './extensions/TrailingNode'
|
||||
|
||||
export { default as Suggestions } from './plugins/Suggestions'
|
||||
export { default as Highlight } from './plugins/Highlight'
|
||||
|
Loading…
Reference in New Issue
Block a user