|
@@ -3,10 +3,10 @@ import { BlockData, BlockType, TextBlockKeyEventHandlerParams } from '$app/inter
|
|
import { keyBoardEventKeyMap } from '$app/constants/document/text_block';
|
|
import { keyBoardEventKeyMap } from '$app/constants/document/text_block';
|
|
import { useAppDispatch } from '$app/stores/store';
|
|
import { useAppDispatch } from '$app/stores/store';
|
|
import { DocumentControllerContext } from '$app/stores/effects/document/document_controller';
|
|
import { DocumentControllerContext } from '$app/stores/effects/document/document_controller';
|
|
-import { turnToBlockThunk } from '$app_reducers/document/async-actions';
|
|
|
|
|
|
+import { turnToBlockThunk, turnToDividerBlockThunk } from '$app_reducers/document/async-actions';
|
|
import { blockConfig } from '$app/constants/document/config';
|
|
import { blockConfig } from '$app/constants/document/config';
|
|
import { Editor } from 'slate';
|
|
import { Editor } from 'slate';
|
|
-import { getBeforeRangeAt } from '$app/utils/document/slate/text';
|
|
|
|
|
|
+import { getBeforeRangeAt } from '$app/utils/document/blocks/text/delta';
|
|
import {
|
|
import {
|
|
getHeadingDataFromEditor,
|
|
getHeadingDataFromEditor,
|
|
getQuoteDataFromEditor,
|
|
getQuoteDataFromEditor,
|
|
@@ -14,27 +14,29 @@ import {
|
|
getBulletedDataFromEditor,
|
|
getBulletedDataFromEditor,
|
|
getNumberedListDataFromEditor,
|
|
getNumberedListDataFromEditor,
|
|
getToggleListDataFromEditor,
|
|
getToggleListDataFromEditor,
|
|
|
|
+ getCalloutDataFromEditor,
|
|
} from '$app/utils/document/blocks';
|
|
} from '$app/utils/document/blocks';
|
|
-
|
|
|
|
-const blockDataFactoryMap: Record<string, (editor: Editor) => BlockData<any> | undefined> = {
|
|
|
|
- [BlockType.HeadingBlock]: getHeadingDataFromEditor,
|
|
|
|
- [BlockType.TodoListBlock]: getTodoListDataFromEditor,
|
|
|
|
- [BlockType.QuoteBlock]: getQuoteDataFromEditor,
|
|
|
|
- [BlockType.BulletedListBlock]: getBulletedDataFromEditor,
|
|
|
|
- [BlockType.NumberedListBlock]: getNumberedListDataFromEditor,
|
|
|
|
- [BlockType.ToggleListBlock]: getToggleListDataFromEditor,
|
|
|
|
-};
|
|
|
|
|
|
+import { getDeltaAfterSelection } from '$app/utils/document/blocks/common';
|
|
|
|
|
|
export function useTurnIntoBlock(id: string) {
|
|
export function useTurnIntoBlock(id: string) {
|
|
const controller = useContext(DocumentControllerContext);
|
|
const controller = useContext(DocumentControllerContext);
|
|
const dispatch = useAppDispatch();
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
|
const turnIntoBlockEvents = useMemo(() => {
|
|
const turnIntoBlockEvents = useMemo(() => {
|
|
- return Object.entries(blockDataFactoryMap).map(([type, getData]) => {
|
|
|
|
|
|
+ const spaceTriggerEvents = Object.entries({
|
|
|
|
+ [BlockType.HeadingBlock]: getHeadingDataFromEditor,
|
|
|
|
+ [BlockType.TodoListBlock]: getTodoListDataFromEditor,
|
|
|
|
+ [BlockType.QuoteBlock]: getQuoteDataFromEditor,
|
|
|
|
+ [BlockType.BulletedListBlock]: getBulletedDataFromEditor,
|
|
|
|
+ [BlockType.NumberedListBlock]: getNumberedListDataFromEditor,
|
|
|
|
+ [BlockType.ToggleListBlock]: getToggleListDataFromEditor,
|
|
|
|
+ [BlockType.CalloutBlock]: getCalloutDataFromEditor,
|
|
|
|
+ }).map(([type, getData]) => {
|
|
const blockType = type as BlockType;
|
|
const blockType = type as BlockType;
|
|
|
|
+ const triggerKey = keyBoardEventKeyMap.Space;
|
|
return {
|
|
return {
|
|
triggerEventKey: keyBoardEventKeyMap.Space,
|
|
triggerEventKey: keyBoardEventKeyMap.Space,
|
|
- canHandle: canHandle(blockType),
|
|
|
|
|
|
+ canHandle: canHandle(blockType, triggerKey),
|
|
handler: (...args: TextBlockKeyEventHandlerParams) => {
|
|
handler: (...args: TextBlockKeyEventHandlerParams) => {
|
|
if (!controller) return;
|
|
if (!controller) return;
|
|
const [_event, editor] = args;
|
|
const [_event, editor] = args;
|
|
@@ -43,7 +45,20 @@ export function useTurnIntoBlock(id: string) {
|
|
dispatch(turnToBlockThunk({ id, data, type: blockType, controller }));
|
|
dispatch(turnToBlockThunk({ id, data, type: blockType, controller }));
|
|
},
|
|
},
|
|
};
|
|
};
|
|
- }, []);
|
|
|
|
|
|
+ });
|
|
|
|
+ return [
|
|
|
|
+ ...spaceTriggerEvents,
|
|
|
|
+ {
|
|
|
|
+ triggerEventKey: keyBoardEventKeyMap.Reduce,
|
|
|
|
+ canHandle: canHandle(BlockType.DividerBlock, keyBoardEventKeyMap.Reduce),
|
|
|
|
+ handler: (...args: TextBlockKeyEventHandlerParams) => {
|
|
|
|
+ if (!controller) return;
|
|
|
|
+ const [_event, editor] = args;
|
|
|
|
+ const delta = getDeltaAfterSelection(editor) || [];
|
|
|
|
+ dispatch(turnToDividerBlockThunk({ id, controller, delta }));
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ ];
|
|
}, [controller, dispatch, id]);
|
|
}, [controller, dispatch, id]);
|
|
|
|
|
|
return {
|
|
return {
|
|
@@ -51,7 +66,7 @@ export function useTurnIntoBlock(id: string) {
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
-function canHandle(type: BlockType) {
|
|
|
|
|
|
+function canHandle(type: BlockType, triggerKey: string) {
|
|
const config = blockConfig[type];
|
|
const config = blockConfig[type];
|
|
|
|
|
|
const regex = config.markdownRegexps;
|
|
const regex = config.markdownRegexps;
|
|
@@ -62,16 +77,16 @@ function canHandle(type: BlockType) {
|
|
|
|
|
|
return (...args: TextBlockKeyEventHandlerParams) => {
|
|
return (...args: TextBlockKeyEventHandlerParams) => {
|
|
const [event, editor] = args;
|
|
const [event, editor] = args;
|
|
- const isSpaceKey = event.key === keyBoardEventKeyMap.Space;
|
|
|
|
|
|
+ const isTrigger = event.key === triggerKey;
|
|
const selection = editor.selection;
|
|
const selection = editor.selection;
|
|
|
|
|
|
- if (!isSpaceKey || !selection) {
|
|
|
|
|
|
+ if (!isTrigger || !selection) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
const flag = Editor.string(editor, getBeforeRangeAt(editor, selection)).trim();
|
|
const flag = Editor.string(editor, getBeforeRangeAt(editor, selection)).trim();
|
|
if (flag === null) return false;
|
|
if (flag === null) return false;
|
|
|
|
|
|
- return regex.some((r) => r.test(flag));
|
|
|
|
|
|
+ return regex.some((r) => r.test(`${flag}${triggerKey}`));
|
|
};
|
|
};
|
|
}
|
|
}
|