Skip to content

Commit 5026e27

Browse files
Fixed cut operations when a block is selected (#1071)
1 parent ff8971b commit 5026e27

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

packages/core/src/api/exporters/copyExtension.ts

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function selectedFragmentToHTML<
2424
}> {
2525
const selectedFragment = view.state.selection.content().content;
2626

27-
const internalHTMLSerializer = await createInternalHTMLSerializer(
27+
const internalHTMLSerializer = createInternalHTMLSerializer(
2828
view.state.schema,
2929
editor
3030
);
@@ -48,6 +48,46 @@ async function selectedFragmentToHTML<
4848
return { internalHTML, externalHTML, plainText };
4949
}
5050

51+
const copyToClipboard = <
52+
BSchema extends BlockSchema,
53+
I extends InlineContentSchema,
54+
S extends StyleSchema
55+
>(
56+
editor: BlockNoteEditor<BSchema, I, S>,
57+
view: EditorView,
58+
event: ClipboardEvent
59+
) => {
60+
// Stops the default browser copy behaviour.
61+
event.preventDefault();
62+
event.clipboardData!.clearData();
63+
64+
// Checks if a `blockContent` node is being copied and expands
65+
// the selection to the parent `blockContainer` node. This is
66+
// for the use-case in which only a block without content is
67+
// selected, e.g. an image block.
68+
if (
69+
"node" in view.state.selection &&
70+
(view.state.selection.node as Node).type.spec.group === "blockContent"
71+
) {
72+
editor.dispatch(
73+
editor._tiptapEditor.state.tr.setSelection(
74+
new NodeSelection(view.state.doc.resolve(view.state.selection.from - 1))
75+
)
76+
);
77+
}
78+
79+
(async () => {
80+
const { internalHTML, externalHTML, plainText } =
81+
await selectedFragmentToHTML(view, editor);
82+
83+
// TODO: Writing to other MIME types not working in Safari for
84+
// some reason.
85+
event.clipboardData!.setData("blocknote/html", internalHTML);
86+
event.clipboardData!.setData("text/html", externalHTML);
87+
event.clipboardData!.setData("text/plain", plainText);
88+
})();
89+
};
90+
5191
export const createCopyToClipboardExtension = <
5292
BSchema extends BlockSchema,
5393
I extends InlineContentSchema,
@@ -63,38 +103,13 @@ export const createCopyToClipboardExtension = <
63103
props: {
64104
handleDOMEvents: {
65105
copy(view, event) {
66-
// Stops the default browser copy behaviour.
67-
event.preventDefault();
68-
event.clipboardData!.clearData();
69-
70-
// Checks if a `blockContent` node is being copied and expands
71-
// the selection to the parent `blockContainer` node. This is
72-
// for the use-case in which only a block without content is
73-
// selected, e.g. an image block.
74-
if (
75-
"node" in view.state.selection &&
76-
(view.state.selection.node as Node).type.spec.group ===
77-
"blockContent"
78-
) {
79-
editor.dispatch(
80-
editor._tiptapEditor.state.tr.setSelection(
81-
new NodeSelection(
82-
view.state.doc.resolve(view.state.selection.from - 1)
83-
)
84-
)
85-
);
86-
}
87-
88-
(async () => {
89-
const { internalHTML, externalHTML, plainText } =
90-
await selectedFragmentToHTML(view, editor);
91-
92-
// TODO: Writing to other MIME types not working in Safari for
93-
// some reason.
94-
event.clipboardData!.setData("blocknote/html", internalHTML);
95-
event.clipboardData!.setData("text/html", externalHTML);
96-
event.clipboardData!.setData("text/plain", plainText);
97-
})();
106+
copyToClipboard(editor, view, event);
107+
// Prevent default PM handler to be called
108+
return true;
109+
},
110+
cut(view, event) {
111+
copyToClipboard(editor, view, event);
112+
view.dispatch(view.state.tr.deleteSelection());
98113
// Prevent default PM handler to be called
99114
return true;
100115
},

0 commit comments

Comments
 (0)