@@ -18,7 +18,6 @@ import {
1818 toggleList ,
1919 Dispatch ,
2020} from './utils/list-utils' ;
21- import { adjustSelectionToFullBlocks } from './utils/selection-utils' ;
2221import { copyPasteLinkCommand } from './utils/link-utils' ;
2322import { findAncestorDepthOfType } from './utils/node-utils' ;
2423
@@ -217,42 +216,42 @@ const createWrapInCommand = (
217216} ;
218217
219218/**
220- * Handles list operations when there is no selection.
219+ * Handles list operations when there is no selection (cursor only).
220+ * If the cursor is within a list item, only that list item is affected.
221221 *
222- * @param state - The current editor state.
223- * @param type - The type of list to toggle.
224- * @param schema - The ProseMirror schema.
225- * @param otherType - The other type of list to convert to.
226- * @param dispatch - The dispatch function.
227- * @returns A command for handling list operations when there is no selection.
222+ * @param EditorState - state - The current editor state.
223+ * @param NodeType - type - The type of list to toggle.
224+ * @param Schema - schema - The ProseMirror schema.
225+ * @param Function - dispatch - The dispatch function.
226+ * @returns boolean - True if the command was executed.
228227 */
229- const handleListNoSelection = (
230- state : EditorState ,
231- type : NodeType ,
232- schema : Schema ,
233- otherType : NodeType ,
234- dispatch : Dispatch ,
235- ) => {
228+ const handleListNoSelection = ( state , type , schema , dispatch ) => {
236229 const { $from } = state . selection ;
237- const blockFrom = $from . start ( ) ;
238- const blockTo = $from . end ( ) ;
239- const adjustedTr = state . tr . setSelection (
240- new TextSelection (
241- state . doc . resolve ( blockFrom ) ,
242- state . doc . resolve ( blockTo ) ,
243- ) ,
230+ // Find the nearest list_item ancestor.
231+ const listItemDepth = findAncestorDepthOfType (
232+ $from ,
233+ schema . nodes . list_item ,
244234 ) ;
245- const newState = state . apply ( adjustedTr ) ;
246235
247- if ( isInListOfType ( newState , type ) ) {
248- return removeListNodes ( newState , type , schema , dispatch ) ;
236+ if ( listItemDepth === null ) {
237+ // Not inside a list item; fallback to toggling list on the current block.
238+ return toggleList ( type ) ( state , dispatch ) ;
249239 }
250240
251- if ( isInListOfType ( newState , otherType ) ) {
252- return convertAllListNodes ( newState , otherType , type , dispatch ) ;
253- }
241+ // Get the content positions within the list item
242+ const listItemStart = $from . start ( listItemDepth ) ;
243+ const listItemEnd = $from . end ( listItemDepth ) ;
244+
245+ // Set selection to the current list item.
246+ const tr = state . tr . setSelection (
247+ new TextSelection (
248+ state . doc . resolve ( listItemStart ) ,
249+ state . doc . resolve ( listItemEnd ) ,
250+ ) ,
251+ ) ;
252+ const newState = state . apply ( tr ) ;
254253
255- return toggleList ( type ) ( newState , dispatch ) ;
254+ return sinkListItem ( schema . nodes . list_item ) ( newState , dispatch ) ;
256255} ;
257256
258257/**
@@ -272,7 +271,7 @@ const handleListWithSelection = (
272271 otherType : NodeType ,
273272 dispatch : Dispatch ,
274273) => {
275- const { $from } = state . selection ;
274+ const { $from, $to } = state . selection ;
276275 const listItemType = schema . nodes . list_item ;
277276 const ancestorDepth = findAncestorDepthOfType ( $from , listItemType ) ;
278277
@@ -291,14 +290,7 @@ const handleListWithSelection = (
291290 return convertAllListNodes ( state , otherType , type , dispatch ) ;
292291 }
293292
294- const { from, to } = adjustSelectionToFullBlocks ( state ) ;
295- if ( from >= to ) {
296- return false ;
297- }
298-
299- const modifiedTr = state . tr . setSelection (
300- new TextSelection ( state . doc . resolve ( from ) , state . doc . resolve ( to ) ) ,
301- ) ;
293+ const modifiedTr = state . tr . setSelection ( new TextSelection ( $from , $to ) ) ;
302294 const updatedState = state . apply ( modifiedTr ) ;
303295
304296 return wrapInList ( type ) ( updatedState , dispatch ) ;
@@ -329,7 +321,7 @@ export const createListCommand = (
329321 const otherType = getOtherListType ( schema , listTypeName ) ;
330322
331323 return noSelection
332- ? handleListNoSelection ( state , type , schema , otherType , dispatch )
324+ ? handleListNoSelection ( state , type , schema , dispatch )
333325 : handleListWithSelection ( state , type , schema , otherType , dispatch ) ;
334326 } ;
335327
0 commit comments