@openartifactsai/editor-kit 0.0.0-stage → 0.23.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +143 -2
- package/package.json +67 -4
- package/src/Announcer.d.ts +2 -0
- package/src/Announcer.js +8 -0
- package/src/FindBar.d.ts +31 -0
- package/src/FindBar.js +37 -0
- package/src/FoldToolbar.d.ts +58 -0
- package/src/FoldToolbar.js +438 -0
- package/src/PanelEdge.d.ts +20 -0
- package/src/PanelEdge.js +74 -0
- package/src/VideoShrinkNotice.d.ts +10 -0
- package/src/VideoShrinkNotice.js +81 -0
- package/src/WheelZoomItem.d.ts +12 -0
- package/src/WheelZoomItem.js +18 -0
- package/src/agent.d.ts +68 -0
- package/src/agent.js +113 -0
- package/src/announce.d.ts +21 -0
- package/src/announce.js +31 -0
- package/src/chrome.d.ts +208 -0
- package/src/chrome.js +148 -0
- package/src/clipboard.d.ts +19 -0
- package/src/clipboard.js +48 -0
- package/src/collab/CommentList.d.ts +32 -0
- package/src/collab/CommentList.js +165 -0
- package/src/collab/CommentViews.d.ts +131 -0
- package/src/collab/CommentViews.js +619 -0
- package/src/collab/HistoryViews.d.ts +38 -0
- package/src/collab/HistoryViews.js +103 -0
- package/src/collab/PresenceViews.d.ts +53 -0
- package/src/collab/PresenceViews.js +48 -0
- package/src/collab/cds/cds.d.ts +151 -0
- package/src/collab/cds/cds.js +231 -0
- package/src/collab/cds/comment-card.d.ts +113 -0
- package/src/collab/cds/comment-card.js +230 -0
- package/src/collab/cds/comment-composer.d.ts +35 -0
- package/src/collab/cds/comment-composer.js +125 -0
- package/src/collab/cds/comments-css.d.ts +2 -0
- package/src/collab/cds/comments-css.js +3 -0
- package/src/collab/cds/comments.css +504 -0
- package/src/collab/cds/edge-piles.d.ts +69 -0
- package/src/collab/cds/edge-piles.js +178 -0
- package/src/collab/cds/layer.d.ts +41 -0
- package/src/collab/cds/layer.js +144 -0
- package/src/collab/cds/markdown.d.ts +20 -0
- package/src/collab/cds/markdown.js +534 -0
- package/src/collab/cds/shield.d.ts +28 -0
- package/src/collab/cds/shield.js +114 -0
- package/src/collab/cds/time.d.ts +2 -0
- package/src/collab/cds/time.js +41 -0
- package/src/collab/collab.d.ts +102 -0
- package/src/collab/collab.js +121 -0
- package/src/collab/comments.d.ts +272 -0
- package/src/collab/comments.js +596 -0
- package/src/collab/history-wiring.d.ts +32 -0
- package/src/collab/history-wiring.js +53 -0
- package/src/collab/history.d.ts +59 -0
- package/src/collab/history.js +96 -0
- package/src/collab/mentions/mention-list.d.ts +63 -0
- package/src/collab/mentions/mention-list.js +257 -0
- package/src/collab/mentions.d.ts +112 -0
- package/src/collab/mentions.js +164 -0
- package/src/collab/peer-colors.d.ts +123 -0
- package/src/collab/peer-colors.js +273 -0
- package/src/collab/pins.d.ts +70 -0
- package/src/collab/pins.js +44 -0
- package/src/collab/presence.d.ts +92 -0
- package/src/collab/presence.js +158 -0
- package/src/design-systems.d.ts +46 -0
- package/src/design-systems.js +61 -0
- package/src/downloads.d.ts +31 -0
- package/src/downloads.js +46 -0
- package/src/file-drop.d.ts +33 -0
- package/src/file-drop.js +36 -0
- package/src/find.d.ts +64 -0
- package/src/find.js +147 -0
- package/src/follow.d.ts +141 -0
- package/src/follow.js +389 -0
- package/src/index.d.ts +32 -0
- package/src/index.js +32 -0
- package/src/keys.d.ts +18 -0
- package/src/keys.js +42 -0
- package/src/menu-fit.d.ts +12 -0
- package/src/menu-fit.js +9 -0
- package/src/merge.d.ts +23 -0
- package/src/merge.js +116 -0
- package/src/page-link.d.ts +33 -0
- package/src/page-link.js +75 -0
- package/src/project-sync.d.ts +161 -0
- package/src/project-sync.js +319 -0
- package/src/react.d.ts +20 -0
- package/src/react.js +20 -0
- package/src/read-retries.d.ts +55 -0
- package/src/read-retries.js +125 -0
- package/src/right-drag-pan.d.ts +128 -0
- package/src/right-drag-pan.js +253 -0
- package/src/roles.d.ts +39 -0
- package/src/roles.js +27 -0
- package/src/session.d.ts +56 -0
- package/src/session.js +57 -0
- package/src/shell.d.ts +60 -0
- package/src/shell.js +111 -0
- package/src/stash.d.ts +26 -0
- package/src/stash.js +69 -0
- package/src/styles.css +17 -0
- package/src/toolbar-fold.d.ts +106 -0
- package/src/toolbar-fold.js +130 -0
- package/src/video-shrink/index.d.ts +8 -0
- package/src/video-shrink/index.js +7 -0
- package/src/video-shrink/plan.d.ts +92 -0
- package/src/video-shrink/plan.js +133 -0
- package/src/video-shrink/protocol.d.ts +29 -0
- package/src/video-shrink/protocol.js +2 -0
- package/src/video-shrink/queue.d.ts +69 -0
- package/src/video-shrink/queue.js +287 -0
- package/src/video-shrink/worker.d.ts +8 -0
- package/src/video-shrink/worker.js +223 -0
- package/src/wheel-zoom.d.ts +14 -0
- package/src/wheel-zoom.js +48 -0
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@openartifactsai/editor-kit`: the logic every artifact editor shares. This
|
|
3
|
+
* entry is pure TypeScript (no JSX), so node's test runner loads it; the
|
|
4
|
+
* React views are in `@openartifactsai/editor-kit/react`.
|
|
5
|
+
*/
|
|
6
|
+
export * from './collab/comments.js';
|
|
7
|
+
export * from './collab/history.js';
|
|
8
|
+
export * from './collab/history-wiring.js';
|
|
9
|
+
export * from './collab/presence.js';
|
|
10
|
+
export * from './collab/peer-colors.js';
|
|
11
|
+
export * from './collab/collab.js';
|
|
12
|
+
export * from './collab/pins.js';
|
|
13
|
+
export * from './announce.js';
|
|
14
|
+
export * from './page-link.js';
|
|
15
|
+
export * from './stash.js';
|
|
16
|
+
export * from './merge.js';
|
|
17
|
+
export * from './project-sync.js';
|
|
18
|
+
export * from './read-retries.js';
|
|
19
|
+
export * from './keys.js';
|
|
20
|
+
export * from './roles.js';
|
|
21
|
+
export * from './session.js';
|
|
22
|
+
export * from './shell.js';
|
|
23
|
+
export * from './agent.js';
|
|
24
|
+
export * from './downloads.js';
|
|
25
|
+
export * from './clipboard.js';
|
|
26
|
+
export * from './find.js';
|
|
27
|
+
export * from './file-drop.js';
|
|
28
|
+
export * from './right-drag-pan.js';
|
|
29
|
+
export * from './wheel-zoom.js';
|
|
30
|
+
export * from './design-systems.js';
|
|
31
|
+
export { createFollower, IDLE_FOLLOW, viewOf as followView, type Follower, type FollowCamera, type FollowHost, type FollowRect, type FollowState, type Heading } from './follow.js';
|
|
32
|
+
export { FOLD_OWNS_ESCAPE } from './toolbar-fold.js';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@openartifactsai/editor-kit`: the logic every artifact editor shares. This
|
|
3
|
+
* entry is pure TypeScript (no JSX), so node's test runner loads it; the
|
|
4
|
+
* React views are in `@openartifactsai/editor-kit/react`.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./collab/comments.js";
|
|
7
|
+
export * from "./collab/history.js";
|
|
8
|
+
export * from "./collab/history-wiring.js";
|
|
9
|
+
export * from "./collab/presence.js";
|
|
10
|
+
export * from "./collab/peer-colors.js";
|
|
11
|
+
export * from "./collab/collab.js";
|
|
12
|
+
export * from "./collab/pins.js";
|
|
13
|
+
export * from "./announce.js";
|
|
14
|
+
export * from "./page-link.js";
|
|
15
|
+
export * from "./stash.js";
|
|
16
|
+
export * from "./merge.js";
|
|
17
|
+
export * from "./project-sync.js";
|
|
18
|
+
export * from "./read-retries.js";
|
|
19
|
+
export * from "./keys.js";
|
|
20
|
+
export * from "./roles.js";
|
|
21
|
+
export * from "./session.js";
|
|
22
|
+
export * from "./shell.js";
|
|
23
|
+
export * from "./agent.js";
|
|
24
|
+
export * from "./downloads.js";
|
|
25
|
+
export * from "./clipboard.js";
|
|
26
|
+
export * from "./find.js";
|
|
27
|
+
export * from "./file-drop.js";
|
|
28
|
+
export * from "./right-drag-pan.js";
|
|
29
|
+
export * from "./wheel-zoom.js";
|
|
30
|
+
export * from "./design-systems.js";
|
|
31
|
+
export { createFollower, IDLE_FOLLOW, viewOf as followView } from "./follow.js";
|
|
32
|
+
export { FOLD_OWNS_ESCAPE } from "./toolbar-fold.js";
|
package/src/keys.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Platform key labels and the text-field test every editor's shortcuts use. Pure. */
|
|
2
|
+
/** True on Apple platforms (⌘ instead of Ctrl in labels and shortcuts). */
|
|
3
|
+
export declare function isApplePlatform(): boolean;
|
|
4
|
+
/** The platform's modifier key, as shortcuts are written. */
|
|
5
|
+
export declare const MOD: string;
|
|
6
|
+
/** The platform's option key. */
|
|
7
|
+
export declare const ALT: string;
|
|
8
|
+
/** The platform's shift key. */
|
|
9
|
+
export declare const SHIFT: string;
|
|
10
|
+
/** The platform's modifier label, read now. */
|
|
11
|
+
export declare function modKey(): string;
|
|
12
|
+
/** True when a key or clipboard event goes to a text field (input, textarea, select or editable text). */
|
|
13
|
+
export declare function isTextField(target: EventTarget | null): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* The next focus index inside a toolbar for Arrow keys, Home and End: arrows step and wrap,
|
|
16
|
+
* Home and End jump to the first and last items, and other keys return null.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getToolbarKeyTarget(key: string, index: number, count: number): number | null;
|
package/src/keys.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Platform key labels and the text-field test every editor's shortcuts use. Pure. */
|
|
2
|
+
/** True on Apple platforms (⌘ instead of Ctrl in labels and shortcuts). */
|
|
3
|
+
export function isApplePlatform() {
|
|
4
|
+
return typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform || navigator.userAgent);
|
|
5
|
+
}
|
|
6
|
+
/** The platform's modifier key, as shortcuts are written. */
|
|
7
|
+
export const MOD = isApplePlatform() ? '⌘' : 'Ctrl+';
|
|
8
|
+
/** The platform's option key. */
|
|
9
|
+
export const ALT = MOD === '⌘' ? '⌥' : 'Alt+';
|
|
10
|
+
/** The platform's shift key. */
|
|
11
|
+
export const SHIFT = MOD === '⌘' ? '⇧' : 'Shift+';
|
|
12
|
+
/** The platform's modifier label, read now. */
|
|
13
|
+
export function modKey() {
|
|
14
|
+
return isApplePlatform() ? '⌘' : 'Ctrl+';
|
|
15
|
+
}
|
|
16
|
+
/** True when a key or clipboard event goes to a text field (input, textarea, select or editable text). */
|
|
17
|
+
export function isTextField(target) {
|
|
18
|
+
const element = target;
|
|
19
|
+
return Boolean(element && (element.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(element.tagName)));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The next focus index inside a toolbar for Arrow keys, Home and End: arrows step and wrap,
|
|
23
|
+
* Home and End jump to the first and last items, and other keys return null.
|
|
24
|
+
*/
|
|
25
|
+
export function getToolbarKeyTarget(key, index, count) {
|
|
26
|
+
if (count <= 0 || index < 0 || index >= count)
|
|
27
|
+
return null;
|
|
28
|
+
switch (key) {
|
|
29
|
+
case 'ArrowRight':
|
|
30
|
+
case 'ArrowDown':
|
|
31
|
+
return (index + 1) % count;
|
|
32
|
+
case 'ArrowLeft':
|
|
33
|
+
case 'ArrowUp':
|
|
34
|
+
return (index - 1 + count) % count;
|
|
35
|
+
case 'Home':
|
|
36
|
+
return 0;
|
|
37
|
+
case 'End':
|
|
38
|
+
return count - 1;
|
|
39
|
+
default:
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** The inline style {@link menuFit} gives a popup. */
|
|
2
|
+
export interface MenuFitStyle {
|
|
3
|
+
maxHeight: string;
|
|
4
|
+
overflowY: 'auto';
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Keeps a menu inside a short window: a popup with no height limit of its
|
|
8
|
+
* own scrolls, headings and all, within the height Base UI measured free
|
|
9
|
+
* next to its trigger (`--available-height`). A popup whose classes set a
|
|
10
|
+
* `max-h-*` keeps that limit.
|
|
11
|
+
*/
|
|
12
|
+
export declare function menuFit(className?: string): MenuFitStyle | undefined;
|
package/src/menu-fit.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps a menu inside a short window: a popup with no height limit of its
|
|
3
|
+
* own scrolls, headings and all, within the height Base UI measured free
|
|
4
|
+
* next to its trigger (`--available-height`). A popup whose classes set a
|
|
5
|
+
* `max-h-*` keeps that limit.
|
|
6
|
+
*/
|
|
7
|
+
export function menuFit(className) {
|
|
8
|
+
return className && /(^|\s)max-h-/.test(className) ? undefined : { maxHeight: 'var(--available-height)', overflowY: 'auto' };
|
|
9
|
+
}
|
package/src/merge.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Three-way line merge of a text file both this page and someone else (the
|
|
3
|
+
* agent, another viewer) changed since the page last read it. When the two
|
|
4
|
+
* sides changed different lines of the version both started from, the
|
|
5
|
+
* merge keeps both changes; when they changed the same lines differently
|
|
6
|
+
* (or inserted different lines at the same place) there is no merge and
|
|
7
|
+
* the editor asks the person.
|
|
8
|
+
*
|
|
9
|
+
* Changes to lines right next to each other merge by default; an editor
|
|
10
|
+
* whose files read as one unit (a slide's HTML) passes
|
|
11
|
+
* `{ adjacent: 'conflict' }` to give up on those too. Pure.
|
|
12
|
+
*/
|
|
13
|
+
/** How {@link mergeLines} treats changes to neighbouring lines. */
|
|
14
|
+
export interface LineMergeOptions {
|
|
15
|
+
/** `merge` (default): neighbouring changes both apply. `conflict`: they need the person. */
|
|
16
|
+
adjacent?: 'merge' | 'conflict';
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Merges `ours` and `theirs`, both edited from `base`, line by line. Returns
|
|
20
|
+
* the merged text, or `null` when both changed the same region differently
|
|
21
|
+
* (or the files are too large to compare).
|
|
22
|
+
*/
|
|
23
|
+
export declare function mergeLines(base: string, ours: string, theirs: string, options?: LineMergeOptions): string | null;
|
package/src/merge.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Three-way line merge of a text file both this page and someone else (the
|
|
3
|
+
* agent, another viewer) changed since the page last read it. When the two
|
|
4
|
+
* sides changed different lines of the version both started from, the
|
|
5
|
+
* merge keeps both changes; when they changed the same lines differently
|
|
6
|
+
* (or inserted different lines at the same place) there is no merge and
|
|
7
|
+
* the editor asks the person.
|
|
8
|
+
*
|
|
9
|
+
* Changes to lines right next to each other merge by default; an editor
|
|
10
|
+
* whose files read as one unit (a slide's HTML) passes
|
|
11
|
+
* `{ adjacent: 'conflict' }` to give up on those too. Pure.
|
|
12
|
+
*/
|
|
13
|
+
/** Largest LCS table the merge builds (base lines × other lines); larger files are not merged. */
|
|
14
|
+
const MAX_CELLS = 4_000_000;
|
|
15
|
+
const same = (a, b) => a.length === b.length && a.every((line, i) => line === b[i]);
|
|
16
|
+
/** The changes that turn `a` into `b`; `null` when the files are too large to compare. */
|
|
17
|
+
function hunks(a, b) {
|
|
18
|
+
// Common head and tail first: most edits are small.
|
|
19
|
+
let head = 0;
|
|
20
|
+
while (head < a.length && head < b.length && a[head] === b[head])
|
|
21
|
+
head++;
|
|
22
|
+
let tail = 0;
|
|
23
|
+
while (tail < a.length - head && tail < b.length - head && a[a.length - 1 - tail] === b[b.length - 1 - tail])
|
|
24
|
+
tail++;
|
|
25
|
+
const x = a.slice(head, a.length - tail);
|
|
26
|
+
const y = b.slice(head, b.length - tail);
|
|
27
|
+
if ((x.length + 1) * (y.length + 1) > MAX_CELLS)
|
|
28
|
+
return null;
|
|
29
|
+
const width = y.length + 1;
|
|
30
|
+
const table = new Uint32Array((x.length + 1) * width);
|
|
31
|
+
for (let i = x.length - 1; i >= 0; i--) {
|
|
32
|
+
for (let j = y.length - 1; j >= 0; j--)
|
|
33
|
+
table[i * width + j] = x[i] === y[j] ? table[(i + 1) * width + j + 1] + 1 : Math.max(table[(i + 1) * width + j], table[i * width + j + 1]);
|
|
34
|
+
}
|
|
35
|
+
const out = [];
|
|
36
|
+
let current = null;
|
|
37
|
+
let i = 0;
|
|
38
|
+
let j = 0;
|
|
39
|
+
const flush = () => {
|
|
40
|
+
if (current)
|
|
41
|
+
out.push(current);
|
|
42
|
+
current = null;
|
|
43
|
+
};
|
|
44
|
+
while (i < x.length || j < y.length) {
|
|
45
|
+
if (i < x.length && j < y.length && x[i] === y[j]) {
|
|
46
|
+
flush();
|
|
47
|
+
i++;
|
|
48
|
+
j++;
|
|
49
|
+
}
|
|
50
|
+
else if (j >= y.length || (i < x.length && table[(i + 1) * width + j] >= table[i * width + j + 1])) {
|
|
51
|
+
current ??= { start: head + i, end: head + i, lines: [] };
|
|
52
|
+
current.end = head + i + 1;
|
|
53
|
+
i++;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
current ??= { start: head + i, end: head + i, lines: [] };
|
|
57
|
+
current.lines.push(y[j]);
|
|
58
|
+
j++;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
flush();
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
/** True when two changes touch the same base lines (or insert at the same place); with `adjacent`, also when they meet. */
|
|
65
|
+
function clash(a, b, adjacent) {
|
|
66
|
+
if (adjacent)
|
|
67
|
+
return Math.max(a.start, b.start) <= Math.min(a.end, b.end);
|
|
68
|
+
if (a.start === b.start && (a.start === a.end || b.start === b.end))
|
|
69
|
+
return true;
|
|
70
|
+
if (a.start === a.end)
|
|
71
|
+
return b.start < a.start && a.start < b.end;
|
|
72
|
+
if (b.start === b.end)
|
|
73
|
+
return a.start < b.start && b.start < a.end;
|
|
74
|
+
return Math.max(a.start, b.start) < Math.min(a.end, b.end);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Merges `ours` and `theirs`, both edited from `base`, line by line. Returns
|
|
78
|
+
* the merged text, or `null` when both changed the same region differently
|
|
79
|
+
* (or the files are too large to compare).
|
|
80
|
+
*/
|
|
81
|
+
export function mergeLines(base, ours, theirs, options = {}) {
|
|
82
|
+
if (ours === theirs)
|
|
83
|
+
return ours;
|
|
84
|
+
if (base === ours)
|
|
85
|
+
return theirs;
|
|
86
|
+
if (base === theirs)
|
|
87
|
+
return ours;
|
|
88
|
+
const adjacent = options.adjacent === 'conflict';
|
|
89
|
+
const lines = base.split('\n');
|
|
90
|
+
const mine = hunks(lines, ours.split('\n'));
|
|
91
|
+
const other = hunks(lines, theirs.split('\n'));
|
|
92
|
+
if (!mine || !other)
|
|
93
|
+
return null;
|
|
94
|
+
const all = [];
|
|
95
|
+
for (const hunk of mine) {
|
|
96
|
+
const twin = other.find((candidate) => clash(hunk, candidate, adjacent));
|
|
97
|
+
if (!twin) {
|
|
98
|
+
all.push(hunk);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
// The same change on both sides is no conflict.
|
|
102
|
+
if (twin.start === hunk.start && twin.end === hunk.end && same(twin.lines, hunk.lines))
|
|
103
|
+
continue;
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
all.push(...other);
|
|
107
|
+
all.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
108
|
+
const out = [];
|
|
109
|
+
let at = 0;
|
|
110
|
+
for (const hunk of all) {
|
|
111
|
+
out.push(...lines.slice(at, hunk.start), ...hunk.lines);
|
|
112
|
+
at = hunk.end;
|
|
113
|
+
}
|
|
114
|
+
out.push(...lines.slice(at));
|
|
115
|
+
return out.join('\n');
|
|
116
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Page addresses: the page shown is kept in the address as `#page=<id>`,
|
|
3
|
+
* so a copied link opens that page and the browser's back and forward step
|
|
4
|
+
* between the pages visited. Other `#` parameters are kept.
|
|
5
|
+
*/
|
|
6
|
+
/** The page named by a location hash, or `null`. */
|
|
7
|
+
export declare function pageFromHash(hash: string): string | null;
|
|
8
|
+
/** The hash showing `page` (`null`: none), keeping other parameters. */
|
|
9
|
+
export declare function hashForPage(hash: string, page: string | null): string;
|
|
10
|
+
/**
|
|
11
|
+
* The shareable link to a page: the editor's address without its query
|
|
12
|
+
* (which can carry session parameters), with the page in the hash.
|
|
13
|
+
*/
|
|
14
|
+
export declare function pageLink(href: string, page: string | null): string;
|
|
15
|
+
/** What {@link syncPageHistory} needs from an editor: plain callbacks, no store. */
|
|
16
|
+
export interface PageHistoryHost {
|
|
17
|
+
/** The page shown now (`null`: the first / only page). */
|
|
18
|
+
current(): string | null;
|
|
19
|
+
/** Shows a page (from back / forward). */
|
|
20
|
+
show(page: string | null): void;
|
|
21
|
+
/** Whether a page id exists. */
|
|
22
|
+
exists(page: string): boolean;
|
|
23
|
+
/** The page to show when the address names none. */
|
|
24
|
+
fallback(): string | null;
|
|
25
|
+
/** Calls `listener` whenever the page shown changes; returns an unsubscribe. */
|
|
26
|
+
subscribe(listener: (page: string | null) => void): () => void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Keeps the address bar's `#page=` in step with the page shown: a page
|
|
30
|
+
* switch pushes a history entry, back and forward switch back. Returns a
|
|
31
|
+
* dispose function. Works on `window`'s location and history.
|
|
32
|
+
*/
|
|
33
|
+
export declare function syncPageHistory(host: PageHistoryHost): () => void;
|
package/src/page-link.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Page addresses: the page shown is kept in the address as `#page=<id>`,
|
|
3
|
+
* so a copied link opens that page and the browser's back and forward step
|
|
4
|
+
* between the pages visited. Other `#` parameters are kept.
|
|
5
|
+
*/
|
|
6
|
+
/** The page named by a location hash, or `null`. */
|
|
7
|
+
export function pageFromHash(hash) {
|
|
8
|
+
const params = new URLSearchParams(hash.replace(/^#/, ''));
|
|
9
|
+
const page = params.get('page');
|
|
10
|
+
return page ? page : null;
|
|
11
|
+
}
|
|
12
|
+
/** The hash showing `page` (`null`: none), keeping other parameters. */
|
|
13
|
+
export function hashForPage(hash, page) {
|
|
14
|
+
const params = new URLSearchParams(hash.replace(/^#/, ''));
|
|
15
|
+
if (page)
|
|
16
|
+
params.set('page', page);
|
|
17
|
+
else
|
|
18
|
+
params.delete('page');
|
|
19
|
+
const text = params.toString();
|
|
20
|
+
return text ? `#${text}` : '';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The shareable link to a page: the editor's address without its query
|
|
24
|
+
* (which can carry session parameters), with the page in the hash.
|
|
25
|
+
*/
|
|
26
|
+
export function pageLink(href, page) {
|
|
27
|
+
const url = new URL(href);
|
|
28
|
+
return `${url.origin}${url.pathname}${hashForPage(url.hash, page)}`;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Keeps the address bar's `#page=` in step with the page shown: a page
|
|
32
|
+
* switch pushes a history entry, back and forward switch back. Returns a
|
|
33
|
+
* dispose function. Works on `window`'s location and history.
|
|
34
|
+
*/
|
|
35
|
+
export function syncPageHistory(host) {
|
|
36
|
+
let applying = false;
|
|
37
|
+
const write = (page, push) => {
|
|
38
|
+
const hash = hashForPage(location.hash, page);
|
|
39
|
+
if (hash === location.hash || (!hash && !location.hash))
|
|
40
|
+
return;
|
|
41
|
+
const url = `${location.pathname}${location.search}${hash}`;
|
|
42
|
+
try {
|
|
43
|
+
if (push)
|
|
44
|
+
history.pushState(history.state, '', url);
|
|
45
|
+
else
|
|
46
|
+
history.replaceState(history.state, '', url);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Sandboxed frames may refuse history changes.
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
write(host.current(), false);
|
|
53
|
+
const unsubscribe = host.subscribe((page) => {
|
|
54
|
+
if (!applying)
|
|
55
|
+
write(page, true);
|
|
56
|
+
});
|
|
57
|
+
const onPop = () => {
|
|
58
|
+
const named = pageFromHash(location.hash);
|
|
59
|
+
const page = named && host.exists(named) ? named : host.fallback();
|
|
60
|
+
if (page === host.current())
|
|
61
|
+
return;
|
|
62
|
+
applying = true;
|
|
63
|
+
try {
|
|
64
|
+
host.show(page);
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
applying = false;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
window.addEventListener('popstate', onPop);
|
|
71
|
+
return () => {
|
|
72
|
+
unsubscribe();
|
|
73
|
+
window.removeEventListener('popstate', onPop);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loading, autosave and changes made elsewhere, the shared part. An editor
|
|
3
|
+
* keeps its project as an index file plus item files under `project/`; this
|
|
4
|
+
* module owns the mechanics around them:
|
|
5
|
+
*
|
|
6
|
+
* - Load: the listing, the named files and every item file ({@link readProject}).
|
|
7
|
+
* - Save: a debounced save after each local change, one at a time (a change
|
|
8
|
+
* during a save saves again after it), one publish batch with `ifMatch` on
|
|
9
|
+
* every file. A conflict reads the files that lost the race and lets the
|
|
10
|
+
* editor merge, then retries (a bounded number of times). Offline: backoff.
|
|
11
|
+
* - Remote changes: pushed by the host, read and handed to the editor. A file
|
|
12
|
+
* held by a text edit waits until the edit ends.
|
|
13
|
+
* - Catching up: when the host's change stream starts again (`resync`), the
|
|
14
|
+
* changes it carried while it was down are lost; the listing is compared with
|
|
15
|
+
* what this tab holds and the difference is merged like a pushed change.
|
|
16
|
+
*
|
|
17
|
+
* What a batch holds, what a save marks saved and how a change is merged
|
|
18
|
+
* are the editor's ({@link ProjectSyncOptions}).
|
|
19
|
+
*/
|
|
20
|
+
import { type ArtifactCapability, type PublishBatch } from '@openartifactsai/runtime';
|
|
21
|
+
import { type LoadReaderOptions } from './read-retries.js';
|
|
22
|
+
/** The save states the engine sets (an editor's badge may know more). */
|
|
23
|
+
export type ProjectSaveState = 'saved' | 'dirty' | 'saving' | 'offline' | 'readonly';
|
|
24
|
+
/** How the editor takes one changed file (see {@link ProjectSyncOptions.remote}). */
|
|
25
|
+
export interface RemoteFile {
|
|
26
|
+
/** The hash this tab holds; a change to the same hash is skipped (unless forced). `undefined`: never skipped. */
|
|
27
|
+
hash: string | null | undefined;
|
|
28
|
+
/** A text edit holds the file: the change waits until it ends (unless forced). */
|
|
29
|
+
held?: boolean;
|
|
30
|
+
/** Takes their version (`text` null: deleted or unreadable). */
|
|
31
|
+
apply(text: string | null, hash: string | null): void;
|
|
32
|
+
}
|
|
33
|
+
/** What {@link createProjectSync} drives. `Sent` is what one batch sent; `Run` is one pass of remote changes. */
|
|
34
|
+
export interface ProjectSyncOptions<Sent, Run> {
|
|
35
|
+
/** The artifact capability (read when used, so it may come from a session set up later). */
|
|
36
|
+
artifact(): Pick<ArtifactCapability, 'readText' | 'publish' | 'onChange'> & Partial<Pick<ArtifactCapability, 'list'>>;
|
|
37
|
+
/** Delay between the last change and the save, ms. */
|
|
38
|
+
saveDelay: number;
|
|
39
|
+
/** Conflict retries per save (default 4). */
|
|
40
|
+
maxRetries?: number;
|
|
41
|
+
readOnly(): boolean;
|
|
42
|
+
/** True when something has not reached the server. */
|
|
43
|
+
isDirty(): boolean;
|
|
44
|
+
save(): string;
|
|
45
|
+
setSave(save: ProjectSaveState): void;
|
|
46
|
+
/** Builds the next batch. */
|
|
47
|
+
collect(): {
|
|
48
|
+
batch: PublishBatch;
|
|
49
|
+
sent: Sent;
|
|
50
|
+
};
|
|
51
|
+
/** The batch went through; `hashes` as the host returned them (artifact paths). */
|
|
52
|
+
acknowledge(sent: Sent, hashes: Record<string, string | null>): void;
|
|
53
|
+
/** The batch was empty (nothing to publish). */
|
|
54
|
+
unsent?(sent: Sent): void;
|
|
55
|
+
/** Everything is saved (after the badge says so). */
|
|
56
|
+
saved?(): void;
|
|
57
|
+
/** The host refused the write (`not_writer`): go read-only and tell the person. */
|
|
58
|
+
lostWriteAccess(): void;
|
|
59
|
+
/** Remote changes, one pass at a time: `begin`, `file` per changed path (`undefined`: ignored), `end`. */
|
|
60
|
+
remote: {
|
|
61
|
+
begin(): Run;
|
|
62
|
+
file(path: string, hash: string | null, run: Run): RemoteFile | undefined;
|
|
63
|
+
end(run: Run): void;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* The artifact paths this tab holds files for. Catching up treats one the
|
|
67
|
+
* listing no longer has as deleted (without it, deletions wait for the next push).
|
|
68
|
+
*/
|
|
69
|
+
known?(): Iterable<string>;
|
|
70
|
+
/** Calls `changed` after each local change (the store's revision moved). */
|
|
71
|
+
watchChanges(changed: () => void): void;
|
|
72
|
+
/** Calls `ended` when a text edit ends. */
|
|
73
|
+
watchTextEdit(ended: () => void): void;
|
|
74
|
+
/** Before a flush on unload with unsaved work. */
|
|
75
|
+
beforeUnload?(): void;
|
|
76
|
+
/** Timers (tests inject fakes). */
|
|
77
|
+
timers?: {
|
|
78
|
+
set(run: () => void, ms: number): unknown;
|
|
79
|
+
clear(handle: unknown): void;
|
|
80
|
+
};
|
|
81
|
+
/** Where `beforeunload` is listened to (default `window`; none in node). */
|
|
82
|
+
window?: Pick<Window, 'addEventListener'> | null;
|
|
83
|
+
}
|
|
84
|
+
/** A running sync (see {@link createProjectSync}). */
|
|
85
|
+
export interface ProjectSync {
|
|
86
|
+
/** Listens to remote changes, local changes, text edits and unload. Call once, after loading. */
|
|
87
|
+
start(): void;
|
|
88
|
+
/** Schedules a save (default: the save delay). */
|
|
89
|
+
schedule(delay?: number): void;
|
|
90
|
+
/** Saves now (coalescing with a save already running). */
|
|
91
|
+
flush(): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Resolves once nothing is left unsaved: waits for a save already running,
|
|
94
|
+
* then saves what is still dirty. Stops early when offline or read-only.
|
|
95
|
+
*/
|
|
96
|
+
settle(): Promise<void>;
|
|
97
|
+
/** Retries a failed save now, resetting the backoff. */
|
|
98
|
+
retryNow(): void;
|
|
99
|
+
/** Applies changes made elsewhere (path → new hash, `null`: deleted). `force` re-reads even known hashes and ignores text edits. */
|
|
100
|
+
applyRemote(changes: Record<string, string | null>, options?: RemoteOptions): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Merges what changed while this tab heard nothing: compares the listing with
|
|
103
|
+
* the hashes it holds, reads every changed file, then merges them as pushed
|
|
104
|
+
* changes. Saves wait meanwhile, so a save that has written but not returned
|
|
105
|
+
* is never taken for someone else's change. A failed listing or read leaves
|
|
106
|
+
* the project as it is. Runs by itself when the change stream starts again.
|
|
107
|
+
*/
|
|
108
|
+
catchUp(): Promise<void>;
|
|
109
|
+
}
|
|
110
|
+
/** How {@link ProjectSync.applyRemote} reads. */
|
|
111
|
+
export interface RemoteOptions {
|
|
112
|
+
/** Re-read even known hashes and ignore text edits. */
|
|
113
|
+
force?: boolean;
|
|
114
|
+
/** Texts already read, by path (read again otherwise). */
|
|
115
|
+
contents?: ReadonlyMap<string, string>;
|
|
116
|
+
}
|
|
117
|
+
/** First offline retry delay, ms (doubles up to {@link OFFLINE_MAX_MS}). */
|
|
118
|
+
export declare const OFFLINE_FIRST_MS = 2000;
|
|
119
|
+
/** Longest offline retry delay, ms. */
|
|
120
|
+
export declare const OFFLINE_MAX_MS = 30000;
|
|
121
|
+
/** Creates the save and remote-change engine of one project. */
|
|
122
|
+
export declare function createProjectSync<Sent, Run>(options: ProjectSyncOptions<Sent, Run>): ProjectSync;
|
|
123
|
+
/** What {@link readProject} read. */
|
|
124
|
+
export interface ProjectFiles {
|
|
125
|
+
title: string;
|
|
126
|
+
/** Each named file, `null` when absent. */
|
|
127
|
+
files: Record<string, {
|
|
128
|
+
text: string;
|
|
129
|
+
hash: string;
|
|
130
|
+
} | null>;
|
|
131
|
+
/** Each item file by its key. */
|
|
132
|
+
items: Record<string, {
|
|
133
|
+
text: string;
|
|
134
|
+
hash: string;
|
|
135
|
+
}>;
|
|
136
|
+
}
|
|
137
|
+
/** Files a load read, kept by path for the next try of the load (see {@link ReadProjectOptions.kept}). */
|
|
138
|
+
export type KeptReads = Map<string, {
|
|
139
|
+
text: string;
|
|
140
|
+
hash: string;
|
|
141
|
+
}>;
|
|
142
|
+
/** How {@link readProject} reads. */
|
|
143
|
+
export interface ReadProjectOptions extends LoadReaderOptions {
|
|
144
|
+
/**
|
|
145
|
+
* Files read by an earlier try of this load. One the listing still has at
|
|
146
|
+
* the same hash is not read again; what this try reads is added, and all is
|
|
147
|
+
* forgotten once a load succeeds. A try that is refused access while some
|
|
148
|
+
* were kept forgets them and reads everything once more.
|
|
149
|
+
*/
|
|
150
|
+
kept?: KeptReads;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Reads the title, the listing, the named `files` and every item file (a path
|
|
154
|
+
* `item` maps to a key; `undefined`: not an item). Reads run 64 at a time; one
|
|
155
|
+
* the server fails is tried again after a wait (see `read-retries.ts`). Rejects
|
|
156
|
+
* when a read fails for good.
|
|
157
|
+
*/
|
|
158
|
+
export declare function readProject(artifact: Pick<ArtifactCapability, 'title' | 'list' | 'readText'>, spec: {
|
|
159
|
+
files: readonly string[];
|
|
160
|
+
item(path: string): string | null | undefined;
|
|
161
|
+
}, options?: ReadProjectOptions): Promise<ProjectFiles>;
|