forked from facebook/lexical
-
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.
[lexical-mark] Feature: include inline decorator nodes in marks (face…
- Loading branch information
1 parent
881c7fe
commit a62a1a6
Showing
2 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
185 changes: 185 additions & 0 deletions
185
packages/lexical-mark/__tests__/unit/LexicalMarkNode.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 |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
import {$wrapSelectionInMarkNode, MarkNode} from '@lexical/mark'; | ||
import { | ||
$createParagraphNode, | ||
$createRangeSelection, | ||
$createTextNode, | ||
$getRoot, | ||
ParagraphNode, | ||
} from 'lexical'; | ||
import { | ||
$createTestDecoratorNode, | ||
$createTestElementNode, | ||
$createTestInlineElementNode, | ||
initializeUnitTest, | ||
} from 'lexical/src/__tests__/utils'; | ||
|
||
describe('LexicalMarkNode tests', () => { | ||
initializeUnitTest((testEnv) => { | ||
describe('$wrapSelectionInMarkNode', () => { | ||
beforeEach(() => { | ||
testEnv.editor.update( | ||
() => { | ||
$getRoot().clear().append($createParagraphNode()); | ||
}, | ||
{discrete: true}, | ||
); | ||
}); | ||
|
||
test('wraps a whole text node', () => { | ||
const {editor} = testEnv; | ||
|
||
editor.update(() => { | ||
const textNode = $createTextNode('marked'); | ||
const paragraphNode = | ||
$getRoot().getFirstChildOrThrow<ParagraphNode>(); | ||
paragraphNode.append(textNode); | ||
const selection = $createRangeSelection(); | ||
selection.anchor.set(textNode.getKey(), 0, 'text'); | ||
selection.focus.set( | ||
textNode.getKey(), | ||
textNode.getTextContent().length, | ||
'text', | ||
); | ||
$wrapSelectionInMarkNode(selection, false, 'my-id'); | ||
|
||
expect(paragraphNode.getChildren()).toHaveLength(1); | ||
const markNode = paragraphNode.getFirstChildOrThrow<MarkNode>(); | ||
expect(markNode.getType()).toEqual('mark'); | ||
expect(markNode.getIDs()).toEqual(['my-id']); | ||
expect(markNode.getChildren()).toHaveLength(1); | ||
expect(markNode.getFirstChildOrThrow().getKey()).toEqual( | ||
textNode.getKey(), | ||
); | ||
expect(markNode.getFirstChildOrThrow().getTextContent()).toEqual( | ||
'marked', | ||
); | ||
}); | ||
}); | ||
|
||
test('splits a text node if the selection is not at the start/end', () => { | ||
const {editor} = testEnv; | ||
|
||
editor.update(() => { | ||
const textNode = $createTextNode('unmarked marked unmarked'); | ||
const paragraphNode = | ||
$getRoot().getFirstChildOrThrow<ParagraphNode>(); | ||
paragraphNode.append(textNode); | ||
const selection = $createRangeSelection(); | ||
selection.anchor.set(textNode.getKey(), 'unmarked '.length, 'text'); | ||
selection.focus.set( | ||
textNode.getKey(), | ||
'unmarked marked'.length, | ||
'text', | ||
); | ||
$wrapSelectionInMarkNode(selection, false, 'my-id'); | ||
|
||
expect(paragraphNode.getTextContent()).toEqual( | ||
'unmarked marked unmarked', | ||
); | ||
expect(paragraphNode.getChildren().map((c) => c.getType())).toEqual([ | ||
'text', | ||
'mark', | ||
'text', | ||
]); | ||
expect( | ||
paragraphNode.getChildren().map((c) => c.getTextContent()), | ||
).toEqual(['unmarked ', 'marked', ' unmarked']); | ||
}); | ||
}); | ||
|
||
test('includes inline decorator nodes', () => { | ||
const {editor} = testEnv; | ||
|
||
editor.update(() => { | ||
const decoratorNode = $createTestDecoratorNode(); | ||
const textNode = $createTextNode('more text'); | ||
const paragraphNode = | ||
$getRoot().getFirstChildOrThrow<ParagraphNode>(); | ||
paragraphNode.append(decoratorNode, textNode); | ||
const selection = $createRangeSelection(); | ||
selection.anchor.set(paragraphNode.getKey(), 0, 'text'); | ||
selection.focus.set( | ||
paragraphNode.getKey(), | ||
paragraphNode.getTextContent().length, | ||
'text', | ||
); | ||
$wrapSelectionInMarkNode(selection, false, 'my-id'); | ||
|
||
expect(paragraphNode.getChildren()).toHaveLength(1); | ||
const markNode = paragraphNode.getFirstChildOrThrow<MarkNode>(); | ||
expect(markNode.getType()).toEqual('mark'); | ||
expect(markNode.getChildren().map((c) => c.getKey())).toEqual([ | ||
decoratorNode.getKey(), | ||
textNode.getKey(), | ||
]); | ||
}); | ||
}); | ||
|
||
test('includes inline element nodes', () => { | ||
const {editor} = testEnv; | ||
|
||
editor.update(() => { | ||
const elementNode = $createTestInlineElementNode(); | ||
const textNode = $createTextNode('more text'); | ||
const paragraphNode = | ||
$getRoot().getFirstChildOrThrow<ParagraphNode>(); | ||
paragraphNode.append(elementNode, textNode); | ||
const selection = $createRangeSelection(); | ||
selection.anchor.set(paragraphNode.getKey(), 0, 'text'); | ||
selection.focus.set( | ||
paragraphNode.getKey(), | ||
paragraphNode.getTextContent().length, | ||
'text', | ||
); | ||
$wrapSelectionInMarkNode(selection, false, 'my-id'); | ||
|
||
expect(paragraphNode.getChildren()).toHaveLength(1); | ||
const markNode = paragraphNode.getFirstChildOrThrow<MarkNode>(); | ||
expect(markNode.getType()).toEqual('mark'); | ||
expect(markNode.getChildren().map((c) => c.getKey())).toEqual([ | ||
elementNode.getKey(), | ||
textNode.getKey(), | ||
]); | ||
}); | ||
}); | ||
|
||
test('does not include block element nodes', () => { | ||
const {editor} = testEnv; | ||
|
||
editor.update(() => { | ||
const elementNode = $createTestElementNode(); | ||
const textNode = $createTextNode('more text'); | ||
const paragraphNode = | ||
$getRoot().getFirstChildOrThrow<ParagraphNode>(); | ||
paragraphNode.append(elementNode, textNode); | ||
const selection = $createRangeSelection(); | ||
selection.anchor.set(paragraphNode.getKey(), 0, 'text'); | ||
selection.focus.set( | ||
paragraphNode.getKey(), | ||
paragraphNode.getTextContent().length, | ||
'text', | ||
); | ||
$wrapSelectionInMarkNode(selection, false, 'my-id'); | ||
|
||
expect(paragraphNode.getChildren()).toHaveLength(2); | ||
expect(paragraphNode.getChildAtIndex(0)!.getKey()).toEqual( | ||
elementNode.getKey(), | ||
); | ||
|
||
// the text part of the selection should still be marked | ||
const markNode = paragraphNode.getChildAtIndex(1) as MarkNode; | ||
expect(markNode.getType()).toEqual('mark'); | ||
expect(markNode.getChildren()).toHaveLength(1); | ||
expect(markNode.getTextContent()).toEqual('more text'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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