add placeholder extension

This commit is contained in:
Philipp Kühn
2018-09-06 22:58:54 +02:00
parent 80ec395c3a
commit db61d67156
7 changed files with 89 additions and 1 deletions

View File

@@ -22,6 +22,7 @@
"dependencies": {
"lowlight": "^1.10.0",
"prosemirror-history": "^1.0.2",
"prosemirror-view": "^1.5.1",
"tiptap": "^0.10.0",
"tiptap-commands": "^0.3.0"
}

View File

@@ -0,0 +1,39 @@
import { Extension, Plugin } from 'tiptap'
import { Decoration, DecorationSet } from 'prosemirror-view'
export default class PlaceholderExtension extends Extension {
get name() {
return 'placeholder'
}
get defaultOptions() {
return {
emptyNodeClass: 'is-empty',
}
}
get plugins() {
return [
new Plugin({
props: {
decorations: state => {
const decorations = []
state.doc.descendants((node, pos) => {
if (node.type.isBlock && node.childCount === 0) {
const decoration = Decoration.node(pos, pos + node.nodeSize, {
class: this.options.emptyNodeClass,
})
decorations.push(decoration)
}
})
return DecorationSet.create(state.doc, decorations)
},
},
}),
]
}
}

View File

@@ -17,3 +17,4 @@ export { default as LinkMark } from './marks/Link'
export { default as StrikeMark } from './marks/Strike'
export { default as HistoryExtension } from './extensions/History'
export { default as PlaceholderExtension } from './extensions/Placeholder'