strata-react 2.1.7 → 2.1.8
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/README.md +1 -0
- package/dist/index.d.ts +253 -3
- package/dist/index.js +1187 -4
- package/dist/index.js.map +1 -1
- package/dist/{microphone-xGKxcK6p.d.ts → microphone-DWh4OaMH.d.ts} +2 -2
- package/dist/testing.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import { a as fieldErrorsOf, i as errorPayload, n as describeActionError, o as r
|
|
|
10
10
|
import { a as PaletteChoice, c as paletteScript, d as usePalette, f as useSystemColorScheme, i as PALETTE_STORAGE_KEY, l as palettes, n as ColorSchemeChoice, o as ThemeProvider, p as Density, r as DEFAULT_PALETTE, s as colorSchemeScript, t as COLOR_SCHEME_STORAGE_KEY, u as useColorScheme } from "./theme-5Mb1ydlA.js";
|
|
11
11
|
import * as React from "react";
|
|
12
12
|
import { Autocomplete, Button as Button$1, Cell, Checkbox as Checkbox$1, Collection, Column, Dialog as Dialog$1, DialogTrigger, Disclosure as Disclosure$1, DisclosurePanel, DropIndicator, DropZone, FileTrigger, GridLayout, GridList, GridListItem, Header, Heading, Input as Input$1, Keyboard, Link, ListBox, ListBoxItem, ListLayout, ListStateContext, Menu as Menu$1, MenuItem, MenuSection, MenuTrigger, Modal, ModalOverlay, Popover as Popover$1, RouterProvider, Row, SearchField, Select, SelectValue, Separator, Slider as Slider$1, SliderThumb, SliderTrack, SubmenuTrigger, Switch as Switch$1, Tab, TabList, TabPanel, Table, TableBody, TableHeader, TableLayout, Tabs as Tabs$1, Text, ToggleButton, ToggleButtonGroup, Tree as Tree$1, TreeItem, TreeItemContent, Virtualizer, useDragAndDrop, useFilter } from "react-aria-components";
|
|
13
|
-
import { ArrowDown, ArrowUp, ArrowUpDown, ArrowUpRight, Ban, Check, ChevronDown, ChevronLeft, ChevronRight, ChevronsUp, CircleAlert, CircleCheck, CircleDashed, Clock, CloudOff, Columns2, Ellipsis, Eye, EyeOff, FastForward, FileQuestion, Flag, Folder, FolderOpen, FolderX, GripVertical, ImageOff, Info, KeyRound, LayoutGrid, List, Loader, LoaderCircle, Lock, Maximize, Maximize2, Minimize, Minus, PanelBottomClose, PanelBottomOpen, PanelLeftClose, PanelLeftOpen, PanelRightClose, PanelRightOpen, PanelTopClose, PanelTopOpen, Pause, Pencil, Play, Redo2, Rewind, RotateCcw, RotateCw, Scan, Search, Send, SkipBack, SkipForward, SlidersHorizontal, Square, StepBack, StepForward, Trash2, TriangleAlert, Undo2, Upload, Volume2, VolumeX, X, ZoomIn, ZoomOut } from "lucide-react";
|
|
13
|
+
import { ArrowDown, ArrowUp, ArrowUpDown, ArrowUpRight, Ban, Brush, Check, ChevronDown, ChevronLeft, ChevronRight, ChevronsUp, CircleAlert, CircleCheck, CircleDashed, Clock, CloudOff, Columns2, Ellipsis, Eraser, Eye, EyeOff, FastForward, FileQuestion, Flag, Folder, FolderOpen, FolderX, GripVertical, Hand, Highlighter, ImageOff, Info, KeyRound, LayoutGrid, List, Loader, LoaderCircle, Lock, Maximize, Maximize2, Minimize, Minus, MousePointer2, Paintbrush, PanelBottomClose, PanelBottomOpen, PanelLeftClose, PanelLeftOpen, PanelRightClose, PanelRightOpen, PanelTopClose, PanelTopOpen, Pause, PenLine, Pencil, Play, Redo2, Rewind, RotateCcw, RotateCw, Scan, Search, Send, SkipBack, SkipForward, SlidersHorizontal, Square, StepBack, StepForward, Trash2, TriangleAlert, Undo2, Upload, Volume2, VolumeX, X, ZoomIn, ZoomOut } from "lucide-react";
|
|
14
14
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
15
15
|
import { Link as Link$1, NavLink, useHref, useNavigate, useSearchParams } from "react-router";
|
|
16
16
|
import { hashKey, useInfiniteQuery, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
@@ -12001,7 +12001,7 @@ const CLICK_MAX_MS = 500;
|
|
|
12001
12001
|
/** A click counts only once this long has passed with no second press: a double click zooms instead. */
|
|
12002
12002
|
const DOUBLE_CLICK_MS = 300;
|
|
12003
12003
|
/** A clipped frame whose children zoom and pan together; the viewport is the caller's, so frames can share one. */
|
|
12004
|
-
const ViewportFrame = React.forwardRef(function ViewportFrame({ label, aspect, viewport, setViewport, children, cover, overlay, maxHeight, onBox, outline, pick, className }, forwardedRef) {
|
|
12004
|
+
const ViewportFrame = React.forwardRef(function ViewportFrame({ label, aspect, viewport, setViewport, children, cover, markup, overlay, maxHeight, onBox, outline, pick, className }, forwardedRef) {
|
|
12005
12005
|
const ref = React.useRef(null);
|
|
12006
12006
|
React.useImperativeHandle(forwardedRef, () => ref.current);
|
|
12007
12007
|
const latest = React.useRef(viewport);
|
|
@@ -12213,6 +12213,14 @@ const ViewportFrame = React.forwardRef(function ViewportFrame({ label, aspect, v
|
|
|
12213
12213
|
children: cover.content
|
|
12214
12214
|
})
|
|
12215
12215
|
}),
|
|
12216
|
+
markup != null && /* @__PURE__ */ jsx("div", {
|
|
12217
|
+
className: "pointer-events-none absolute inset-0",
|
|
12218
|
+
style: {
|
|
12219
|
+
transform: viewportTransform(viewport),
|
|
12220
|
+
transformOrigin: "center"
|
|
12221
|
+
},
|
|
12222
|
+
children: markup
|
|
12223
|
+
}),
|
|
12216
12224
|
outline && viewport.scale === 1 && /* @__PURE__ */ jsx("div", {
|
|
12217
12225
|
"aria-hidden": true,
|
|
12218
12226
|
className: "pointer-events-none absolute border-2 border-foreground ring-1 ring-background",
|
|
@@ -16510,7 +16518,7 @@ function inkWords(m, a, b) {
|
|
|
16510
16518
|
return `${format.share(m.agreement)} of the ink agrees. Ink only in ${a}: ${only(m.matchedA, m.inkA)} of its ink; only in ${b}: ${only(m.matchedB, m.inkB)}.`;
|
|
16511
16519
|
}
|
|
16512
16520
|
/** Two pictures to judge, side by side or laid over each other, zoomed and panned together, and full screen. */
|
|
16513
|
-
function Compare({ label, a, b, blind, missing, mode, onModeChange, modes = ALL_MODES, regions, onPick, pick = true, pickWords, keys = true, extraShortcuts = [], footer, maxHeight, reserve, cannotCompare = "Could not read these pictures to compare their pixels.", readPixels, defaultDifference = "brightness", className }) {
|
|
16521
|
+
function Compare({ label, a, b, blind, missing, mode, onModeChange, modes = ALL_MODES, regions, onPick, pick = true, pickWords, keys = true, extraShortcuts = [], markup, footer, maxHeight, reserve, cannotCompare = "Could not read these pictures to compare their pixels.", readPixels, defaultDifference = "brightness", className }) {
|
|
16514
16522
|
const { viewport, setViewport } = useViewport();
|
|
16515
16523
|
const [full, setFull] = React.useState(false);
|
|
16516
16524
|
const pairKey = `${a.media.src}|${b.media.src}`;
|
|
@@ -16581,6 +16589,10 @@ function Compare({ label, a, b, blind, missing, mode, onModeChange, modes = ALL_
|
|
|
16581
16589
|
size: [w, h]
|
|
16582
16590
|
});
|
|
16583
16591
|
const picker = pick && onPick ? makePicker(a, b, onPick, pickWords) : null;
|
|
16592
|
+
const layer = (primary) => markup && natural ? markup({
|
|
16593
|
+
size: natural,
|
|
16594
|
+
primary
|
|
16595
|
+
}) : void 0;
|
|
16584
16596
|
const frameProps = {
|
|
16585
16597
|
viewport,
|
|
16586
16598
|
setViewport: rg.setViewport,
|
|
@@ -16602,6 +16614,7 @@ function Compare({ label, a, b, blind, missing, mode, onModeChange, modes = ALL_
|
|
|
16602
16614
|
label: blind ? `Picture ${c.label}` : `${c.label}`,
|
|
16603
16615
|
ref: i === 0 ? frameRef : void 0,
|
|
16604
16616
|
pick: picker?.one(i === 0 ? "a" : "b"),
|
|
16617
|
+
markup: layer(i === 0),
|
|
16605
16618
|
children: pane(c, i === 0 ? imgRef : void 0, i === 0 ? learn : void 0)
|
|
16606
16619
|
})]
|
|
16607
16620
|
}, i))
|
|
@@ -16627,6 +16640,7 @@ function Compare({ label, a, b, blind, missing, mode, onModeChange, modes = ALL_
|
|
|
16627
16640
|
}), /* @__PURE__ */ jsx(ViewportFrame, {
|
|
16628
16641
|
...frameProps,
|
|
16629
16642
|
ref: frameRef,
|
|
16643
|
+
markup: layer(true),
|
|
16630
16644
|
label: `Difference between ${a.label} and ${b.label}`,
|
|
16631
16645
|
children: style === "brightness" ? /* @__PURE__ */ jsxs("div", {
|
|
16632
16646
|
className: "absolute inset-0 isolate",
|
|
@@ -16677,6 +16691,7 @@ function Compare({ label, a, b, blind, missing, mode, onModeChange, modes = ALL_
|
|
|
16677
16691
|
}), /* @__PURE__ */ jsxs(ViewportFrame, {
|
|
16678
16692
|
...frameProps,
|
|
16679
16693
|
ref: frameRef,
|
|
16694
|
+
markup: layer(true),
|
|
16680
16695
|
label: `Ink of ${a.label} matched against ${b.label}`,
|
|
16681
16696
|
children: [
|
|
16682
16697
|
/* @__PURE__ */ jsx("div", {
|
|
@@ -16717,6 +16732,7 @@ function Compare({ label, a, b, blind, missing, mode, onModeChange, modes = ALL_
|
|
|
16717
16732
|
}), /* @__PURE__ */ jsxs(ViewportFrame, {
|
|
16718
16733
|
...frameProps,
|
|
16719
16734
|
ref: frameRef,
|
|
16735
|
+
markup: layer(true),
|
|
16720
16736
|
cover: mode === "swipe" ? {
|
|
16721
16737
|
content: pane(a, imgRef, learn),
|
|
16722
16738
|
shown: divider,
|
|
@@ -19198,6 +19214,1173 @@ function useHistoryState(initial, options) {
|
|
|
19198
19214
|
};
|
|
19199
19215
|
}
|
|
19200
19216
|
|
|
19217
|
+
// src/lib/drawing.ts
|
|
19218
|
+
/** A drawing with nothing on it. */
|
|
19219
|
+
function emptyDrawing(width, height) {
|
|
19220
|
+
return {
|
|
19221
|
+
width,
|
|
19222
|
+
height,
|
|
19223
|
+
strokes: []
|
|
19224
|
+
};
|
|
19225
|
+
}
|
|
19226
|
+
/** The tools' starting brushes. The widths are for a drawing about 1000 units wide; scale them for another. */
|
|
19227
|
+
const BRUSHES = {
|
|
19228
|
+
pen: {
|
|
19229
|
+
kind: "pen",
|
|
19230
|
+
colour: "#1b1b1f",
|
|
19231
|
+
width: 3,
|
|
19232
|
+
opacity: 1,
|
|
19233
|
+
hardness: 1,
|
|
19234
|
+
nib: "round",
|
|
19235
|
+
pressure: true
|
|
19236
|
+
},
|
|
19237
|
+
pencil: {
|
|
19238
|
+
kind: "pencil",
|
|
19239
|
+
colour: "#3a3a40",
|
|
19240
|
+
width: 2.5,
|
|
19241
|
+
opacity: .85,
|
|
19242
|
+
hardness: .7,
|
|
19243
|
+
nib: "round",
|
|
19244
|
+
pressure: true
|
|
19245
|
+
},
|
|
19246
|
+
marker: {
|
|
19247
|
+
kind: "marker",
|
|
19248
|
+
colour: "#e5156b",
|
|
19249
|
+
width: 10,
|
|
19250
|
+
opacity: .9,
|
|
19251
|
+
hardness: .9,
|
|
19252
|
+
nib: "round",
|
|
19253
|
+
pressure: false
|
|
19254
|
+
},
|
|
19255
|
+
highlighter: {
|
|
19256
|
+
kind: "highlighter",
|
|
19257
|
+
colour: "#ffe600",
|
|
19258
|
+
width: 22,
|
|
19259
|
+
opacity: .4,
|
|
19260
|
+
hardness: 1,
|
|
19261
|
+
nib: "chisel",
|
|
19262
|
+
nibAngle: 60,
|
|
19263
|
+
pressure: false,
|
|
19264
|
+
blend: "multiply"
|
|
19265
|
+
},
|
|
19266
|
+
brush: {
|
|
19267
|
+
kind: "brush",
|
|
19268
|
+
colour: "#1f5fbf",
|
|
19269
|
+
width: 14,
|
|
19270
|
+
opacity: .8,
|
|
19271
|
+
hardness: .4,
|
|
19272
|
+
nib: "round",
|
|
19273
|
+
pressure: true
|
|
19274
|
+
}
|
|
19275
|
+
};
|
|
19276
|
+
/**
|
|
19277
|
+
* The feedback marker: thick, half see-through, a strong bright colour, so it shows on a white page,
|
|
19278
|
+
* a yellowed scan and black type alike and is never mistaken for the page's own ink. `scale` is the
|
|
19279
|
+
* drawing's width over 1000.
|
|
19280
|
+
*/
|
|
19281
|
+
function feedbackMarker(scale = 1) {
|
|
19282
|
+
return {
|
|
19283
|
+
kind: "marker",
|
|
19284
|
+
colour: "#ff1493",
|
|
19285
|
+
width: 24 * scale,
|
|
19286
|
+
opacity: .45,
|
|
19287
|
+
hardness: 1,
|
|
19288
|
+
nib: "round",
|
|
19289
|
+
pressure: false
|
|
19290
|
+
};
|
|
19291
|
+
}
|
|
19292
|
+
/** The drawing after one operation. Pure: the drawing given is not changed. An op that does not apply (an unknown id) gives the drawing back as it was. */
|
|
19293
|
+
function applyOp(d, op) {
|
|
19294
|
+
if (op.op === "add") {
|
|
19295
|
+
const strokes = d.strokes.slice();
|
|
19296
|
+
strokes.splice(Math.min(Math.max(op.index, 0), strokes.length), 0, op.stroke);
|
|
19297
|
+
return {
|
|
19298
|
+
...d,
|
|
19299
|
+
strokes
|
|
19300
|
+
};
|
|
19301
|
+
}
|
|
19302
|
+
if (op.op === "remove") {
|
|
19303
|
+
const at = d.strokes.findIndex((s) => s.id === op.stroke.id);
|
|
19304
|
+
if (at < 0) return d;
|
|
19305
|
+
return {
|
|
19306
|
+
...d,
|
|
19307
|
+
strokes: d.strokes.filter((_, i) => i !== at)
|
|
19308
|
+
};
|
|
19309
|
+
}
|
|
19310
|
+
const at = d.strokes.findIndex((s) => s.id === op.id);
|
|
19311
|
+
if (at < 0) return d;
|
|
19312
|
+
const strokes = d.strokes.slice();
|
|
19313
|
+
strokes[at] = withChange(strokes[at], op.after);
|
|
19314
|
+
return {
|
|
19315
|
+
...d,
|
|
19316
|
+
strokes
|
|
19317
|
+
};
|
|
19318
|
+
}
|
|
19319
|
+
function withChange(s, c) {
|
|
19320
|
+
const next = {
|
|
19321
|
+
...s,
|
|
19322
|
+
...c
|
|
19323
|
+
};
|
|
19324
|
+
if ("note" in c && !c.note) delete next.note;
|
|
19325
|
+
return next;
|
|
19326
|
+
}
|
|
19327
|
+
/** The operation that undoes `op`. Needs no document: every op carries what it replaced. */
|
|
19328
|
+
function invertOp(op) {
|
|
19329
|
+
if (op.op === "add") return {
|
|
19330
|
+
op: "remove",
|
|
19331
|
+
stroke: op.stroke,
|
|
19332
|
+
index: op.index
|
|
19333
|
+
};
|
|
19334
|
+
if (op.op === "remove") return {
|
|
19335
|
+
op: "add",
|
|
19336
|
+
stroke: op.stroke,
|
|
19337
|
+
index: op.index
|
|
19338
|
+
};
|
|
19339
|
+
return {
|
|
19340
|
+
op: "change",
|
|
19341
|
+
id: op.id,
|
|
19342
|
+
before: op.after,
|
|
19343
|
+
after: op.before
|
|
19344
|
+
};
|
|
19345
|
+
}
|
|
19346
|
+
/** Applies each operation in turn: the log from `useDrawing` rebuilds the drawing it made. */
|
|
19347
|
+
function replay(d, ops) {
|
|
19348
|
+
return ops.reduce(applyOp, d);
|
|
19349
|
+
}
|
|
19350
|
+
/** The fields of `s` that `change` would replace, as they are now: the `before` of a change op. */
|
|
19351
|
+
function currentOf(s, change) {
|
|
19352
|
+
const before = {};
|
|
19353
|
+
if ("points" in change) before.points = s.points;
|
|
19354
|
+
if ("brush" in change) before.brush = s.brush;
|
|
19355
|
+
if ("note" in change) before.note = s.note ?? "";
|
|
19356
|
+
return before;
|
|
19357
|
+
}
|
|
19358
|
+
/** A drawing scaled into another coordinate space (the same picture served at another size). Widths scale with the mean of the two factors. */
|
|
19359
|
+
function rescaleDrawing(d, width, height) {
|
|
19360
|
+
if (d.width === width && d.height === height) return d;
|
|
19361
|
+
const sx = d.width ? width / d.width : 1;
|
|
19362
|
+
const sy = d.height ? height / d.height : 1;
|
|
19363
|
+
const sw = (sx + sy) / 2;
|
|
19364
|
+
return {
|
|
19365
|
+
width,
|
|
19366
|
+
height,
|
|
19367
|
+
strokes: d.strokes.map((s) => ({
|
|
19368
|
+
...s,
|
|
19369
|
+
brush: {
|
|
19370
|
+
...s.brush,
|
|
19371
|
+
width: s.brush.width * sw
|
|
19372
|
+
},
|
|
19373
|
+
points: s.points.map((p) => scalePoint(p, 0, 0, sx, sy))
|
|
19374
|
+
}))
|
|
19375
|
+
};
|
|
19376
|
+
}
|
|
19377
|
+
function scalePoint(p, ox, oy, sx, sy) {
|
|
19378
|
+
const x = ox + (p[0] - ox) * sx;
|
|
19379
|
+
const y = oy + (p[1] - oy) * sy;
|
|
19380
|
+
return p[2] === void 0 ? [x, y] : [
|
|
19381
|
+
x,
|
|
19382
|
+
y,
|
|
19383
|
+
p[2]
|
|
19384
|
+
];
|
|
19385
|
+
}
|
|
19386
|
+
/** The box a stroke's points span, not counting its width. */
|
|
19387
|
+
function pointsBox(points) {
|
|
19388
|
+
let x0 = Infinity;
|
|
19389
|
+
let y0 = Infinity;
|
|
19390
|
+
let x1 = -Infinity;
|
|
19391
|
+
let y1 = -Infinity;
|
|
19392
|
+
for (const [x, y] of points) {
|
|
19393
|
+
if (x < x0) x0 = x;
|
|
19394
|
+
if (y < y0) y0 = y;
|
|
19395
|
+
if (x > x1) x1 = x;
|
|
19396
|
+
if (y > y1) y1 = y;
|
|
19397
|
+
}
|
|
19398
|
+
return points.length ? [
|
|
19399
|
+
x0,
|
|
19400
|
+
y0,
|
|
19401
|
+
x1,
|
|
19402
|
+
y1
|
|
19403
|
+
] : [
|
|
19404
|
+
0,
|
|
19405
|
+
0,
|
|
19406
|
+
0,
|
|
19407
|
+
0
|
|
19408
|
+
];
|
|
19409
|
+
}
|
|
19410
|
+
/** The box a stroke covers, ink and all: its points' box grown by half its width. */
|
|
19411
|
+
function strokeBox(s) {
|
|
19412
|
+
const [x0, y0, x1, y1] = pointsBox(s.points);
|
|
19413
|
+
const h = s.brush.width / 2;
|
|
19414
|
+
return [
|
|
19415
|
+
x0 - h,
|
|
19416
|
+
y0 - h,
|
|
19417
|
+
x1 + h,
|
|
19418
|
+
y1 + h
|
|
19419
|
+
];
|
|
19420
|
+
}
|
|
19421
|
+
/** The stroke's points moved by `dx`, `dy`. */
|
|
19422
|
+
function movePoints(points, dx, dy) {
|
|
19423
|
+
return points.map((p) => p[2] === void 0 ? [p[0] + dx, p[1] + dy] : [
|
|
19424
|
+
p[0] + dx,
|
|
19425
|
+
p[1] + dy,
|
|
19426
|
+
p[2]
|
|
19427
|
+
]);
|
|
19428
|
+
}
|
|
19429
|
+
/** The stroke's points mapped from the box `from` onto the box `to` (a resize by a corner). A box with no width or height keeps that axis. */
|
|
19430
|
+
function fitPoints(points, from, to) {
|
|
19431
|
+
const fw = from[2] - from[0];
|
|
19432
|
+
const fh = from[3] - from[1];
|
|
19433
|
+
const sx = fw ? (to[2] - to[0]) / fw : 1;
|
|
19434
|
+
const sy = fh ? (to[3] - to[1]) / fh : 1;
|
|
19435
|
+
const dx = fw ? to[0] - from[0] : (to[0] + to[2]) / 2 - (from[0] + from[2]) / 2;
|
|
19436
|
+
const dy = fh ? to[1] - from[1] : (to[1] + to[3]) / 2 - (from[1] + from[3]) / 2;
|
|
19437
|
+
return points.map((p) => {
|
|
19438
|
+
const x = from[0] + (p[0] - from[0]) * sx + dx;
|
|
19439
|
+
const y = from[1] + (p[1] - from[1]) * sy + dy;
|
|
19440
|
+
return p[2] === void 0 ? [x, y] : [
|
|
19441
|
+
x,
|
|
19442
|
+
y,
|
|
19443
|
+
p[2]
|
|
19444
|
+
];
|
|
19445
|
+
});
|
|
19446
|
+
}
|
|
19447
|
+
/** Distance from a point to the segment a-b. */
|
|
19448
|
+
function segmentDistance(px, py, a, b) {
|
|
19449
|
+
const dx = b[0] - a[0];
|
|
19450
|
+
const dy = b[1] - a[1];
|
|
19451
|
+
const len = dx * dx + dy * dy;
|
|
19452
|
+
const t = len ? Math.min(1, Math.max(0, ((px - a[0]) * dx + (py - a[1]) * dy) / len)) : 0;
|
|
19453
|
+
return Math.hypot(px - (a[0] + t * dx), py - (a[1] + t * dy));
|
|
19454
|
+
}
|
|
19455
|
+
/** Whether the point lies on the stroke's ink, or within `slop` units of it. */
|
|
19456
|
+
function hitsStroke(s, x, y, slop = 0) {
|
|
19457
|
+
const reach = s.brush.width / 2 + slop;
|
|
19458
|
+
const pts = curvePoints(s.points);
|
|
19459
|
+
if (pts.length === 1) return Math.hypot(x - pts[0][0], y - pts[0][1]) <= reach;
|
|
19460
|
+
for (let i = 1; i < pts.length; i++) if (segmentDistance(x, y, pts[i - 1], pts[i]) <= reach) return true;
|
|
19461
|
+
return false;
|
|
19462
|
+
}
|
|
19463
|
+
/** The points of the path `smoothPath` draws, its curves cut into eight straight pieces each: what hit tests measure against. */
|
|
19464
|
+
function curvePoints(points) {
|
|
19465
|
+
if (points.length < 3) return points.slice();
|
|
19466
|
+
const out = [points[0]];
|
|
19467
|
+
let from = [points[0][0], points[0][1]];
|
|
19468
|
+
for (let i = 1; i < points.length - 1; i++) {
|
|
19469
|
+
const c = points[i];
|
|
19470
|
+
const n = points[i + 1];
|
|
19471
|
+
const to = [(c[0] + n[0]) / 2, (c[1] + n[1]) / 2];
|
|
19472
|
+
for (let k = 1; k <= 8; k++) {
|
|
19473
|
+
const t = k / 8;
|
|
19474
|
+
const u = 1 - t;
|
|
19475
|
+
out.push([u * u * from[0] + 2 * u * t * c[0] + t * t * to[0], u * u * from[1] + 2 * u * t * c[1] + t * t * to[1]]);
|
|
19476
|
+
}
|
|
19477
|
+
from = to;
|
|
19478
|
+
}
|
|
19479
|
+
out.push(points[points.length - 1]);
|
|
19480
|
+
return out;
|
|
19481
|
+
}
|
|
19482
|
+
/** The topmost stroke under the point, or undefined. */
|
|
19483
|
+
function strokeAt(d, x, y, slop = 0) {
|
|
19484
|
+
for (let i = d.strokes.length - 1; i >= 0; i--) if (hitsStroke(d.strokes[i], x, y, slop)) return d.strokes[i];
|
|
19485
|
+
}
|
|
19486
|
+
/**
|
|
19487
|
+
* Fewer points along the same line (Ramer-Douglas-Peucker, within `tolerance` units), so a stroke
|
|
19488
|
+
* saved is a few dozen points, not one per pointer event. A point where the pressure changes by more
|
|
19489
|
+
* than a tenth is kept, so a pressed line keeps its swell.
|
|
19490
|
+
*/
|
|
19491
|
+
function simplifyPoints(points, tolerance) {
|
|
19492
|
+
if (points.length < 3) return points.slice();
|
|
19493
|
+
const keep = new Uint8Array(points.length);
|
|
19494
|
+
keep[0] = keep[points.length - 1] = 1;
|
|
19495
|
+
const stack = [[0, points.length - 1]];
|
|
19496
|
+
while (stack.length) {
|
|
19497
|
+
const [a, b] = stack.pop();
|
|
19498
|
+
let worst = -1;
|
|
19499
|
+
let at = -1;
|
|
19500
|
+
for (let i = a + 1; i < b; i++) {
|
|
19501
|
+
const d = segmentDistance(points[i][0], points[i][1], points[a], points[b]);
|
|
19502
|
+
const score = Math.abs((points[i][2] ?? .5) - ((points[a][2] ?? .5) + (points[b][2] ?? .5)) / 2) > .1 ? Infinity : d;
|
|
19503
|
+
if (score > worst) {
|
|
19504
|
+
worst = score;
|
|
19505
|
+
at = i;
|
|
19506
|
+
}
|
|
19507
|
+
}
|
|
19508
|
+
if (at >= 0 && worst > tolerance) {
|
|
19509
|
+
keep[at] = 1;
|
|
19510
|
+
stack.push([a, at], [at, b]);
|
|
19511
|
+
}
|
|
19512
|
+
}
|
|
19513
|
+
return points.filter((_, i) => keep[i]);
|
|
19514
|
+
}
|
|
19515
|
+
/** The width of the stroke at each of its points: the brush's width, times its pressure where it follows it, times a chisel's angle against the stroke's direction. */
|
|
19516
|
+
function widthsOf(s) {
|
|
19517
|
+
const { brush, points } = s;
|
|
19518
|
+
const angle = (brush.nibAngle ?? 45) * Math.PI / 180;
|
|
19519
|
+
return points.map((p, i) => {
|
|
19520
|
+
let w = brush.width;
|
|
19521
|
+
if (brush.pressure) w *= .25 + .75 * (p[2] ?? .5);
|
|
19522
|
+
if (brush.nib === "chisel" && points.length > 1) {
|
|
19523
|
+
const a = points[Math.max(0, i - 1)];
|
|
19524
|
+
const b = points[Math.min(points.length - 1, i + 1)];
|
|
19525
|
+
const dir = Math.atan2(b[1] - a[1], b[0] - a[0]);
|
|
19526
|
+
w *= .2 + .8 * Math.abs(Math.sin(dir - angle));
|
|
19527
|
+
}
|
|
19528
|
+
return w;
|
|
19529
|
+
});
|
|
19530
|
+
}
|
|
19531
|
+
const fix = (n) => Math.round(n * 100) / 100;
|
|
19532
|
+
/** Quadratic curves through `points`: to the first midpoint, through each point to the next midpoint, then to the last. `start` is "M" to begin a path, "L" to join one. */
|
|
19533
|
+
function curveThrough(points, start) {
|
|
19534
|
+
let d = `${start}${fix(points[0][0])} ${fix(points[0][1])}`;
|
|
19535
|
+
for (let i = 1; i < points.length - 1; i++) {
|
|
19536
|
+
const [x, y] = points[i];
|
|
19537
|
+
const [nx, ny] = points[i + 1];
|
|
19538
|
+
d += `Q${fix(x)} ${fix(y)} ${fix((x + nx) / 2)} ${fix((y + ny) / 2)}`;
|
|
19539
|
+
}
|
|
19540
|
+
if (points.length > 1) {
|
|
19541
|
+
const last = points[points.length - 1];
|
|
19542
|
+
d += `L${fix(last[0])} ${fix(last[1])}`;
|
|
19543
|
+
}
|
|
19544
|
+
return d;
|
|
19545
|
+
}
|
|
19546
|
+
/** A smooth path through the points. One point is a zero-length line, which a round or square cap draws as a dot. */
|
|
19547
|
+
function smoothPath(points) {
|
|
19548
|
+
if (!points.length) return "";
|
|
19549
|
+
if (points.length === 1) {
|
|
19550
|
+
const [x, y] = points[0];
|
|
19551
|
+
return `M${fix(x)} ${fix(y)}L${fix(x)} ${fix(y)}`;
|
|
19552
|
+
}
|
|
19553
|
+
return curveThrough(points, "M");
|
|
19554
|
+
}
|
|
19555
|
+
/** The outline of a stroke whose width varies: the left side forward, a cap, the right side back, a cap. */
|
|
19556
|
+
function outlinePath(s, widths) {
|
|
19557
|
+
const pts = s.points;
|
|
19558
|
+
const left = [];
|
|
19559
|
+
const right = [];
|
|
19560
|
+
for (let i = 0; i < pts.length; i++) {
|
|
19561
|
+
const a = pts[Math.max(0, i - 1)];
|
|
19562
|
+
const b = pts[Math.min(pts.length - 1, i + 1)];
|
|
19563
|
+
let nx = -(b[1] - a[1]);
|
|
19564
|
+
let ny = b[0] - a[0];
|
|
19565
|
+
const len = Math.hypot(nx, ny) || 1;
|
|
19566
|
+
nx /= len;
|
|
19567
|
+
ny /= len;
|
|
19568
|
+
const h = widths[i] / 2;
|
|
19569
|
+
left.push([pts[i][0] + nx * h, pts[i][1] + ny * h]);
|
|
19570
|
+
right.push([pts[i][0] - nx * h, pts[i][1] - ny * h]);
|
|
19571
|
+
}
|
|
19572
|
+
const back = right.reverse();
|
|
19573
|
+
const cap = (i, to) => {
|
|
19574
|
+
const r = fix(widths[i] / 2);
|
|
19575
|
+
return s.brush.nib === "round" ? `A${r} ${r} 0 0 1 ${fix(to[0])} ${fix(to[1])}` : `L${fix(to[0])} ${fix(to[1])}`;
|
|
19576
|
+
};
|
|
19577
|
+
return `${curveThrough(left, "M")}${cap(pts.length - 1, back[0])}${curveThrough(back, "L")}${cap(0, left[0])}Z`;
|
|
19578
|
+
}
|
|
19579
|
+
/** How a stroke is drawn: the one rendering used on screen and in export. */
|
|
19580
|
+
function strokeShape(s) {
|
|
19581
|
+
const { brush } = s;
|
|
19582
|
+
const widths = widthsOf(s);
|
|
19583
|
+
const varies = s.points.length > 1 && widths.some((w) => Math.abs(w - widths[0]) > 1e-6);
|
|
19584
|
+
const base = {
|
|
19585
|
+
colour: brush.colour,
|
|
19586
|
+
width: fix(brush.width),
|
|
19587
|
+
cap: brush.nib === "round" ? "round" : "square",
|
|
19588
|
+
opacity: brush.opacity,
|
|
19589
|
+
blend: brush.blend ?? "normal",
|
|
19590
|
+
soften: fix(Math.max(0, 1 - brush.hardness) * brush.width * .25),
|
|
19591
|
+
grain: brush.kind === "pencil" ? fix(1.2 / Math.max(1, brush.width)) : 0
|
|
19592
|
+
};
|
|
19593
|
+
if (varies) return {
|
|
19594
|
+
...base,
|
|
19595
|
+
kind: "fill",
|
|
19596
|
+
d: outlinePath(s, widths)
|
|
19597
|
+
};
|
|
19598
|
+
return {
|
|
19599
|
+
...base,
|
|
19600
|
+
kind: "line",
|
|
19601
|
+
width: fix(widths[0] ?? brush.width),
|
|
19602
|
+
d: smoothPath(s.points.map((p) => [p[0], p[1]]))
|
|
19603
|
+
};
|
|
19604
|
+
}
|
|
19605
|
+
const escapeXml = (t) => t.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
19606
|
+
/**
|
|
19607
|
+
* The SVG filter for a soft edge or a pencil's grain, or "" when the stroke needs none. `id` must be
|
|
19608
|
+
* unique in the document. The region is in the drawing's units around the stroke's ink (`box`), not
|
|
19609
|
+
* the default share of its bounding box, which is zero high for a level line and would hide it.
|
|
19610
|
+
*/
|
|
19611
|
+
function filterMarkup(shape, id, box) {
|
|
19612
|
+
if (!shape.soften && !shape.grain) return "";
|
|
19613
|
+
const pad = shape.soften * 3 + 1;
|
|
19614
|
+
const region = `x="${fix(box[0] - pad)}" y="${fix(box[1] - pad)}" width="${fix(box[2] - box[0] + 2 * pad)}" height="${fix(box[3] - box[1] + 2 * pad)}"`;
|
|
19615
|
+
const parts = [];
|
|
19616
|
+
if (shape.soften) parts.push(`<feGaussianBlur in="SourceGraphic" stdDeviation="${shape.soften}" result="soft"/>`);
|
|
19617
|
+
if (shape.grain) parts.push(`<feTurbulence type="fractalNoise" baseFrequency="${shape.grain}" numOctaves="2" seed="3" result="noise"/>`, `<feColorMatrix in="noise" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -2 1.6" result="grain"/>`, `<feComposite in="${shape.soften ? "soft" : "SourceGraphic"}" in2="grain" operator="in"/>`);
|
|
19618
|
+
return `<filter id="${id}" filterUnits="userSpaceOnUse" ${region}>${parts.join("")}</filter>`;
|
|
19619
|
+
}
|
|
19620
|
+
/** The drawing as a standalone SVG document, drawn as `DrawingLayer` draws it; each stroke's note is its `<title>`. */
|
|
19621
|
+
function drawingToSvg(d, { idPrefix = "s" } = {}) {
|
|
19622
|
+
const defs = [];
|
|
19623
|
+
const body = d.strokes.map((s, i) => {
|
|
19624
|
+
const shape = strokeShape(s);
|
|
19625
|
+
const fid = `${idPrefix}-f${i}`;
|
|
19626
|
+
const filter = filterMarkup(shape, fid, strokeBox(s));
|
|
19627
|
+
if (filter) defs.push(filter);
|
|
19628
|
+
const paint = shape.kind === "line" ? `<path d="${shape.d}" fill="none" stroke="${shape.colour}" stroke-width="${shape.width}" stroke-linecap="${shape.cap}" stroke-linejoin="round"/>` : `<path d="${shape.d}" fill="${shape.colour}"/>`;
|
|
19629
|
+
const title = s.note ? `<title>${escapeXml(s.note)}</title>` : "";
|
|
19630
|
+
const blend = shape.blend === "multiply" ? ` style="mix-blend-mode:multiply"` : "";
|
|
19631
|
+
return `<g id="${escapeXml(s.id)}" opacity="${shape.opacity}"${blend}${filter ? ` filter="url(#${fid})"` : ""}>${title}${paint}</g>`;
|
|
19632
|
+
});
|
|
19633
|
+
const defsMarkup = defs.length ? `<defs>${defs.join("")}</defs>` : "";
|
|
19634
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${d.width} ${d.height}" width="${d.width}" height="${d.height}">${defsMarkup}${body.join("")}</svg>`;
|
|
19635
|
+
}
|
|
19636
|
+
let counter = 0;
|
|
19637
|
+
/** A stroke id unique in this page's life: time and a counter, so two tabs drawing on one document rarely clash. */
|
|
19638
|
+
function newStrokeId() {
|
|
19639
|
+
counter += 1;
|
|
19640
|
+
return `s${Date.now().toString(36)}${counter.toString(36)}`;
|
|
19641
|
+
}
|
|
19642
|
+
|
|
19643
|
+
// src/components/drawing.tsx
|
|
19644
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
19645
|
+
/** A vector drawing with every change an operation, its log, a selection and undo. Pass the returned state to `DrawingLayer` and `DrawingToolbar`. */
|
|
19646
|
+
function useDrawing({ initial, history: shared, limit }) {
|
|
19647
|
+
const own = useHistory({ limit });
|
|
19648
|
+
const history = shared ?? own;
|
|
19649
|
+
const [state, setState] = React.useState(() => ({
|
|
19650
|
+
drawing: typeof initial === "function" ? initial() : initial,
|
|
19651
|
+
log: []
|
|
19652
|
+
}));
|
|
19653
|
+
const [selected, setSelected] = React.useState(null);
|
|
19654
|
+
const latest = React.useRef(state.drawing);
|
|
19655
|
+
const run = React.useCallback((ops) => {
|
|
19656
|
+
if (!ops.length) return;
|
|
19657
|
+
latest.current = ops.reduce(applyOp, latest.current);
|
|
19658
|
+
const drawing = latest.current;
|
|
19659
|
+
setState((s) => ({
|
|
19660
|
+
drawing,
|
|
19661
|
+
log: [...s.log, ...ops]
|
|
19662
|
+
}));
|
|
19663
|
+
setSelected((id) => id && drawing.strokes.some((s) => s.id === id) ? id : null);
|
|
19664
|
+
}, []);
|
|
19665
|
+
const step = React.useCallback((ops, label, key) => {
|
|
19666
|
+
if (!ops.length) return;
|
|
19667
|
+
run(ops);
|
|
19668
|
+
const undo = ops.map(invertOp).reverse();
|
|
19669
|
+
history.push({
|
|
19670
|
+
label,
|
|
19671
|
+
key,
|
|
19672
|
+
undo: () => run(undo),
|
|
19673
|
+
redo: () => run(ops)
|
|
19674
|
+
});
|
|
19675
|
+
}, [history, run]);
|
|
19676
|
+
return {
|
|
19677
|
+
drawing: state.drawing,
|
|
19678
|
+
log: state.log,
|
|
19679
|
+
history,
|
|
19680
|
+
selected,
|
|
19681
|
+
select: setSelected,
|
|
19682
|
+
add: (stroke, label = "draw mark") => step([{
|
|
19683
|
+
op: "add",
|
|
19684
|
+
stroke,
|
|
19685
|
+
index: latest.current.strokes.length
|
|
19686
|
+
}], label),
|
|
19687
|
+
remove: (ids, label = "delete mark") => {
|
|
19688
|
+
const want = new Set(typeof ids === "string" ? [ids] : ids);
|
|
19689
|
+
const ops = latest.current.strokes.map((stroke, index) => ({
|
|
19690
|
+
stroke,
|
|
19691
|
+
index
|
|
19692
|
+
})).filter(({ stroke }) => want.has(stroke.id)).reverse().map(({ stroke, index }) => ({
|
|
19693
|
+
op: "remove",
|
|
19694
|
+
stroke,
|
|
19695
|
+
index
|
|
19696
|
+
}));
|
|
19697
|
+
step(ops, label);
|
|
19698
|
+
},
|
|
19699
|
+
change: (id, change, label, key) => {
|
|
19700
|
+
const s = latest.current.strokes.find((x) => x.id === id);
|
|
19701
|
+
if (!s) return;
|
|
19702
|
+
const before = currentOf(s, change);
|
|
19703
|
+
if (same(before, change)) return;
|
|
19704
|
+
step([{
|
|
19705
|
+
op: "change",
|
|
19706
|
+
id,
|
|
19707
|
+
before,
|
|
19708
|
+
after: change
|
|
19709
|
+
}], label, key);
|
|
19710
|
+
},
|
|
19711
|
+
apply: (ops, label) => step(ops.slice(), label),
|
|
19712
|
+
clear: (label = "clear marks") => {
|
|
19713
|
+
const ops = latest.current.strokes.map((stroke, index) => ({
|
|
19714
|
+
op: "remove",
|
|
19715
|
+
stroke,
|
|
19716
|
+
index
|
|
19717
|
+
})).reverse();
|
|
19718
|
+
step(ops, label);
|
|
19719
|
+
},
|
|
19720
|
+
fit: (width, height) => {
|
|
19721
|
+
const d = latest.current;
|
|
19722
|
+
if (d.strokes.length || d.width === width && d.height === height) return;
|
|
19723
|
+
latest.current = emptyDrawing(width, height);
|
|
19724
|
+
const drawing = latest.current;
|
|
19725
|
+
setState((s) => ({
|
|
19726
|
+
...s,
|
|
19727
|
+
drawing
|
|
19728
|
+
}));
|
|
19729
|
+
}
|
|
19730
|
+
};
|
|
19731
|
+
}
|
|
19732
|
+
/** Screen pixels within which a press still counts as on a thin stroke or a handle. */
|
|
19733
|
+
const SLOP_PX = 6;
|
|
19734
|
+
const HANDLE_PX = 9;
|
|
19735
|
+
/** The SVG layer a drawing is made on, laid over a picture of the same shape (inside a `ViewportFrame` it zooms and pans with it). */
|
|
19736
|
+
function DrawingLayer({ state, tool, brush, label, size, onDrawn, noteFor = null, onNoteClose, onNoteRequest, noteWords = { label: "Note" }, noun = "Mark", className }) {
|
|
19737
|
+
const { drawing, selected } = state;
|
|
19738
|
+
const svgRef = React.useRef(null);
|
|
19739
|
+
const anchorRef = React.useRef(null);
|
|
19740
|
+
const prefix = React.useId().replace(/:/g, "");
|
|
19741
|
+
const [drag, setDrag] = React.useState(null);
|
|
19742
|
+
const dragRef = React.useRef(drag);
|
|
19743
|
+
dragRef.current = drag;
|
|
19744
|
+
const fitRef = React.useRef(state.fit);
|
|
19745
|
+
fitRef.current = state.fit;
|
|
19746
|
+
const [sw, sh] = size ?? [0, 0];
|
|
19747
|
+
React.useEffect(() => {
|
|
19748
|
+
if (sw && sh) fitRef.current(sw, sh);
|
|
19749
|
+
}, [sw, sh]);
|
|
19750
|
+
const unitsPerPx = () => {
|
|
19751
|
+
const r = svgRef.current?.getBoundingClientRect();
|
|
19752
|
+
return r && r.width > 0 ? drawing.width / r.width : 1;
|
|
19753
|
+
};
|
|
19754
|
+
const toUnits = (e) => {
|
|
19755
|
+
const r = svgRef.current.getBoundingClientRect();
|
|
19756
|
+
return {
|
|
19757
|
+
x: r.width > 0 ? (e.clientX - r.left) / r.width * drawing.width : 0,
|
|
19758
|
+
y: r.height > 0 ? (e.clientY - r.top) / r.height * drawing.height : 0
|
|
19759
|
+
};
|
|
19760
|
+
};
|
|
19761
|
+
const chosen = selected ? drawing.strokes.find((s) => s.id === selected) : void 0;
|
|
19762
|
+
const handleAt = (x, y) => {
|
|
19763
|
+
if (!chosen) return null;
|
|
19764
|
+
const [x0, y0, x1, y1] = strokeBox(chosen);
|
|
19765
|
+
const reach = HANDLE_PX * unitsPerPx();
|
|
19766
|
+
for (const [hx, hy, ox, oy] of [
|
|
19767
|
+
[
|
|
19768
|
+
x0,
|
|
19769
|
+
y0,
|
|
19770
|
+
x1,
|
|
19771
|
+
y1
|
|
19772
|
+
],
|
|
19773
|
+
[
|
|
19774
|
+
x1,
|
|
19775
|
+
y0,
|
|
19776
|
+
x0,
|
|
19777
|
+
y1
|
|
19778
|
+
],
|
|
19779
|
+
[
|
|
19780
|
+
x0,
|
|
19781
|
+
y1,
|
|
19782
|
+
x1,
|
|
19783
|
+
y0
|
|
19784
|
+
],
|
|
19785
|
+
[
|
|
19786
|
+
x1,
|
|
19787
|
+
y1,
|
|
19788
|
+
x0,
|
|
19789
|
+
y0
|
|
19790
|
+
]
|
|
19791
|
+
]) if (Math.abs(x - hx) <= reach && Math.abs(y - hy) <= reach) return {
|
|
19792
|
+
x: ox,
|
|
19793
|
+
y: oy
|
|
19794
|
+
};
|
|
19795
|
+
return null;
|
|
19796
|
+
};
|
|
19797
|
+
const pressureOf = (e, at, prev) => {
|
|
19798
|
+
if (e.pointerType === "pen" && e.pressure > 0) return e.pressure;
|
|
19799
|
+
if (!prev) return .5;
|
|
19800
|
+
const dt = Math.max(1, e.timeStamp - prev.last.t);
|
|
19801
|
+
const speed = Math.hypot(at.x - prev.last.x, at.y - prev.last.y) / unitsPerPx() / dt;
|
|
19802
|
+
const target = Math.max(.2, Math.min(1, 1.1 - speed / 2.5));
|
|
19803
|
+
return prev.last.p + (target - prev.last.p) * .3;
|
|
19804
|
+
};
|
|
19805
|
+
const onPointerDown = (e) => {
|
|
19806
|
+
if (tool === "none" || e.button !== 0) return;
|
|
19807
|
+
e.stopPropagation();
|
|
19808
|
+
e.preventDefault();
|
|
19809
|
+
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
19810
|
+
const at = toUnits(e);
|
|
19811
|
+
const slop = SLOP_PX * unitsPerPx();
|
|
19812
|
+
if (tool === "draw") {
|
|
19813
|
+
const p = pressureOf(e, at);
|
|
19814
|
+
setDrag({
|
|
19815
|
+
kind: "draw",
|
|
19816
|
+
id: e.pointerId,
|
|
19817
|
+
points: [[
|
|
19818
|
+
at.x,
|
|
19819
|
+
at.y,
|
|
19820
|
+
p
|
|
19821
|
+
]],
|
|
19822
|
+
last: {
|
|
19823
|
+
...at,
|
|
19824
|
+
t: e.timeStamp,
|
|
19825
|
+
p
|
|
19826
|
+
}
|
|
19827
|
+
});
|
|
19828
|
+
} else if (tool === "erase") {
|
|
19829
|
+
const hit = /* @__PURE__ */ new Set();
|
|
19830
|
+
const s = strokeAt(drawing, at.x, at.y, slop);
|
|
19831
|
+
if (s) hit.add(s.id);
|
|
19832
|
+
setDrag({
|
|
19833
|
+
kind: "erase",
|
|
19834
|
+
id: e.pointerId,
|
|
19835
|
+
hit,
|
|
19836
|
+
last: at
|
|
19837
|
+
});
|
|
19838
|
+
} else {
|
|
19839
|
+
const fixed = handleAt(at.x, at.y);
|
|
19840
|
+
if (chosen && fixed) {
|
|
19841
|
+
setDrag({
|
|
19842
|
+
kind: "resize",
|
|
19843
|
+
id: e.pointerId,
|
|
19844
|
+
stroke: chosen,
|
|
19845
|
+
fixed,
|
|
19846
|
+
points: chosen.points
|
|
19847
|
+
});
|
|
19848
|
+
return;
|
|
19849
|
+
}
|
|
19850
|
+
const s = strokeAt(drawing, at.x, at.y, slop);
|
|
19851
|
+
state.select(s?.id ?? null);
|
|
19852
|
+
if (s) setDrag({
|
|
19853
|
+
kind: "move",
|
|
19854
|
+
id: e.pointerId,
|
|
19855
|
+
stroke: s,
|
|
19856
|
+
from: at,
|
|
19857
|
+
points: s.points
|
|
19858
|
+
});
|
|
19859
|
+
}
|
|
19860
|
+
};
|
|
19861
|
+
const onPointerMove = (e) => {
|
|
19862
|
+
const d = dragRef.current;
|
|
19863
|
+
if (!d || d.id !== e.pointerId) return;
|
|
19864
|
+
e.stopPropagation();
|
|
19865
|
+
const events = e.nativeEvent.getCoalescedEvents?.() ?? [];
|
|
19866
|
+
const all = events.length ? events : [e.nativeEvent];
|
|
19867
|
+
if (d.kind === "draw") {
|
|
19868
|
+
let next = d;
|
|
19869
|
+
for (const ev of all) {
|
|
19870
|
+
const at = toUnits(ev);
|
|
19871
|
+
const p = pressureOf({
|
|
19872
|
+
...e,
|
|
19873
|
+
pressure: ev.pressure,
|
|
19874
|
+
timeStamp: ev.timeStamp
|
|
19875
|
+
}, at, next);
|
|
19876
|
+
next = {
|
|
19877
|
+
...next,
|
|
19878
|
+
points: [...next.points, [
|
|
19879
|
+
at.x,
|
|
19880
|
+
at.y,
|
|
19881
|
+
p
|
|
19882
|
+
]],
|
|
19883
|
+
last: {
|
|
19884
|
+
...at,
|
|
19885
|
+
t: ev.timeStamp,
|
|
19886
|
+
p
|
|
19887
|
+
}
|
|
19888
|
+
};
|
|
19889
|
+
}
|
|
19890
|
+
setDrag(next);
|
|
19891
|
+
} else if (d.kind === "erase") {
|
|
19892
|
+
const slop = SLOP_PX * unitsPerPx();
|
|
19893
|
+
const hit = new Set(d.hit);
|
|
19894
|
+
let last = d.last;
|
|
19895
|
+
for (const ev of all) {
|
|
19896
|
+
const at = toUnits(ev);
|
|
19897
|
+
const steps = Math.max(1, Math.ceil(Math.hypot(at.x - last.x, at.y - last.y) / Math.max(slop, 1e-6)));
|
|
19898
|
+
for (let k = 1; k <= steps; k++) {
|
|
19899
|
+
const x = last.x + (at.x - last.x) * k / steps;
|
|
19900
|
+
const y = last.y + (at.y - last.y) * k / steps;
|
|
19901
|
+
for (const s of drawing.strokes) if (hitsStroke(s, x, y, slop)) hit.add(s.id);
|
|
19902
|
+
}
|
|
19903
|
+
last = at;
|
|
19904
|
+
}
|
|
19905
|
+
setDrag({
|
|
19906
|
+
...d,
|
|
19907
|
+
hit,
|
|
19908
|
+
last
|
|
19909
|
+
});
|
|
19910
|
+
} else if (d.kind === "move") {
|
|
19911
|
+
const at = toUnits(e);
|
|
19912
|
+
setDrag({
|
|
19913
|
+
...d,
|
|
19914
|
+
points: movePoints(d.stroke.points, at.x - d.from.x, at.y - d.from.y)
|
|
19915
|
+
});
|
|
19916
|
+
} else {
|
|
19917
|
+
const at = toUnits(e);
|
|
19918
|
+
const from = pointsBox(d.stroke.points);
|
|
19919
|
+
const h = d.stroke.brush.width / 2;
|
|
19920
|
+
const x0 = Math.min(at.x, d.fixed.x);
|
|
19921
|
+
const x1 = Math.max(at.x, d.fixed.x);
|
|
19922
|
+
const y0 = Math.min(at.y, d.fixed.y);
|
|
19923
|
+
const y1 = Math.max(at.y, d.fixed.y);
|
|
19924
|
+
const to = [
|
|
19925
|
+
x0 + h,
|
|
19926
|
+
y0 + h,
|
|
19927
|
+
Math.max(x0 + h, x1 - h),
|
|
19928
|
+
Math.max(y0 + h, y1 - h)
|
|
19929
|
+
];
|
|
19930
|
+
setDrag({
|
|
19931
|
+
...d,
|
|
19932
|
+
points: fitPoints(d.stroke.points, from, to)
|
|
19933
|
+
});
|
|
19934
|
+
}
|
|
19935
|
+
};
|
|
19936
|
+
const finish = (e, cancelled) => {
|
|
19937
|
+
const d = dragRef.current;
|
|
19938
|
+
if (!d || d.id !== e.pointerId) return;
|
|
19939
|
+
e.stopPropagation();
|
|
19940
|
+
setDrag(null);
|
|
19941
|
+
if (cancelled) return;
|
|
19942
|
+
if (d.kind === "draw") {
|
|
19943
|
+
const stroke = {
|
|
19944
|
+
id: newStrokeId(),
|
|
19945
|
+
brush,
|
|
19946
|
+
points: simplifyPoints(d.points, .4 * unitsPerPx())
|
|
19947
|
+
};
|
|
19948
|
+
state.add(stroke, `draw ${noun.toLowerCase()}`);
|
|
19949
|
+
onDrawn?.(stroke);
|
|
19950
|
+
} else if (d.kind === "erase") {
|
|
19951
|
+
if (d.hit.size) state.remove([...d.hit], d.hit.size > 1 ? `erase ${d.hit.size} marks` : "erase mark");
|
|
19952
|
+
} else if (d.points !== d.stroke.points) state.change(d.stroke.id, { points: d.points }, d.kind === "move" ? "move mark" : "resize mark");
|
|
19953
|
+
};
|
|
19954
|
+
const onKeyDown = (e, s) => {
|
|
19955
|
+
const step = (e.shiftKey ? .1 : .01) * drawing.width;
|
|
19956
|
+
const move = (dx, dy) => state.change(s.id, { points: movePoints(s.points, dx, dy) }, "move mark", `key-move-${s.id}`);
|
|
19957
|
+
const scale = (k) => {
|
|
19958
|
+
const b = pointsBox(s.points);
|
|
19959
|
+
const cx = (b[0] + b[2]) / 2;
|
|
19960
|
+
const cy = (b[1] + b[3]) / 2;
|
|
19961
|
+
const to = [
|
|
19962
|
+
cx + (b[0] - cx) * k,
|
|
19963
|
+
cy + (b[1] - cy) * k,
|
|
19964
|
+
cx + (b[2] - cx) * k,
|
|
19965
|
+
cy + (b[3] - cy) * k
|
|
19966
|
+
];
|
|
19967
|
+
state.change(s.id, { points: fitPoints(s.points, b, to) }, "resize mark", `key-size-${s.id}`);
|
|
19968
|
+
};
|
|
19969
|
+
if (e.key === "Enter" || e.key === " ") state.select(s.id);
|
|
19970
|
+
else if (e.key === "ArrowLeft") move(-step, 0);
|
|
19971
|
+
else if (e.key === "ArrowRight") move(step, 0);
|
|
19972
|
+
else if (e.key === "ArrowUp") move(0, -step);
|
|
19973
|
+
else if (e.key === "ArrowDown") move(0, step);
|
|
19974
|
+
else if (e.key === "[") scale(1 / 1.1);
|
|
19975
|
+
else if (e.key === "]") scale(1.1);
|
|
19976
|
+
else if (e.key === "Delete" || e.key === "Backspace") state.remove(s.id);
|
|
19977
|
+
else if (e.key === "Escape") state.select(null);
|
|
19978
|
+
else if ((e.key === "n" || e.key === "N") && onNoteRequest) onNoteRequest(s.id);
|
|
19979
|
+
else return;
|
|
19980
|
+
e.preventDefault();
|
|
19981
|
+
e.stopPropagation();
|
|
19982
|
+
};
|
|
19983
|
+
const live = drag?.kind === "draw" ? {
|
|
19984
|
+
id: "live",
|
|
19985
|
+
brush,
|
|
19986
|
+
points: drag.points
|
|
19987
|
+
} : null;
|
|
19988
|
+
const shown = drawing.strokes.map((s) => (drag?.kind === "move" || drag?.kind === "resize") && drag.stroke.id === s.id ? {
|
|
19989
|
+
...s,
|
|
19990
|
+
points: drag.points
|
|
19991
|
+
} : s);
|
|
19992
|
+
const box = chosen && tool === "select" ? strokeBox(shown.find((s) => s.id === chosen.id) ?? chosen) : null;
|
|
19993
|
+
const handle = HANDLE_PX * unitsPerPx();
|
|
19994
|
+
const noteStroke = noteFor ? drawing.strokes.find((s) => s.id === noteFor) : void 0;
|
|
19995
|
+
const noteBox = noteStroke ? strokeBox(noteStroke) : null;
|
|
19996
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
19997
|
+
className: cn("pointer-events-none absolute inset-0", className),
|
|
19998
|
+
children: [
|
|
19999
|
+
/* @__PURE__ */ jsxs("svg", {
|
|
20000
|
+
ref: svgRef,
|
|
20001
|
+
role: "group",
|
|
20002
|
+
"aria-label": label,
|
|
20003
|
+
viewBox: `0 0 ${drawing.width || 1} ${drawing.height || 1}`,
|
|
20004
|
+
preserveAspectRatio: "none",
|
|
20005
|
+
className: cn("absolute inset-0 size-full overflow-visible", tool === "none" ? "pointer-events-none" : "pointer-events-auto", tool === "draw" && "cursor-crosshair", tool === "erase" && "cursor-cell"),
|
|
20006
|
+
style: { touchAction: tool === "none" ? void 0 : "none" },
|
|
20007
|
+
onPointerDown,
|
|
20008
|
+
onPointerMove,
|
|
20009
|
+
onPointerUp: (e) => finish(e, false),
|
|
20010
|
+
onPointerCancel: (e) => finish(e, true),
|
|
20011
|
+
onDoubleClick: (e) => tool !== "none" && e.stopPropagation(),
|
|
20012
|
+
children: [
|
|
20013
|
+
shown.map((s, i) => /* @__PURE__ */ jsx(StrokeView, {
|
|
20014
|
+
stroke: s,
|
|
20015
|
+
filterId: `${prefix}-${i}`,
|
|
20016
|
+
faded: drag?.kind === "erase" && drag.hit.has(s.id),
|
|
20017
|
+
focusable: tool === "select",
|
|
20018
|
+
label: `${noun} ${i + 1}${s.note ? `: ${s.note}` : ""}`,
|
|
20019
|
+
selected: s.id === selected,
|
|
20020
|
+
onKeyDown: (e) => onKeyDown(e, s),
|
|
20021
|
+
onFocus: () => tool === "select" && state.select(s.id)
|
|
20022
|
+
}, s.id)),
|
|
20023
|
+
live && /* @__PURE__ */ jsx(StrokeView, {
|
|
20024
|
+
stroke: live,
|
|
20025
|
+
filterId: `${prefix}-live`
|
|
20026
|
+
}),
|
|
20027
|
+
box && /* @__PURE__ */ jsxs("g", {
|
|
20028
|
+
"aria-hidden": true,
|
|
20029
|
+
className: "pointer-events-none",
|
|
20030
|
+
children: [/* @__PURE__ */ jsx("rect", {
|
|
20031
|
+
x: box[0],
|
|
20032
|
+
y: box[1],
|
|
20033
|
+
width: box[2] - box[0],
|
|
20034
|
+
height: box[3] - box[1],
|
|
20035
|
+
fill: "none",
|
|
20036
|
+
className: "stroke-foreground",
|
|
20037
|
+
strokeWidth: 1.5,
|
|
20038
|
+
strokeDasharray: "4 3",
|
|
20039
|
+
vectorEffect: "non-scaling-stroke"
|
|
20040
|
+
}), [
|
|
20041
|
+
[box[0], box[1]],
|
|
20042
|
+
[box[2], box[1]],
|
|
20043
|
+
[box[0], box[3]],
|
|
20044
|
+
[box[2], box[3]]
|
|
20045
|
+
].map(([x, y], i) => /* @__PURE__ */ jsx("rect", {
|
|
20046
|
+
x: x - handle / 2,
|
|
20047
|
+
y: y - handle / 2,
|
|
20048
|
+
width: handle,
|
|
20049
|
+
height: handle,
|
|
20050
|
+
className: "fill-background stroke-foreground",
|
|
20051
|
+
strokeWidth: 1.5,
|
|
20052
|
+
vectorEffect: "non-scaling-stroke"
|
|
20053
|
+
}, i))]
|
|
20054
|
+
})
|
|
20055
|
+
]
|
|
20056
|
+
}),
|
|
20057
|
+
noteBox && /* @__PURE__ */ jsx("span", {
|
|
20058
|
+
ref: anchorRef,
|
|
20059
|
+
"aria-hidden": true,
|
|
20060
|
+
className: "absolute",
|
|
20061
|
+
style: {
|
|
20062
|
+
left: `${noteBox[0] / (drawing.width || 1) * 100}%`,
|
|
20063
|
+
top: `${noteBox[1] / (drawing.height || 1) * 100}%`,
|
|
20064
|
+
width: `${(noteBox[2] - noteBox[0]) / (drawing.width || 1) * 100}%`,
|
|
20065
|
+
height: `${(noteBox[3] - noteBox[1]) / (drawing.height || 1) * 100}%`
|
|
20066
|
+
}
|
|
20067
|
+
}),
|
|
20068
|
+
noteStroke && /* @__PURE__ */ jsx(StrokeNote, {
|
|
20069
|
+
stroke: noteStroke,
|
|
20070
|
+
anchor: anchorRef,
|
|
20071
|
+
words: noteWords,
|
|
20072
|
+
onSave: (note) => state.change(noteStroke.id, { note: note.trim() }, "note"),
|
|
20073
|
+
onRemove: () => state.remove(noteStroke.id),
|
|
20074
|
+
onClose: () => onNoteClose?.()
|
|
20075
|
+
}, noteStroke.id)
|
|
20076
|
+
]
|
|
20077
|
+
});
|
|
20078
|
+
}
|
|
20079
|
+
/** One stroke in the layer: its group (opacity, blend, filter) and its path, focusable as a button for the select tool. */
|
|
20080
|
+
function StrokeView({ stroke, filterId, faded, focusable, label, selected, onKeyDown, onFocus }) {
|
|
20081
|
+
const shape = strokeShape(stroke);
|
|
20082
|
+
const filter = filterMarkup(shape, filterId, strokeBox(stroke));
|
|
20083
|
+
return /* @__PURE__ */ jsxs("g", {
|
|
20084
|
+
"data-stroke": stroke.id,
|
|
20085
|
+
opacity: faded ? shape.opacity * .25 : shape.opacity,
|
|
20086
|
+
style: shape.blend === "multiply" ? { mixBlendMode: "multiply" } : void 0,
|
|
20087
|
+
filter: filter ? `url(#${filterId})` : void 0,
|
|
20088
|
+
role: focusable ? "button" : void 0,
|
|
20089
|
+
tabIndex: focusable ? 0 : void 0,
|
|
20090
|
+
"aria-label": focusable ? label : void 0,
|
|
20091
|
+
"aria-pressed": focusable ? !!selected : void 0,
|
|
20092
|
+
onKeyDown: focusable ? onKeyDown : void 0,
|
|
20093
|
+
onFocus: focusable ? onFocus : void 0,
|
|
20094
|
+
className: "outline-none focus-visible:outline-2 focus-visible:outline-ring",
|
|
20095
|
+
children: [filter && /* @__PURE__ */ jsx("defs", { dangerouslySetInnerHTML: { __html: filter } }), shape.kind === "line" ? /* @__PURE__ */ jsx("path", {
|
|
20096
|
+
d: shape.d,
|
|
20097
|
+
fill: "none",
|
|
20098
|
+
stroke: shape.colour,
|
|
20099
|
+
strokeWidth: shape.width,
|
|
20100
|
+
strokeLinecap: shape.cap,
|
|
20101
|
+
strokeLinejoin: "round"
|
|
20102
|
+
}) : /* @__PURE__ */ jsx("path", {
|
|
20103
|
+
d: shape.d,
|
|
20104
|
+
fill: shape.colour
|
|
20105
|
+
})]
|
|
20106
|
+
});
|
|
20107
|
+
}
|
|
20108
|
+
/** The note box for one stroke, anchored to the stroke's box. The text is saved when it closes, however it closes. */
|
|
20109
|
+
function StrokeNote({ stroke, anchor, words, onSave, onRemove, onClose }) {
|
|
20110
|
+
const [text, setText] = React.useState(stroke.note ?? "");
|
|
20111
|
+
const removed = React.useRef(false);
|
|
20112
|
+
const close = () => {
|
|
20113
|
+
if (!removed.current) onSave(text);
|
|
20114
|
+
onClose();
|
|
20115
|
+
};
|
|
20116
|
+
const [ready, setReady] = React.useState(false);
|
|
20117
|
+
React.useEffect(() => setReady(true), []);
|
|
20118
|
+
if (!ready || !anchor.current) return null;
|
|
20119
|
+
return /* @__PURE__ */ jsx(Popover$1, {
|
|
20120
|
+
triggerRef: anchor,
|
|
20121
|
+
isOpen: true,
|
|
20122
|
+
onOpenChange: (open) => !open && close(),
|
|
20123
|
+
placement: "bottom start",
|
|
20124
|
+
offset: 6,
|
|
20125
|
+
className: cn("layer-overlay w-72 p-3", overlaySurface),
|
|
20126
|
+
children: /* @__PURE__ */ jsxs(Dialog$1, {
|
|
20127
|
+
"aria-label": words.label,
|
|
20128
|
+
className: "grid gap-3 outline-none",
|
|
20129
|
+
children: [/* @__PURE__ */ jsx(Field, {
|
|
20130
|
+
label: words.label,
|
|
20131
|
+
hint: words.hint,
|
|
20132
|
+
children: /* @__PURE__ */ jsx(Textarea, {
|
|
20133
|
+
autoFocus: true,
|
|
20134
|
+
rows: 3,
|
|
20135
|
+
value: text,
|
|
20136
|
+
onChange: (e) => setText(e.target.value),
|
|
20137
|
+
onKeyDown: (e) => {
|
|
20138
|
+
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
|
20139
|
+
e.preventDefault();
|
|
20140
|
+
close();
|
|
20141
|
+
}
|
|
20142
|
+
}
|
|
20143
|
+
})
|
|
20144
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
20145
|
+
className: "flex justify-between gap-2",
|
|
20146
|
+
children: [/* @__PURE__ */ jsxs(Button, {
|
|
20147
|
+
variant: "ghost",
|
|
20148
|
+
size: "sm",
|
|
20149
|
+
onPress: () => {
|
|
20150
|
+
removed.current = true;
|
|
20151
|
+
onRemove();
|
|
20152
|
+
onClose();
|
|
20153
|
+
},
|
|
20154
|
+
children: [/* @__PURE__ */ jsx(Trash2, {
|
|
20155
|
+
"aria-hidden": true,
|
|
20156
|
+
className: "size-4"
|
|
20157
|
+
}), words.remove ?? "Delete mark"]
|
|
20158
|
+
}), /* @__PURE__ */ jsx(Button, {
|
|
20159
|
+
size: "sm",
|
|
20160
|
+
onPress: close,
|
|
20161
|
+
children: words.done ?? "Done"
|
|
20162
|
+
})]
|
|
20163
|
+
})]
|
|
20164
|
+
})
|
|
20165
|
+
});
|
|
20166
|
+
}
|
|
20167
|
+
const TOOL_WORDS = {
|
|
20168
|
+
none: {
|
|
20169
|
+
label: "Look",
|
|
20170
|
+
icon: Hand
|
|
20171
|
+
},
|
|
20172
|
+
select: {
|
|
20173
|
+
label: "Select",
|
|
20174
|
+
icon: MousePointer2
|
|
20175
|
+
},
|
|
20176
|
+
pen: {
|
|
20177
|
+
label: "Pen",
|
|
20178
|
+
icon: PenLine
|
|
20179
|
+
},
|
|
20180
|
+
pencil: {
|
|
20181
|
+
label: "Pencil",
|
|
20182
|
+
icon: Pencil
|
|
20183
|
+
},
|
|
20184
|
+
marker: {
|
|
20185
|
+
label: "Marker",
|
|
20186
|
+
icon: Paintbrush
|
|
20187
|
+
},
|
|
20188
|
+
highlighter: {
|
|
20189
|
+
label: "Highlighter",
|
|
20190
|
+
icon: Highlighter
|
|
20191
|
+
},
|
|
20192
|
+
brush: {
|
|
20193
|
+
label: "Brush",
|
|
20194
|
+
icon: Brush
|
|
20195
|
+
},
|
|
20196
|
+
erase: {
|
|
20197
|
+
label: "Eraser",
|
|
20198
|
+
icon: Eraser
|
|
20199
|
+
}
|
|
20200
|
+
};
|
|
20201
|
+
const NIB_WORDS = {
|
|
20202
|
+
round: "Round",
|
|
20203
|
+
square: "Square",
|
|
20204
|
+
chisel: "Chisel"
|
|
20205
|
+
};
|
|
20206
|
+
const DEFAULT_TOOLS = [
|
|
20207
|
+
"none",
|
|
20208
|
+
"select",
|
|
20209
|
+
"pen",
|
|
20210
|
+
"pencil",
|
|
20211
|
+
"marker",
|
|
20212
|
+
"highlighter",
|
|
20213
|
+
"brush",
|
|
20214
|
+
"erase"
|
|
20215
|
+
];
|
|
20216
|
+
const DEFAULT_CONTROLS = [
|
|
20217
|
+
"colour",
|
|
20218
|
+
"width",
|
|
20219
|
+
"opacity",
|
|
20220
|
+
"hardness",
|
|
20221
|
+
"nib"
|
|
20222
|
+
];
|
|
20223
|
+
const isBrushKind = (t) => t in BRUSHES;
|
|
20224
|
+
/** The whole drawing kit: tools, colour, width, opacity, hardness and nib, with Undo, Redo and Delete for the chosen stroke. */
|
|
20225
|
+
function DrawingToolbar({ state, tool, brush, onChange, tools = DEFAULT_TOOLS, controls = DEFAULT_CONTROLS, palette, maxWidth, scale = 1, onNote, className }) {
|
|
20226
|
+
const choice = tool === "draw" ? brush.kind : tool;
|
|
20227
|
+
const most = maxWidth ?? 60 * scale;
|
|
20228
|
+
const { history, selected } = state;
|
|
20229
|
+
const choose = (t) => {
|
|
20230
|
+
if (isBrushKind(t)) {
|
|
20231
|
+
const base = BRUSHES[t];
|
|
20232
|
+
onChange({
|
|
20233
|
+
tool: "draw",
|
|
20234
|
+
brush: {
|
|
20235
|
+
...base,
|
|
20236
|
+
width: base.width * scale,
|
|
20237
|
+
colour: brush.colour
|
|
20238
|
+
}
|
|
20239
|
+
});
|
|
20240
|
+
} else onChange({
|
|
20241
|
+
tool: t,
|
|
20242
|
+
brush
|
|
20243
|
+
});
|
|
20244
|
+
};
|
|
20245
|
+
const chosen = tool === "select" && selected ? state.drawing.strokes.find((x) => x.id === selected) : void 0;
|
|
20246
|
+
const shown = chosen?.brush ?? brush;
|
|
20247
|
+
const set = (part) => {
|
|
20248
|
+
if (chosen) state.change(chosen.id, { brush: {
|
|
20249
|
+
...chosen.brush,
|
|
20250
|
+
...part
|
|
20251
|
+
} }, "restyle mark", `restyle-${chosen.id}-${Object.keys(part).join()}`);
|
|
20252
|
+
else onChange({
|
|
20253
|
+
tool,
|
|
20254
|
+
brush: {
|
|
20255
|
+
...brush,
|
|
20256
|
+
...part
|
|
20257
|
+
}
|
|
20258
|
+
});
|
|
20259
|
+
};
|
|
20260
|
+
const showBrush = tool === "draw" || chosen !== void 0;
|
|
20261
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
20262
|
+
className: cn("grid gap-2", className),
|
|
20263
|
+
children: [/* @__PURE__ */ jsxs(Toolbar, {
|
|
20264
|
+
label: "Drawing tools",
|
|
20265
|
+
className: "flex-wrap items-center gap-2",
|
|
20266
|
+
children: [
|
|
20267
|
+
tools.length > 1 && /* @__PURE__ */ jsx(ToggleGroup, {
|
|
20268
|
+
label: "Tool",
|
|
20269
|
+
size: "sm",
|
|
20270
|
+
value: choice,
|
|
20271
|
+
onChange: choose,
|
|
20272
|
+
options: tools.map((t) => ({
|
|
20273
|
+
value: t,
|
|
20274
|
+
label: TOOL_WORDS[t].label,
|
|
20275
|
+
icon: TOOL_WORDS[t].icon,
|
|
20276
|
+
iconOnly: true
|
|
20277
|
+
}))
|
|
20278
|
+
}),
|
|
20279
|
+
/* @__PURE__ */ jsx(ToolbarSeparator, {}),
|
|
20280
|
+
/* @__PURE__ */ jsx(IconButton, {
|
|
20281
|
+
size: "sm",
|
|
20282
|
+
variant: "ghost",
|
|
20283
|
+
icon: Undo2,
|
|
20284
|
+
label: history.undoLabel ? `Undo ${history.undoLabel}` : "Nothing to undo",
|
|
20285
|
+
disabled: !history.canUndo,
|
|
20286
|
+
onPress: history.undo
|
|
20287
|
+
}),
|
|
20288
|
+
/* @__PURE__ */ jsx(IconButton, {
|
|
20289
|
+
size: "sm",
|
|
20290
|
+
variant: "ghost",
|
|
20291
|
+
icon: Redo2,
|
|
20292
|
+
label: history.redoLabel ? `Redo ${history.redoLabel}` : "Nothing to redo",
|
|
20293
|
+
disabled: !history.canRedo,
|
|
20294
|
+
onPress: history.redo
|
|
20295
|
+
}),
|
|
20296
|
+
selected && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
20297
|
+
/* @__PURE__ */ jsx(ToolbarSeparator, {}),
|
|
20298
|
+
onNote && /* @__PURE__ */ jsx(Button, {
|
|
20299
|
+
size: "sm",
|
|
20300
|
+
variant: "ghost",
|
|
20301
|
+
onPress: () => onNote(selected),
|
|
20302
|
+
children: "Note"
|
|
20303
|
+
}),
|
|
20304
|
+
/* @__PURE__ */ jsx(IconButton, {
|
|
20305
|
+
size: "sm",
|
|
20306
|
+
variant: "ghost",
|
|
20307
|
+
icon: Trash2,
|
|
20308
|
+
label: "Delete mark",
|
|
20309
|
+
onPress: () => state.remove(selected)
|
|
20310
|
+
})
|
|
20311
|
+
] })
|
|
20312
|
+
]
|
|
20313
|
+
}), showBrush && controls.length > 0 && /* @__PURE__ */ jsxs("div", {
|
|
20314
|
+
className: "flex flex-wrap items-center gap-x-4 gap-y-2",
|
|
20315
|
+
children: [
|
|
20316
|
+
controls.includes("colour") && /* @__PURE__ */ jsx(ColorField, {
|
|
20317
|
+
"aria-label": "Colour",
|
|
20318
|
+
value: shown.colour,
|
|
20319
|
+
onChange: (c) => c && set({ colour: c }),
|
|
20320
|
+
suggestions: palette,
|
|
20321
|
+
className: "w-56"
|
|
20322
|
+
}),
|
|
20323
|
+
controls.includes("width") && /* @__PURE__ */ jsx(Labelled, {
|
|
20324
|
+
text: "Width",
|
|
20325
|
+
children: /* @__PURE__ */ jsx(Slider, {
|
|
20326
|
+
"aria-label": "Width",
|
|
20327
|
+
value: Math.round(shown.width * 10) / 10,
|
|
20328
|
+
onChange: (w) => set({ width: w }),
|
|
20329
|
+
min: Math.max(.5, scale),
|
|
20330
|
+
max: most,
|
|
20331
|
+
step: .5,
|
|
20332
|
+
className: "w-36"
|
|
20333
|
+
})
|
|
20334
|
+
}),
|
|
20335
|
+
controls.includes("opacity") && /* @__PURE__ */ jsx(Labelled, {
|
|
20336
|
+
text: "Opacity",
|
|
20337
|
+
children: /* @__PURE__ */ jsx(Slider, {
|
|
20338
|
+
"aria-label": "Opacity",
|
|
20339
|
+
value: Math.round(shown.opacity * 100),
|
|
20340
|
+
onChange: (o) => set({ opacity: o / 100 }),
|
|
20341
|
+
min: 5,
|
|
20342
|
+
max: 100,
|
|
20343
|
+
unit: "%",
|
|
20344
|
+
className: "w-36"
|
|
20345
|
+
})
|
|
20346
|
+
}),
|
|
20347
|
+
controls.includes("hardness") && /* @__PURE__ */ jsx(Labelled, {
|
|
20348
|
+
text: "Hardness",
|
|
20349
|
+
children: /* @__PURE__ */ jsx(Slider, {
|
|
20350
|
+
"aria-label": "Hardness",
|
|
20351
|
+
value: Math.round(shown.hardness * 100),
|
|
20352
|
+
onChange: (h) => set({ hardness: h / 100 }),
|
|
20353
|
+
min: 0,
|
|
20354
|
+
max: 100,
|
|
20355
|
+
unit: "%",
|
|
20356
|
+
className: "w-36"
|
|
20357
|
+
})
|
|
20358
|
+
}),
|
|
20359
|
+
controls.includes("nib") && /* @__PURE__ */ jsx(ToggleGroup, {
|
|
20360
|
+
label: "Nib",
|
|
20361
|
+
size: "sm",
|
|
20362
|
+
value: shown.nib,
|
|
20363
|
+
onChange: (n) => set({ nib: n }),
|
|
20364
|
+
options: Object.keys(NIB_WORDS).map((n) => ({
|
|
20365
|
+
value: n,
|
|
20366
|
+
label: NIB_WORDS[n]
|
|
20367
|
+
}))
|
|
20368
|
+
})
|
|
20369
|
+
]
|
|
20370
|
+
})]
|
|
20371
|
+
});
|
|
20372
|
+
}
|
|
20373
|
+
function Labelled({ text, children }) {
|
|
20374
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
20375
|
+
className: "inline-flex items-center gap-2",
|
|
20376
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
20377
|
+
"aria-hidden": true,
|
|
20378
|
+
className: "type-caption text-muted-foreground",
|
|
20379
|
+
children: text
|
|
20380
|
+
}), children]
|
|
20381
|
+
});
|
|
20382
|
+
}
|
|
20383
|
+
|
|
19201
20384
|
// src/components/save.tsx
|
|
19202
20385
|
/** Saves `value` after changes settle, never two at once, and says where it stands. The document's first `value` counts as saved. */
|
|
19203
20386
|
function useAutoSave({ value, save, delayMs = 1500, equals = Object.is, describeError = describeActionError }) {
|
|
@@ -22084,5 +23267,5 @@ function FolderPicker({ label, useFolders: useListing, value, onChange, onChoose
|
|
|
22084
23267
|
});
|
|
22085
23268
|
}
|
|
22086
23269
|
|
|
22087
|
-
export { ActiveList, Alert, AlertDialog, AppShell, AsyncButton, BookViewer, BoxList, BoxOverlay, BoxedPicture, Breadcrumb, BulkBar, Button, ButtonLink, ByteView, COLOR_SCHEME_STORAGE_KEY, Checkbox, ChoiceGrid, ChoiceGridSkeleton, ChoiceGroup, ClipsProvider, ColorField, ColorSchemeChoice, CommandLegend, CommandMenu, Compare, CompareAudio, CompareGrid, CompareText, CompareVideo, CompareVideoGrid, Composer, CompositionBar, Console, ContextMenu, Conversation, CursorScope, DEFAULT_PALETTE, DEFAULT_TOLERANCE, DataTable, DataView$1 as DataView, Density, Dialog, Disclosure, Done, DragSource, EditListPlayer, EmptyState, FIT, Field, FileButton, FileDropOverlay, FileDropZone, FileOutcomes, FilmStrip, FolderPicker, FullscreenFrame, Glimpse, HttpError, INK_COLOURS, IconButton, InfiniteList, Input, InputLevel, Inspector, InspectorSection, JobStatus, JobsTable, KeysLegend, LEVEL_FLOOR_DB, Lightbox, LineChart, LineChartGroup, LinkCard, LinkCardList, ListeningLevel, LoadError, LoadMore, MAX_SCALE, MAX_SIDE, MIN_SCALE, MISSING, Mark, MarkSummary, MarkableText, MediaContents, MediaGroup, MediaPlayer, MediaStage, Menu, Meter, Metric, MetricStrip, MicrophoneContext, MicrophoneError, MissingFile, NBSP, NativeSelect, NumberField, OutboxStatus, PALETTE_STORAGE_KEY, PageHeader, PairJudge, PairVote, PaletteChoice, Pane, PauseButton, PixelImage, PlaceBoard, PlayWindowButton, PlayerNotice, Popover, PropertyList, PropertyRow, RateChoice, RegionEditor, RichText, RowAction, SPEECH_RATES, STRATA_DRAG_TYPE, SaveStatus, SayAgain, SayPicture, ScheduleTable, SecretField, SeekBar, SelectableGrid, SelectableGridSkeleton, SequenceTimeline, ShortcutEditor, SideNav, Skeleton, Slider, SortableList, SpanActions, SpanStrip, Split, StageGuides, StaleNotice, Status, StrataQueryProvider, StrataToastRegion, Switch, SystemPanel, TableSkeleton, Tabs, TakeLevel, TextLink, Textarea, ThemeProvider, Thumbnail, ThumbnailSkeleton, TileGrid, TileGridSkeleton, TimeReadout, Timeline, TimelinePlayer, TimelineZoomControls, Toggle, ToggleGroup, Toolbar, ToolbarSeparator, Tooltip, TranscriptView, Transport, Tree, UndoBar, ViewOptions, ViewportFrame, VirtualList, VolumeControl, Waveform, WorkflowSteps, WorkflowSummary, Workspace, WorkspaceBar, actualPixelsScale, ancestorsOf, apiPage, appBase, boxViewport, browserMicrophone, buttonVariants, canFullscreen, canPlayClips, canSay, canSpeak, checkKey, clampToRoom, clampViewport, clipsQueryKey, cn, colorSchemeScript, commandShortcut, continues, countMarks, createApi, createFormat, createOutbox, createQueryClient, createReserve, createSegmenter, createTypedApi, crossedThreshold, currentItem, describeActionError, describeHttpError, describeRegion, describeWorkflowSummary, detectPlatform, diffWords, differences, diskThresholds, drawRect, ease, editTimeOf, editTimes, encodeWav, errorPayload, explainMicrophoneError, fetchJson, fieldErrorsOf, findPage, fitRect, followAim, format, formatChord, frameAt, gainToVolume, geo, gridPoints, gutterOf, inkMatch, inputLevelThresholds, isDoubtful, isRefusal, isReservedChord, isTaskRunning, isTypingTarget, isWorkflowRunning, jobProgressText, joinMediaGroup, joinRects, joinSamples, keyName, labelOf, lerpRect, locateInEdit, logScale, loudnessOf, marksFor, matchEntities, matchGains, matchesAccept, matchesChord, measureLoudness, mediaStatRows, moveGutter, moveRect, nextBreak, noOverlap, normaliseAngle, pageName, paletteScript, palettes, parentOf, parseChord, parseInline, parseRichText, parseSort, peakDb, peakGain, peaksFromAudio, phraseAt, phraseDoubtful, phraseOfWordId, pickEvenly, pickVoice, pixelZoom, plainText, rangeBetween, readErrorBody, readPixels, readingOrder, regionShares, regionStyle, resample, resamplePeaks, resizeRect, rgbaToBlob, rmsDb, safeHref, sayPosition, serialiseSort, settleCut, skipAction, snapToGrid, sortRows, speak, speechLevel, splitRect, staleOf, stepId, stepOnGrid, stopSpeaking, summariseWorkflow, textOf, tickLabel, tickStep, ticksBetween, toDate, toLoad, toPeakPair, toast, toastTimeout, transportShortcuts, turnRect, turnedSize, typeSteps, useActiveItem, useAsyncAction, useAttachedSource, useAutoRecorder, useAutoSave, useBookUrl, useBoxes, useChoices, useClientWork, useClips, useColorScheme, useCommands, useCountdown, useDifference, useEditList, useExpanded, useFileIntake, useHistory, useHistoryState, useHotkeys, useInView, useInfiniteLoad, useInkMatch, useJob, useJobAction, useLeaveGuard, useLightboxUrl, useLoad, useMediaElement, useMediaSpans, useMediaStats, useMicrophone, useObjectUrl, useOutbox, useOutboxStarted, usePalette, usePlayerWarnings, usePoll, useRebindableShortcuts, useRecorder, useReserve, useSayOnShow, useSearchParam, useSelection, useSettledStatus, useSharedCursor, useSpeech, useSystemColorScheme, useSystemInfo, useTimeline, useTimelineZoom, useUndoable, useViewOptions, useViewPointers, useViewport, useWheelZoom, useWorkflowRun, useWorkspaceState, valueAt, viewOf, viewsOf, withBase, wordAt, wordId, zoomAt, zoomToBox };
|
|
23270
|
+
export { ActiveList, Alert, AlertDialog, AppShell, AsyncButton, BRUSHES, BookViewer, BoxList, BoxOverlay, BoxedPicture, Breadcrumb, BulkBar, Button, ButtonLink, ByteView, COLOR_SCHEME_STORAGE_KEY, Checkbox, ChoiceGrid, ChoiceGridSkeleton, ChoiceGroup, ClipsProvider, ColorField, ColorSchemeChoice, CommandLegend, CommandMenu, Compare, CompareAudio, CompareGrid, CompareText, CompareVideo, CompareVideoGrid, Composer, CompositionBar, Console, ContextMenu, Conversation, CursorScope, DEFAULT_PALETTE, DEFAULT_TOLERANCE, DataTable, DataView$1 as DataView, Density, Dialog, Disclosure, Done, DragSource, DrawingLayer, DrawingToolbar, EditListPlayer, EmptyState, FIT, Field, FileButton, FileDropOverlay, FileDropZone, FileOutcomes, FilmStrip, FolderPicker, FullscreenFrame, Glimpse, HttpError, INK_COLOURS, IconButton, InfiniteList, Input, InputLevel, Inspector, InspectorSection, JobStatus, JobsTable, KeysLegend, LEVEL_FLOOR_DB, Lightbox, LineChart, LineChartGroup, LinkCard, LinkCardList, ListeningLevel, LoadError, LoadMore, MAX_SCALE, MAX_SIDE, MIN_SCALE, MISSING, Mark, MarkSummary, MarkableText, MediaContents, MediaGroup, MediaPlayer, MediaStage, Menu, Meter, Metric, MetricStrip, MicrophoneContext, MicrophoneError, MissingFile, NBSP, NativeSelect, NumberField, OutboxStatus, PALETTE_STORAGE_KEY, PageHeader, PairJudge, PairVote, PaletteChoice, Pane, PauseButton, PixelImage, PlaceBoard, PlayWindowButton, PlayerNotice, Popover, PropertyList, PropertyRow, RateChoice, RegionEditor, RichText, RowAction, SPEECH_RATES, STRATA_DRAG_TYPE, SaveStatus, SayAgain, SayPicture, ScheduleTable, SecretField, SeekBar, SelectableGrid, SelectableGridSkeleton, SequenceTimeline, ShortcutEditor, SideNav, Skeleton, Slider, SortableList, SpanActions, SpanStrip, Split, StageGuides, StaleNotice, Status, StrataQueryProvider, StrataToastRegion, Switch, SystemPanel, TableSkeleton, Tabs, TakeLevel, TextLink, Textarea, ThemeProvider, Thumbnail, ThumbnailSkeleton, TileGrid, TileGridSkeleton, TimeReadout, Timeline, TimelinePlayer, TimelineZoomControls, Toggle, ToggleGroup, Toolbar, ToolbarSeparator, Tooltip, TranscriptView, Transport, Tree, UndoBar, ViewOptions, ViewportFrame, VirtualList, VolumeControl, Waveform, WorkflowSteps, WorkflowSummary, Workspace, WorkspaceBar, actualPixelsScale, ancestorsOf, apiPage, appBase, applyOp, boxViewport, browserMicrophone, buttonVariants, canFullscreen, canPlayClips, canSay, canSpeak, checkKey, clampToRoom, clampViewport, clipsQueryKey, cn, colorSchemeScript, commandShortcut, continues, countMarks, createApi, createFormat, createOutbox, createQueryClient, createReserve, createSegmenter, createTypedApi, crossedThreshold, currentItem, currentOf, curvePoints, describeActionError, describeHttpError, describeRegion, describeWorkflowSummary, detectPlatform, diffWords, differences, diskThresholds, drawRect, drawingToSvg, ease, editTimeOf, editTimes, emptyDrawing, encodeWav, errorPayload, explainMicrophoneError, feedbackMarker, fetchJson, fieldErrorsOf, filterMarkup, findPage, fitPoints, fitRect, followAim, format, formatChord, frameAt, gainToVolume, geo, gridPoints, gutterOf, hitsStroke, inkMatch, inputLevelThresholds, invertOp, isDoubtful, isRefusal, isReservedChord, isTaskRunning, isTypingTarget, isWorkflowRunning, jobProgressText, joinMediaGroup, joinRects, joinSamples, keyName, labelOf, lerpRect, locateInEdit, logScale, loudnessOf, marksFor, matchEntities, matchGains, matchesAccept, matchesChord, measureLoudness, mediaStatRows, moveGutter, movePoints, moveRect, newStrokeId, nextBreak, noOverlap, normaliseAngle, pageName, paletteScript, palettes, parentOf, parseChord, parseInline, parseRichText, parseSort, peakDb, peakGain, peaksFromAudio, phraseAt, phraseDoubtful, phraseOfWordId, pickEvenly, pickVoice, pixelZoom, plainText, pointsBox, rangeBetween, readErrorBody, readPixels, readingOrder, regionShares, regionStyle, replay, resample, resamplePeaks, rescaleDrawing, resizeRect, rgbaToBlob, rmsDb, safeHref, sayPosition, serialiseSort, settleCut, simplifyPoints, skipAction, smoothPath, snapToGrid, sortRows, speak, speechLevel, splitRect, staleOf, stepId, stepOnGrid, stopSpeaking, strokeAt, strokeBox, strokeShape, summariseWorkflow, textOf, tickLabel, tickStep, ticksBetween, toDate, toLoad, toPeakPair, toast, toastTimeout, transportShortcuts, turnRect, turnedSize, typeSteps, useActiveItem, useAsyncAction, useAttachedSource, useAutoRecorder, useAutoSave, useBookUrl, useBoxes, useChoices, useClientWork, useClips, useColorScheme, useCommands, useCountdown, useDifference, useDrawing, useEditList, useExpanded, useFileIntake, useHistory, useHistoryState, useHotkeys, useInView, useInfiniteLoad, useInkMatch, useJob, useJobAction, useLeaveGuard, useLightboxUrl, useLoad, useMediaElement, useMediaSpans, useMediaStats, useMicrophone, useObjectUrl, useOutbox, useOutboxStarted, usePalette, usePlayerWarnings, usePoll, useRebindableShortcuts, useRecorder, useReserve, useSayOnShow, useSearchParam, useSelection, useSettledStatus, useSharedCursor, useSpeech, useSystemColorScheme, useSystemInfo, useTimeline, useTimelineZoom, useUndoable, useViewOptions, useViewPointers, useViewport, useWheelZoom, useWorkflowRun, useWorkspaceState, valueAt, viewOf, viewsOf, widthsOf, withBase, wordAt, wordId, zoomAt, zoomToBox };
|
|
22088
23271
|
//# sourceMappingURL=index.js.map
|