mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
Removing the first node from the pasted slice kept the original open depths, which are no longer valid for the remaining content. When the clipboard held a single top-level node the content became empty while openEnd stayed above zero, and ProseMirror walked into a null child. Fixes OUTLINE-CLOUD-51V
71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
import { Fragment, Schema, Slice } from "prosemirror-model";
|
|
import { EditorState, Selection } from "prosemirror-state";
|
|
import { sliceWithoutFirstNode } from "./sliceWithoutFirstNode";
|
|
|
|
const schema = new Schema({
|
|
nodes: {
|
|
doc: { content: "block+" },
|
|
paragraph: { content: "inline*", group: "block" },
|
|
horizontal_rule: { group: "block" },
|
|
text: { group: "inline" },
|
|
},
|
|
});
|
|
|
|
const paragraph = (text: string) =>
|
|
schema.nodes.paragraph.create(null, text ? schema.text(text) : null);
|
|
|
|
describe("sliceWithoutFirstNode", () => {
|
|
it("returns undefined for an empty slice", () => {
|
|
expect(sliceWithoutFirstNode(Slice.empty)).toBeUndefined();
|
|
});
|
|
|
|
it("returns undefined when the slice holds a single node", () => {
|
|
const slice = new Slice(Fragment.from(paragraph("one")), 1, 1);
|
|
expect(sliceWithoutFirstNode(slice)).toBeUndefined();
|
|
});
|
|
|
|
it("removes the first node and keeps the open depths", () => {
|
|
const slice = new Slice(
|
|
Fragment.fromArray([paragraph("one"), paragraph("two")]),
|
|
1,
|
|
1
|
|
);
|
|
const result = sliceWithoutFirstNode(slice);
|
|
|
|
expect(result?.content.childCount).toBe(1);
|
|
expect(result?.content.firstChild?.textContent).toBe("two");
|
|
expect(result?.openStart).toBe(1);
|
|
expect(result?.openEnd).toBe(1);
|
|
});
|
|
|
|
it("clamps the open depths to leaf nodes", () => {
|
|
const slice = new Slice(
|
|
Fragment.fromArray([
|
|
paragraph("one"),
|
|
schema.nodes.horizontal_rule.create(),
|
|
]),
|
|
1,
|
|
1
|
|
);
|
|
const result = sliceWithoutFirstNode(slice);
|
|
|
|
expect(result?.content.childCount).toBe(1);
|
|
expect(result?.openStart).toBe(0);
|
|
expect(result?.openEnd).toBe(0);
|
|
});
|
|
|
|
it("returns a slice that can replace a selection", () => {
|
|
const slice = new Slice(
|
|
Fragment.fromArray([paragraph("one"), paragraph("two")]),
|
|
1,
|
|
1
|
|
);
|
|
const state = EditorState.create({ schema });
|
|
const tr = state.tr
|
|
.setSelection(Selection.atStart(state.doc))
|
|
.replaceSelection(sliceWithoutFirstNode(slice) ?? Slice.empty);
|
|
|
|
expect(tr.doc.textContent).toBe("two");
|
|
});
|
|
});
|