Skip to content

Commit

Permalink
⏸ [0.7] Remove $cloneContents (facebook#3483)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Dec 7, 2022
1 parent 09e315a commit da2e863
Show file tree
Hide file tree
Showing 8 changed files with 2 additions and 571 deletions.
14 changes: 0 additions & 14 deletions packages/lexical-selection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ This package contains selection helpers for Lexical.

### Methods

#### `$cloneContents`

Clones the Lexical nodes in the selection, returning a map of Key -> LexicalNode and a list containing the keys
of all direct children of the RootNode. Useful for insertion/transfer operations, such as copy and paste.

```ts
export function $cloneContents(
selection: RangeSelection | NodeSelection | GridSelection,
): {
nodeMap: Array<[NodeKey, LexicalNode]>;
range: Array<NodeKey>;
};
```

#### `getStyleObjectFromCSS`

Given a CSS string, returns an object from the style cache.
Expand Down
6 changes: 0 additions & 6 deletions packages/lexical-selection/flow/LexicalSelection.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ import type {
Point,
RangeSelection,
} from 'lexical';
declare export function $cloneContents<T: LexicalNode>(
selection: RangeSelection | NodeSelection | GridSelection,
): {
nodeMap: Array<[NodeKey, T]>,
range: Array<NodeKey>,
};
declare export function $cloneWithProperties<T: LexicalNode>(node: T): T;
declare export function getStyleObjectFromCSS(css: string): {
[string]: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {$createLinkNode} from '@lexical/link';
import {$createHeadingNode} from '@lexical/rich-text';
import {$cloneContents, $patchStyleText} from '@lexical/selection';
import {$patchStyleText} from '@lexical/selection';
import {
$createParagraphNode,
$createTextNode,
Expand All @@ -22,8 +22,6 @@ import {
} from 'lexical';
import {
$createTestDecoratorNode,
$createTestElementNode,
$createTestExcludeFromCopyElementNode,
createTestEditor,
TestDecoratorNode,
} from 'lexical/src/__tests__/utils';
Expand Down Expand Up @@ -241,17 +239,6 @@ describe('LexicalSelectionHelpers tests', () => {
setupTestCase((selection, state) => {
expect(selection.extract()).toEqual([$getNodeByKey('a')]);
});

// cloneContents
setupTestCase((selection, element) => {
expect($cloneContents(selection)).toEqual({
nodeMap: [
['a', {...$getNodeByKey('a'), __text: ''}],
[element.getKey(), {...element, __children: ['a']}],
],
range: [element.getKey()],
});
});
});

test('Has correct text point after removal after merge', async () => {
Expand Down Expand Up @@ -831,14 +818,6 @@ describe('LexicalSelectionHelpers tests', () => {
setupTestCase((selection, element) => {
expect(selection.extract()).toEqual([element]);
});

// cloneContents
setupTestCase((selection, element) => {
expect($cloneContents(selection)).toEqual({
nodeMap: [[element.getKey(), element]],
range: [element.getKey()],
});
});
});

test('Can handle a start element point', () => {
Expand Down Expand Up @@ -991,17 +970,6 @@ describe('LexicalSelectionHelpers tests', () => {
setupTestCase((selection, element) => {
expect(selection.extract()).toEqual([$getNodeByKey('a')]);
});

// cloneContents
setupTestCase((selection, element) => {
expect($cloneContents(selection)).toEqual({
nodeMap: [
['a', {...$getNodeByKey('a'), __text: ''}],
[element.getKey(), {...element, __children: ['a']}],
],
range: [element.getKey()],
});
});
});

test('Can handle an end element point', () => {
Expand Down Expand Up @@ -1154,17 +1122,6 @@ describe('LexicalSelectionHelpers tests', () => {
setupTestCase((selection, element) => {
expect(selection.extract()).toEqual([$getNodeByKey('c')]);
});

// cloneContents
setupTestCase((selection, element) => {
expect($cloneContents(selection)).toEqual({
nodeMap: [
['c', {...$getNodeByKey('c'), __text: ''}],
[element.getKey(), {...element, __children: ['c']}],
],
range: [element.getKey()],
});
});
});

test('Has correct element point after merge from middle', async () => {
Expand Down Expand Up @@ -1481,18 +1438,6 @@ describe('LexicalSelectionHelpers tests', () => {
setupTestCase((selection, state) => {
expect(selection.extract()).toEqual([{...$getNodeByKey('a')}]);
});

// cloneContents
setupTestCase((selection, element) => {
expect($cloneContents(selection)).toEqual({
nodeMap: [
['a', $getNodeByKey('a')],
[element.getKey(), {...element, __children: ['a', 'b']}],
['b', {...$getNodeByKey('b'), __text: ''}],
],
range: [element.getKey()],
});
});
});

test('Can handle multiple element points', () => {
Expand Down Expand Up @@ -1649,18 +1594,6 @@ describe('LexicalSelectionHelpers tests', () => {

expect(selection.extract()).toEqual([firstChild]);
});

// cloneContents
setupTestCase((selection, element) => {
expect($cloneContents(selection)).toEqual({
nodeMap: [
['a', $getNodeByKey('a')],
[element.getKey(), {...element, __children: ['a', 'b']}],
['b', {...$getNodeByKey('b'), __text: ''}],
],
range: [element.getKey()],
});
});
});

test('Can handle a mix of text and element points', () => {
Expand Down Expand Up @@ -1825,199 +1758,6 @@ describe('LexicalSelectionHelpers tests', () => {
$getNodeByKey('c'),
]);
});

// cloneContents
setupTestCase((selection, element) => {
expect($cloneContents(selection)).toEqual({
nodeMap: [
['a', $getNodeByKey('a')],
[element.getKey(), element],
['b', $getNodeByKey('b')],
['c', $getNodeByKey('c')],
],
range: [element.getKey()],
});
});
});
});

test('range with multiple paragraphs', async () => {
const editor = createTestEditor();

const element = document.createElement('div');

editor.setRootElement(element);

await editor.update(() => {
const root = $getRoot();

const paragraph1 = $createParagraphNode();
const paragraph2 = $createParagraphNode();
const paragraph3 = $createParagraphNode();

const text1 = $createTextNode('First');
const text2 = $createTextNode('Second');
const text3 = $createTextNode('Third');

root.append(paragraph1, paragraph2, paragraph3);

paragraph1.append(text1);
paragraph2.append(text2);
paragraph3.append(text3);
text1.select(0, 0);
const selection1 = $getSelection();

if ($isNodeSelection(selection1)) {
return;
}

selection1.focus.set(text3.getKey(), 1, 'text');

const selectedNodes1 = $cloneContents($getSelection());

expect(selectedNodes1.range).toEqual([
paragraph1.getKey(),
paragraph2.getKey(),
paragraph3.getKey(),
]);

expect(selectedNodes1.nodeMap[0][0]).toEqual(text1.getKey());
expect((selectedNodes1.nodeMap[0][1] as TextNode).__text).toBe('First');
expect(selectedNodes1.nodeMap[1][0]).toEqual(paragraph1.getKey());
expect(selectedNodes1.nodeMap[2][0]).toEqual(paragraph2.getKey());
expect(selectedNodes1.nodeMap[3][0]).toEqual(text2.getKey());
expect(selectedNodes1.nodeMap[4][0]).toEqual(paragraph3.getKey());
expect(selectedNodes1.nodeMap[5][0]).toEqual(text3.getKey());
expect((selectedNodes1.nodeMap[5][1] as TextNode).__text).toBe('T');

expect(() => selectedNodes1.nodeMap[5][1].getTextContent()).toThrow();
text1.select(1, 1);
const selection2 = $getSelection();

if ($isNodeSelection(selection2)) {
return;
}

selection2.focus.set(text3.getKey(), 4, 'text');

const selectedNodes2 = $cloneContents($getSelection());

expect(selectedNodes2.range).toEqual([
paragraph1.getKey(),
paragraph2.getKey(),
paragraph3.getKey(),
]);
expect(selectedNodes2.nodeMap[0][0]).toEqual(text1.getKey());
expect((selectedNodes2.nodeMap[0][1] as TextNode).__text).toBe('irst');
expect(selectedNodes2.nodeMap[1][0]).toEqual(paragraph1.getKey());
expect(selectedNodes2.nodeMap[2][0]).toEqual(paragraph2.getKey());
expect(selectedNodes2.nodeMap[3][0]).toEqual(text2.getKey());
expect(selectedNodes2.nodeMap[4][0]).toEqual(paragraph3.getKey());
expect(selectedNodes2.nodeMap[5][0]).toEqual(text3.getKey());
expect((selectedNodes2.nodeMap[5][1] as TextNode).__text).toBe('Thir');
});
});

test('range with excludeFromCopy nodes', async () => {
const editor = createTestEditor();

const element = document.createElement('div');

editor.setRootElement(element);

await editor.update(() => {
const root = $getRoot();

const paragraph = $createParagraphNode();

root.append(paragraph);

const excludeElementNode1 = $createTestExcludeFromCopyElementNode();

paragraph.append(excludeElementNode1);

paragraph.select(0, 0);

const selectedNodes1 = $cloneContents($getSelection());

expect(selectedNodes1.range).toEqual([]);

const text1 = $createTextNode('1');

excludeElementNode1.append(text1);

excludeElementNode1.select(0, 0);

const selectedNodes2 = $cloneContents($getSelection());

expect(selectedNodes2.range).toEqual([paragraph.getKey()]);

paragraph.select(0, 0);

const selectedNodes3 = $cloneContents($getSelection());

expect(selectedNodes3.range).toEqual([paragraph.getKey()]);

const text2 = $createTextNode('2');

excludeElementNode1.insertAfter(text2);

paragraph.select(0, 2);

const selectedNodes4 = $cloneContents($getSelection());

expect(selectedNodes4.range).toEqual([paragraph.getKey()]);
expect(selectedNodes4.nodeMap[0][0]).toEqual(text1.getKey());
expect(selectedNodes4.nodeMap[1][0]).toEqual(paragraph.getKey());
expect(selectedNodes4.nodeMap[2][0]).toEqual(text2.getKey());

const text3 = $createTextNode('3');

excludeElementNode1.append(text3);

paragraph.select(0, 2);

const selectedNodes5 = $cloneContents($getSelection());

expect(selectedNodes5.range).toEqual([paragraph.getKey()]);
expect(selectedNodes5.nodeMap[0][0]).toEqual(text1.getKey());
expect(selectedNodes5.nodeMap[1][0]).toEqual(paragraph.getKey());
expect(selectedNodes5.nodeMap[2][0]).toEqual(text3.getKey());
expect(selectedNodes5.nodeMap[3][0]).toEqual(text2.getKey());

const testElementNode = $createTestElementNode();
const excludeElementNode2 = $createTestExcludeFromCopyElementNode();
const text4 = $createTextNode('4');

text1.insertBefore(testElementNode);

testElementNode.append(excludeElementNode2);
excludeElementNode2.append(text4);

paragraph.select(0, 3);

const selectedNodes6 = $cloneContents($getSelection());

expect(selectedNodes6.range).toEqual([paragraph.getKey()]);
expect(selectedNodes6.nodeMap[0][0]).toEqual(text4.getKey());
expect(selectedNodes6.nodeMap[1][0]).toEqual(testElementNode.getKey());
expect(selectedNodes6.nodeMap[2][0]).toEqual(paragraph.getKey());
expect(selectedNodes6.nodeMap[3][0]).toEqual(text1.getKey());
expect(selectedNodes6.nodeMap[4][0]).toEqual(text3.getKey());
expect(selectedNodes6.nodeMap[5][0]).toEqual(text2.getKey());

text4.remove();

paragraph.select(0, 3);

const selectedNodes7 = $cloneContents($getSelection());

expect(selectedNodes7.range).toEqual([paragraph.getKey()]);
expect(selectedNodes7.nodeMap[0][0]).toEqual(testElementNode.getKey());
expect(selectedNodes7.nodeMap[1][0]).toEqual(paragraph.getKey());
expect(selectedNodes7.nodeMap[2][0]).toEqual(text1.getKey());
expect(selectedNodes7.nodeMap[3][0]).toEqual(text3.getKey());
expect(selectedNodes7.nodeMap[4][0]).toEqual(text2.getKey());
});
});

Expand Down
29 changes: 0 additions & 29 deletions packages/lexical-selection/src/grid-selection.ts

This file was deleted.

Loading

0 comments on commit da2e863

Please sign in to comment.