Skip to content

Commit

Permalink
Merge pull request #3220 from dimaanj/expose-editor-focus-event-callb…
Browse files Browse the repository at this point in the history
…acks

[event-editor] expose focus event callbacks
  • Loading branch information
zbeyens committed Jun 7, 2024
2 parents b195c05 + 314138a commit 17cbdc7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-wolves-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-core": patch
---

[event-editor] expose focus event callbacks
3 changes: 3 additions & 0 deletions packages/core/src/shared/plugins/event-editor/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const FOCUS_EDITOR_EVENT = 'focus-editor-event';

export const BLUR_EDITOR_EVENT = 'blur-editor-event';
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createPluginFactory } from '../../utils/createPluginFactory';
import { BLUR_EDITOR_EVENT, FOCUS_EDITOR_EVENT } from './constants';
import { eventEditorActions, eventEditorSelectors } from './eventEditorStore';

export const KEY_EVENT_EDITOR = 'event-editor';
Expand All @@ -13,9 +14,21 @@ export const createEventEditorPlugin = createPluginFactory({
}

eventEditorActions.blur(editor.id);

document.dispatchEvent(
new CustomEvent(BLUR_EDITOR_EVENT, {
detail: { id: editor.id },
})
);
},
onFocus: (editor) => () => {
eventEditorActions.focus(editor.id);

document.dispatchEvent(
new CustomEvent(FOCUS_EDITOR_EVENT, {
detail: { id: editor.id },
})
);
},
},
key: KEY_EVENT_EDITOR,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/shared/plugins/event-editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
export * from './createEventEditorPlugin';
export * from './eventEditorStore';
export * from './getEventPlateId';

export * from './useFocusEditorEvents';
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useEffect } from 'react';

import type { Value } from '@udecode/slate';

import type { PlateEditor } from '../../types';

import { BLUR_EDITOR_EVENT, FOCUS_EDITOR_EVENT } from './constants';

export const useFocusEditorEvents = ({
editorRef,
onEditorBlur,
onEditorFocus,
}: {
editorRef: PlateEditor<Value> | null;
onEditorBlur?: () => void;
onEditorFocus?: () => void;
}) => {
useEffect(() => {
const onFocusEditor = (event: Event) => {
const id = (event as any).detail.id;

if (!!onEditorFocus && editorRef && editorRef.id === id) {
onEditorFocus();
}
};
const onBlurEditor = (event: Event) => {
const id = (event as any).detail.id;

if (!!onEditorBlur && editorRef && editorRef.id === id) {
onEditorBlur();
}
};

document.addEventListener(FOCUS_EDITOR_EVENT, onFocusEditor);
document.addEventListener(BLUR_EDITOR_EVENT, onBlurEditor);

return () => {
document.removeEventListener(FOCUS_EDITOR_EVENT, onFocusEditor);
document.removeEventListener(BLUR_EDITOR_EVENT, onBlurEditor);
};
}, [editorRef, onEditorBlur, onEditorFocus]);
};

0 comments on commit 17cbdc7

Please sign in to comment.