Skip to content

Commit

Permalink
Implemented PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewlipski committed Oct 3, 2024
1 parent 3769b71 commit 817e3aa
Show file tree
Hide file tree
Showing 11 changed files with 877 additions and 47 deletions.
32 changes: 7 additions & 25 deletions packages/core/src/api/clipboard/clipboard.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Node } from "prosemirror-model";
import { NodeSelection, Selection, TextSelection } from "prosemirror-state";
import { CellSelection } from "prosemirror-tables";
import * as pmView from "prosemirror-view";
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";

import { PartialBlock } from "../../blocks/defaultBlocks";
import { BlockNoteEditor } from "../../editor/BlockNoteEditor";
import { doPaste } from "../testUtil/paste";
import { initializeESMDependencies } from "../../util/esmDependencies";
import { selectedFragmentToHTML } from "./toClipboard/copyExtension";

Expand Down Expand Up @@ -173,22 +173,6 @@ describe("Test ProseMirror selection clipboard HTML", () => {
)
);

if (
"node" in editor._tiptapEditor.view.state.selection &&
(editor._tiptapEditor.view.state.selection.node as Node).type.spec
.group === "blockContent"
) {
editor.dispatch(
editor._tiptapEditor.state.tr.setSelection(
new NodeSelection(
editor._tiptapEditor.view.state.doc.resolve(
editor._tiptapEditor.view.state.selection.from - 1
)
)
)
);
}

const { clipboardHTML, externalHTML } = await selectedFragmentToHTML(
editor._tiptapEditor.view,
editor
Expand All @@ -199,14 +183,12 @@ describe("Test ProseMirror selection clipboard HTML", () => {
);

const originalDocument = editor.document;
editor._tiptapEditor.state.tr.replaceSelection(
(pmView as any).__parseFromClipboard(
editor._tiptapEditor.view,
"text",
clipboardHTML,
false,
editor._tiptapEditor.view.state.selection.$from
)
doPaste(
editor._tiptapEditor.view,
"text",
clipboardHTML,
false,
new ClipboardEvent("paste")
);
const newDocument = editor.document;

Expand Down
30 changes: 15 additions & 15 deletions packages/core/src/api/clipboard/toClipboard/copyExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ export async function selectedFragmentToHTML<
externalHTML: string;
markdown: string;
}> {
// 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))
)
);
}

// Uses default ProseMirror clipboard serialization.
const clipboardHTML: string = (pmView as any).__serializeForClipboard(
view,
Expand Down Expand Up @@ -88,21 +103,6 @@ const copyToClipboard = <
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 { clipboardHTML, externalHTML, markdown } =
await selectedFragmentToHTML(view, editor);
Expand Down
Loading

0 comments on commit 817e3aa

Please sign in to comment.