@@ -17,8 +17,8 @@ import {
1717 convertAllListNodes ,
1818 toggleList ,
1919 Dispatch ,
20+ convertSingleListNode ,
2021} from './utils/list-utils' ;
21- import { adjustSelectionToFullBlocks } from './utils/selection-utils' ;
2222import { copyPasteLinkCommand } from './utils/link-utils' ;
2323import { findAncestorDepthOfType } from './utils/node-utils' ;
2424
@@ -217,42 +217,42 @@ const createWrapInCommand = (
217217} ;
218218
219219/**
220- * Handles list operations when there is no selection.
220+ * Handles list operations when there is no selection (cursor only).
221+ * If the cursor is within a list item, only that list item is affected.
221222 *
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.
223+ * @param EditorState - state - The current editor state.
224+ * @param NodeType - type - The type of list to toggle.
225+ * @param Schema - schema - The ProseMirror schema.
226+ * @param Function - dispatch - The dispatch function.
227+ * @returns boolean - True if the command was executed.
228228 */
229- const handleListNoSelection = (
230- state : EditorState ,
231- type : NodeType ,
232- schema : Schema ,
233- otherType : NodeType ,
234- dispatch : Dispatch ,
235- ) => {
229+ const handleListNoSelection = ( state , type , schema , dispatch ) => {
236230 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- ) ,
231+ // Find the nearest list_item ancestor.
232+ const listItemDepth = findAncestorDepthOfType (
233+ $from ,
234+ schema . nodes . list_item ,
244235 ) ;
245- const newState = state . apply ( adjustedTr ) ;
246236
247- if ( isInListOfType ( newState , type ) ) {
248- return removeListNodes ( newState , type , schema , dispatch ) ;
237+ if ( listItemDepth === null ) {
238+ // Not inside a list item; fallback to toggling list on the current block.
239+ return toggleList ( type ) ( state , dispatch ) ;
249240 }
250241
251- if ( isInListOfType ( newState , otherType ) ) {
252- return convertAllListNodes ( newState , otherType , type , dispatch ) ;
253- }
242+ // Get the content positions within the list item
243+ const listItemStart = $from . start ( listItemDepth ) ;
244+ const listItemEnd = $from . end ( listItemDepth ) ;
245+
246+ // Set selection to the current list item.
247+ const tr = state . tr . setSelection (
248+ new TextSelection (
249+ state . doc . resolve ( listItemStart ) ,
250+ state . doc . resolve ( listItemEnd ) ,
251+ ) ,
252+ ) ;
253+ const newState = state . apply ( tr ) ;
254254
255- return toggleList ( type ) ( newState , dispatch ) ;
255+ return sinkListItem ( schema . nodes . list_item ) ( newState , dispatch ) ;
256256} ;
257257
258258/**
@@ -272,7 +272,7 @@ const handleListWithSelection = (
272272 otherType : NodeType ,
273273 dispatch : Dispatch ,
274274) => {
275- const { $from } = state . selection ;
275+ const { $from, $to } = state . selection ;
276276 const listItemType = schema . nodes . list_item ;
277277 const ancestorDepth = findAncestorDepthOfType ( $from , listItemType ) ;
278278
@@ -291,14 +291,7 @@ const handleListWithSelection = (
291291 return convertAllListNodes ( state , otherType , type , dispatch ) ;
292292 }
293293
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- ) ;
294+ const modifiedTr = state . tr . setSelection ( new TextSelection ( $from , $to ) ) ;
302295 const updatedState = state . apply ( modifiedTr ) ;
303296
304297 return wrapInList ( type ) ( updatedState , dispatch ) ;
@@ -329,7 +322,7 @@ export const createListCommand = (
329322 const otherType = getOtherListType ( schema , listTypeName ) ;
330323
331324 return noSelection
332- ? handleListNoSelection ( state , type , schema , otherType , dispatch )
325+ ? handleListNoSelection ( state , type , schema , dispatch )
333326 : handleListWithSelection ( state , type , schema , otherType , dispatch ) ;
334327 } ;
335328
0 commit comments