From 8bba7f706e8f77656665a75b854575a317abb39c Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 28 Apr 2024 00:12:44 +0800 Subject: [PATCH] chore: update IME mode checks --- .../components/MemoEditor/Editor/index.tsx | 38 ++++++++++++++-- web/src/components/MemoEditor/hooks/index.ts | 3 -- .../MemoEditor/hooks/useAutoComplete.ts | 43 ------------------- 3 files changed, 34 insertions(+), 50 deletions(-) delete mode 100644 web/src/components/MemoEditor/hooks/index.ts delete mode 100644 web/src/components/MemoEditor/hooks/useAutoComplete.ts diff --git a/web/src/components/MemoEditor/Editor/index.tsx b/web/src/components/MemoEditor/Editor/index.tsx index 1ebfcc52..c4ab235f 100644 --- a/web/src/components/MemoEditor/Editor/index.tsx +++ b/web/src/components/MemoEditor/Editor/index.tsx @@ -1,6 +1,7 @@ import classNames from "classnames"; -import { forwardRef, ReactNode, useCallback, useEffect, useImperativeHandle, useRef } from "react"; -import { useAutoComplete } from "../hooks"; +import { last } from "lodash-es"; +import { forwardRef, ReactNode, useCallback, useEffect, useImperativeHandle, useRef, useState } from "react"; +import { NodeType, OrderedListNode, TaskListNode, UnorderedListNode } from "@/types/node"; import TagSuggestions from "./TagSuggestions"; export interface EditorRefActions { @@ -30,6 +31,7 @@ interface Props { const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef) { const { className, initialContent, placeholder, onPaste, onContentChange: handleContentChangeCallback } = props; + const [isInIME, setIsInIME] = useState(false); const editorRef = useRef(null); useEffect(() => { @@ -133,8 +135,6 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef< }, }; - useAutoComplete(editorActions); - useImperativeHandle(ref, () => editorActions, []); const updateEditorHeight = () => { @@ -149,6 +149,33 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef< updateEditorHeight(); }, []); + const handleEditorKeyDown = (event: React.KeyboardEvent) => { + if (event.key === "Enter" && !isInIME) { + const cursorPosition = editorActions.getCursorPosition(); + const prevContent = editorActions.getContent().substring(0, cursorPosition); + const lastNode = last(window.parse(prevContent)); + if (!lastNode) { + return; + } + + let insertText = ""; + if (lastNode.type === NodeType.TASK_LIST) { + const { complete } = lastNode.value as TaskListNode; + insertText = complete ? "- [x] " : "- [ ] "; + } else if (lastNode.type === NodeType.UNORDERED_LIST) { + const { symbol } = lastNode.value as UnorderedListNode; + insertText = `${symbol} `; + } else if (lastNode.type === NodeType.ORDERED_LIST) { + const { number } = lastNode.value as OrderedListNode; + insertText = `${Number(number) + 1}. `; + } + if (insertText) { + editorActions.insertText(`\n${insertText}`); + event.preventDefault(); + } + } + }; + return (
setIsInIME(true)} + onCompositionEnd={() => setTimeout(() => setIsInIME(false))} >
diff --git a/web/src/components/MemoEditor/hooks/index.ts b/web/src/components/MemoEditor/hooks/index.ts deleted file mode 100644 index d1293f1a..00000000 --- a/web/src/components/MemoEditor/hooks/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -import useAutoComplete from "./useAutoComplete"; - -export { useAutoComplete }; diff --git a/web/src/components/MemoEditor/hooks/useAutoComplete.ts b/web/src/components/MemoEditor/hooks/useAutoComplete.ts deleted file mode 100644 index e28e1b1c..00000000 --- a/web/src/components/MemoEditor/hooks/useAutoComplete.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { last } from "lodash-es"; -import { useEffect } from "react"; -import { NodeType, OrderedListNode, TaskListNode, UnorderedListNode } from "@/types/node"; -import { EditorRefActions } from "../Editor"; - -const useAutoComplete = (actions: EditorRefActions) => { - useEffect(() => { - const editor = actions.getEditor(); - if (!editor) return; - - editor.addEventListener("keydown", (event) => { - if (event.key === "Enter") { - if (event.isComposing) { - return; - } - const cursorPosition = actions.getCursorPosition(); - const prevContent = actions.getContent().substring(0, cursorPosition); - const lastNode = last(window.parse(prevContent)); - if (!lastNode) { - return; - } - - let insertText = ""; - if (lastNode.type === NodeType.TASK_LIST) { - const { complete } = lastNode.value as TaskListNode; - insertText = complete ? "- [x] " : "- [ ] "; - } else if (lastNode.type === NodeType.UNORDERED_LIST) { - const { symbol } = lastNode.value as UnorderedListNode; - insertText = `${symbol} `; - } else if (lastNode.type === NodeType.ORDERED_LIST) { - const { number } = lastNode.value as OrderedListNode; - insertText = `${Number(number) + 1}. `; - } - if (insertText) { - actions.insertText(`\n${insertText}`); - event.preventDefault(); - } - } - }); - }, []); -}; - -export default useAutoComplete;