Skip to content

Commit 7047a16

Browse files
authored
Merge pull request #4447 from udecode/getSelectedDomFragment
Add `getSelectedDomFragment`
2 parents 0d37d31 + 39ebdd9 commit 7047a16

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@platejs/core': patch
3+
---
4+
5+
- Added `getSelectedDomFragment` utility function that returns Slate nodes from DOM selection.
6+
- Deprecated `getSelectedDomBlocks`. Use `getSelectedDomFragment` instead.

packages/core/src/lib/static/plugins/ViewPlugin.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import type { Descendant } from '@platejs/slate';
2-
31
import { DOMPlugin } from '../../plugins';
42
import { getPlainText } from '../internal/getPlainText';
5-
import { getSelectedDomBlocks } from '../utils/getSelectedDomBlocks';
3+
import { getSelectedDomFragment } from '../utils/getSelectedDomFragment';
64
import { getSelectedDomNode } from '../utils/getSelectedDomNode';
75
import { isSelectOutside } from '../utils/isSelectOutside';
86

@@ -12,29 +10,17 @@ export const ViewPlugin = DOMPlugin.overrideEditor(
1210
setFragmentData(data, originEvent) {
1311
if (originEvent !== 'copy') return setFragmentData(data, originEvent);
1412

15-
const domBlocks = getSelectedDomBlocks();
13+
const fragment = getSelectedDomFragment(editor);
1614
const html = getSelectedDomNode();
1715

18-
if (!html || !domBlocks) return;
16+
if (!html || !fragment) return;
1917

2018
const selectOutside = isSelectOutside(html);
2119

2220
if (selectOutside) return;
2321

2422
// only crossing multiple blocks
25-
if (domBlocks.length > 0) {
26-
const fragment: Descendant[] = [];
27-
28-
Array.from(domBlocks).forEach((node: any) => {
29-
const blockId = node.dataset.slateId;
30-
const block = editor.api.node({ id: blockId, at: [] });
31-
32-
// prevent inline elements like link and table cells.
33-
if (block && block[1].length === 1) {
34-
fragment.push(block[0]);
35-
}
36-
});
37-
23+
if (fragment.length > 0) {
3824
const string = JSON.stringify(fragment);
3925
const encoded = window.btoa(encodeURIComponent(string));
4026

packages/core/src/lib/static/utils/getSelectedDomBlocks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** Get the slate nodes from the DOM selection */
2+
/** @deprecated use getSelectedDomFragment instead */
23
export const getSelectedDomBlocks = () => {
34
const selection = window.getSelection();
45

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { Descendant } from "@platejs/slate";
2+
3+
import type { SlateEditor } from "../../editor";
4+
5+
export const getSelectedDomFragment = (editor: SlateEditor): Descendant[] => {
6+
const selection = window.getSelection();
7+
8+
if (!selection || selection.rangeCount === 0) return [];
9+
10+
const range = selection.getRangeAt(0);
11+
const fragment = range.cloneContents();
12+
13+
const _domBlocks = fragment.querySelectorAll(
14+
'[data-slate-node="element"][data-slate-id]'
15+
);
16+
17+
const domBlocks = Array.from(_domBlocks);
18+
19+
20+
if (domBlocks.length === 0) return [];
21+
22+
const nodes: Descendant[] = [];
23+
24+
domBlocks.forEach((node: any) => {
25+
const blockId = (node as HTMLElement).dataset.slateId;
26+
const block = editor.api.node({ id: blockId, at: [] });
27+
28+
// prevent inline elements like link and table cells.
29+
if (block && block[1].length === 1) {
30+
nodes.push(block[0]);
31+
}
32+
});
33+
34+
35+
return nodes;
36+
};

packages/core/src/lib/static/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './createStaticString';
66
export * from './getNodeDataAttributes';
77
export * from './getRenderNodeStaticProps';
88
export * from './getSelectedDomBlocks';
9+
export * from './getSelectedDomFragment';
910
export * from './getSelectedDomNode';
1011
export * from './isSelectOutside';
1112
export * from './pipeDecorate';

0 commit comments

Comments
 (0)