Skip to content

Commit 125442b

Browse files
fix: cut/copy handling in non-editable blocks and empty selections (#1427)
1 parent 2b4b252 commit 125442b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

packages/core/src/api/clipboard/toClipboard/copyExtension.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,34 @@ export function selectedFragmentToHTML<
146146
return { clipboardHTML, externalHTML, markdown };
147147
}
148148

149+
const checkIfSelectionInNonEditableBlock = () => {
150+
// Let browser handle event if selection is empty (nothing
151+
// happens).
152+
const selection = window.getSelection();
153+
if (!selection || selection.isCollapsed) {
154+
return true;
155+
}
156+
157+
// Let browser handle event if it's within a non-editable
158+
// "island". This means it's in selectable content within a
159+
// non-editable block. We only need to check one node as it's
160+
// not possible for the browser selection to start in an
161+
// editable block and end in a non-editable one.
162+
let node = selection.focusNode;
163+
while (node) {
164+
if (
165+
node instanceof HTMLElement &&
166+
node.getAttribute("contenteditable") === "false"
167+
) {
168+
return true;
169+
}
170+
171+
node = node.parentElement;
172+
}
173+
174+
return false;
175+
};
176+
149177
const copyToClipboard = <
150178
BSchema extends BlockSchema,
151179
I extends InlineContentSchema,
@@ -186,11 +214,19 @@ export const createCopyToClipboardExtension = <
186214
props: {
187215
handleDOMEvents: {
188216
copy(view, event) {
217+
if (checkIfSelectionInNonEditableBlock()) {
218+
return true;
219+
}
220+
189221
copyToClipboard(editor, view, event);
190222
// Prevent default PM handler to be called
191223
return true;
192224
},
193225
cut(view, event) {
226+
if (checkIfSelectionInNonEditableBlock()) {
227+
return true;
228+
}
229+
194230
copyToClipboard(editor, view, event);
195231
if (view.editable) {
196232
view.dispatch(view.state.tr.deleteSelection());

0 commit comments

Comments
 (0)