-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lexical: Improved list tab handling, Improved test utils
- Made tab work on empty list items - Improved select preservation on single list item tab - Altered test context creation for more standard testing
- Loading branch information
1 parent
e50cd33
commit ace8af0
Showing
4 changed files
with
151 additions
and
67 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
158 changes: 99 additions & 59 deletions
158
resources/js/wysiwyg/services/__tests__/keyboard-handling.test.ts
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,95 +1,135 @@ | ||
import { | ||
createTestContext, | ||
createTestContext, destroyFromContext, | ||
dispatchKeydownEventForNode, | ||
dispatchKeydownEventForSelectedNode, | ||
initializeUnitTest | ||
} from "lexical/__tests__/utils"; | ||
import { | ||
$createParagraphNode, $createTextNode, | ||
$getRoot, LexicalNode, | ||
ParagraphNode, | ||
$getRoot, $getSelection, LexicalEditor, LexicalNode, | ||
ParagraphNode, TextNode, | ||
} from "lexical"; | ||
import {$createDetailsNode, DetailsNode} from "@lexical/rich-text/LexicalDetailsNode"; | ||
import {registerKeyboardHandling} from "../keyboard-handling"; | ||
import {registerRichText} from "@lexical/rich-text"; | ||
import {EditorUiContext} from "../../ui/framework/core"; | ||
import {$createListItemNode, $createListNode, ListItemNode, ListNode} from "@lexical/list"; | ||
|
||
describe('Keyboard-handling service tests', () => { | ||
initializeUnitTest((testEnv) => { | ||
|
||
test('Details: down key on last lines creates new sibling node', () => { | ||
const {editor} = testEnv; | ||
let context!: EditorUiContext; | ||
let editor!: LexicalEditor; | ||
|
||
registerRichText(editor); | ||
registerKeyboardHandling(createTestContext(testEnv)); | ||
beforeEach(() => { | ||
context = createTestContext(); | ||
editor = context.editor; | ||
registerRichText(editor); | ||
registerKeyboardHandling(context); | ||
}); | ||
|
||
let lastRootChild!: LexicalNode|null; | ||
let detailsPara!: ParagraphNode; | ||
afterEach(() => { | ||
destroyFromContext(context); | ||
}); | ||
|
||
editor.updateAndCommit(() => { | ||
const root = $getRoot() | ||
const details = $createDetailsNode(); | ||
detailsPara = $createParagraphNode(); | ||
details.append(detailsPara); | ||
$getRoot().append(details); | ||
detailsPara.select(); | ||
test('Details: down key on last lines creates new sibling node', () => { | ||
let lastRootChild!: LexicalNode|null; | ||
let detailsPara!: ParagraphNode; | ||
|
||
lastRootChild = root.getLastChild(); | ||
}); | ||
editor.updateAndCommit(() => { | ||
const root = $getRoot() | ||
const details = $createDetailsNode(); | ||
detailsPara = $createParagraphNode(); | ||
details.append(detailsPara); | ||
$getRoot().append(details); | ||
detailsPara.select(); | ||
|
||
expect(lastRootChild).toBeInstanceOf(DetailsNode); | ||
lastRootChild = root.getLastChild(); | ||
}); | ||
|
||
dispatchKeydownEventForNode(detailsPara, editor, 'ArrowDown'); | ||
editor.commitUpdates(); | ||
expect(lastRootChild).toBeInstanceOf(DetailsNode); | ||
|
||
editor.getEditorState().read(() => { | ||
lastRootChild = $getRoot().getLastChild(); | ||
}); | ||
dispatchKeydownEventForNode(detailsPara, editor, 'ArrowDown'); | ||
editor.commitUpdates(); | ||
|
||
expect(lastRootChild).toBeInstanceOf(ParagraphNode); | ||
editor.getEditorState().read(() => { | ||
lastRootChild = $getRoot().getLastChild(); | ||
}); | ||
|
||
test('Details: enter on last empy block creates new sibling node', () => { | ||
const {editor} = testEnv; | ||
expect(lastRootChild).toBeInstanceOf(ParagraphNode); | ||
}); | ||
|
||
test('Details: enter on last empty block creates new sibling node', () => { | ||
registerRichText(editor); | ||
|
||
let lastRootChild!: LexicalNode|null; | ||
let detailsPara!: ParagraphNode; | ||
|
||
registerRichText(editor); | ||
registerKeyboardHandling(createTestContext(testEnv)); | ||
editor.updateAndCommit(() => { | ||
const root = $getRoot() | ||
const details = $createDetailsNode(); | ||
const text = $createTextNode('Hello!'); | ||
detailsPara = $createParagraphNode(); | ||
detailsPara.append(text); | ||
details.append(detailsPara); | ||
$getRoot().append(details); | ||
text.selectEnd(); | ||
|
||
let lastRootChild!: LexicalNode|null; | ||
let detailsPara!: ParagraphNode; | ||
lastRootChild = root.getLastChild(); | ||
}); | ||
|
||
expect(lastRootChild).toBeInstanceOf(DetailsNode); | ||
|
||
dispatchKeydownEventForNode(detailsPara, editor, 'Enter'); | ||
editor.commitUpdates(); | ||
|
||
editor.updateAndCommit(() => { | ||
const root = $getRoot() | ||
const details = $createDetailsNode(); | ||
const text = $createTextNode('Hello!'); | ||
detailsPara = $createParagraphNode(); | ||
detailsPara.append(text); | ||
details.append(detailsPara); | ||
$getRoot().append(details); | ||
text.selectEnd(); | ||
dispatchKeydownEventForSelectedNode(editor, 'Enter'); | ||
editor.commitUpdates(); | ||
|
||
lastRootChild = root.getLastChild(); | ||
}); | ||
let detailsChildren!: LexicalNode[]; | ||
let lastDetailsText!: string; | ||
|
||
expect(lastRootChild).toBeInstanceOf(DetailsNode); | ||
editor.getEditorState().read(() => { | ||
detailsChildren = (lastRootChild as DetailsNode).getChildren(); | ||
lastRootChild = $getRoot().getLastChild(); | ||
lastDetailsText = detailsChildren[0].getTextContent(); | ||
}); | ||
|
||
dispatchKeydownEventForNode(detailsPara, editor, 'Enter'); | ||
editor.commitUpdates(); | ||
expect(lastRootChild).toBeInstanceOf(ParagraphNode); | ||
expect(detailsChildren).toHaveLength(1); | ||
expect(lastDetailsText).toBe('Hello!'); | ||
}); | ||
|
||
dispatchKeydownEventForSelectedNode(editor, 'Enter'); | ||
editor.commitUpdates(); | ||
test('Lists: tab on empty list item insets item', () => { | ||
|
||
let detailsChildren!: LexicalNode[]; | ||
let lastDetailsText!: string; | ||
let list!: ListNode; | ||
let listItemB!: ListItemNode; | ||
|
||
editor.getEditorState().read(() => { | ||
detailsChildren = (lastRootChild as DetailsNode).getChildren(); | ||
lastRootChild = $getRoot().getLastChild(); | ||
lastDetailsText = detailsChildren[0].getTextContent(); | ||
}); | ||
editor.updateAndCommit(() => { | ||
const root = $getRoot(); | ||
list = $createListNode('bullet'); | ||
const listItemA = $createListItemNode(); | ||
listItemA.append($createTextNode('Hello!')); | ||
listItemB = $createListItemNode(); | ||
list.append(listItemA, listItemB); | ||
root.append(list); | ||
listItemB.selectStart(); | ||
}); | ||
|
||
expect(lastRootChild).toBeInstanceOf(ParagraphNode); | ||
expect(detailsChildren).toHaveLength(1); | ||
expect(lastDetailsText).toBe('Hello!'); | ||
dispatchKeydownEventForNode(listItemB, editor, 'Tab'); | ||
editor.commitUpdates(); | ||
|
||
editor.getEditorState().read(() => { | ||
const list = $getRoot().getChildren()[0] as ListNode; | ||
const listChild = list.getChildren()[0] as ListItemNode; | ||
const children = listChild.getChildren(); | ||
expect(children).toHaveLength(2); | ||
expect(children[0]).toBeInstanceOf(TextNode); | ||
expect(children[0].getTextContent()).toBe('Hello!'); | ||
expect(children[1]).toBeInstanceOf(ListNode); | ||
|
||
const innerList = children[1] as ListNode; | ||
const selectedNode = $getSelection()?.getNodes()[0]; | ||
expect(selectedNode).toBeInstanceOf(ListItemNode); | ||
expect(selectedNode?.getKey()).toBe(innerList.getChildren()[0].getKey()); | ||
}); | ||
}); | ||
}); |
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