1 | import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
|
2 | import { PluginKey } from '@atlaskit/editor-prosemirror/state';
|
3 | const key = new PluginKey('focusPluginHandler');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const focusPlugin = ({
|
9 | api
|
10 | }) => {
|
11 | return {
|
12 | name: 'focus',
|
13 | getSharedState(editorState) {
|
14 | var _key$getState;
|
15 | if (!editorState) {
|
16 | return {
|
17 | hasFocus: false
|
18 | };
|
19 | }
|
20 | return {
|
21 | hasFocus: Boolean((_key$getState = key.getState(editorState)) === null || _key$getState === void 0 ? void 0 : _key$getState.hasFocus)
|
22 | };
|
23 | },
|
24 | pmPlugins() {
|
25 | const plugin = new SafePlugin({
|
26 | key,
|
27 | state: {
|
28 | init() {
|
29 | return {
|
30 | hasFocus: false
|
31 | };
|
32 | },
|
33 | apply(tr, oldPluginState) {
|
34 | const meta = tr.getMeta(key);
|
35 | if (typeof meta === 'boolean') {
|
36 | if (meta !== oldPluginState.hasFocus) {
|
37 | return {
|
38 | hasFocus: meta
|
39 | };
|
40 | }
|
41 | }
|
42 | return oldPluginState;
|
43 | }
|
44 | },
|
45 | props: {
|
46 | handleDOMEvents: {
|
47 | focus: view => {
|
48 | const focusState = key.getState(view.state);
|
49 | if (!(focusState !== null && focusState !== void 0 && focusState.hasFocus)) {
|
50 | view.dispatch(view.state.tr.setMeta(key, true));
|
51 | }
|
52 | return false;
|
53 | },
|
54 | blur: view => {
|
55 | const focusState = key.getState(view.state);
|
56 | if (focusState !== null && focusState !== void 0 && focusState.hasFocus) {
|
57 | view.dispatch(view.state.tr.setMeta(key, false));
|
58 | }
|
59 | return false;
|
60 | }
|
61 | }
|
62 | }
|
63 | });
|
64 | return [{
|
65 | name: 'focusHandlerPlugin',
|
66 | plugin: () => plugin
|
67 | }];
|
68 | }
|
69 | };
|
70 | };
|
71 | export { focusPlugin }; |
\ | No newline at end of file |