Files
outline/shared/editor/extensions/Placeholder.ts
Tom Moor 75aea90972 chore: Editor 'plugin' -> 'extension'
They've always been called extensions, not sure why the folder was plugins. Part of ongoing spring cleaning
2023-04-09 17:27:09 -04:00

51 lines
1.3 KiB
TypeScript

import { Plugin } from "prosemirror-state";
import { Decoration, DecorationSet } from "prosemirror-view";
import Extension from "../lib/Extension";
export default class Placeholder extends Extension {
get name() {
return "empty-placeholder";
}
get defaultOptions() {
return {
emptyNodeClass: "placeholder",
placeholder: "",
};
}
get plugins() {
return [
new Plugin({
props: {
decorations: (state) => {
const { doc } = state;
const decorations: Decoration[] = [];
const completelyEmpty =
doc.textContent === "" &&
doc.childCount <= 1 &&
doc.content.size <= 2;
doc.descendants((node, pos) => {
if (!completelyEmpty) {
return;
}
if (pos !== 0 || node.type.name !== "paragraph") {
return;
}
const decoration = Decoration.node(pos, pos + node.nodeSize, {
class: this.options.emptyNodeClass,
"data-empty-text": this.options.placeholder,
});
decorations.push(decoration);
});
return DecorationSet.create(doc, decorations);
},
},
}),
];
}
}