@zsherman/canvas 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +373 -0
- package/README.md +15 -2
- package/THIRD_PARTY_NOTICES.md +117 -0
- package/dist/dom/canvas/assets.js +89 -0
- package/dist/dom/canvas/board-store.js +56 -0
- package/dist/dom/canvas/canvas-actions.js +78 -0
- package/dist/dom/canvas/canvas-board.js +557 -0
- package/dist/dom/canvas/canvas-board.styles.js +110 -0
- package/dist/dom/canvas/canvas-chrome.js +1167 -0
- package/dist/dom/canvas/canvas-chrome.styles.js +220 -0
- package/dist/dom/canvas/canvas-comments.js +950 -0
- package/dist/dom/canvas/canvas-comments.styles.js +231 -0
- package/dist/dom/canvas/canvas-context.js +196 -0
- package/dist/dom/canvas/canvas-controller.js +697 -0
- package/dist/dom/canvas/canvas-cursor-chat.js +120 -0
- package/dist/dom/canvas/canvas-cursor-chat.styles.js +55 -0
- package/dist/dom/canvas/canvas-menus.js +276 -0
- package/dist/dom/canvas/canvas-menus.styles.js +7 -0
- package/dist/dom/canvas/canvas-style-bar.js +930 -0
- package/dist/dom/canvas/canvas-style-bar.styles.js +52 -0
- package/dist/dom/canvas/canvas-surface.js +1353 -0
- package/dist/dom/canvas/canvas-surface.styles.js +345 -0
- package/dist/dom/canvas/canvas-tool-rail.js +406 -0
- package/dist/dom/canvas/canvas-tool-rail.styles.js +20 -0
- package/dist/dom/canvas/comments.js +58 -0
- package/dist/dom/canvas/equality.js +13 -0
- package/dist/dom/canvas/export-png.js +496 -0
- package/dist/dom/canvas/gesture-machine.js +91 -0
- package/dist/dom/canvas/handlers.js +18 -0
- package/dist/dom/canvas/index.js +9 -0
- package/dist/dom/canvas/input.js +85 -0
- package/dist/dom/canvas/instant.js +28 -0
- package/dist/dom/canvas/kernel.js +39 -0
- package/dist/dom/canvas/motion.js +686 -0
- package/dist/dom/canvas/palette.js +165 -0
- package/dist/dom/canvas/random-id.js +15 -0
- package/dist/dom/canvas/scene-renderer.js +559 -0
- package/dist/dom/canvas/scene.styles.js +7 -0
- package/dist/dom/canvas/surface-model.js +324 -0
- package/dist/dom/canvas/uploads.js +294 -0
- package/dist/dom/index.js +9 -0
- package/dist/dom/kernel/actions.js +455 -0
- package/dist/dom/kernel/board.js +856 -0
- package/dist/dom/kernel/claims.js +40 -0
- package/dist/dom/kernel/clipboard.js +30 -0
- package/dist/dom/kernel/constants.js +210 -0
- package/dist/dom/kernel/contract.js +0 -0
- package/dist/dom/kernel/decode.js +85 -0
- package/dist/dom/kernel/editor.js +2077 -0
- package/dist/dom/kernel/errors.js +82 -0
- package/dist/dom/kernel/fonts/inter.js +7924 -0
- package/dist/dom/kernel/fractional-index.js +54 -0
- package/dist/dom/kernel/geometry.js +123 -0
- package/dist/dom/kernel/hit-test.js +156 -0
- package/dist/dom/kernel/index.js +22 -0
- package/dist/dom/kernel/inspect.js +116 -0
- package/dist/dom/kernel/kernel.js +251 -0
- package/dist/dom/kernel/outline.js +64 -0
- package/dist/dom/kernel/plan.js +1370 -0
- package/dist/dom/kernel/rev.js +173 -0
- package/dist/dom/kernel/scene.js +580 -0
- package/dist/dom/kernel/schema.js +1109 -0
- package/dist/dom/kernel/snap.js +258 -0
- package/dist/dom/kernel/svg.js +148 -0
- package/dist/dom/kernel/text.js +142 -0
- package/dist/solid/canvas/assets.jsx +89 -0
- package/dist/solid/canvas/board-store.jsx +56 -0
- package/dist/solid/canvas/canvas-actions.jsx +78 -0
- package/dist/solid/canvas/canvas-board.jsx +326 -0
- package/dist/solid/canvas/canvas-board.styles.jsx +110 -0
- package/dist/solid/canvas/canvas-chrome.jsx +612 -0
- package/dist/solid/canvas/canvas-chrome.styles.jsx +220 -0
- package/dist/solid/canvas/canvas-comments.jsx +435 -0
- package/dist/solid/canvas/canvas-comments.styles.jsx +231 -0
- package/dist/solid/canvas/canvas-context.jsx +174 -0
- package/dist/solid/canvas/canvas-controller.jsx +697 -0
- package/dist/solid/canvas/canvas-cursor-chat.jsx +87 -0
- package/dist/solid/canvas/canvas-cursor-chat.styles.jsx +55 -0
- package/dist/solid/canvas/canvas-menus.jsx +223 -0
- package/dist/solid/canvas/canvas-menus.styles.jsx +7 -0
- package/dist/solid/canvas/canvas-style-bar.jsx +486 -0
- package/dist/solid/canvas/canvas-style-bar.styles.jsx +52 -0
- package/dist/solid/canvas/canvas-surface.jsx +681 -0
- package/dist/solid/canvas/canvas-surface.styles.jsx +345 -0
- package/dist/solid/canvas/canvas-tool-rail.jsx +195 -0
- package/dist/solid/canvas/canvas-tool-rail.styles.jsx +20 -0
- package/dist/solid/canvas/comments.jsx +58 -0
- package/dist/solid/canvas/equality.jsx +13 -0
- package/dist/solid/canvas/export-png.jsx +496 -0
- package/dist/solid/canvas/gesture-machine.jsx +91 -0
- package/dist/solid/canvas/handlers.jsx +18 -0
- package/dist/solid/canvas/index.jsx +9 -0
- package/dist/solid/canvas/input.jsx +85 -0
- package/dist/solid/canvas/instant.jsx +28 -0
- package/dist/solid/canvas/kernel.jsx +39 -0
- package/dist/solid/canvas/motion.jsx +686 -0
- package/dist/solid/canvas/palette.jsx +165 -0
- package/dist/solid/canvas/random-id.jsx +15 -0
- package/dist/solid/canvas/scene-renderer.jsx +559 -0
- package/dist/solid/canvas/scene.styles.jsx +7 -0
- package/dist/solid/canvas/surface-model.jsx +324 -0
- package/dist/solid/canvas/uploads.jsx +294 -0
- package/dist/solid/index.jsx +9 -0
- package/dist/solid/kernel/actions.jsx +455 -0
- package/dist/solid/kernel/board.jsx +856 -0
- package/dist/solid/kernel/claims.jsx +40 -0
- package/dist/solid/kernel/clipboard.jsx +30 -0
- package/dist/solid/kernel/constants.jsx +210 -0
- package/dist/solid/kernel/contract.jsx +0 -0
- package/dist/solid/kernel/decode.jsx +85 -0
- package/dist/solid/kernel/editor.jsx +2077 -0
- package/dist/solid/kernel/errors.jsx +82 -0
- package/dist/solid/kernel/fonts/inter.jsx +7924 -0
- package/dist/solid/kernel/fractional-index.jsx +54 -0
- package/dist/solid/kernel/geometry.jsx +123 -0
- package/dist/solid/kernel/hit-test.jsx +156 -0
- package/dist/solid/kernel/index.jsx +22 -0
- package/dist/solid/kernel/inspect.jsx +116 -0
- package/dist/solid/kernel/kernel.jsx +251 -0
- package/dist/solid/kernel/outline.jsx +64 -0
- package/dist/solid/kernel/plan.jsx +1370 -0
- package/dist/solid/kernel/rev.jsx +173 -0
- package/dist/solid/kernel/scene.jsx +580 -0
- package/dist/solid/kernel/schema.jsx +1109 -0
- package/dist/solid/kernel/snap.jsx +258 -0
- package/dist/solid/kernel/svg.jsx +148 -0
- package/dist/solid/kernel/text.jsx +142 -0
- package/dist/styles.css +1 -0
- package/dist/types/canvas/assets.d.ts +111 -0
- package/dist/types/canvas/board-store.d.ts +86 -0
- package/dist/types/canvas/canvas-actions.d.ts +21 -0
- package/dist/types/canvas/canvas-board.d.ts +53 -0
- package/dist/types/canvas/canvas-board.styles.d.ts +94 -0
- package/dist/types/canvas/canvas-chrome.d.ts +109 -0
- package/dist/types/canvas/canvas-chrome.styles.d.ts +193 -0
- package/dist/types/canvas/canvas-comments.d.ts +20 -0
- package/dist/types/canvas/canvas-comments.styles.d.ts +208 -0
- package/dist/types/canvas/canvas-context.d.ts +104 -0
- package/dist/types/canvas/canvas-controller.d.ts +159 -0
- package/dist/types/canvas/canvas-cursor-chat.d.ts +13 -0
- package/dist/types/canvas/canvas-cursor-chat.styles.d.ts +54 -0
- package/dist/types/canvas/canvas-menus.d.ts +21 -0
- package/dist/types/canvas/canvas-menus.styles.d.ts +6 -0
- package/dist/types/canvas/canvas-style-bar.d.ts +19 -0
- package/dist/types/canvas/canvas-style-bar.styles.d.ts +43 -0
- package/dist/types/canvas/canvas-surface.d.ts +15 -0
- package/dist/types/canvas/canvas-surface.styles.d.ts +313 -0
- package/dist/types/canvas/canvas-tool-rail.d.ts +41 -0
- package/dist/types/canvas/canvas-tool-rail.styles.d.ts +15 -0
- package/dist/types/canvas/comments.d.ts +106 -0
- package/dist/types/canvas/equality.d.ts +4 -0
- package/dist/types/canvas/export-png.d.ts +77 -0
- package/dist/types/canvas/gesture-machine.d.ts +46 -0
- package/dist/types/canvas/handlers.d.ts +7 -0
- package/dist/types/canvas/index.d.ts +18 -0
- package/dist/types/canvas/input.d.ts +63 -0
- package/dist/types/canvas/instant.d.ts +13 -0
- package/dist/types/canvas/kernel.d.ts +27 -0
- package/dist/types/canvas/motion.d.ts +72 -0
- package/dist/types/canvas/palette.d.ts +46 -0
- package/dist/types/canvas/random-id.d.ts +6 -0
- package/dist/types/canvas/scene-renderer.d.ts +27 -0
- package/dist/types/canvas/scene.styles.d.ts +4 -0
- package/dist/types/canvas/surface-model.d.ts +113 -0
- package/dist/types/canvas/uploads.d.ts +58 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/kernel/actions.d.ts +19 -0
- package/dist/types/kernel/board.d.ts +137 -0
- package/dist/types/kernel/claims.d.ts +29 -0
- package/dist/types/kernel/clipboard.d.ts +10 -0
- package/dist/types/kernel/constants.d.ts +195 -0
- package/dist/types/kernel/contract.d.ts +1533 -0
- package/dist/types/kernel/decode.d.ts +60 -0
- package/dist/types/kernel/editor.d.ts +12 -0
- package/dist/types/kernel/errors.d.ts +11 -0
- package/dist/types/kernel/fractional-index.d.ts +7 -0
- package/dist/types/kernel/geometry.d.ts +42 -0
- package/dist/types/kernel/hit-test.d.ts +16 -0
- package/dist/types/kernel/index.d.ts +8 -0
- package/dist/types/kernel/inspect.d.ts +6 -0
- package/dist/types/kernel/kernel.d.ts +6 -0
- package/dist/types/kernel/outline.d.ts +4 -0
- package/dist/types/kernel/plan.d.ts +20 -0
- package/dist/types/kernel/rev.d.ts +21 -0
- package/dist/types/kernel/scene.d.ts +16 -0
- package/dist/types/kernel/schema.d.ts +3436 -0
- package/dist/types/kernel/snap.d.ts +41 -0
- package/dist/types/kernel/svg.d.ts +3 -0
- package/dist/types/kernel/text.d.ts +21 -0
- package/docs/api.json +207 -0
- package/docs/canvas.md +444 -0
- package/docs/kernel.md +108 -0
- package/docs/llms.txt +41 -0
- package/docs/search-index.json +52 -0
- package/package.json +55 -4
package/docs/canvas.md
ADDED
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
# Canvas
|
|
2
|
+
|
|
3
|
+
<!-- search: canvas, whiteboard, board, miro, figjam, tldraw, excalidraw, sticky notes, infinite canvas, collaborative board, brainstorm, retro board, mood board, drawing, sketch, diagram, multiplayer cursors, presence -->
|
|
4
|
+
|
|
5
|
+
A shared whiteboard: sticky notes, rectangles and ellipses, text, arrows, pen and highlighter ink, timestamps and images, with live cursors, following, cursor chat, comment threads, agent proposals people keep or discard, undo per person, a context menu, keyboard shortcuts for everything and PNG export. The board is optimistic: a change shows at once and your store confirms or refuses it later.
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { CanvasBoard, createMemoryBoardStore } from "@zsherman/canvas/canvas";
|
|
9
|
+
|
|
10
|
+
function Retro() {
|
|
11
|
+
// One store per board, created once (a component body runs once).
|
|
12
|
+
const store = createMemoryBoardStore({ actor: "ada" });
|
|
13
|
+
return (
|
|
14
|
+
<div style={{ height: "640px" }}>
|
|
15
|
+
<CanvasBoard label="Retro board" store={store} />
|
|
16
|
+
</div>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage guidelines
|
|
22
|
+
|
|
23
|
+
- Use it for a board people share: in a meeting, beside a doc, in a narrow side panel, or one an agent edits beside them. For a diagram people only look at, render the kernel's `toSvg` as an image ([kernel.md](./kernel.md)); for graphs of steps, `@zsherman/viz`'s Flow.
|
|
24
|
+
- **Give it a height.** `CanvasBoard` fills its parent (`width` and `height: 100%`): put it in a box with a height or a flex child with `min-height: 0`.
|
|
25
|
+
- Keep the board's data in your store ([The store](#the-store)). `createMemoryBoardStore` is a single-player board and the reference for the contract.
|
|
26
|
+
- Name each board with `label`, such as "Retro board": screen readers read it, and **Export as PNG** saves `<label>.png`.
|
|
27
|
+
- Load Inter (`@fontsource-variable/inter`): the kernel lays text out with Inter's metrics, so the board draws in it. Another font would wrap where the kernel didn't plan.
|
|
28
|
+
|
|
29
|
+
## Anatomy
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
<CanvasBoard // fills its parent; lays its chrome out by its own size
|
|
33
|
+
store={store} // CanvasBoardStore: the board's data, which your app owns
|
|
34
|
+
label="Retro board" // the board's name
|
|
35
|
+
assets={assets} // CanvasAssets: image storage (optional; adds the image tool)
|
|
36
|
+
comments={comments} // CanvasComments: threads (optional; adds the comment tool)
|
|
37
|
+
status={<CanvasStatus tone="warning">Offline</CanvasStatus>}
|
|
38
|
+
/>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The board draws a surface (the shapes, everyone's cursors and the text editor, `role="application"`) and its chrome over it: the status row top left, presence, export and help top right, the tool rail on the left (a row at the bottom on a narrow board), the style bar top centre while something is selected or a drawing tool is in hand, zoom bottom right, a context menu on the surface and a shortcuts dialog.
|
|
42
|
+
|
|
43
|
+
Layouts follow the board's own size, not the window's: under 960px wide the top-centre bars drop below the top row; under 560px wide (or 520px tall) the tools become a bottom row with an overflow menu, the zoom moves into a menu and the style bar replaces the tools while something is selected.
|
|
44
|
+
|
|
45
|
+
## The store
|
|
46
|
+
|
|
47
|
+
The board owns no data. A `CanvasBoardStore` holds the board, sends each change on and tells the board when the board or other people's presence changes:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import type { BoardJson, PresenceJson, EditorPresenceJson, CommandErrorJson, PlanJson, TransactionJson } from "@zsherman/canvas/kernel";
|
|
51
|
+
|
|
52
|
+
interface CanvasBoardStore {
|
|
53
|
+
/** The principal the viewer acts as. Transactions and presence carry it. */
|
|
54
|
+
readonly actor: string;
|
|
55
|
+
/** One id per open board (this tab), read once at mount; left out, the board makes one up. */
|
|
56
|
+
readonly connection?: string;
|
|
57
|
+
/** The board now: the same object until it changes. */
|
|
58
|
+
readonly getBoard: () => BoardJson;
|
|
59
|
+
/** Everyone else: people, agents, and the viewer's other tabs; never this connection. Same array until it changes. */
|
|
60
|
+
readonly getPresence: () => readonly PresenceJson[];
|
|
61
|
+
/** Called after the board or presence changes. Returns an unsubscribe. */
|
|
62
|
+
readonly subscribe: (listener: () => void) => () => void;
|
|
63
|
+
/** Keep a change the editor planned and applied (see the contract below). */
|
|
64
|
+
readonly commit: (commit: CanvasCommit) => CanvasCommitResult | Promise<CanvasCommitResult>;
|
|
65
|
+
/** The viewer's presence, at most once a frame: relay it to everyone else. */
|
|
66
|
+
readonly publish: (presence: EditorPresenceJson, connection: string) => void;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface CanvasCommit {
|
|
70
|
+
readonly transaction: TransactionJson; // what the viewer did, with its id
|
|
71
|
+
readonly plan: PlanJson; // the rows to write
|
|
72
|
+
readonly board: BoardJson; // the board with it applied: show it now
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type CanvasCommitResult =
|
|
76
|
+
| { readonly _tag: "Confirmed" }
|
|
77
|
+
| { readonly _tag: "Refused"; readonly error: CommandErrorJson | null };
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The contract:
|
|
81
|
+
|
|
82
|
+
1. When `commit` returns, `getBoard()` must already include the change.
|
|
83
|
+
2. `commit` returns `{ _tag: "Confirmed" }` or `{ _tag: "Refused", error }` when the answer is known, or a promise that settles once with one of them.
|
|
84
|
+
3. On `Refused`, `getBoard()` must already have dropped the change; the board removes its undo step and shows the reason top left.
|
|
85
|
+
4. A rejected promise, or a `commit` that throws, counts as `Refused`.
|
|
86
|
+
5. `getBoard()` and `getPresence()` return the same object until it changes; a new object each call redraws the whole board.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
// `replica` applies locally and returns a promise for the server's answer.
|
|
90
|
+
const store: CanvasBoardStore = {
|
|
91
|
+
...replica,
|
|
92
|
+
commit: (change) =>
|
|
93
|
+
replica.apply(change.plan).then(
|
|
94
|
+
() => ({ _tag: "Confirmed" }) as const,
|
|
95
|
+
(error) => ({ _tag: "Refused", error: error.kernel ?? null }) as const,
|
|
96
|
+
),
|
|
97
|
+
};
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`createMemoryBoardStore({ actor, board?, presence?, connection? })` keeps every commit, confirms it at once and refuses none. It adds `setBoard(board)` (a change made outside the editor) and `setPresence(presence)` (everyone else), which is enough for demos, tests and a single-player board.
|
|
101
|
+
|
|
102
|
+
## Presence
|
|
103
|
+
|
|
104
|
+
`publish` sends the viewer's cursor, selection, view, who they follow, what they're doing, what they claim (shapes they edit), their uploads and cursor chat, at most once a frame. Relay all of it as a `PresenceJson` with who it is:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
publish: (presence, connection) =>
|
|
108
|
+
socket.send({ ...presence, actor: user.id, connection, name: user.name, kind: "person" }),
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`getPresence()` returns everyone else's, including the viewer's other tabs (same `actor`, another `connection`): a shape claimed in another tab reads "You're editing this in another tab". Agents publish with `kind: "agent"`: their cursor and proposals are violet and carry a sparkle. Others' cursors, drags and views move every frame, one update late, smoothed.
|
|
112
|
+
|
|
113
|
+
People's colors come from their names, so a person has the same color in every tab and on every board. Following someone (from the presence stack) eases the view to theirs and frames the board in their color until you pan or zoom; someone out of view is a chip on the edge that glides the view to them.
|
|
114
|
+
|
|
115
|
+
## Images
|
|
116
|
+
|
|
117
|
+
Give the board `assets` and it has an image tool, and takes dropped files, pasted screenshots and (with `fromUrl`) dropped links. The board stores only an opaque `asset` reference on each image; your app uploads files and turns references into URLs:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
interface CanvasAssets {
|
|
121
|
+
/** Store a file; resolve with its reference. Reject with a CanvasUploadFailure to say why. */
|
|
122
|
+
readonly upload: (file: File, options: { onProgress: (fraction: number) => void; signal: AbortSignal }) => Promise<{ asset: string }>;
|
|
123
|
+
/** What to draw for an asset now, at least `width` device pixels wide (256 to 4096, powers of two). Same object until it changes. */
|
|
124
|
+
readonly resolve: (asset: string, hint: { width: number }) => CanvasAssetSource;
|
|
125
|
+
/** Called when a `resolve` result may have changed (a signed URL renewed). */
|
|
126
|
+
readonly subscribe?: (listener: () => void) => () => void;
|
|
127
|
+
/** MIME types you store; the picker offers them and the board turns others away by name. */
|
|
128
|
+
readonly accept: readonly string[];
|
|
129
|
+
/** The largest file, in bytes, checked before sending ("Images can be up to 10 MB"). */
|
|
130
|
+
readonly maxBytes: number;
|
|
131
|
+
/** The original file to open or download from the image's bar. */
|
|
132
|
+
readonly original?: (asset: string) => { href: string; name: string } | null;
|
|
133
|
+
/** Store an image from a dropped link. Without it, dropped links are ignored. */
|
|
134
|
+
readonly fromUrl?: (url: string, options: { signal: AbortSignal }) => Promise<{ asset: string; naturalW: number; naturalH: number }>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
type CanvasAssetSource =
|
|
138
|
+
| { status: "ready"; src: string }
|
|
139
|
+
| { status: "pending" }
|
|
140
|
+
| { status: "failed"; reason: "forbidden" | "missing" };
|
|
141
|
+
|
|
142
|
+
/** A refusal written for the person: shown as written; `retryable: false` drops Retry. */
|
|
143
|
+
type CanvasUploadFailure = Error & { readonly retryable: boolean };
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
const urls = new Map<string, CanvasAssetSource>();
|
|
148
|
+
const listeners = new Set<() => void>();
|
|
149
|
+
const assets: CanvasAssets = {
|
|
150
|
+
accept: ["image/png", "image/jpeg", "image/webp", "image/gif"],
|
|
151
|
+
maxBytes: 10_000_000,
|
|
152
|
+
async upload(file, { onProgress, signal }) {
|
|
153
|
+
const { asset } = await api.upload(file, { onProgress, signal });
|
|
154
|
+
return { asset };
|
|
155
|
+
},
|
|
156
|
+
// Cache sources: a new object per call reloads every image.
|
|
157
|
+
resolve: (asset) => urls.get(asset) ?? (sign(asset), { status: "pending" }),
|
|
158
|
+
subscribe(listener) {
|
|
159
|
+
listeners.add(listener);
|
|
160
|
+
return () => listeners.delete(listener);
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
async function sign(asset: string) {
|
|
165
|
+
urls.set(asset, { status: "pending" });
|
|
166
|
+
const src = await api.signedUrl(asset);
|
|
167
|
+
urls.set(asset, { status: "ready", src });
|
|
168
|
+
for (const listener of listeners) listener();
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
To refuse a file with a reason, reject with an error that has `retryable`:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
throw Object.assign(new Error("This board has reached its 200-image limit."), { retryable: false });
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
States: an upload shows a placeholder at once for everyone; the uploader sees its file name, progress and **Cancel**, others "Being added". A failed upload leaves others' boards; the uploader keeps the spot with your reason, **Retry** (when retryable) and **Remove**. A file outside `accept` or `maxBytes` is named top left before anything is sent. **Replace** (the image's bar or context menu) is undoable, and cancelling keeps the old picture. Waiting images pulse at their final size; `forbidden` ones read "You don't have access", `missing` ones "Image unavailable", and a source that fails to load is retried after 1, 4, 15 and then every 60 seconds. A selected image's bar sets its description (what it shows, such as "Lobby at dawn": screen readers read it) and downloads the original when `original` gives one.
|
|
179
|
+
|
|
180
|
+
## Comments
|
|
181
|
+
|
|
182
|
+
Give the board `comments` and it has a comment tool (C): a click starts a thread where you click, on the shape under the pointer when there is one, and its pin follows that shape. Your app keeps the threads and carries out each command:
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
interface CanvasComments {
|
|
186
|
+
readonly threads: readonly CanvasCommentThread[];
|
|
187
|
+
readonly viewer: string; // the viewer's principal: their comments are theirs to edit
|
|
188
|
+
readonly canComment: boolean; // start threads, reply, resolve and reopen
|
|
189
|
+
readonly canManage: boolean; // delete anyone's (implies canComment)
|
|
190
|
+
// Return the request's promise: typed text stays in its field until it resolves, and a rejection
|
|
191
|
+
// keeps it there with "Couldn't send. Try again."
|
|
192
|
+
readonly onCommand: (command: CanvasCommentCommand) => unknown;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
interface CanvasCommentThread {
|
|
196
|
+
readonly id: string;
|
|
197
|
+
readonly anchor:
|
|
198
|
+
| { _tag: "Point"; at: { x: number; y: number } }
|
|
199
|
+
| { _tag: "Shape"; shape: string; offset: { x: number; y: number }; at: { x: number; y: number } };
|
|
200
|
+
readonly comments: readonly [CanvasComment, ...CanvasComment[]]; // the first starts the thread
|
|
201
|
+
readonly resolved: { by: CanvasCommentAuthor; at: number } | null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
interface CanvasComment {
|
|
205
|
+
readonly id: string;
|
|
206
|
+
readonly author: CanvasCommentAuthor; // { principal, name, kind: "person" | "agent" }
|
|
207
|
+
readonly body: string; // plain text, 1–2000 characters
|
|
208
|
+
readonly createdAt: number; // epoch ms
|
|
209
|
+
readonly editedAt: number | null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
type CanvasCommentCommand =
|
|
213
|
+
| { _tag: "StartThread"; thread: string; comment: string; anchor: CanvasCommentThread["anchor"]; body: string }
|
|
214
|
+
| { _tag: "Reply"; thread: string; comment: string; body: string }
|
|
215
|
+
| { _tag: "EditComment"; thread: string; comment: string; body: string }
|
|
216
|
+
| { _tag: "DeleteComment"; thread: string; comment: string } // the first comment deletes the thread
|
|
217
|
+
| { _tag: "SetResolved"; thread: string; resolved: boolean };
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
```tsx
|
|
221
|
+
const [threads, setThreads] = createSignal<CanvasCommentThread[]>([]);
|
|
222
|
+
|
|
223
|
+
<CanvasBoard
|
|
224
|
+
store={store}
|
|
225
|
+
comments={{
|
|
226
|
+
get threads() {
|
|
227
|
+
return threads();
|
|
228
|
+
},
|
|
229
|
+
viewer: user.id,
|
|
230
|
+
canComment: true,
|
|
231
|
+
canManage: user.isOwner,
|
|
232
|
+
onCommand: (command) => api.comments.send(command).then(setThreads),
|
|
233
|
+
}}
|
|
234
|
+
/>;
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Resolved threads are hidden; the comment bar (shown with the tool) shows them dimmed. Pins are named buttons; Escape closes a thread. A viewer without `canComment` reads threads but has no comment tool.
|
|
238
|
+
|
|
239
|
+
## Status
|
|
240
|
+
|
|
241
|
+
`status` puts your app's word on the board's state in the board's top-left row. Build it with `CanvasStatus`: a `status` region, so a change is read out without moving focus. It stands in for the "View only" badge, so say why a read-only board is read-only.
|
|
242
|
+
|
|
243
|
+
```tsx
|
|
244
|
+
import { CanvasBoard, CanvasStatus } from "@zsherman/canvas/canvas";
|
|
245
|
+
import { Icon } from "@zsherman/kit/icon";
|
|
246
|
+
import { WifiOff } from "@zsherman/kit/icons";
|
|
247
|
+
import { Button } from "@zsherman/kit/button";
|
|
248
|
+
|
|
249
|
+
<CanvasBoard
|
|
250
|
+
store={store}
|
|
251
|
+
readOnly={offline()}
|
|
252
|
+
status={
|
|
253
|
+
offline() ? (
|
|
254
|
+
<CanvasStatus
|
|
255
|
+
tone="warning"
|
|
256
|
+
icon={<Icon icon={WifiOff} aria-hidden="true" />}
|
|
257
|
+
action={<Button size="sm" variant="ghost" onClick={reconnect}>Retry now</Button>}
|
|
258
|
+
>
|
|
259
|
+
Offline
|
|
260
|
+
</CanvasStatus>
|
|
261
|
+
) : undefined
|
|
262
|
+
}
|
|
263
|
+
/>;
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Tones: `neutral` for progress the viewer waits on (catching up after a reconnect), `warning` for a state that limits editing (offline), `error` for a change the board couldn't make. Keep the text to a word or two; the board shows refusals itself.
|
|
267
|
+
|
|
268
|
+
## Agents
|
|
269
|
+
|
|
270
|
+
An agent edits the board through the same kernel as people. Its changes land as proposals: ghosts with **Keep** and **Discard** in a bar at the top, with a count of what it adds, changes and removes and its `reason`. Give an agent the board as text with the kernel's `outline` ([kernel.md](./kernel.md)), and run its commands as the viewer's own (one undo step) with `useCanvasCommands().run` from inside a `CanvasProvider`:
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
const commands = useCanvasCommands();
|
|
274
|
+
commands.run(
|
|
275
|
+
[
|
|
276
|
+
{
|
|
277
|
+
_tag: "Create",
|
|
278
|
+
shape: { kind: "Sticky", text: "Owner: Ada", color: "yellow" },
|
|
279
|
+
place: { _tag: "Beside", shape: "decision-1", side: "right" },
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
"adds an owner to each decision",
|
|
283
|
+
);
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
Write `reason` as what it does ("adds an owner to each decision"), and name agents in presence as people would ("Hyper"). See [kernel.md](./kernel.md) for every command.
|
|
287
|
+
|
|
288
|
+
## Export
|
|
289
|
+
|
|
290
|
+
**Export as PNG** (top right) saves `<label>.png`; it's off for an empty board. For your own menu, `exportBoardPng` returns the `Blob` and `downloadBoardPng` saves it:
|
|
291
|
+
|
|
292
|
+
```tsx
|
|
293
|
+
import { downloadBoardPng, loadCanvasKernel } from "@zsherman/canvas/canvas";
|
|
294
|
+
|
|
295
|
+
async function exportPng() {
|
|
296
|
+
const canvas = await loadCanvasKernel();
|
|
297
|
+
await downloadBoardPng({ assets, board: store.getBoard(), canvas, title: "Retro board" });
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
<MenuItem disabled={store.getBoard().shapes.length === 0} onClick={() => void exportPng().catch(() => toast.error("Couldn't export this canvas"))}>
|
|
301
|
+
Export as PNG
|
|
302
|
+
</MenuItem>;
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
The PNG is drawn in the scheme the board sits in. `resolvePalette(element)` gives the board's colors as hex for the kernel's `toSvg` (a standalone SVG can't read custom properties).
|
|
306
|
+
|
|
307
|
+
## Your own chrome
|
|
308
|
+
|
|
309
|
+
`CanvasBoard` is the surface with the kit's chrome. To put your own around it, compose `CanvasProvider` and `CanvasSurface`, and drive the board with `useCanvasCommands()`; `useCanvas()` is an accessor of everything the board knows (`board`, the editor `state` with its `tool`, `selection` and `view`, `presence`):
|
|
310
|
+
|
|
311
|
+
```tsx
|
|
312
|
+
import { CanvasProvider, CanvasSurface, useCanvas, useCanvasCommands, useCanvasKernel } from "@zsherman/canvas/canvas";
|
|
313
|
+
import { Show } from "solid-js";
|
|
314
|
+
|
|
315
|
+
function Tools() {
|
|
316
|
+
const canvas = useCanvas();
|
|
317
|
+
const commands = useCanvasCommands();
|
|
318
|
+
return (
|
|
319
|
+
<div role="toolbar" aria-label="Tools">
|
|
320
|
+
<button aria-pressed={canvas().state.tool === "sticky" ? "true" : "false"} onClick={() => commands.action("tool-sticky")}>
|
|
321
|
+
Note
|
|
322
|
+
</button>
|
|
323
|
+
<button onClick={() => commands.action("undo")}>Undo</button>
|
|
324
|
+
</div>
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function MyBoard(props: { store: CanvasBoardStore }) {
|
|
329
|
+
const kernel = useCanvasKernel();
|
|
330
|
+
return (
|
|
331
|
+
<Show when={kernel().status === "ready" ? kernel() : undefined} keyed>
|
|
332
|
+
{(ready) => (
|
|
333
|
+
<CanvasProvider canvas={(ready as { canvas: CanvasKernel }).canvas} store={props.store}>
|
|
334
|
+
<Tools />
|
|
335
|
+
<div style={{ height: "480px" }}>
|
|
336
|
+
<CanvasSurface label="Sketch" />
|
|
337
|
+
</div>
|
|
338
|
+
</CanvasProvider>
|
|
339
|
+
)}
|
|
340
|
+
</Show>
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
`CanvasProvider` takes `canvas` (a loaded kernel), `store`, and the board's `readOnly`, `initialView`, `assets`, `comments` (a boolean: the comment tool works), `onClipboard` and `onError`. A new `store` or `canvas` starts a new editor.
|
|
346
|
+
|
|
347
|
+
## States
|
|
348
|
+
|
|
349
|
+
- `readOnly` — only Select and Hand work; a "View only" badge shows (or your `status`) and only presence is sent.
|
|
350
|
+
- Empty — a hint offers one action: add a sticky note.
|
|
351
|
+
- Opening — without `initialView`, the board fits its shapes, at 100% at most, in the area the chrome leaves clear.
|
|
352
|
+
- Loading — without `canvas`, the board loads the kernel after it mounts (the server and the first paint show a "Loading board" status for screen readers); if it can't, it says so.
|
|
353
|
+
- Style bar — top centre; between gestures, it moves to the bottom when the selection is under it. Pen, marker and highlighter show the pen tray (`@zsherman/kit/draw-toolbar`) instead.
|
|
354
|
+
- Compact — on a small board the tools are a row; the style bar replaces them during a selection.
|
|
355
|
+
- Following, cursors (a crowded label drops the activity, then shortens to a chip; hover shows it in full), off screen (an edge chip).
|
|
356
|
+
- Refused — a refused change rolls back and its reason shows top left for six seconds.
|
|
357
|
+
- Unsaved — text typed into a shape someone else claimed first is offered back with **Copy text**.
|
|
358
|
+
- Held — a blocked action names who holds it.
|
|
359
|
+
- Timestamp — Shift+T stamps the current time, shown in each viewer's zone.
|
|
360
|
+
- Cursor chat — `/` shows what you type at your cursor to everyone; it clears 4 seconds after Enter or Escape.
|
|
361
|
+
|
|
362
|
+
## Keyboard and screen readers
|
|
363
|
+
|
|
364
|
+
- The surface is a focusable `application` named by `label` and described by its shapes and selection; Tab reads each shape out.
|
|
365
|
+
- Tab and Shift+Tab move between shapes; Escape clears the selection, and Tab then leaves the board.
|
|
366
|
+
- Arrow keys nudge the selection, Enter edits its text, and every action has a shortcut listed under `?` (or ⌘/ and Ctrl+/).
|
|
367
|
+
- Shortcuts work while a toolbar control has focus; Enter, Space, arrows, Tab and Escape stay with the control.
|
|
368
|
+
- Right click, the context-menu key, Shift+F10 or a still long press opens the actions for the selection.
|
|
369
|
+
- Leaving the board mid-typing keeps the text, as Escape does.
|
|
370
|
+
- Refusals and follow changes are read from a polite live region; an image reads out by its description; each upload's progress bar names its file.
|
|
371
|
+
- Tabbing in draws an inset ring while nothing is selected; clicks draw none.
|
|
372
|
+
- Under reduced motion, glides and following jump; ink and text keep 4.5:1 contrast on every paper color in both schemes.
|
|
373
|
+
|
|
374
|
+
## Theming
|
|
375
|
+
|
|
376
|
+
Board colors are tokens, one set per color name a shape stores (`ink`, `gray`, `yellow`, `orange`, `red`, `pink`, `violet`, `blue`, `teal`, `green`): `--sui-canvas-<name>-fill` (a shape's pale fill), `--sui-canvas-<name>-stroke` (its outline and text; people's cursors too) and `--sui-canvas-<name>-note` (sticky note paper), plus `--sui-canvas-note-ink` (text on notes, dark in both schemes). Each is a `light-dark()` pair; override one in plain CSS:
|
|
377
|
+
|
|
378
|
+
```css
|
|
379
|
+
:root {
|
|
380
|
+
--sui-canvas-yellow-note: light-dark(oklch(0.95 0.12 100), oklch(0.88 0.09 100));
|
|
381
|
+
}
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
Selection and handles use `--sui-focus`; the board's background is `--sui-surface`. Keep 4.5:1 between each stroke and its fill and the board, and between note ink and every note.
|
|
385
|
+
|
|
386
|
+
## Gotchas
|
|
387
|
+
|
|
388
|
+
- A store whose `getBoard()` returns a new object every call redraws the whole board on every frame. Return the same object until the board changes.
|
|
389
|
+
- A `commit` that never settles leaves the change pending forever: settle the promise with `Confirmed` or `Refused`.
|
|
390
|
+
- `resolve` that returns a new source object per call reloads every image: cache them.
|
|
391
|
+
- The board is client-side: on the server it renders its frame and a loading status, and draws once the kernel loads.
|
|
392
|
+
- Its words are English for now (the kernel writes its announcements, action names and descriptions in English).
|
|
393
|
+
|
|
394
|
+
## API reference: `@zsherman/canvas/canvas`
|
|
395
|
+
|
|
396
|
+
<!-- generated:props:canvas (node tooling/props-doc.ts; edit the JSDoc in the source, not this table) -->
|
|
397
|
+
|
|
398
|
+
Generated from the types of `@zsherman/canvas/canvas`. Each part also takes its element's native and ARIA attributes.
|
|
399
|
+
|
|
400
|
+
#### `CanvasBoard`
|
|
401
|
+
|
|
402
|
+
| Prop | Type | Default | Description |
|
|
403
|
+
| --- | --- | --- | --- |
|
|
404
|
+
| `assets` | `CanvasAssets` | | Where the board keeps image files. With it the board has an image tool and takes dropped and pasted images; without it, it has neither. |
|
|
405
|
+
| `canvas` | `CanvasKernel` | | A loaded kernel (`loadCanvasKernel`); the board loads one itself when this is left out. |
|
|
406
|
+
| `comments` | `CanvasComments` | | Comment threads on the board and what the viewer may do with them. With it the board has a comment tool (C) and shows each thread as a pin; without it, it has neither. The app keeps the threads and carries out each `onCommand`. |
|
|
407
|
+
| `initialView` | `{ readonly x: number; readonly y: number; readonly zoom: number; }` | | Where the view starts. Left out, the board fits its shapes in the area the chrome leaves clear. |
|
|
408
|
+
| `label` | `string` | | Names the board, such as "Retro board": for assistive technology, and as the file name of **Export as PNG** ("canvas.png" without one). |
|
|
409
|
+
| `onClipboard` | `((data: ClipboardDataJson) => void)` | | Copies from the menus. By default the text goes to the system clipboard; the board keeps the shapes for pasting back. |
|
|
410
|
+
| `onError` | `((error: CanvasBoardError) => void)` | | A change was refused, after the board told the viewer why. For logging. |
|
|
411
|
+
| `readOnly` | `boolean` | `false` | Only Select and Hand work; a "View only" badge shows and only presence is sent. |
|
|
412
|
+
| `status` | `Element` | | The app's word on the board's state, such as its connection, in the board's top-left row; build it with `CanvasStatus`. It stands in for the "View only" badge, so say why the board is read-only when it is. |
|
|
413
|
+
| `store` (required) | `CanvasBoardStore` | | The board's data, which the app owns: commit each change to a backend and confirm or refuse it. |
|
|
414
|
+
|
|
415
|
+
#### `CanvasStatus`
|
|
416
|
+
|
|
417
|
+
| Prop | Type | Default | Description |
|
|
418
|
+
| --- | --- | --- | --- |
|
|
419
|
+
| `action` | `Element` | | One small control, such as `<Button size="sm" variant="ghost">Retry now</Button>`, after the text inside the pill. |
|
|
420
|
+
| `children` (required) | `Element` | | A word or two, such as "Offline"; a narrow board truncates the rest. |
|
|
421
|
+
| `icon` | `Element` | | An icon before the text, such as a spinner or a connection glyph (`<Icon icon={WifiOff} />`). |
|
|
422
|
+
| `tone` | `"error" \| "neutral" \| "warning"` | `"neutral"` | What the status is about: `warning` (a state that limits editing, such as being offline) and `error` color the icon; `neutral` is progress the viewer waits on, such as catching up after a reconnect. |
|
|
423
|
+
|
|
424
|
+
#### `CanvasProvider`
|
|
425
|
+
|
|
426
|
+
| Prop | Type | Default | Description |
|
|
427
|
+
| --- | --- | --- | --- |
|
|
428
|
+
| `assets` | `CanvasAssets` | | Where images are stored. Left out, the board has no image tool and ignores dropped and pasted files. |
|
|
429
|
+
| `canvas` (required) | `CanvasKernel` | | A loaded kernel, from `useCanvasKernel` or `loadCanvasKernel`. A new one starts a new editor. |
|
|
430
|
+
| `children` | `Element` | | |
|
|
431
|
+
| `comments` | `boolean` | `false` | The host shows comments, so the comment tool and its shortcut work. `CanvasBoard` sets it from its `comments`. |
|
|
432
|
+
| `initialView` | `{ readonly x: number; readonly y: number; readonly zoom: number; }` | | Where the view starts: the board point at the top left, and the zoom. Left out, the board fits its shapes. |
|
|
433
|
+
| `onClipboard` | `((data: ClipboardDataJson) => void)` | | Receives copies and cuts made outside a clipboard event, such as from a menu. |
|
|
434
|
+
| `onError` | `((error: CanvasBoardError) => void)` | | A change was refused, by the kernel or the store, after the board told the viewer why. For a host's own logging; the board already shows it. |
|
|
435
|
+
| `readOnly` | `boolean` | `false` | Only Select and Hand work, and nothing is sent but presence. |
|
|
436
|
+
| `store` (required) | `CanvasBoardStore` | | The board's data, which the app owns. A new store starts a new editor. |
|
|
437
|
+
|
|
438
|
+
#### `CanvasSurface`
|
|
439
|
+
|
|
440
|
+
| Prop | Type | Default | Description |
|
|
441
|
+
| --- | --- | --- | --- |
|
|
442
|
+
| `label` | `string` | `"Board"` | Names the board for assistive technology, such as "Retro board". |
|
|
443
|
+
|
|
444
|
+
<!-- /generated:props:canvas -->
|
package/docs/kernel.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Canvas kernel
|
|
2
|
+
|
|
3
|
+
<!-- search: canvas kernel, board kernel, whiteboard engine, headless whiteboard, board outline, board to svg, agent board edits, board json, canvas commands -->
|
|
4
|
+
|
|
5
|
+
The headless kernel the board runs on: it plans and applies changes, lays out text, builds the scene to draw, hit-tests, writes a plain-text outline for agents, renders SVG and runs the editor machine that turns pointer and keyboard input into transactions. Pure functions over plain JSON: every argument and result survives `JSON.stringify`, nothing throws and nothing is async except `loadKernel`. Every call returns `{ ok }` or `{ error }`, and every error message ends with the fix.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { loadCanvasKernel } from "@zsherman/canvas/canvas";
|
|
9
|
+
import { EMPTY_BOARD, type BoardJson, type Outcome } from "@zsherman/canvas/kernel";
|
|
10
|
+
|
|
11
|
+
const unwrap = <A,>(outcome: Outcome<A>): A => {
|
|
12
|
+
if ("error" in outcome) throw new Error(outcome.error.message);
|
|
13
|
+
return outcome.ok;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// The kernel with its font data, loaded once per page (the board shares it).
|
|
17
|
+
const { kernel } = await loadCanvasKernel();
|
|
18
|
+
const board: BoardJson = EMPTY_BOARD;
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage guidelines
|
|
22
|
+
|
|
23
|
+
- The board (`@zsherman/canvas/canvas`) loads the kernel itself, on first use: you don't need this entry to show a board. Use it for what runs without one: an outline of a board for a model, an SVG of a board for an email or a preview, planning an agent's changes on a server, or the types of the board your store keeps (`BoardJson`, `PresenceJson`, `CommandJson`, …).
|
|
24
|
+
- In a page that also shows a board, get the loaded kernel from `loadCanvasKernel()` (`@zsherman/canvas/canvas`): it loads once per page, with its font data, and its `kernel` is this API.
|
|
25
|
+
|
|
26
|
+
## Font data
|
|
27
|
+
|
|
28
|
+
The kernel measures text with Inter Variable's advance tables (about 68 kB, a chunk of their own). `loadCanvasKernel()` loads them with the kernel and passes them to `loadKernel`; the package doesn't export them on their own. Draw the text in Inter Variable (`@fontsource-variable/inter`) so it wraps where the kernel laid it out.
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
| Call | Returns |
|
|
33
|
+
| ------------------------------------------------------- | ----------------------------------------------------------------------- |
|
|
34
|
+
| `loadKernel(fonts)` | `Promise<Outcome<Kernel>>`; the one async call |
|
|
35
|
+
| `EMPTY_BOARD` | A frozen board with no shapes, head 0 and no history |
|
|
36
|
+
| `BOARD_HISTORY` | The bound on every board's history |
|
|
37
|
+
| `decodeClipboard(text)` | A board's shapes from clipboard text, someone else's text, or a refusal |
|
|
38
|
+
| `plan(board, transaction, actor, presence, connection)` | The rows to write and the transaction record, or why it was refused |
|
|
39
|
+
| `apply(board, plan)` | The board with a plan applied |
|
|
40
|
+
| `scene(board, view, presence, editor)` | Keyed draw items for the view, with the viewer's editor overlay |
|
|
41
|
+
| `hitTest(board, probe)` | The shape, handle or connector end under a point |
|
|
42
|
+
| `outline(board, presence)` | A plain-text outline of the board for agents |
|
|
43
|
+
| `toSvg(scene, palette, assets?, options?)` | A standalone SVG of a scene; `assets` maps image assets to URLs |
|
|
44
|
+
| `editor(state, board, input, presence)` | The next editor state, actions for the host, `handled` and the cursor |
|
|
45
|
+
| `actions(state, board, presence)` | Every action, its label, shortcuts, and whether it is enabled and why |
|
|
46
|
+
| `inspect(board, ids)` | Shared styles of a selection (`"mixed"` where they differ) |
|
|
47
|
+
| `describe(board, id)` | One sentence describing a shape, for screen readers |
|
|
48
|
+
| `proposals(board)` | Pending proposals with counts of shapes added, changed and removed |
|
|
49
|
+
| `initialEditor(session, view)` | A fresh editor state |
|
|
50
|
+
|
|
51
|
+
`Outcome<A>` is `{ ok: A } | { error: CommandErrorJson }`. The functions from `plan` down are the loaded kernel's methods (`kernel.plan(…)`).
|
|
52
|
+
|
|
53
|
+
## An outline for a model
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const text = unwrap(kernel.outline(store.getBoard(), store.getPresence()));
|
|
57
|
+
// One line per shape, back to front, then proposals and people, such as
|
|
58
|
+
// `sticky-1: yellow sticky at (120, 80) size 200×200 "Ship the beta"`.
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Give a model the outline and ask for `CommandJson`s back. Run them as the viewer's own, one undo step, with `useCanvasCommands().run(commands, reason)` ([canvas.md#agents](./canvas.md#agents)), or plan them on a server with `kernel.plan(board, { id, commands, reason }, actor, presence, null)` and `kernel.apply`.
|
|
62
|
+
|
|
63
|
+
## Commands
|
|
64
|
+
|
|
65
|
+
A transaction holds commands, applied in order as one step. Each command names shapes by id:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
type CommandJson =
|
|
69
|
+
| { _tag: "Create"; shape: ShapeInputJson; place?: PlacementJson }
|
|
70
|
+
| { _tag: "Update"; id: string; patch: ShapePatchJson }
|
|
71
|
+
| { _tag: "Move"; ids: string[]; place: PlacementJson }
|
|
72
|
+
| { _tag: "Resize"; ids: string[]; box: BoxJson }
|
|
73
|
+
| { _tag: "Delete"; ids: string[] }
|
|
74
|
+
| { _tag: "Reorder"; ids: string[]; to: "front" | "back" | "forward" | "backward" }
|
|
75
|
+
| { _tag: "Align"; ids: string[]; to: AlignJson }
|
|
76
|
+
| { _tag: "Distribute"; ids: string[]; axis: "horizontal" | "vertical" }
|
|
77
|
+
| { _tag: "Duplicate"; ids: string[]; dx?: number; dy?: number; as?: string[] }
|
|
78
|
+
| { _tag: "Paste"; clipboard: ClipboardJson; place?: PlacementJson; as?: string[] }
|
|
79
|
+
| { _tag: "Group"; ids: string[]; group?: string }
|
|
80
|
+
| { _tag: "Ungroup"; ids: string[] }
|
|
81
|
+
| { _tag: "Lock"; ids: string[]; locked: boolean }
|
|
82
|
+
| …; // the rest are in the types
|
|
83
|
+
|
|
84
|
+
type PlacementJson =
|
|
85
|
+
| { _tag: "At"; x: number; y: number }
|
|
86
|
+
| { _tag: "Beside"; shape: string; side: "top" | "right" | "bottom" | "left"; gap?: number }; // gap: 40
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`ShapeInputJson` is one of `Sticky`, `Box` (rectangle or ellipse: `form`), `Text`, `Arrow` (between two anchors: a point or a shape), `Ink`, `Image` and `Timestamp`, each with optional `id`, `text`, `color` (`ink`, `gray`, `yellow`, `orange`, `red`, `pink`, `violet`, `blue`, `teal`, `green`), `size`, `align` and size fields; left-out fields take the documented defaults (the types say which).
|
|
90
|
+
|
|
91
|
+
## An SVG without a board
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { resolvePalette } from "@zsherman/canvas/canvas";
|
|
95
|
+
|
|
96
|
+
const view = { x: 0, y: 0, w: 1200, h: 800, zoom: 1, viewer: "export" };
|
|
97
|
+
const scene = unwrap(kernel.scene(board, view, [], unwrap(kernel.initialEditor("export", view))));
|
|
98
|
+
// The board's colors as hex, in the scheme `element` sits in: an SVG file can't read custom properties.
|
|
99
|
+
const svg = unwrap(kernel.toSvg(scene, resolvePalette(element), { [asset]: url }));
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`toSvg` takes image URLs that are `https:`, `http:`, `blob:`, or a base64 `data:` URL of PNG, JPEG, GIF, WebP or AVIF; anything else is refused. For a PNG, `exportBoardPng` ([canvas.md#export](./canvas.md#export)).
|
|
103
|
+
|
|
104
|
+
## Types
|
|
105
|
+
|
|
106
|
+
Every type is exported from `@zsherman/canvas/kernel`: `BoardJson` (`{ head, shapes, transactions }`), `ShapeJson`, `PresenceJson` (`actor`, `connection`, `name`, `kind`, `cursor`, `selection`, `viewport`, `following`, `activity`, `claims`, `uploads`, `chat`), `EditorPresenceJson` (the same without who), `TransactionJson`, `PlanJson`, `CommandJson`, `ShapeInputJson`, `ShapePatchJson`, `SceneJson`, `PaletteJson`, `EditorStateJson`, `EditorInputJson`, `EditorActionJson`, `ActionIdJson`, `ActionJson`, `CommandErrorJson`, `Outcome`, and the rest the signatures above name.
|
|
107
|
+
|
|
108
|
+
The kernel is a copy of Hyper's `@hypervideo/canvas-kernel` 0.3.0 (MPL-2.0; the package's THIRD_PARTY_NOTICES.md).
|
package/docs/llms.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @zsherman/canvas
|
|
2
|
+
|
|
3
|
+
> A shared whiteboard for @zsherman/kit: sticky notes, shapes, text, arrows, pen and highlighter ink, timestamps, images, comment threads, live cursors and presence, following, cursor chat, agent proposals people keep or discard, undo per person, a context menu, keyboard shortcuts and PNG export. The board is optimistic over a store your app owns: each change shows at once and your backend confirms or refuses it. A headless kernel (`@zsherman/canvas/kernel`, pure functions over plain JSON) plans, lays out and draws; the board loads it on first use. Themed with the kit's `--sui-*` tokens. Your app does not need a StyleX compiler.
|
|
4
|
+
|
|
5
|
+
Read this file first, then the page. These docs ship in the package, so they match the installed version.
|
|
6
|
+
|
|
7
|
+
## Setup
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @zsherman/canvas @zsherman/kit @fontsource-variable/inter solid-js@^2 @solidjs/web@^2
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Load the stylesheets once, in the app entry, the kit's first, and the font the board draws its text in:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import "@zsherman/kit/styles.css";
|
|
17
|
+
import "@zsherman/canvas/styles.css";
|
|
18
|
+
import "@fontsource-variable/inter/standard.css";
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`@zsherman/canvas/styles.css` carries the board's StyleX rules and its colors (`--sui-canvas-*`, which the kit's per-entry `css/base.css` leaves out), in the kit's cascade layers, so your own CSS wins without `!important`. The board follows the kit's light and dark schemes (`data-color-scheme`, `<Theme scheme>`). It fills its parent: give the parent a height.
|
|
22
|
+
|
|
23
|
+
Common tasks:
|
|
24
|
+
|
|
25
|
+
- A board in a page or a call's stage: [canvas.md](./canvas.md)
|
|
26
|
+
- Keep the board on your backend (the store contract): [canvas.md#the-store](./canvas.md#the-store)
|
|
27
|
+
- Share cursors, selections and views (presence): [canvas.md#presence](./canvas.md#presence)
|
|
28
|
+
- Images: uploads, signed URLs, limits and refusals: [canvas.md#images](./canvas.md#images)
|
|
29
|
+
- Comment threads: [canvas.md#comments](./canvas.md#comments)
|
|
30
|
+
- Connection state in the board's chrome: [canvas.md#status](./canvas.md#status)
|
|
31
|
+
- An agent that edits the board beside people: [canvas.md#agents](./canvas.md#agents)
|
|
32
|
+
- Export as PNG or SVG: [canvas.md#export](./canvas.md#export)
|
|
33
|
+
- Your own chrome around the surface: [canvas.md#your-own-chrome](./canvas.md#your-own-chrome)
|
|
34
|
+
- An outline of the board for a model, or an SVG without a board: [kernel.md](./kernel.md)
|
|
35
|
+
|
|
36
|
+
## Components
|
|
37
|
+
|
|
38
|
+
- `@zsherman/canvas/canvas` — `CanvasBoard`, `CanvasStatus`, `createMemoryBoardStore`, `CanvasProvider`, `CanvasSurface`, `useCanvas`, `useCanvasCommands`, `useCanvasKernel`, `loadCanvasKernel`, `exportBoardPng`, `downloadBoardPng`, `boardFileName`, `resolvePalette` and their types — [canvas.md](./canvas.md)
|
|
39
|
+
- `@zsherman/canvas/kernel` — the headless kernel: `loadKernel`, `EMPTY_BOARD`, `decodeClipboard` and every board type (`BoardJson`, `PresenceJson`, `CommandJson`, …) — [kernel.md](./kernel.md)
|
|
40
|
+
|
|
41
|
+
`@zsherman/canvas` re-exports the board's entry (not the kernel's, which the board loads on demand).
|