1
1
import { last } from "lodash-es" ;
2
2
import { forwardRef , ReactNode , useCallback , useEffect , useImperativeHandle , useRef , useState } from "react" ;
3
3
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" ;
5
5
import { cn } from "@/utils" ;
6
6
import TagSuggestions from "./TagSuggestions" ;
7
7
@@ -150,6 +150,20 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
150
150
updateEditorHeight ( ) ;
151
151
} , [ ] ) ;
152
152
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
+
153
167
const handleEditorKeyDown = async ( event : React . KeyboardEvent < HTMLTextAreaElement > ) => {
154
168
if ( event . key === "Enter" && ! isInIME ) {
155
169
if ( event . shiftKey || event . ctrlKey || event . metaKey || event . altKey ) {
@@ -159,7 +173,7 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
159
173
const cursorPosition = editorActions . getCursorPosition ( ) ;
160
174
const prevContent = editorActions . getContent ( ) . substring ( 0 , cursorPosition ) ;
161
175
const { nodes } = await markdownServiceClient . parseMarkdown ( { markdown : prevContent } ) ;
162
- const lastNode = last ( last ( nodes ) ?. listNode ?. children ) ;
176
+ const lastNode = getLastNode ( nodes ) ;
163
177
if ( ! lastNode ) {
164
178
return ;
165
179
}
@@ -171,13 +185,13 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
171
185
let insertText = indentationMatch ? indentationMatch [ 0 ] : "" ; // Keep the indentation of the previous line
172
186
if ( lastNode . type === NodeType . TASK_LIST_ITEM ) {
173
187
const { symbol } = lastNode . taskListItemNode as TaskListItemNode ;
174
- insertText = `${ symbol } [ ] ` ;
188
+ insertText + = `${ symbol } [ ] ` ;
175
189
} else if ( lastNode . type === NodeType . UNORDERED_LIST_ITEM ) {
176
190
const { symbol } = lastNode . unorderedListItemNode as UnorderedListItemNode ;
177
- insertText = `${ symbol } ` ;
191
+ insertText + = `${ symbol } ` ;
178
192
} else if ( lastNode . type === NodeType . ORDERED_LIST_ITEM ) {
179
193
const { number } = lastNode . orderedListItemNode as OrderedListItemNode ;
180
- insertText = `${ Number ( number ) + 1 } . ` ;
194
+ insertText + = `${ Number ( number ) + 1 } . ` ;
181
195
}
182
196
if ( insertText ) {
183
197
editorActions . insertText ( insertText ) ;
0 commit comments