Skip to content

fix: Cut events when a block is selected #1071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 48 additions & 33 deletions packages/core/src/api/exporters/copyExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function selectedFragmentToHTML<
}> {
const selectedFragment = view.state.selection.content().content;

const internalHTMLSerializer = await createInternalHTMLSerializer(
const internalHTMLSerializer = createInternalHTMLSerializer(
view.state.schema,
editor
);
Expand All @@ -48,6 +48,46 @@ async function selectedFragmentToHTML<
return { internalHTML, externalHTML, plainText };
}

const copyToClipboard = <
BSchema extends BlockSchema,
I extends InlineContentSchema,
S extends StyleSchema
>(
editor: BlockNoteEditor<BSchema, I, S>,
view: EditorView,
event: ClipboardEvent
) => {
// Stops the default browser copy behaviour.
event.preventDefault();
event.clipboardData!.clearData();

// Checks if a `blockContent` node is being copied and expands
// the selection to the parent `blockContainer` node. This is
// for the use-case in which only a block without content is
// selected, e.g. an image block.
if (
"node" in view.state.selection &&
(view.state.selection.node as Node).type.spec.group === "blockContent"
) {
editor.dispatch(
editor._tiptapEditor.state.tr.setSelection(
new NodeSelection(view.state.doc.resolve(view.state.selection.from - 1))
)
);
}

(async () => {
const { internalHTML, externalHTML, plainText } =
await selectedFragmentToHTML(view, editor);

// TODO: Writing to other MIME types not working in Safari for
// some reason.
event.clipboardData!.setData("blocknote/html", internalHTML);
event.clipboardData!.setData("text/html", externalHTML);
event.clipboardData!.setData("text/plain", plainText);
})();
};

export const createCopyToClipboardExtension = <
BSchema extends BlockSchema,
I extends InlineContentSchema,
Expand All @@ -63,38 +103,13 @@ export const createCopyToClipboardExtension = <
props: {
handleDOMEvents: {
copy(view, event) {
// Stops the default browser copy behaviour.
event.preventDefault();
event.clipboardData!.clearData();

// Checks if a `blockContent` node is being copied and expands
// the selection to the parent `blockContainer` node. This is
// for the use-case in which only a block without content is
// selected, e.g. an image block.
if (
"node" in view.state.selection &&
(view.state.selection.node as Node).type.spec.group ===
"blockContent"
) {
editor.dispatch(
editor._tiptapEditor.state.tr.setSelection(
new NodeSelection(
view.state.doc.resolve(view.state.selection.from - 1)
)
)
);
}

(async () => {
const { internalHTML, externalHTML, plainText } =
await selectedFragmentToHTML(view, editor);

// TODO: Writing to other MIME types not working in Safari for
// some reason.
event.clipboardData!.setData("blocknote/html", internalHTML);
event.clipboardData!.setData("text/html", externalHTML);
event.clipboardData!.setData("text/plain", plainText);
})();
copyToClipboard(editor, view, event);
// Prevent default PM handler to be called
return true;
},
cut(view, event) {
copyToClipboard(editor, view, event);
view.dispatch(view.state.tr.deleteSelection());
// Prevent default PM handler to be called
return true;
},
Expand Down
Loading