|
@@ -2,13 +2,16 @@ import { Editor } from 'slate';
|
|
import { useTurnIntoBlock } from './TurnIntoEvents.hooks';
|
|
import { useTurnIntoBlock } from './TurnIntoEvents.hooks';
|
|
import { useCallback, useContext, useMemo } from 'react';
|
|
import { useCallback, useContext, useMemo } from 'react';
|
|
import { keyBoardEventKeyMap } from '$app/constants/document/text_block';
|
|
import { keyBoardEventKeyMap } from '$app/constants/document/text_block';
|
|
-import { TextBlockKeyEventHandlerParams } from '$app/interfaces/document';
|
|
|
|
|
|
+import { BlockType, TextBlockKeyEventHandlerParams } from '$app/interfaces/document';
|
|
import isHotkey from 'is-hotkey';
|
|
import isHotkey from 'is-hotkey';
|
|
import { indentNodeThunk, outdentNodeThunk, splitNodeThunk } from '$app_reducers/document/async-actions';
|
|
import { indentNodeThunk, outdentNodeThunk, splitNodeThunk } from '$app_reducers/document/async-actions';
|
|
import { DocumentControllerContext } from '$app/stores/effects/document/document_controller';
|
|
import { DocumentControllerContext } from '$app/stores/effects/document/document_controller';
|
|
import { useAppDispatch, useAppSelector } from '$app/stores/store';
|
|
import { useAppDispatch, useAppSelector } from '$app/stores/store';
|
|
import { useDefaultTextInputEvents } from '$app/components/document/_shared/Text/TextEvents.hooks';
|
|
import { useDefaultTextInputEvents } from '$app/components/document/_shared/Text/TextEvents.hooks';
|
|
import { ReactEditor } from 'slate-react';
|
|
import { ReactEditor } from 'slate-react';
|
|
|
|
+import { getBeforeRangeAt } from '$app/utils/document/blocks/text/delta';
|
|
|
|
+import { slashCommandActions } from '$app_reducers/document/slice';
|
|
|
|
+import { useSubscribeNode } from '$app/components/document/_shared/SubscribeNode.hooks';
|
|
|
|
|
|
export function useTextBlockKeyEvent(id: string, editor: ReactEditor) {
|
|
export function useTextBlockKeyEvent(id: string, editor: ReactEditor) {
|
|
const controller = useContext(DocumentControllerContext);
|
|
const controller = useContext(DocumentControllerContext);
|
|
@@ -20,6 +23,9 @@ export function useTextBlockKeyEvent(id: string, editor: ReactEditor) {
|
|
return anchor.id === id && focus.id === id;
|
|
return anchor.id === id && focus.id === id;
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+ const { node } = useSubscribeNode(id);
|
|
|
|
+ const nodeType = node?.type;
|
|
|
|
+
|
|
const { turnIntoBlockEvents } = useTurnIntoBlock(id);
|
|
const { turnIntoBlockEvents } = useTurnIntoBlock(id);
|
|
|
|
|
|
// Here custom key events for TextBlock
|
|
// Here custom key events for TextBlock
|
|
@@ -81,8 +87,27 @@ export function useTextBlockKeyEvent(id: string, editor: ReactEditor) {
|
|
);
|
|
);
|
|
},
|
|
},
|
|
},
|
|
},
|
|
|
|
+ {
|
|
|
|
+ // Here custom slash key event for TextBlock
|
|
|
|
+ triggerEventKey: keyBoardEventKeyMap.Slash,
|
|
|
|
+ canHandle: (...args: TextBlockKeyEventHandlerParams) => {
|
|
|
|
+ const [e, editor] = args;
|
|
|
|
+ if (!editor.selection) return false;
|
|
|
|
+
|
|
|
|
+ return isHotkey('/', e) && Editor.string(editor, getBeforeRangeAt(editor, editor.selection)) === '';
|
|
|
|
+ },
|
|
|
|
+ handler: (...args: TextBlockKeyEventHandlerParams) => {
|
|
|
|
+ const [e, _] = args;
|
|
|
|
+ if (!controller) return;
|
|
|
|
+ dispatch(
|
|
|
|
+ slashCommandActions.openSlashCommand({
|
|
|
|
+ blockId: id,
|
|
|
|
+ })
|
|
|
|
+ );
|
|
|
|
+ },
|
|
|
|
+ },
|
|
],
|
|
],
|
|
- [defaultTextInputEvents, controller, dispatch, id]
|
|
|
|
|
|
+ [defaultTextInputEvents, controller, dispatch, id, nodeType]
|
|
);
|
|
);
|
|
|
|
|
|
const onKeyDown = useCallback(
|
|
const onKeyDown = useCallback(
|