-
-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3220 from dimaanj/expose-editor-focus-event-callb…
…acks [event-editor] expose focus event callbacks
- Loading branch information
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@udecode/plate-core": patch | ||
--- | ||
|
||
[event-editor] expose focus event callbacks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/core/src/shared/plugins/event-editor/useFocusEditorEvents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}; |