@copilotz/work 0.0.0-stage → 1.0.0-rc.35

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.
Files changed (147) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -2
  3. package/boards/actions/create-board.d.ts +1 -0
  4. package/boards/actions/create-board.js +47 -0
  5. package/boards/actions/create-card.d.ts +1 -0
  6. package/boards/actions/create-card.js +83 -0
  7. package/boards/actions/delete-board.d.ts +1 -0
  8. package/boards/actions/delete-board.js +30 -0
  9. package/boards/actions/delete-card.d.ts +1 -0
  10. package/boards/actions/delete-card.js +24 -0
  11. package/boards/actions/move-card.d.ts +1 -0
  12. package/boards/actions/move-card.js +85 -0
  13. package/boards/actions/update-board.d.ts +1 -0
  14. package/boards/actions/update-board.js +110 -0
  15. package/boards/actions/update-card.d.ts +1 -0
  16. package/boards/actions/update-card.js +67 -0
  17. package/boards/collections/board.d.ts +71 -0
  18. package/boards/collections/board.js +32 -0
  19. package/boards/collections/card.d.ts +74 -0
  20. package/boards/collections/card.js +29 -0
  21. package/boards/contract.d.ts +67 -0
  22. package/boards/contract.js +20 -0
  23. package/boards/mod.d.ts +34 -0
  24. package/boards/mod.js +72 -0
  25. package/boards/resources/tools.d.ts +2 -0
  26. package/boards/resources/tools.js +414 -0
  27. package/boards/shared/columns.d.ts +39 -0
  28. package/boards/shared/columns.js +88 -0
  29. package/boards/shared/dates.d.ts +11 -0
  30. package/boards/shared/dates.js +22 -0
  31. package/boards/shared/errors.d.ts +24 -0
  32. package/boards/shared/errors.js +20 -0
  33. package/boards/shared/fields.d.ts +65 -0
  34. package/boards/shared/fields.js +44 -0
  35. package/boards/shared/load.d.ts +22 -0
  36. package/boards/shared/load.js +32 -0
  37. package/boards/shared/order.d.ts +30 -0
  38. package/boards/shared/order.js +47 -0
  39. package/boards/shared/people.d.ts +30 -0
  40. package/boards/shared/people.js +87 -0
  41. package/boards/shared/said.d.ts +28 -0
  42. package/boards/shared/said.js +30 -0
  43. package/boards/views/web/board-head.d.ts +62 -0
  44. package/boards/views/web/board-head.js +170 -0
  45. package/boards/views/web/board-page.d.ts +17 -0
  46. package/boards/views/web/board-page.js +97 -0
  47. package/boards/views/web/board-view.d.ts +40 -0
  48. package/boards/views/web/board-view.js +341 -0
  49. package/boards/views/web/boards-tab.d.ts +13 -0
  50. package/boards/views/web/boards-tab.js +109 -0
  51. package/boards/views/web/card-sheet.d.ts +47 -0
  52. package/boards/views/web/card-sheet.js +314 -0
  53. package/boards/views/web/card-tile.d.ts +29 -0
  54. package/boards/views/web/card-tile.js +20 -0
  55. package/boards/views/web/column.d.ts +38 -0
  56. package/boards/views/web/column.js +171 -0
  57. package/boards/views/web/drag.d.ts +36 -0
  58. package/boards/views/web/drag.js +276 -0
  59. package/boards/views/web/glyphs.d.ts +29 -0
  60. package/boards/views/web/glyphs.js +25 -0
  61. package/boards/views/web/load.d.ts +32 -0
  62. package/boards/views/web/load.js +33 -0
  63. package/boards/views/web/palette.d.ts +15 -0
  64. package/boards/views/web/palette.js +180 -0
  65. package/boards/views/web/pickers.d.ts +45 -0
  66. package/boards/views/web/pickers.js +83 -0
  67. package/boards/views/web/shown.d.ts +77 -0
  68. package/boards/views/web/shown.js +136 -0
  69. package/boards/views/web/step.d.ts +13 -0
  70. package/boards/views/web/step.js +41 -0
  71. package/boards/views/web/style.d.ts +10 -0
  72. package/boards/views/web/style.js +380 -0
  73. package/boards/views/web/toolbar.d.ts +21 -0
  74. package/boards/views/web/toolbar.js +39 -0
  75. package/boards/web.d.ts +26 -0
  76. package/boards/web.js +52 -0
  77. package/contract.d.ts +7 -0
  78. package/contract.js +7 -0
  79. package/documents/actions/create.d.ts +1 -0
  80. package/documents/actions/create.js +28 -0
  81. package/documents/actions/delete.d.ts +1 -0
  82. package/documents/actions/delete.js +32 -0
  83. package/documents/actions/presence.d.ts +1 -0
  84. package/documents/actions/presence.js +45 -0
  85. package/documents/actions/rename.d.ts +1 -0
  86. package/documents/actions/rename.js +33 -0
  87. package/documents/actions/restore.d.ts +1 -0
  88. package/documents/actions/restore.js +39 -0
  89. package/documents/actions/save.d.ts +1 -0
  90. package/documents/actions/save.js +78 -0
  91. package/documents/collections/document-presence.d.ts +79 -0
  92. package/documents/collections/document-presence.js +25 -0
  93. package/documents/collections/document-version.d.ts +166 -0
  94. package/documents/collections/document-version.js +60 -0
  95. package/documents/collections/document.d.ts +131 -0
  96. package/documents/collections/document.js +72 -0
  97. package/documents/contract.d.ts +17 -0
  98. package/documents/contract.js +1 -0
  99. package/documents/mod.d.ts +38 -0
  100. package/documents/mod.js +75 -0
  101. package/documents/resources/tools.d.ts +2 -0
  102. package/documents/resources/tools.js +253 -0
  103. package/documents/shared/diff.d.ts +37 -0
  104. package/documents/shared/diff.js +148 -0
  105. package/documents/shared/merge.d.ts +29 -0
  106. package/documents/shared/merge.js +120 -0
  107. package/documents/shared/presence.d.ts +7 -0
  108. package/documents/shared/presence.js +7 -0
  109. package/documents/shared/text.d.ts +36 -0
  110. package/documents/shared/text.js +67 -0
  111. package/documents/shared/who.d.ts +23 -0
  112. package/documents/shared/who.js +39 -0
  113. package/documents/shared/write.d.ts +54 -0
  114. package/documents/shared/write.js +123 -0
  115. package/documents/views/web/changes.d.ts +22 -0
  116. package/documents/views/web/changes.js +57 -0
  117. package/documents/views/web/conflict.d.ts +38 -0
  118. package/documents/views/web/conflict.js +96 -0
  119. package/documents/views/web/draft.d.ts +18 -0
  120. package/documents/views/web/draft.js +32 -0
  121. package/documents/views/web/editor.d.ts +57 -0
  122. package/documents/views/web/editor.js +268 -0
  123. package/documents/views/web/format.d.ts +56 -0
  124. package/documents/views/web/format.js +245 -0
  125. package/documents/views/web/history.d.ts +23 -0
  126. package/documents/views/web/history.js +118 -0
  127. package/documents/views/web/list.d.ts +37 -0
  128. package/documents/views/web/list.js +211 -0
  129. package/documents/views/web/page.d.ts +35 -0
  130. package/documents/views/web/page.js +416 -0
  131. package/documents/views/web/palette.d.ts +16 -0
  132. package/documents/views/web/palette.js +168 -0
  133. package/documents/views/web/people.d.ts +60 -0
  134. package/documents/views/web/people.js +63 -0
  135. package/documents/views/web/presence.d.ts +30 -0
  136. package/documents/views/web/presence.js +75 -0
  137. package/documents/views/web/step.d.ts +25 -0
  138. package/documents/views/web/step.js +81 -0
  139. package/documents/views/web/style.d.ts +7 -0
  140. package/documents/views/web/style.js +266 -0
  141. package/documents/web.d.ts +18 -0
  142. package/documents/web.js +46 -0
  143. package/mod.d.ts +18 -0
  144. package/mod.js +18 -0
  145. package/package.json +61 -4
  146. package/web.d.ts +13 -0
  147. package/web.js +13 -0
