@yuntijs/ui
Version:
☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps
35 lines • 1.49 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { mergeRegister } from '@lexical/utils';
import { BLUR_COMMAND, COMMAND_PRIORITY_EDITOR, FOCUS_COMMAND, KEY_ESCAPE_COMMAND } from 'lexical';
import { useEffect, useRef } from 'react';
import { CLEAR_HIDE_MENU_TIMEOUT } from "./mention-node";
export var OnBlurBlockPlugin = function OnBlurBlockPlugin(_ref) {
var onBlur = _ref.onBlur,
onFocus = _ref.onFocus;
var _useLexicalComposerCo = useLexicalComposerContext(),
_useLexicalComposerCo2 = _slicedToArray(_useLexicalComposerCo, 1),
editor = _useLexicalComposerCo2[0];
var ref = useRef(null);
useEffect(function () {
return mergeRegister(editor.registerCommand(CLEAR_HIDE_MENU_TIMEOUT, function () {
if (ref.current) {
clearTimeout(ref.current);
ref.current = null;
}
return true;
}, COMMAND_PRIORITY_EDITOR), editor.registerCommand(BLUR_COMMAND, function (_payload, _editor) {
ref.current = setTimeout(function () {
editor.dispatchCommand(KEY_ESCAPE_COMMAND, new KeyboardEvent('keydown', {
key: 'Escape'
}));
}, 200);
if (onBlur) onBlur();
return true;
}, COMMAND_PRIORITY_EDITOR), editor.registerCommand(FOCUS_COMMAND, function () {
if (onFocus) onFocus();
return true;
}, COMMAND_PRIORITY_EDITOR));
}, [editor, onBlur, onFocus]);
return null;
};