-
Notifications
You must be signed in to change notification settings - Fork 67
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 #2214 from epam/improve-editor-focus
[PlateEditor] Improve editor focus
- Loading branch information
Showing
9 changed files
with
165 additions
and
87 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
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
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
69 changes: 69 additions & 0 deletions
69
uui-editor/src/plugins/eventEditorPlugin/eventEditorPlugin.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,69 @@ | ||
import { createPluginFactory, eventEditorActions, eventEditorSelectors, KEY_EVENT_EDITOR } from '@udecode/plate-core'; | ||
import { MutableRefObject, useEffect } from 'react'; | ||
import { uuiMod } from '@epam/uui-core'; | ||
|
||
export const FOCUS_EDITOR_EVENT = 'uui-focus-editor'; | ||
export const BLUR_EDITOR_EVENT = 'uui-blur-editor'; | ||
|
||
// TODO: move to plate | ||
export const createEventEditorPlugin = createPluginFactory({ | ||
key: KEY_EVENT_EDITOR, | ||
handlers: { | ||
onFocus: (editor) => () => { | ||
eventEditorActions.focus(editor.id); | ||
|
||
document.dispatchEvent( | ||
new CustomEvent(FOCUS_EDITOR_EVENT, { | ||
detail: { id: editor.id }, | ||
}), | ||
); | ||
}, | ||
onBlur: (editor) => () => { | ||
const focus = eventEditorSelectors.focus(); | ||
if (focus === editor.id) { | ||
eventEditorActions.focus(null); | ||
} | ||
eventEditorActions.blur(editor.id); | ||
|
||
document.dispatchEvent( | ||
new CustomEvent(BLUR_EDITOR_EVENT, { | ||
detail: { id: editor.id }, | ||
}), | ||
); | ||
}, | ||
}, | ||
}); | ||
|
||
export const useFocusEvents = ({ | ||
editorWrapperRef, | ||
editorId, | ||
isReadonly, | ||
}: { | ||
editorWrapperRef: MutableRefObject<HTMLDivElement> | ||
editorId: string, | ||
isReadonly?: boolean, | ||
}) => { | ||
useEffect(() => { | ||
const onFocusEditor = (event: Event) => { | ||
const id = (event as any).detail.id; | ||
const allowFocus = editorWrapperRef.current && !isReadonly && editorId === id; | ||
if (allowFocus) { | ||
editorWrapperRef.current.classList.add(uuiMod.focus); | ||
} | ||
}; | ||
const onBlurEditor = (event: Event) => { | ||
const id = (event as any).detail.id; | ||
if (editorWrapperRef.current && editorId === id) { | ||
editorWrapperRef.current.classList.remove(uuiMod.focus); | ||
} | ||
}; | ||
|
||
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); | ||
}; | ||
}, [editorId, editorWrapperRef, isReadonly]); | ||
}; |
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
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