@@ -0,0 +1,96 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ /**
3
+ * When two saves changed the same lines: who saved what's there now, and
4
+ * when, what yours would change in theirs (a diff), and three ways on:
5
+ * keep yours (saved as a new version over theirs, which stays in History),
6
+ * take theirs (yours put aside, to bring back), or merge by hand: each
7
+ * place you both changed, side by side, to keep theirs, yours, both, or
8
+ * lines of your own; the rest is merged already. Nothing of yours is lost
9
+ * on the way: until it's saved, it's still your draft.
10
+ */
11
+ import { useMemo, useState } from "react";
12
+ import { Badge, Button, Card, CodeBlock, Icon, Notice, Tabs, Textarea, Time } from "copilotz/web";
13
+ import { merge, mergeField, textOf } from "../../shared/merge.js";
14
+ import { Diff } from "./changes.js";
15
+ import { Face } from "./people.js";
16
+ export function Conflict({ base, mine, theirs, busy, error, onKeep, onTake, onSave, onEdit }) {
17
+ const [by, setBy] = useState("choose");
18
+ const [view, setView] = useState("both");
19
+ const who = theirs.by.name;
20
+ return (_jsxs("section", { className: "doc-conflict", "aria-labelledby": "doc-conflict-title", children: [_jsxs("div", { className: "doc-conflict-head", children: [_jsx("span", { className: "doc-conflict-icon", "aria-hidden": "true", children: _jsx(Icon, { name: "branch", size: 18 }) }), _jsxs("div", { children: [_jsxs("h2", { id: "doc-conflict-title", children: [who, " saved version ", theirs.version, " while you were editing version ", base.version] }), _jsxs("p", { children: [_jsx(Face, { who: theirs.by, size: 18 }), " ", who, " \u00B7", " ", _jsx(Time, { at: theirs.at, long: true }), ". You both changed the same lines, so nothing was saved yet. Your text is safe here: choose what to keep."] })] })] }), error ? _jsx(Notice, { tone: "destructive", role: "alert", children: error }) : null, by === "choose"
21
+ ? (_jsxs(_Fragment, { children: [_jsxs("div", { className: "doc-choices", children: [_jsxs("button", { type: "button", className: "doc-choice", disabled: busy, onClick: onKeep, children: [_jsxs("span", { className: "doc-choice-name", children: [_jsx(Icon, { name: "check", size: 15 }), " Keep mine"] }), _jsxs("span", { className: "doc-choice-says", children: ["Save yours as version ", theirs.version + 1, ", over ", who, "'s. Theirs stays in History."] })] }), _jsxs("button", { type: "button", className: "doc-choice", disabled: busy, onClick: onTake, children: [_jsxs("span", { className: "doc-choice-name", children: [_jsx(Icon, { name: "download", size: 15 }), " Take theirs"] }), _jsxs("span", { className: "doc-choice-says", children: ["Load ", who, "'s version", " ", theirs.version, ". Yours is put aside, to bring back if you change your mind."] })] }), _jsxs("button", { type: "button", className: "doc-choice doc-choice-primary", disabled: busy, onClick: () => setBy("hand"), children: [_jsxs("span", { className: "doc-choice-name", children: [_jsx(Icon, { name: "branch", size: 15 }), " Merge by hand"] }), _jsx("span", { className: "doc-choice-says", children: "Go through each place you both changed and choose. The rest is merged already." })] })] }), _jsxs("div", { className: "doc-conflict-diff", children: [_jsx(Tabs, { label: "Which changes", variant: "line", value: view, onChange: setView, tabs: [
22
+ { value: "both", label: "Yours against theirs" },
23
+ { value: "theirs", label: `${who}'s changes` },
24
+ { value: "mine", label: "Your changes" },
25
+ ] }), view === "both"
26
+ ? (_jsx(Diff, { before: theirs.text, after: mine.text, title: `− ${who}'s version ${theirs.version} · + yours`, empty: "The texts are the same: only the titles differ." }))
27
+ : view === "theirs"
28
+ ? (_jsx(Diff, { before: base.text, after: theirs.text, title: `What ${who} changed since version ${base.version}` }))
29
+ : (_jsx(Diff, { before: base.text, after: mine.text, title: `What you changed since version ${base.version}` })), mine.title !== theirs.title
30
+ ? (_jsxs("p", { className: "doc-conflict-titles", children: ["Titles: ", who, "'s ", _jsxs("b", { children: ["\u201C", theirs.title, "\u201D"] }), ", yours ", _jsxs("b", { children: ["\u201C", mine.title, "\u201D"] }), "."] }))
31
+ : null] })] }))
32
+ : (_jsx(ByHand, { base: base, mine: mine, theirs: theirs, busy: busy, back: () => setBy("choose"), onSave: onSave, onEdit: onEdit }))] }));
33
+ }
34
+ /** A conflict's lines, as a block: whose, and what. */
35
+ function Side({ lines, title, tone }) {
36
+ return (_jsx(CodeBlock, { className: `doc-side doc-side-${tone}`, title: title, code: lines.join("\n"), empty: "(nothing: these lines were taken out)", wrap: true, lines: 14 }));
37
+ }
38
+ /** The lines just before a conflict, for where it is. */
39
+ const before = (chunks, at) => {
40
+ const prev = chunks[at - 1];
41
+ return prev?.kind === "same" ? prev.lines.slice(-2).filter((l) => l.trim()) : [];
42
+ };
43
+ function ByHand({ base, mine, theirs, busy, back, onSave, onEdit }) {
44
+ const merged = useMemo(() => merge(base.text, mine.text, theirs.text), [
45
+ base.text,
46
+ mine.text,
47
+ theirs.text,
48
+ ]);
49
+ const clashes = merged.chunks.flatMap((c, i) => c.kind === "conflict" ? [{ c, i }] : []);
50
+ const [picks, setPicks] = useState([]);
51
+ const [editing, setEditing] = useState();
52
+ const titled = mergeField(base.title, mine.title, theirs.title);
53
+ const [title, setTitle] = useState(titled);
54
+ const settled = picks.filter(Boolean).length;
55
+ const ready = settled === clashes.length && title !== undefined;
56
+ const result = () => ({
57
+ text: textOf(merged.chunks, clashes.map((_, n) => picks[n] ?? "mine")),
58
+ title: title ?? mine.title,
59
+ });
60
+ const pick = (n, p) => {
61
+ const next = [...picks];
62
+ next[n] = p;
63
+ setPicks(next);
64
+ };
65
+ const who = theirs.by.name;
66
+ return (_jsxs("div", { className: "doc-hand", children: [_jsxs("div", { className: "doc-hand-head", children: [_jsx(Button, { variant: "ghost", size: "sm", icon: "chevron-left", onClick: back, children: "Back" }), _jsxs("p", { role: "status", children: [clashes.length ? `${settled} of ${clashes.length} settled` : "No lines clash any more", title === undefined ? " · the title too" : ""] })] }), clashes.map(({ c, i }, n) => {
67
+ if (c.kind !== "conflict")
68
+ return null;
69
+ const p = picks[n];
70
+ const near = before(merged.chunks, i);
71
+ return (_jsxs(Card, { as: "section", padding: "none", className: "doc-clash", "data-settled": !!p || undefined, children: [_jsxs("header", { className: "doc-clash-head", children: [_jsxs("b", { children: ["Clash ", n + 1, " of ", clashes.length] }), near.length ? _jsxs("span", { className: "doc-clash-near", children: ["after \u201C", near.at(-1), "\u201D"] }) : null, p
72
+ ? (_jsx(Badge, { tone: "success", children: p === "mine"
73
+ ? "Yours"
74
+ : p === "theirs"
75
+ ? `${who}'s`
76
+ : p === "both"
77
+ ? "Both"
78
+ : "Your own" }))
79
+ : _jsx(Badge, { tone: "warning", children: "To choose" })] }), _jsxs("div", { className: "doc-clash-sides", children: [_jsx(Side, { lines: c.theirs, title: `${who}'s`, tone: "theirs" }), _jsx(Side, { lines: c.mine, title: "Yours", tone: "mine" })] }), editing === n
80
+ ? (_jsx(OwnLines, { start: typeof p === "object" ? p.lines : [...c.theirs, ...c.mine], done: (lines) => {
81
+ if (lines)
82
+ pick(n, { lines });
83
+ setEditing(undefined);
84
+ } }))
85
+ : (_jsxs("div", { className: "doc-clash-picks", role: "group", "aria-label": `Clash ${n + 1}: keep`, children: [_jsxs(Button, { size: "sm", variant: p === "theirs" ? "primary" : "outline", "aria-pressed": p === "theirs", onClick: () => pick(n, "theirs"), children: ["Use ", who, "'s"] }), _jsx(Button, { size: "sm", variant: p === "mine" ? "primary" : "outline", "aria-pressed": p === "mine", onClick: () => pick(n, "mine"), children: "Use yours" }), _jsxs(Button, { size: "sm", variant: p === "both" ? "primary" : "outline", "aria-pressed": p === "both", onClick: () => pick(n, "both"), children: ["Both, ", who, "'s first"] }), _jsx(Button, { size: "sm", variant: typeof p === "object" ? "primary" : "ghost", icon: "pencil", onClick: () => setEditing(n), children: "Write it\u2026" })] }))] }, i));
86
+ }), titled === undefined
87
+ ? (_jsxs(Card, { as: "section", padding: "none", className: "doc-clash", "data-settled": title !== undefined || undefined, children: [_jsxs("header", { className: "doc-clash-head", children: [_jsx("b", { children: "The title" }), title !== undefined
88
+ ? _jsx(Badge, { tone: "success", children: "Chosen" })
89
+ : _jsx(Badge, { tone: "warning", children: "To choose" })] }), _jsxs("div", { className: "doc-clash-picks", role: "group", "aria-label": "The title: keep", children: [_jsxs(Button, { size: "sm", variant: title === theirs.title ? "primary" : "outline", onClick: () => setTitle(theirs.title), children: [who, "'s: \u201C", theirs.title, "\u201D"] }), _jsxs(Button, { size: "sm", variant: title === mine.title ? "primary" : "outline", onClick: () => setTitle(mine.title), children: ["Yours: \u201C", mine.title, "\u201D"] })] })] }))
90
+ : null, _jsxs("div", { className: "doc-hand-foot", children: [_jsxs(Button, { variant: "primary", icon: "check", loading: busy, disabled: !ready, onClick: () => onSave(result()), children: ["Save the merge as version ", theirs.version + 1] }), _jsx(Button, { disabled: !ready || busy, icon: "pencil", onClick: () => onEdit(result()), children: "Open it in the editor first" })] })] }));
91
+ }
92
+ /** Lines of one's own for a clash: a field with both sides to start from. */
93
+ function OwnLines({ start, done }) {
94
+ const [text, setText] = useState(start.join("\n"));
95
+ return (_jsxs("div", { className: "doc-own", children: [_jsx(Textarea, { autoGrow: true, maxHeight: 360, value: text, "aria-label": "The lines to keep here", onChange: (event) => setText(event.target.value), autoFocus: true }), _jsxs("div", { className: "doc-clash-picks", children: [_jsx(Button, { size: "sm", variant: "primary", onClick: () => done(text.split("\n")), children: "Use these lines" }), _jsx(Button, { size: "sm", variant: "ghost", onClick: () => done(), children: "Cancel" })] })] }));
96
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * What someone wrote and hasn't saved, kept in this browser per person and
3
+ * document, so a closed tab or a lost connection loses nothing: the text and
4
+ * title, and the version they started from (with its text, to merge with
5
+ * what others saved since). Gone once it's saved.
6
+ */
7
+ export type Draft = {
8
+ base: number;
9
+ baseText: string;
10
+ baseTitle: string;
11
+ text: string;
12
+ title: string;
13
+ at: string;
14
+ };
15
+ /** The draft kept for a document, if there's one. */
16
+ export declare function draftOf(viewer: string, document: string): Draft | undefined;
17
+ export declare function keepDraft(viewer: string, document: string, draft: Draft): void;
18
+ export declare function dropDraft(viewer: string, document: string): void;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * What someone wrote and hasn't saved, kept in this browser per person and
3
+ * document, so a closed tab or a lost connection loses nothing: the text and
4
+ * title, and the version they started from (with its text, to merge with
5
+ * what others saved since). Gone once it's saved.
6
+ */
7
+ const key = (viewer, document) => `copilotz-document-draft:${viewer}:${document}`;
8
+ /** The draft kept for a document, if there's one. */
9
+ export function draftOf(viewer, document) {
10
+ try {
11
+ const raw = globalThis.localStorage?.getItem(key(viewer, document));
12
+ const draft = raw ? JSON.parse(raw) : undefined;
13
+ return draft && typeof draft.text === "string" && typeof draft.base === "number"
14
+ ? draft
15
+ : undefined;
16
+ }
17
+ catch {
18
+ return undefined;
19
+ }
20
+ }
21
+ export function keepDraft(viewer, document, draft) {
22
+ try {
23
+ globalThis.localStorage?.setItem(key(viewer, document), JSON.stringify(draft));
24
+ }
25
+ catch { /* Kept in the page only: storage is full, or not ours to use. */ }
26
+ }
27
+ export function dropDraft(viewer, document) {
28
+ try {
29
+ globalThis.localStorage?.removeItem(key(viewer, document));
30
+ }
31
+ catch { /* Nothing kept. */ }
32
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * The writing surface: Markdown as it's written, in a field as tall as what
3
+ * it holds (the page scrolls, as a document does), a small toolbar over it
4
+ * (bold, italic, heading, lists, a task, a quote, code, a link) and the
5
+ * shortcuts people know (⌘B, ⌘I, ⌘E, ⌘K on selected words, ⌘⇧7/8/9,
6
+ * ⌘⌥1–3; Enter goes on with a list, Tab indents it; an address pasted over
7
+ * words links them). Write, Preview (the toolkit's `Markdown`, as the page
8
+ * reads) or both side by side (Split, where there's room, the preview
9
+ * following the page's scroll). Every change goes in as typing would, so
10
+ * undo undoes it.
11
+ */
12
+ import { type ReactNode, type RefObject } from "react";
13
+ import { type Change } from "./format.js";
14
+ export type Mode = "write" | "preview" | "split";
15
+ /** Makes a change in the field as typing would (undo undoes it), then selects what it says. */
16
+ export declare function apply(el: HTMLTextAreaElement, c: Change): void;
17
+ /** A toolbar button's drawing, on a 24 by 24 grid, in the text's colour. */
18
+ declare const GLYPHS: {
19
+ readonly bold: "M7 5h6a3.5 3.5 0 0 1 0 7H7zM7 12h7a3.5 3.5 0 0 1 0 7H7z";
20
+ readonly italic: "M19 4h-9M14 20H5M15 4 9 20";
21
+ readonly heading: "M6 4v16M18 4v16M6 12h12";
22
+ readonly bullet: "M9 6h11M9 12h11M9 18h11M4.5 6h.01M4.5 12h.01M4.5 18h.01";
23
+ readonly number: "M10 6h10M10 12h10M10 18h10M4 4h1.5v5M4 9h3M7 19H4c0-1.6 3-2.4 3-4a1.5 1.5 0 0 0-3 0";
24
+ readonly task: "M3.5 6.5 5 8l3-3M3.5 16.5 5 18l3-3M11 7h10M11 17h10";
25
+ readonly quote: "M4 6v12M9 8h11M9 12h11M9 16h7";
26
+ readonly code: "m16 18 6-6-6-6M8 6l-6 6 6 6";
27
+ readonly link: string;
28
+ readonly keys: "M3 7h18v10H3zM7 11h.01M11 11h.01M15 11h.01M8 14h8";
29
+ readonly history: "M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8M3 3v5h5M12 7v5l4 2";
30
+ };
31
+ /** A drawing of the editor's own (a tool, History), in the text's colour. */
32
+ export declare function Glyph({ name, size }: {
33
+ name: keyof typeof GLYPHS;
34
+ size?: number;
35
+ }): ReactNode;
36
+ /** The toolbar: tools for the field, Write / Preview / Split, and the shortcuts. */
37
+ export declare function Toolbar({ field, mode, onMode, split, editing }: {
38
+ field: RefObject<HTMLTextAreaElement | null>;
39
+ mode: Mode;
40
+ onMode(mode: Mode): void;
41
+ /** Whether there's room side by side. */
42
+ split: boolean;
43
+ /** Whether the field shows (not Preview alone). */
44
+ editing: boolean;
45
+ }): ReactNode;
46
+ /** The field, as tall as what it holds, and its shortcuts. */
47
+ export declare function Editor({ value, onChange, field, placeholder, label, mode }: {
48
+ value: string;
49
+ onChange(value: string): void;
50
+ field: RefObject<HTMLTextAreaElement | null>;
51
+ placeholder: string;
52
+ label: string;
53
+ mode: Mode;
54
+ }): ReactNode;
55
+ /** Whether the window is wide enough for Split, kept current. */
56
+ export declare function useWide(width?: number): boolean;
57
+ export {};
@@ -0,0 +1,268 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * The writing surface: Markdown as it's written, in a field as tall as what
4
+ * it holds (the page scrolls, as a document does), a small toolbar over it
5
+ * (bold, italic, heading, lists, a task, a quote, code, a link) and the
6
+ * shortcuts people know (⌘B, ⌘I, ⌘E, ⌘K on selected words, ⌘⇧7/8/9,
7
+ * ⌘⌥1–3; Enter goes on with a list, Tab indents it; an address pasted over
8
+ * words links them). Write, Preview (the toolkit's `Markdown`, as the page
9
+ * reads) or both side by side (Split, where there's room, the preview
10
+ * following the page's scroll). Every change goes in as typing would, so
11
+ * undo undoes it.
12
+ */
13
+ import { useDeferredValue, useEffect, useRef, useState, } from "react";
14
+ import { IconButton, Kbd, Markdown, Popover, Tabs, Textarea, Tooltip } from "copilotz/web";
15
+ import { enter, fence, heading, indent, lines, link, mark, pasted, } from "./format.js";
16
+ import { MOD, modded } from "./people.js";
17
+ /** Makes a change in the field as typing would (undo undoes it), then selects what it says. */
18
+ export function apply(el, c) {
19
+ el.focus();
20
+ if (c.from === c.to && !c.insert) {
21
+ el.setSelectionRange(c.select[0], c.select[1]);
22
+ return;
23
+ }
24
+ el.setSelectionRange(c.from, c.to);
25
+ const typed = c.insert
26
+ ? document.execCommand("insertText", false, c.insert)
27
+ : document.execCommand("delete");
28
+ if (!typed || el.value.slice(c.from, c.from + c.insert.length) !== c.insert) {
29
+ el.setRangeText(c.insert, c.from, c.to, "end");
30
+ el.dispatchEvent(new Event("input", { bubbles: true }));
31
+ }
32
+ el.setSelectionRange(c.select[0], c.select[1]);
33
+ }
34
+ const fieldOf = (el) => ({
35
+ value: el.value,
36
+ start: el.selectionStart,
37
+ end: el.selectionEnd,
38
+ });
39
+ /** A toolbar button's drawing, on a 24 by 24 grid, in the text's colour. */
40
+ const GLYPHS = {
41
+ bold: "M7 5h6a3.5 3.5 0 0 1 0 7H7zM7 12h7a3.5 3.5 0 0 1 0 7H7z",
42
+ italic: "M19 4h-9M14 20H5M15 4 9 20",
43
+ heading: "M6 4v16M18 4v16M6 12h12",
44
+ bullet: "M9 6h11M9 12h11M9 18h11M4.5 6h.01M4.5 12h.01M4.5 18h.01",
45
+ number: "M10 6h10M10 12h10M10 18h10M4 4h1.5v5M4 9h3M7 19H4c0-1.6 3-2.4 3-4a1.5 1.5 0 0 0-3 0",
46
+ task: "M3.5 6.5 5 8l3-3M3.5 16.5 5 18l3-3M11 7h10M11 17h10",
47
+ quote: "M4 6v12M9 8h11M9 12h11M9 16h7",
48
+ code: "m16 18 6-6-6-6M8 6l-6 6 6 6",
49
+ link: "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71" +
50
+ "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71",
51
+ keys: "M3 7h18v10H3zM7 11h.01M11 11h.01M15 11h.01M8 14h8",
52
+ history: "M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8M3 3v5h5M12 7v5l4 2",
53
+ };
54
+ /** A drawing of the editor's own (a tool, History), in the text's colour. */
55
+ export function Glyph({ name, size = 16 }) {
56
+ return (_jsx("svg", { className: "doc-glyph", viewBox: "0 0 24 24", width: size, height: size, fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: _jsx("path", { d: GLYPHS[name] }) }));
57
+ }
58
+ /** The toolbar's tools, in order, each with its shortcut as it's shown. */
59
+ const TOOLS = [
60
+ { key: "bold", label: "Bold", keys: [MOD, "B"], change: (f) => mark(f, "bold") },
61
+ { key: "italic", label: "Italic", keys: [MOD, "I"], change: (f) => mark(f, "italic") },
62
+ {
63
+ key: "heading",
64
+ label: "Heading (again for a smaller one)",
65
+ keys: [MOD, "⌥", "1–3"],
66
+ change: heading,
67
+ },
68
+ "|",
69
+ {
70
+ key: "bullet",
71
+ label: "Bulleted list",
72
+ keys: [MOD, "⇧", "8"],
73
+ change: (f) => lines(f, "bullet"),
74
+ },
75
+ {
76
+ key: "number",
77
+ label: "Numbered list",
78
+ keys: [MOD, "⇧", "7"],
79
+ change: (f) => lines(f, "number"),
80
+ },
81
+ { key: "task", label: "Checklist", keys: [MOD, "⇧", "9"], change: (f) => lines(f, "task") },
82
+ { key: "quote", label: "Quote", keys: [MOD, "⇧", "."], change: (f) => lines(f, "quote") },
83
+ "|",
84
+ { key: "code", label: "Code", keys: [MOD, "E"], change: (f) => mark(f, "code") },
85
+ { key: "link", label: "Link", keys: [MOD, "K"], change: (f) => link(f) },
86
+ ];
87
+ /** What a key press does in the field, if it's a shortcut: the change to make. */
88
+ function shortcut(event, f) {
89
+ const mod = modded(event);
90
+ const key = event.key.toLowerCase();
91
+ if (mod && !event.altKey && !event.shiftKey) {
92
+ if (key === "b")
93
+ return mark(f, "bold");
94
+ if (key === "i")
95
+ return mark(f, "italic");
96
+ if (key === "e")
97
+ return mark(f, "code");
98
+ }
99
+ if (mod && event.shiftKey && !event.altKey) {
100
+ if (event.code === "Digit7")
101
+ return lines(f, "number");
102
+ if (event.code === "Digit8")
103
+ return lines(f, "bullet");
104
+ if (event.code === "Digit9")
105
+ return lines(f, "task");
106
+ if (event.code === "Period")
107
+ return lines(f, "quote");
108
+ if (key === "x")
109
+ return mark(f, "strike");
110
+ if (event.code === "KeyC")
111
+ return fence(f);
112
+ }
113
+ if (mod && event.altKey && !event.shiftKey) {
114
+ if (event.code === "Digit1")
115
+ return lines(f, "h1");
116
+ if (event.code === "Digit2")
117
+ return lines(f, "h2");
118
+ if (event.code === "Digit3")
119
+ return lines(f, "h3");
120
+ if (event.code === "Digit0")
121
+ return lines(f, "plain");
122
+ }
123
+ if (!mod && !event.altKey && event.key === "Enter" && !event.shiftKey)
124
+ return enter(f);
125
+ if (!mod && !event.altKey && event.key === "Tab")
126
+ return indent(f, event.shiftKey);
127
+ return undefined;
128
+ }
129
+ /** The shortcuts, as the help lists them. */
130
+ const KEYS = [
131
+ ["Save", [MOD, "S"]],
132
+ ["Bold", [MOD, "B"]],
133
+ ["Italic", [MOD, "I"]],
134
+ ["Strikethrough", [MOD, "⇧", "X"]],
135
+ ["Code", [MOD, "E"]],
136
+ ["Code block", [MOD, "⇧", "C"]],
137
+ ["Link (words selected)", [MOD, "K"]],
138
+ ["Heading 1, 2, 3", [MOD, "⌥", "1–3"]],
139
+ ["Plain text", [MOD, "⌥", "0"]],
140
+ ["Bulleted list", [MOD, "⇧", "8"]],
141
+ ["Numbered list", [MOD, "⇧", "7"]],
142
+ ["Checklist", [MOD, "⇧", "9"]],
143
+ ["Quote", [MOD, "⇧", "."]],
144
+ ["Indent a list item", ["Tab"]],
145
+ ["Outdent it", ["⇧", "Tab"]],
146
+ ];
147
+ function Keys({ keys }) {
148
+ return (_jsx("span", { className: "doc-keys", children: keys.map((k) => _jsx(Kbd, { children: k }, k)) }));
149
+ }
150
+ /** The toolbar: tools for the field, Write / Preview / Split, and the shortcuts. */
151
+ export function Toolbar({ field, mode, onMode, split, editing }) {
152
+ // On a phone, the tools take the row, and Preview is one button.
153
+ const roomy = useWide(560);
154
+ const use = (tool) => {
155
+ const el = field.current;
156
+ if (!el)
157
+ return;
158
+ const c = tool.change(fieldOf(el));
159
+ if (c)
160
+ apply(el, c);
161
+ };
162
+ return (_jsxs("div", { className: "doc-toolbar", role: "toolbar", "aria-label": "Formatting", children: [_jsxs("div", { className: "doc-tools", "data-off": !editing || undefined, children: [TOOLS.map((tool, i) => tool === "|" ? _jsx("span", { className: "doc-tools-gap", "aria-hidden": "true" }, i) : (_jsx(Tooltip, { text: _jsxs(_Fragment, { children: [tool.label, " ", _jsx(Keys, { keys: tool.keys })] }), describe: false, children: _jsx("button", { type: "button", className: "doc-tool", "aria-label": tool.label, disabled: !editing,
163
+ // Keep the field's focus and selection (mouse, pen or touch): the tool works on it.
164
+ onPointerDown: (event) => event.preventDefault(), onMouseDown: (event) => event.preventDefault(), onClick: () => use(tool), children: _jsx(Glyph, { name: tool.key }) }) }, tool.key))), _jsx("span", { className: "doc-tools-gap", "aria-hidden": "true" }), _jsx(Popover, { label: "Keyboard shortcuts", width: 300, align: "start", trigger: (props) => (_jsx("button", { ...props, type: "button", className: "doc-tool", "aria-label": "Keyboard shortcuts", children: _jsx(Glyph, { name: "keys" }) })), children: _jsxs("div", { className: "doc-help", children: [_jsx("p", { className: "doc-help-title", children: "Keyboard shortcuts" }), _jsx("dl", { children: KEYS.map(([what, keys]) => (_jsxs("div", { children: [_jsx("dt", { children: what }), _jsx("dd", { children: _jsx(Keys, { keys: keys }) })] }, what))) }), _jsx("p", { className: "doc-help-note", children: "Enter goes on with a list. Pasting a link over selected words links them." })] }) })] }), roomy
165
+ ? (_jsx(Tabs, { label: "How it shows", className: "doc-modes", value: mode === "split" && !split ? "write" : mode, onChange: onMode, tabs: [
166
+ { value: "write", label: "Write" },
167
+ { value: "preview", label: "Preview" },
168
+ ...(split ? [{ value: "split", label: "Split" }] : []),
169
+ ] }))
170
+ : (_jsx(IconButton, { className: "doc-mode-toggle", icon: mode === "preview" ? "pencil" : "eye", label: mode === "preview" ? "Write" : "Preview", variant: mode === "preview" ? "secondary" : "ghost", "aria-pressed": mode === "preview", onClick: () => onMode(mode === "preview" ? "write" : "preview") }))] }));
171
+ }
172
+ /** Where the page scrolls: the `Page` this is in. */
173
+ const scrollerOf = (el) => el?.closest("[data-density=full]") ?? null;
174
+ /** Writing at the end of a long text: the end stays in sight as the field grows. */
175
+ function inSight(el) {
176
+ if (el.selectionEnd < el.value.length - 1)
177
+ return;
178
+ requestAnimationFrame(() => {
179
+ const page = scrollerOf(el);
180
+ const bottom = el.getBoundingClientRect().bottom;
181
+ const room = globalThis.visualViewport?.height ?? globalThis.innerHeight;
182
+ if (page && bottom > room - 48)
183
+ page.scrollTop += bottom - room + 96;
184
+ });
185
+ }
186
+ /** The field, as tall as what it holds, and its shortcuts. */
187
+ export function Editor({ value, onChange, field, placeholder, label, mode }) {
188
+ const shown = useDeferredValue(value);
189
+ const preview = useRef(null);
190
+ const write = mode !== "preview";
191
+ const read = mode !== "write";
192
+ // ⌘K is the command palette's, everywhere; but on words selected here, as in other editors,
193
+ // it links them. The shell hears ⌘K before anything else does, so this hears it first.
194
+ useEffect(() => {
195
+ const pressed = (event) => {
196
+ const el = field.current;
197
+ if (!el || globalThis.document.activeElement !== el || event.key.toLowerCase() !== "k") {
198
+ return;
199
+ }
200
+ if (!modded(event) || event.altKey || event.shiftKey || el.selectionStart === el.selectionEnd) {
201
+ return;
202
+ }
203
+ event.preventDefault();
204
+ event.stopImmediatePropagation();
205
+ apply(el, link(fieldOf(el)));
206
+ };
207
+ globalThis.addEventListener("keydown", pressed, true);
208
+ return () => globalThis.removeEventListener("keydown", pressed, true);
209
+ }, []);
210
+ // Split: the preview follows the page's scroll, as far down as the field is.
211
+ useEffect(() => {
212
+ if (mode !== "split")
213
+ return;
214
+ const page = scrollerOf(field.current);
215
+ const pane = preview.current;
216
+ if (!page || !pane)
217
+ return;
218
+ const follow = () => {
219
+ const most = page.scrollHeight - page.clientHeight;
220
+ const ratio = most > 0 ? page.scrollTop / most : 0;
221
+ pane.scrollTop = ratio * (pane.scrollHeight - pane.clientHeight);
222
+ };
223
+ page.addEventListener("scroll", follow, { passive: true });
224
+ return () => page.removeEventListener("scroll", follow);
225
+ }, [mode]);
226
+ const keyDown = (event) => {
227
+ if (event.nativeEvent.isComposing)
228
+ return;
229
+ const el = event.currentTarget;
230
+ const c = shortcut(event, fieldOf(el));
231
+ if (!c)
232
+ return;
233
+ event.preventDefault();
234
+ event.stopPropagation();
235
+ apply(el, c);
236
+ };
237
+ return (_jsxs("div", { className: `doc-surface doc-surface-${mode}`, children: [write
238
+ ? (_jsx("div", { className: "doc-grow", "data-value": `${value}\n`, children: _jsx(Textarea, { ref: field, bare: true, className: "doc-input", value: value, placeholder: placeholder, "aria-label": label, spellCheck: true, onChange: (event) => {
239
+ onChange(event.target.value);
240
+ inSight(event.target);
241
+ }, onKeyDown: keyDown, onPaste: (event) => {
242
+ const el = event.currentTarget;
243
+ const c = pasted(fieldOf(el), event.clipboardData.getData("text/plain"));
244
+ if (!c)
245
+ return;
246
+ event.preventDefault();
247
+ apply(el, c);
248
+ } }) }))
249
+ : null, read
250
+ ? (_jsx("div", { ref: preview, className: "doc-preview", "aria-label": mode === "split" ? "Preview" : undefined, role: mode === "split" ? "region" : undefined, children: shown.trim()
251
+ ? _jsx(Markdown, { text: shown, className: "doc-md" })
252
+ : _jsx("p", { className: "doc-empty-preview", children: "Nothing written yet." }) }))
253
+ : null] }));
254
+ }
255
+ /** Whether the window is wide enough for Split, kept current. */
256
+ export function useWide(width = 980) {
257
+ const query = `(min-width: ${width}px)`;
258
+ const [wide, setWide] = useState(() => globalThis.matchMedia?.(query).matches ?? true);
259
+ useEffect(() => {
260
+ const list = globalThis.matchMedia?.(query);
261
+ if (!list)
262
+ return;
263
+ const changed = () => setWide(list.matches);
264
+ list.addEventListener("change", changed);
265
+ return () => list.removeEventListener("change", changed);
266
+ }, [query]);
267
+ return wide;
268
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Markdown, written: what the editor's toolbar and shortcuts do to the text
3
+ * and its selection. Each takes the field as it is (its text, where the
4
+ * selection starts and ends) and says what to replace (`from` to `to` with
5
+ * `insert`) and what to select after, so the editor applies it as typing
6
+ * would, and undo undoes it. Pure, so tested.
7
+ *
8
+ * - **Marks** around the selection (bold, italic, code, strikethrough),
9
+ * or around the word at the caret, or, with nothing there, an empty pair
10
+ * to type in; marked already, they come off.
11
+ * - **Line kinds** on every line the selection touches (headings, lists,
12
+ * tasks, quotes); all of them that kind already, it comes off.
13
+ * - **Links:** the selection as a link's text, with its address selected.
14
+ * - **Enter** in a list goes on with it (the next number, an open task);
15
+ * on an empty item, the list ends. **Tab** in a list indents it, and
16
+ * Shift+Tab outdents.
17
+ */
18
+ /** A text field as it is: its text, and the selection's start and end. */
19
+ export type Field = {
20
+ value: string;
21
+ start: number;
22
+ end: number;
23
+ };
24
+ /** A change to a field: `from` to `to` becomes `insert`, then `select` is selected. */
25
+ export type Change = {
26
+ from: number;
27
+ to: number;
28
+ insert: string;
29
+ select: [number, number];
30
+ };
31
+ /** A mark that goes around text. */
32
+ export type Mark = "bold" | "italic" | "code" | "strike";
33
+ /** What a field becomes with a change made. */
34
+ export declare const applied: (field: Field, c: Change) => Field;
35
+ /** Marks the selection (or the word at the caret), or takes the mark off what has it. */
36
+ export declare function mark(field: Field, kind: Mark): Change;
37
+ /** The selection as a fenced code block, on lines of its own. */
38
+ export declare function fence(field: Field): Change;
39
+ /** Kinds a line may be. */
40
+ export type LineKind = "h1" | "h2" | "h3" | "bullet" | "number" | "task" | "quote" | "plain";
41
+ /** What a line is, from how it starts. */
42
+ export declare function kindOf(line: string): LineKind;
43
+ /** Makes every line the selection touches `kind` (numbered in order), or plain if all are. */
44
+ export declare function lines(field: Field, kind: LineKind): Change;
45
+ /** A heading that goes up a level each time: none, #, ##, ###, then none again. */
46
+ export declare function heading(field: Field): Change;
47
+ /** The selection as a link's text (or a link to type), its address selected to type over. */
48
+ export declare function link(field: Field, href?: string): Change;
49
+ /** Pasting an address over selected words: they become its link. */
50
+ export declare function pasted(field: Field, clip: string): Change | undefined;
51
+ /** Enter in a list item: the next item, or the end of the list on an empty one. */
52
+ export declare function enter(field: Field): Change | undefined;
53
+ /** Tab in a list: its lines in by two spaces, or (`out`) back out. Elsewhere, nothing. */
54
+ export declare function indent(field: Field, out?: boolean): Change | undefined;
55
+ /** How many words a text has. */
56
+ export declare const wordsIn: (text: string) => number;