|
1 | 1 | import { Extension } from '@core/Extension.js'; |
2 | | -import { AllSelection, Plugin, PluginKey, TextSelection } from 'prosemirror-state'; |
| 2 | +import { Plugin, PluginKey, TextSelection } from 'prosemirror-state'; |
3 | 3 | import { Decoration, DecorationSet } from 'prosemirror-view'; |
4 | 4 |
|
5 | 5 | export const CustomSelectionPluginKey = new PluginKey('CustomSelection'); |
6 | 6 |
|
| 7 | +const handleClickOutside = (event, editor) => { |
| 8 | + const editorElem = editor?.options?.element; |
| 9 | + if (!editorElem) return; |
| 10 | + |
| 11 | + const isInsideEditor = editorElem?.contains(event.target); |
| 12 | + |
| 13 | + if (!isInsideEditor) { |
| 14 | + editor.setOptions({ |
| 15 | + focusTarget: event.target, |
| 16 | + }); |
| 17 | + } else { |
| 18 | + editor.setOptions({ |
| 19 | + focusTarget: null, |
| 20 | + }); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +function getFocusMeta(tr) { |
| 25 | + return tr.getMeta(CustomSelectionPluginKey); |
| 26 | +} |
| 27 | + |
| 28 | +function setFocusMeta(tr, value) { |
| 29 | + return tr.setMeta(CustomSelectionPluginKey, value); |
| 30 | +} |
| 31 | + |
| 32 | +function getFocusState(state) { |
| 33 | + return CustomSelectionPluginKey.getState(state); |
| 34 | +} |
| 35 | + |
7 | 36 | export const CustomSelection = Extension.create({ |
8 | 37 | name: 'customSelection', |
9 | | - |
10 | 38 | addPmPlugins() { |
| 39 | + const editor = this.editor; |
11 | 40 | const customSelectionPlugin = new Plugin({ |
12 | 41 | key: CustomSelectionPluginKey, |
13 | | - |
14 | 42 | state: { |
15 | | - init() { |
16 | | - return DecorationSet.empty; |
| 43 | + init: () => false, |
| 44 | + apply: (tr, value) => { |
| 45 | + return getFocusMeta(tr) ?? value; |
17 | 46 | }, |
18 | | - apply(tr, oldDecorationSet, oldState, newState) { |
19 | | - const sel = tr.selection; |
20 | | - let newDecos = []; |
21 | | - |
22 | | - // Only apply to text selections or whole doc selections |
23 | | - if (sel.from !== sel.to && (tr.doc.resolve(sel.from).parent.isTextblock || sel instanceof AllSelection)) { |
24 | | - newDecos.push( |
25 | | - Decoration.inline(sel.from, sel.to, { |
26 | | - class: 'sd-custom-selection', |
27 | | - }), |
28 | | - ); |
29 | | - } |
| 47 | + }, |
| 48 | + view: () => { |
| 49 | + document?.addEventListener('mousedown', (event) => handleClickOutside(event, editor)); |
30 | 50 |
|
31 | | - return DecorationSet.create(newState.doc, newDecos); |
32 | | - }, |
| 51 | + return { |
| 52 | + destroy: () => { |
| 53 | + document?.removeEventListener('mouseout', handleClickOutside); |
| 54 | + }, |
| 55 | + }; |
33 | 56 | }, |
34 | 57 | props: { |
35 | 58 | handleDOMEvents: { |
36 | | - focusout: (view, event) => { |
37 | | - const isDropDownOption = this.editor.options.focusTarget?.getAttribute('data-dropdown-option'); |
38 | | - if (document.activeElement && !event.relatedTarget && !view.state.selection.empty && !isDropDownOption) { |
| 59 | + mousedown: (view) => { |
| 60 | + const { selection } = view.state; |
| 61 | + const isToolbarButton = this.editor.options.focusTarget?.closest('.toolbar-button'); |
| 62 | + |
| 63 | + if (!isToolbarButton) { |
| 64 | + view.dispatch(setFocusMeta(view.state.tr, false)); |
| 65 | + } |
| 66 | + if (!selection.empty) { |
39 | 67 | this.editor.setOptions({ |
40 | 68 | lastSelection: view.state.selection, |
41 | 69 | }); |
42 | 70 | const clearSelectionTr = view.state.tr.setSelection(TextSelection.create(view.state.doc, 0)); |
43 | 71 |
|
44 | 72 | view.dispatch(clearSelectionTr); |
45 | 73 | } |
46 | | - return false; |
| 74 | + }, |
| 75 | + focus: (view) => { |
| 76 | + const isToolbarButton = this.editor.options.focusTarget?.closest('.toolbar-button'); |
| 77 | + |
| 78 | + if (isToolbarButton) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + view.dispatch(setFocusMeta(view.state.tr, false)); |
| 83 | + }, |
| 84 | + |
| 85 | + blur: (view) => { |
| 86 | + const isToolbarButton = this.editor.options.focusTarget?.closest('.toolbar-button'); |
| 87 | + |
| 88 | + if (isToolbarButton) { |
| 89 | + view.dispatch(setFocusMeta(view.state.tr, true)); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + view.dispatch(setFocusMeta(view.state.tr, false)); |
47 | 94 | }, |
48 | 95 | }, |
49 | | - decorations(state) { |
50 | | - return CustomSelectionPluginKey.getState(state); |
| 96 | + decorations: (state) => { |
| 97 | + const { selection, doc } = state; |
| 98 | + |
| 99 | + if (selection.empty || !getFocusState(state)) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + return DecorationSet.create(doc, [ |
| 104 | + Decoration.inline(selection.from, selection.to, { |
| 105 | + class: 'sd-custom-selection', |
| 106 | + }), |
| 107 | + ]); |
51 | 108 | }, |
52 | 109 | }, |
53 | 110 | }); |
|
0 commit comments