Skip to content

Commit

Permalink
Add ability to indent/un-indent multiple list items (#1642)
Browse files Browse the repository at this point in the history
* Add ability to indent/un-indent multiple list items

* some improvement

* fix using unhang; add more test cases

* Adding changset for list indentation fix
  • Loading branch information
zakishaheen authored Jul 1, 2022
1 parent ad05e72 commit bb0dd7f
Show file tree
Hide file tree
Showing 3 changed files with 428 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-queens-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-list': minor
---

Improved list item indentation when selection spans across different elements
33 changes: 29 additions & 4 deletions packages/nodes/list/src/onKeyDownList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import {
getAboveNode,
HotkeyPlugin,
Hotkeys,
isCollapsed,
KeyboardHandlerReturnType,
PlateEditor,
select,
unhangRange,
Value,
WithPlatePlugin,
} from '@udecode/plate-core';
import isHotkey from 'is-hotkey';
import { castArray } from 'lodash';
import { Range } from 'slate';
import { moveListItems, toggleList } from './transforms';

export const onKeyDownList = <
Expand All @@ -20,16 +24,37 @@ export const onKeyDownList = <
): KeyboardHandlerReturnType => (e) => {
const isTab = Hotkeys.isTab(editor, e);
const isUntab = Hotkeys.isUntab(editor, e);

let workRange = editor.selection;

if (editor.selection && (isTab || isUntab)) {
const { selection } = editor;

// Unhang the expanded selection
if (!isCollapsed(editor.selection)) {
const { anchor, focus } = Range.isBackward(selection)
? { anchor: selection.focus, focus: selection.anchor }
: { anchor: selection.anchor, focus: selection.focus };

// This is a workaround for a Slate bug
// See: https://github.com/ianstormtaylor/slate/pull/5039
anchor.offset = 0;
const unHungRange = unhangRange(editor, { anchor, focus });
if (unHungRange) {
workRange = unHungRange;
select(editor, unHungRange);
}
}

// check if we're in a list context.
const listSelected = getAboveNode(editor, {
at: editor.selection,
match: { type },
});

if (listSelected) {
if (workRange && listSelected) {
e.preventDefault();
moveListItems(editor, { increase: isTab });
return;
moveListItems(editor, { at: workRange, increase: isTab });
return true;
}
}

Expand Down
Loading

1 comment on commit bb0dd7f

@vercel
Copy link

@vercel vercel bot commented on bb0dd7f Jul 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

plate – ./

plate-git-main-udecode.vercel.app
plate-udecode.vercel.app
plate.udecode.io
www.plate.udecode.io

Please sign in to comment.