placeholder.ts 712 B

1234567891011121314151617181920212223
  1. import { Plugin } from 'prosemirror-state';
  2. import { Decoration, DecorationSet } from 'prosemirror-view';
  3. import type { Node } from 'prosemirror-model';
  4. const isEmptyParagraph = (node: Node) => {
  5. return node.type.name === 'paragraph' && node.nodeSize === 2;
  6. };
  7. export const placeholderPlugin = (placeholder: string) => {
  8. return new Plugin({
  9. props: {
  10. decorations(state) {
  11. const { $from } = state.selection;
  12. if (isEmptyParagraph($from.parent)) {
  13. const decoration = Decoration.node($from.before(), $from.after(), {
  14. 'data-placeholder': placeholder,
  15. });
  16. return DecorationSet.create(state.doc, [decoration]);
  17. }
  18. },
  19. },
  20. });
  21. };