103 lines
2.4 KiB
TypeScript
103 lines
2.4 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import {
|
|
createRegistry,
|
|
defineBlock,
|
|
defineDocumentKind,
|
|
json,
|
|
object,
|
|
record,
|
|
string,
|
|
type BlockDocument,
|
|
} from "@wrnexus/blocks";
|
|
import { createBlockEditorState } from "../src/index.ts";
|
|
|
|
const styles = record(json) as never;
|
|
const definitions = [
|
|
defineBlock({
|
|
manifest: {
|
|
type: "layout.root",
|
|
version: 1,
|
|
name: { key: "root", fallback: "Root" },
|
|
icon: "root",
|
|
category: "layout",
|
|
props: object({}) as never,
|
|
styles,
|
|
slots: { content: { accepts: { capabilities: ["content"] } } },
|
|
},
|
|
create: ({ id }) => ({
|
|
id: id(),
|
|
type: "layout.root",
|
|
version: 1,
|
|
props: {},
|
|
styles: {},
|
|
slots: { content: [] },
|
|
}),
|
|
}),
|
|
defineBlock({
|
|
manifest: {
|
|
type: "content.text",
|
|
version: 1,
|
|
name: { key: "text", fallback: "Text" },
|
|
icon: "text",
|
|
category: "content",
|
|
capabilities: ["content"],
|
|
props: object({ text: string() }) as never,
|
|
styles,
|
|
slots: {},
|
|
},
|
|
create: ({ id }) => ({
|
|
id: id(),
|
|
type: "content.text",
|
|
version: 1,
|
|
props: { text: "" },
|
|
styles: {},
|
|
slots: {},
|
|
}),
|
|
}),
|
|
];
|
|
const kind = defineDocumentKind({
|
|
kind: "test",
|
|
version: 1,
|
|
metadata: object({ title: string() }) as never,
|
|
rootTypes: ["layout.root"],
|
|
blocks: definitions,
|
|
variables: {},
|
|
breakpoints: {},
|
|
capabilities: [],
|
|
targets: [],
|
|
limits: {
|
|
maxBytes: 10000,
|
|
maxNodes: 20,
|
|
maxDepth: 5,
|
|
maxChildrenPerSlot: 10,
|
|
maxStringLength: 100,
|
|
maxOutputBytes: 1000,
|
|
},
|
|
urlSchemes: ["https"],
|
|
});
|
|
const registry = createRegistry(definitions).freeze();
|
|
const document: BlockDocument = {
|
|
schemaVersion: 1,
|
|
kind: "test",
|
|
meta: { title: "x" },
|
|
theme: { tokens: {} },
|
|
root: definitions[0]!.create({ id: () => "root" }),
|
|
};
|
|
|
|
test("editor state executes undo and redo", () => {
|
|
const state = createBlockEditorState({ document, revision: 1, kind, registry });
|
|
const node = definitions[1]!.create({ id: () => "text" });
|
|
expect(
|
|
state.execute({
|
|
type: "insert",
|
|
location: { parentId: "root", slot: "content", index: 0 },
|
|
nodes: [node],
|
|
}).ok,
|
|
).toBe(true);
|
|
expect(state.get().dirty).toBe(true);
|
|
expect(state.undo()).toBe(true);
|
|
expect(state.get().document.root.slots.content).toEqual([]);
|
|
expect(state.redo()).toBe(true);
|
|
expect(state.get().document.root.slots.content[0]?.id).toBe("text");
|
|
});
|