add dynamic empty text option for placeholder extension

This commit is contained in:
Philipp Kühn
2019-06-21 22:36:48 +02:00
parent b64eca7ab5
commit 0f17abeee6
2 changed files with 14 additions and 9 deletions

View File

@@ -17,18 +17,21 @@ export default {
data() { data() {
return { return {
editor: new Editor({ editor: new Editor({
autoFocus: true,
extensions: [ extensions: [
new Doc(), new Doc(),
new Title(), new Title(),
new Placeholder({ new Placeholder({
emptyNodeClass: 'is-empty', showOnlyCurrent: false,
emptyNodeText: 'Write something …', emptyNodeText: node => {
if (node.type.name === 'title') {
return 'Give me a name'
}
return 'Write something'
},
}), }),
], ],
content: `
<h1>This is a fixed title.</h1>
<p>With ProseMirror you can force a document layout. Try to remove this title it it's not possible.</p>
`,
}), }),
} }
}, },
@@ -39,8 +42,8 @@ export default {
</script> </script>
<style lang="scss"> <style lang="scss">
.editor h1.is-empty::before, .editor *.is-empty:nth-child(1)::before,
.editor p.is-empty::before { .editor *.is-empty:nth-child(2)::before {
content: attr(data-empty-text); content: attr(data-empty-text);
float: left; float: left;
color: #aaa; color: #aaa;

View File

@@ -44,7 +44,9 @@ export default class Placeholder extends Extension {
if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) { if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
const decoration = Decoration.node(pos, pos + node.nodeSize, { const decoration = Decoration.node(pos, pos + node.nodeSize, {
class: this.options.emptyNodeClass, class: this.options.emptyNodeClass,
'data-empty-text': this.options.emptyNodeText, 'data-empty-text': typeof this.options.emptyNodeText === 'function'
? this.options.emptyNodeText(node)
: this.options.emptyNodeText,
}) })
decorations.push(decoration) decorations.push(decoration)
} }