Skip to content

Commit 9f01b45

Browse files
authored
feat: match sublist indentation when adding a new item (#4433)
* match sublist indentation * recursively get last node * fix linting issues
1 parent 964ae16 commit 9f01b45

File tree

1 file changed

+19
-5
lines changed
  • web/src/components/MemoEditor/Editor

1 file changed

+19
-5
lines changed

web/src/components/MemoEditor/Editor/index.tsx

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { last } from "lodash-es";
22
import { forwardRef, ReactNode, useCallback, useEffect, useImperativeHandle, useRef, useState } from "react";
33
import { markdownServiceClient } from "@/grpcweb";
4-
import { NodeType, OrderedListItemNode, TaskListItemNode, UnorderedListItemNode } from "@/types/proto/api/v1/markdown_service";
4+
import { Node, NodeType, OrderedListItemNode, TaskListItemNode, UnorderedListItemNode } from "@/types/proto/api/v1/markdown_service";
55
import { cn } from "@/utils";
66
import TagSuggestions from "./TagSuggestions";
77

@@ -150,6 +150,20 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
150150
updateEditorHeight();
151151
}, []);
152152

153+
const getLastNode = (nodes: Node[]): Node | undefined => {
154+
const lastNode = last(nodes);
155+
if (!lastNode) {
156+
return undefined;
157+
}
158+
if (lastNode.type === NodeType.LIST) {
159+
const children = lastNode.listNode?.children;
160+
if (children) {
161+
return getLastNode(children);
162+
}
163+
}
164+
return lastNode;
165+
};
166+
153167
const handleEditorKeyDown = async (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
154168
if (event.key === "Enter" && !isInIME) {
155169
if (event.shiftKey || event.ctrlKey || event.metaKey || event.altKey) {
@@ -159,7 +173,7 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
159173
const cursorPosition = editorActions.getCursorPosition();
160174
const prevContent = editorActions.getContent().substring(0, cursorPosition);
161175
const { nodes } = await markdownServiceClient.parseMarkdown({ markdown: prevContent });
162-
const lastNode = last(last(nodes)?.listNode?.children);
176+
const lastNode = getLastNode(nodes);
163177
if (!lastNode) {
164178
return;
165179
}
@@ -171,13 +185,13 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
171185
let insertText = indentationMatch ? indentationMatch[0] : ""; // Keep the indentation of the previous line
172186
if (lastNode.type === NodeType.TASK_LIST_ITEM) {
173187
const { symbol } = lastNode.taskListItemNode as TaskListItemNode;
174-
insertText = `${symbol} [ ] `;
188+
insertText += `${symbol} [ ] `;
175189
} else if (lastNode.type === NodeType.UNORDERED_LIST_ITEM) {
176190
const { symbol } = lastNode.unorderedListItemNode as UnorderedListItemNode;
177-
insertText = `${symbol} `;
191+
insertText += `${symbol} `;
178192
} else if (lastNode.type === NodeType.ORDERED_LIST_ITEM) {
179193
const { number } = lastNode.orderedListItemNode as OrderedListItemNode;
180-
insertText = `${Number(number) + 1}. `;
194+
insertText += `${Number(number) + 1}. `;
181195
}
182196
if (insertText) {
183197
editorActions.insertText(insertText);

0 commit comments

Comments
 (0)