forked from udecode/plate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5e874c
commit c404823
Showing
8 changed files
with
323 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
deleteFragment, | ||
getAbove, | ||
getChildren, | ||
getNode, | ||
isLastChild, | ||
isSelectionAtBlockEnd, | ||
} from '@udecode/plate-common'; | ||
import { | ||
getPlatePluginOptions, | ||
SPEditor, | ||
TDescendant, | ||
} from '@udecode/plate-core'; | ||
import { Editor, Node, Path } from 'slate'; | ||
import { getListItemEntry } from './queries'; | ||
import {moveListItemsToList, removeFirstListItem, removeListItem} from './transforms'; | ||
import { ELEMENT_LI, ELEMENT_UL } from './defaults'; | ||
import { getLastChild } from '../../../common/src'; | ||
|
||
export const getListDeleteForward = ( | ||
editor: SPEditor, | ||
unit: 'character' | 'word' | 'line' | 'block' | ||
) => { | ||
// at next child | ||
const res = getListItemEntry(editor, {}); | ||
// next lic | ||
|
||
let moved: boolean | undefined = false; | ||
if (!isSelectionAtBlockEnd(editor) || !res) { | ||
return moved; | ||
} | ||
|
||
Editor.withoutNormalizing(editor, () => { | ||
let { list, listItem } = res; | ||
|
||
const nestedListPath = Path.next([...listItem[1], 0]); | ||
const nestedList = getNode<TDescendant>(editor, nestedListPath); | ||
if (nestedList) { | ||
list = [nestedList, nestedListPath]; | ||
listItem = getChildren([nestedList, nestedListPath])[0]; | ||
} else if (getLastChild(list)?.[1] === listItem[1]) { | ||
return; | ||
} | ||
|
||
moved = removeFirstListItem(editor, { | ||
list: list, | ||
listItem: listItem, | ||
}); | ||
if (moved) return; | ||
|
||
moved = removeListItem(editor, { | ||
list: list, | ||
listItem: listItem, | ||
}); | ||
if (moved) return; | ||
|
||
deleteFragment(editor, { unit }); | ||
|
||
const li = getPlatePluginOptions(editor, ELEMENT_LI); | ||
const lisWithSiblings = Array.from( | ||
Editor.nodes(editor, { | ||
at: listItem[1], | ||
mode: 'lowest', | ||
match: (node: TDescendant, path) => { | ||
if (path.length === 0) { | ||
return false; | ||
} | ||
|
||
const isNodeLi = node.type === li.type; | ||
const isSiblingOfNodeLi = | ||
(getNode(editor, Path.next(path)) as TDescendant)?.type === li.type; | ||
|
||
return isNodeLi && isSiblingOfNodeLi; | ||
}, | ||
}) | ||
); | ||
|
||
const nextListItemPath = Path.next(lisWithSiblings[0][1]); | ||
const nextListItem = getNode(editor, nextListItemPath) as TDescendant; | ||
const nextList = getAbove<TDescendant>(editor, { | ||
mode: 'lowest', | ||
at: nextListItemPath, | ||
match: { | ||
type: getPlatePluginOptions(editor, ELEMENT_UL).type, | ||
}, | ||
})!; | ||
|
||
// If it's the first li, move the sublist to the parent list | ||
moved = !!moveListItemsToList(editor, { | ||
fromListItem: [nextListItem, nextListItemPath], | ||
toList: list, | ||
toListIndex: 1, | ||
}); | ||
if (moved) return; | ||
|
||
moved = true; | ||
}); | ||
|
||
return moved; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { SPEditor } from '@udecode/plate-core'; | ||
import { moveListItems } from './moveListItems'; | ||
import { moveListItems, MoveListItemsOptions } from './moveListItems'; | ||
|
||
export const unindentListItems = (editor: SPEditor) => { | ||
moveListItems(editor, false); | ||
}; | ||
export type UnindentListItemsOptions = Omit<MoveListItemsOptions, 'increase'>; | ||
|
||
export const unindentListItems = ( | ||
editor: SPEditor, | ||
options: UnindentListItemsOptions = {} | ||
): void => moveListItems(editor, { ...options, increase: false }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.