tiptap: only use dynamic placeholder in edit

This commit is contained in:
fantasticit 2022-08-19 16:14:00 +08:00
parent cb811ab21f
commit 2f97380e63
1 changed files with 25 additions and 11 deletions

View File

@ -56,6 +56,9 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
const start = pos; const start = pos;
const end = pos + node.nodeSize; const end = pos + node.nodeSize;
let placeholder = '';
if (this.editor.isEditable) {
const key = `${start}-${end}`; const key = `${start}-${end}`;
if (!this.editor.storage[this.name].has(key)) { if (!this.editor.storage[this.name].has(key)) {
@ -70,15 +73,26 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
: this.options.placeholder : this.options.placeholder
); );
} }
placeholder = this.editor.storage[this.name].get(key);
const decoration = Decoration.node(start, end, {
'class': classes.join(' '),
'data-placeholder': this.editor.storage[this.name].get(key),
});
setTimeout(() => { setTimeout(() => {
this.editor.storage[this.name].delete(key); this.editor.storage[this.name].delete(key);
}, 500); }, 500);
} else {
placeholder =
typeof this.options.placeholder === 'function'
? this.options.placeholder({
editor: this.editor,
node,
pos,
})
: this.options.placeholder;
}
const decoration = Decoration.node(start, end, {
'class': classes.join(' '),
'data-placeholder': placeholder,
});
decorations.push(decoration); decorations.push(decoration);
} }