|
1 | 1 | import { Extension } from "@tiptap/core"; |
2 | 2 | import { Decoration, DecorationSet } from "@tiptap/pm/view"; |
3 | | -import { Plugin } from "@tiptap/pm/state"; |
4 | | -import { cn } from "@/utils/cn"; |
| 3 | +import { Plugin, EditorState } from "@tiptap/pm/state"; |
5 | 4 |
|
6 | | -export const DecorateHiddenCharacters = Extension.create({ |
7 | | - name: 'decorateHiddenCharacters', |
| 5 | +type Key = "nbsp" | "shy"; |
| 6 | + |
| 7 | +type Options = { |
| 8 | + class?: string; |
| 9 | + characters?: { |
| 10 | + [key in Key]: { class: string }; |
| 11 | + }; |
| 12 | +}; |
| 13 | + |
| 14 | +export const DecorateHiddenCharacters = Extension.create<Options>({ |
| 15 | + name: "decorateHiddenCharacters", |
8 | 16 |
|
9 | 17 | addOptions() { |
10 | | - return { |
11 | | - class: '', |
12 | | - } |
| 18 | + return {}; |
13 | 19 | }, |
14 | 20 |
|
15 | | - |
16 | 21 | addProseMirrorPlugins() { |
17 | | - const buildDecorations = (doc) => { |
18 | | - const decorations = [] |
| 22 | + const buildCharDecorations = ( |
| 23 | + state: EditorState, |
| 24 | + { char, key }: { char: string, key: Key } |
| 25 | + ) => { |
| 26 | + const decorations: Decoration[] = []; |
19 | 27 |
|
| 28 | + state.doc.descendants((node, pos) => { |
| 29 | + if (!node.isText) return true; |
20 | 30 |
|
21 | | - doc.descendants((node, pos) => { |
22 | | - if (!node.isText) return true |
| 31 | + const text = node.text; |
| 32 | + if (!text) return true; |
23 | 33 |
|
| 34 | + let idx = text.indexOf(char); |
| 35 | + while (idx !== -1) { |
| 36 | + const from = pos + idx; |
| 37 | + const to = from + 1; |
24 | 38 |
|
25 | | - const text = node.text |
26 | | - if (!text) return true |
| 39 | + const isSelected = |
| 40 | + (from >= state.selection.from && from < state.selection.to) || |
| 41 | + (to > state.selection.from && to <= state.selection.to); |
27 | 42 |
|
28 | | - let idx = text.indexOf('\u00A0') |
29 | | - while (idx !== -1) { |
30 | | - const from = pos + idx |
31 | | - const to = from + 1 |
32 | 43 | const deco = Decoration.inline(from, to, { |
33 | | - 'data-key': 'nbsp', |
34 | | - class: this.options.class, |
35 | | - }) |
36 | | - decorations.push(deco) |
37 | | - idx = text.indexOf('\u00A0', idx + 1) |
38 | | - } |
| 44 | + 'data-key': key, |
| 45 | + 'data-selected': isSelected ? 'true' : null, |
| 46 | + class: [ |
| 47 | + this.options.class, |
| 48 | + this.options.characters?.[key]?.class, |
| 49 | + ] |
| 50 | + .filter(Boolean) |
| 51 | + .join(' '), |
| 52 | + }); |
| 53 | + decorations.push(deco); |
39 | 54 |
|
| 55 | + idx = text.indexOf(char, idx + 1); |
| 56 | + } |
40 | 57 |
|
41 | | - return true |
42 | | - }) |
43 | | - |
| 58 | + return true; |
| 59 | + }); |
44 | 60 |
|
45 | | - return DecorationSet.create(doc, decorations) |
46 | | - } |
| 61 | + return decorations; |
| 62 | + }; |
47 | 63 |
|
| 64 | + const buildDecorations = (state: EditorState) => { |
| 65 | + return DecorationSet.create(state.doc, [ |
| 66 | + ...buildCharDecorations(state, { char: '\u00A0', key: 'nbsp' }), |
| 67 | + ...buildCharDecorations(state, { char: '\u00AD', key: 'shy' }), |
| 68 | + ]); |
| 69 | + }; |
48 | 70 |
|
49 | 71 | return [ |
50 | 72 | new Plugin({ |
51 | 73 | state: { |
52 | | - init(_, { doc }) { |
53 | | - return buildDecorations(doc) |
| 74 | + init(_, state) { |
| 75 | + return buildDecorations(state); |
54 | 76 | }, |
55 | | - apply(tr, old) { |
56 | | - if (!tr.docChanged) return old |
57 | | - return buildDecorations(tr.doc) |
| 77 | + apply(tr, old, oldState, newState) { |
| 78 | + if (!tr.docChanged && !tr.selectionSet) return old; |
| 79 | + return buildDecorations(newState); |
58 | 80 | }, |
59 | 81 | }, |
60 | 82 | props: { |
61 | 83 | decorations(state) { |
62 | | - return this.getState(state) |
| 84 | + return this.getState(state); |
63 | 85 | }, |
64 | 86 | }, |
65 | 87 | }), |
66 | | - ] |
| 88 | + ]; |
67 | 89 | }, |
68 | | -}) |
| 90 | +}); |
0 commit comments