@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
|
@@ -0,0 +1,319 @@
|
|
|
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 { isArtifactError } from '@openartifactsai/runtime';
|
|
21
|
+
import { createLoadReader, isAccessRefused } from "./read-retries.js";
|
|
22
|
+
/** First offline retry delay, ms (doubles up to {@link OFFLINE_MAX_MS}). */
|
|
23
|
+
export const OFFLINE_FIRST_MS = 2_000;
|
|
24
|
+
/** Longest offline retry delay, ms. */
|
|
25
|
+
export const OFFLINE_MAX_MS = 30_000;
|
|
26
|
+
/** Creates the save and remote-change engine of one project. */
|
|
27
|
+
export function createProjectSync(options) {
|
|
28
|
+
const maxRetries = options.maxRetries ?? 4;
|
|
29
|
+
const timers = options.timers ?? { set: (run, ms) => setTimeout(run, ms), clear: (handle) => clearTimeout(handle) };
|
|
30
|
+
let timer = null;
|
|
31
|
+
let running = false;
|
|
32
|
+
let again = false;
|
|
33
|
+
/** The save in progress, if any. */
|
|
34
|
+
let current = null;
|
|
35
|
+
let offlineDelay = OFFLINE_FIRST_MS;
|
|
36
|
+
/** Paths whose remote change waits for a text edit to end. */
|
|
37
|
+
const deferred = new Map();
|
|
38
|
+
/** A catch-up is running (saves wait for it); `catchUpAgain`: another was asked for meanwhile. */
|
|
39
|
+
let catching = null;
|
|
40
|
+
let catchUpAgain = false;
|
|
41
|
+
function schedule(delay = options.saveDelay) {
|
|
42
|
+
if (options.readOnly())
|
|
43
|
+
return;
|
|
44
|
+
if (options.isDirty())
|
|
45
|
+
options.setSave(options.save() === 'offline' ? 'offline' : 'dirty');
|
|
46
|
+
if (timer !== null)
|
|
47
|
+
timers.clear(timer);
|
|
48
|
+
timer = timers.set(() => {
|
|
49
|
+
timer = null;
|
|
50
|
+
void flush();
|
|
51
|
+
}, delay);
|
|
52
|
+
}
|
|
53
|
+
function flush() {
|
|
54
|
+
if (running || catching) {
|
|
55
|
+
again = true;
|
|
56
|
+
return Promise.resolve();
|
|
57
|
+
}
|
|
58
|
+
const run = saveNow();
|
|
59
|
+
current = run;
|
|
60
|
+
const done = () => {
|
|
61
|
+
if (current === run)
|
|
62
|
+
current = null;
|
|
63
|
+
};
|
|
64
|
+
run.then(done, done);
|
|
65
|
+
return run;
|
|
66
|
+
}
|
|
67
|
+
async function settle() {
|
|
68
|
+
for (let round = 0; round <= maxRetries; round++) {
|
|
69
|
+
if (catching) {
|
|
70
|
+
await catching;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (current) {
|
|
74
|
+
await current.catch(() => undefined);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (options.readOnly() || !options.isDirty())
|
|
78
|
+
return;
|
|
79
|
+
await flush();
|
|
80
|
+
if (options.save() === 'offline')
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async function saveNow() {
|
|
85
|
+
running = true;
|
|
86
|
+
try {
|
|
87
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
88
|
+
const { batch, sent } = options.collect();
|
|
89
|
+
if (Object.keys(batch).length === 0) {
|
|
90
|
+
options.unsent?.(sent);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
options.setSave('saving');
|
|
94
|
+
try {
|
|
95
|
+
const result = await options.artifact().publish(batch);
|
|
96
|
+
options.acknowledge(sent, result.hashes);
|
|
97
|
+
offlineDelay = OFFLINE_FIRST_MS;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (isArtifactError(error, 'conflict') && attempt < maxRetries) {
|
|
102
|
+
await resolveConflicts(error.paths ?? []);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (isArtifactError(error, 'not_writer')) {
|
|
106
|
+
options.lostWriteAccess();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
// Nothing is left to save (undone, or saved by another way meanwhile): no retry to wait for.
|
|
110
|
+
if (!options.isDirty())
|
|
111
|
+
break;
|
|
112
|
+
// Offline or host error: keep the edits and retry with backoff.
|
|
113
|
+
options.setSave('offline');
|
|
114
|
+
offlineDelay = Math.min(offlineDelay * 2, OFFLINE_MAX_MS);
|
|
115
|
+
schedule(offlineDelay);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
running = false;
|
|
122
|
+
}
|
|
123
|
+
const dirty = options.isDirty();
|
|
124
|
+
if (again || dirty) {
|
|
125
|
+
again = false;
|
|
126
|
+
schedule(dirty ? options.saveDelay : 0);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
options.setSave('saved');
|
|
130
|
+
options.saved?.();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** Reads the files that lost a race and merges ours on top. */
|
|
134
|
+
async function resolveConflicts(paths) {
|
|
135
|
+
const changes = {};
|
|
136
|
+
for (const conflict of paths)
|
|
137
|
+
changes[conflict.path] = conflict.actual;
|
|
138
|
+
await applyRemote(changes, { force: true });
|
|
139
|
+
}
|
|
140
|
+
async function applyRemote(changes, { force = false, contents } = {}) {
|
|
141
|
+
const artifact = options.artifact();
|
|
142
|
+
const run = options.remote.begin();
|
|
143
|
+
for (const [path, hash] of Object.entries(changes)) {
|
|
144
|
+
const file = options.remote.file(path, hash, run);
|
|
145
|
+
if (!file)
|
|
146
|
+
continue;
|
|
147
|
+
if (!force && hash === file.hash)
|
|
148
|
+
continue;
|
|
149
|
+
if (!force && file.held) {
|
|
150
|
+
deferred.set(path, hash);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const text = hash === null ? null : contents?.get(path) ?? await artifact.readText(path).catch(() => null);
|
|
154
|
+
file.apply(text, hash);
|
|
155
|
+
}
|
|
156
|
+
options.remote.end(run);
|
|
157
|
+
}
|
|
158
|
+
function catchUp() {
|
|
159
|
+
if (catching) {
|
|
160
|
+
catchUpAgain = true;
|
|
161
|
+
return catching;
|
|
162
|
+
}
|
|
163
|
+
const run = (async () => {
|
|
164
|
+
try {
|
|
165
|
+
do {
|
|
166
|
+
catchUpAgain = false;
|
|
167
|
+
await catchUpOnce();
|
|
168
|
+
} while (catchUpAgain);
|
|
169
|
+
}
|
|
170
|
+
finally {
|
|
171
|
+
catching = null;
|
|
172
|
+
// Saves asked for meanwhile waited for this.
|
|
173
|
+
if (again || options.isDirty()) {
|
|
174
|
+
again = false;
|
|
175
|
+
schedule(0);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
})();
|
|
179
|
+
catching = run;
|
|
180
|
+
return run;
|
|
181
|
+
}
|
|
182
|
+
async function catchUpOnce() {
|
|
183
|
+
// A save that has written but not returned still holds the old hashes here: wait it out.
|
|
184
|
+
while (current)
|
|
185
|
+
await current.catch(() => undefined);
|
|
186
|
+
const artifact = options.artifact();
|
|
187
|
+
if (!artifact.list)
|
|
188
|
+
return;
|
|
189
|
+
const probe = options.remote.begin();
|
|
190
|
+
/** The hash this tab holds for a path (`undefined`: unknown), or `false` when the editor ignores the path. */
|
|
191
|
+
const held = (path, hash) => {
|
|
192
|
+
const file = options.remote.file(path, hash, probe);
|
|
193
|
+
return file ? file.hash : false;
|
|
194
|
+
};
|
|
195
|
+
let listing;
|
|
196
|
+
try {
|
|
197
|
+
listing = await artifact.list();
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return; // Still unreachable: the next start of the stream tries again.
|
|
201
|
+
}
|
|
202
|
+
const changes = {};
|
|
203
|
+
const before = new Map();
|
|
204
|
+
for (const path of new Set([...Object.keys(listing), ...(options.known?.() ?? [])])) {
|
|
205
|
+
const hash = listing[path]?.sha256 ?? null;
|
|
206
|
+
const ours = held(path, hash);
|
|
207
|
+
// Ignored paths, files held at this hash, and absent files never had.
|
|
208
|
+
if (ours === false || ours === hash || (hash === null && (ours === null || ours === undefined)))
|
|
209
|
+
continue;
|
|
210
|
+
changes[path] = hash;
|
|
211
|
+
before.set(path, ours);
|
|
212
|
+
}
|
|
213
|
+
if (!Object.keys(changes).length)
|
|
214
|
+
return;
|
|
215
|
+
// Read everything first: a failed read must leave the project as it is.
|
|
216
|
+
const contents = new Map();
|
|
217
|
+
try {
|
|
218
|
+
for (const [path, hash] of Object.entries(changes))
|
|
219
|
+
if (hash !== null)
|
|
220
|
+
contents.set(path, await artifact.readText(path));
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
// A pushed change that landed while reading is newer than what was read.
|
|
226
|
+
for (const path of Object.keys(changes))
|
|
227
|
+
if (held(path, changes[path]) !== before.get(path))
|
|
228
|
+
delete changes[path];
|
|
229
|
+
await applyRemote(changes, { contents });
|
|
230
|
+
}
|
|
231
|
+
function start() {
|
|
232
|
+
options.artifact().onChange((event) => {
|
|
233
|
+
if (event.resync)
|
|
234
|
+
void catchUp();
|
|
235
|
+
else
|
|
236
|
+
void applyRemote(event.changes);
|
|
237
|
+
});
|
|
238
|
+
options.watchChanges(() => schedule());
|
|
239
|
+
// Merge deferred remote changes once a text edit ends.
|
|
240
|
+
options.watchTextEdit(() => {
|
|
241
|
+
if (!deferred.size)
|
|
242
|
+
return;
|
|
243
|
+
const changes = Object.fromEntries(deferred);
|
|
244
|
+
deferred.clear();
|
|
245
|
+
void applyRemote(changes);
|
|
246
|
+
});
|
|
247
|
+
const target = options.window === undefined ? globalThis.window : options.window;
|
|
248
|
+
target?.addEventListener('beforeunload', (event) => {
|
|
249
|
+
if (options.isDirty()) {
|
|
250
|
+
options.beforeUnload?.();
|
|
251
|
+
void flush();
|
|
252
|
+
event.preventDefault();
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
start,
|
|
258
|
+
schedule,
|
|
259
|
+
flush,
|
|
260
|
+
settle,
|
|
261
|
+
retryNow() {
|
|
262
|
+
offlineDelay = OFFLINE_FIRST_MS;
|
|
263
|
+
schedule(0);
|
|
264
|
+
},
|
|
265
|
+
applyRemote,
|
|
266
|
+
catchUp,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Reads the title, the listing, the named `files` and every item file (a path
|
|
271
|
+
* `item` maps to a key; `undefined`: not an item). Reads run 64 at a time; one
|
|
272
|
+
* the server fails is tried again after a wait (see `read-retries.ts`). Rejects
|
|
273
|
+
* when a read fails for good.
|
|
274
|
+
*/
|
|
275
|
+
export async function readProject(artifact, spec, options = {}) {
|
|
276
|
+
const keptAny = (options.kept?.size ?? 0) > 0;
|
|
277
|
+
try {
|
|
278
|
+
return await readProjectOnce(artifact, spec, options);
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
if (!keptAny || !isAccessRefused(error))
|
|
282
|
+
throw error;
|
|
283
|
+
options.kept.clear();
|
|
284
|
+
return readProjectOnce(artifact, spec, options);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
async function readProjectOnce(artifact, spec, { kept, ...reading }) {
|
|
288
|
+
const reader = createLoadReader(reading);
|
|
289
|
+
const ending = (error) => {
|
|
290
|
+
throw reader.end(error);
|
|
291
|
+
};
|
|
292
|
+
const readFile = async (path, hash) => {
|
|
293
|
+
const held = kept?.get(path);
|
|
294
|
+
if (held && held.hash === hash)
|
|
295
|
+
return held;
|
|
296
|
+
const file = { text: await reader.run(path, () => artifact.readText(path)), hash };
|
|
297
|
+
kept?.set(path, file);
|
|
298
|
+
return file;
|
|
299
|
+
};
|
|
300
|
+
const [title, listing] = await Promise.all([reader.run('title', () => artifact.title()), reader.run('listing', () => artifact.list())]).catch(ending);
|
|
301
|
+
const items = {};
|
|
302
|
+
const reads = Object.entries(listing).flatMap(([path, entry]) => {
|
|
303
|
+
const key = spec.item(path);
|
|
304
|
+
if (key === undefined || key === null)
|
|
305
|
+
return [];
|
|
306
|
+
return [
|
|
307
|
+
(async () => {
|
|
308
|
+
items[key] = await readFile(path, entry.sha256);
|
|
309
|
+
})(),
|
|
310
|
+
];
|
|
311
|
+
});
|
|
312
|
+
const named = spec.files.map(async (path) => {
|
|
313
|
+
const entry = listing[path];
|
|
314
|
+
return [path, entry ? await readFile(path, entry.sha256) : null];
|
|
315
|
+
});
|
|
316
|
+
const [read] = await Promise.all([Promise.all(named), ...reads]).catch(ending);
|
|
317
|
+
kept?.clear();
|
|
318
|
+
return { title, files: Object.fromEntries(read), items };
|
|
319
|
+
}
|
package/src/react.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@openartifactsai/editor-kit/react`: the views editors share. Every one is
|
|
3
|
+
* driven by a controller from the pure entry (`@openartifactsai/editor-kit`)
|
|
4
|
+
* plus props for what differs per editor. Include
|
|
5
|
+
* `@openartifactsai/editor-kit/styles.css` so Tailwind sees their classes.
|
|
6
|
+
*/
|
|
7
|
+
export * from './collab/CommentViews.js';
|
|
8
|
+
export * from './collab/CommentList.js';
|
|
9
|
+
export { CommentsLayer, InLayer } from './collab/cds/layer.js';
|
|
10
|
+
export * from './collab/HistoryViews.js';
|
|
11
|
+
export * from './collab/PresenceViews.js';
|
|
12
|
+
export * from './Announcer.js';
|
|
13
|
+
export * from './chrome.js';
|
|
14
|
+
export { FoldDivider, FoldGroup, FoldToolbar, useFolded, type FoldBand, type FoldGroupProps, type FoldToolbarProps } from './FoldToolbar.js';
|
|
15
|
+
export { menuFit } from './menu-fit.js';
|
|
16
|
+
export * from './FindBar.js';
|
|
17
|
+
export * from './PanelEdge.js';
|
|
18
|
+
export { useWheelZooms, WheelZoomMenuItem } from './WheelZoomItem.js';
|
|
19
|
+
/** Key labels for the chrome's shortcut chips (also in the pure entry). */
|
|
20
|
+
export { ALT, MOD, SHIFT } from './keys.js';
|
package/src/react.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@openartifactsai/editor-kit/react`: the views editors share. Every one is
|
|
3
|
+
* driven by a controller from the pure entry (`@openartifactsai/editor-kit`)
|
|
4
|
+
* plus props for what differs per editor. Include
|
|
5
|
+
* `@openartifactsai/editor-kit/styles.css` so Tailwind sees their classes.
|
|
6
|
+
*/
|
|
7
|
+
export * from "./collab/CommentViews.js";
|
|
8
|
+
export * from "./collab/CommentList.js";
|
|
9
|
+
export { CommentsLayer, InLayer } from "./collab/cds/layer.js";
|
|
10
|
+
export * from "./collab/HistoryViews.js";
|
|
11
|
+
export * from "./collab/PresenceViews.js";
|
|
12
|
+
export * from "./Announcer.js";
|
|
13
|
+
export * from "./chrome.js";
|
|
14
|
+
export { FoldDivider, FoldGroup, FoldToolbar, useFolded } from "./FoldToolbar.js";
|
|
15
|
+
export { menuFit } from "./menu-fit.js";
|
|
16
|
+
export * from "./FindBar.js";
|
|
17
|
+
export * from "./PanelEdge.js";
|
|
18
|
+
export { useWheelZooms, WheelZoomMenuItem } from "./WheelZoomItem.js";
|
|
19
|
+
/** Key labels for the chrome's shortcut chips (also in the pure entry). */
|
|
20
|
+
export { ALT, MOD, SHIFT } from "./keys.js";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a project load reads its files through a flaky host: a cap on reads in
|
|
3
|
+
* flight, waits that grow between tries of a read the server failed, and the
|
|
4
|
+
* point where so much is failing that waiting longer is pointless.
|
|
5
|
+
*/
|
|
6
|
+
/** The numbers a load reads by. */
|
|
7
|
+
export interface ReadPolicy {
|
|
8
|
+
/** Reads in flight at once. */
|
|
9
|
+
concurrency: number;
|
|
10
|
+
/** The waits between tries of a read the server failed, ms (one more try after each). */
|
|
11
|
+
retryWaits: readonly number[];
|
|
12
|
+
/** How many of the latest read outcomes weigh in on giving up. */
|
|
13
|
+
window: number;
|
|
14
|
+
}
|
|
15
|
+
export declare const READ_POLICY: ReadPolicy;
|
|
16
|
+
/**
|
|
17
|
+
* A read that may work if tried again: the server failed (5xx) or the
|
|
18
|
+
* network dropped. A refusal, a missing file or a bad request is final.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isServerFailure(error: unknown): boolean;
|
|
21
|
+
/** The host refused this viewer access to the files. */
|
|
22
|
+
export declare function isAccessRefused(error: unknown): boolean;
|
|
23
|
+
/** A wait of `step` ms spread into [step / 2, step] by `random` in [0, 1). */
|
|
24
|
+
export declare function spreadWait(step: number, random: number): number;
|
|
25
|
+
/** The outcomes a load has seen: whether its server failures are too many to wait out. */
|
|
26
|
+
export interface ReadTally {
|
|
27
|
+
/** One read ended; `failed`: the server failed it. */
|
|
28
|
+
note(failed: boolean): void;
|
|
29
|
+
/** True when more files wait to be retried than may be read at once, and at least half the latest reads (those in flight too) failed. */
|
|
30
|
+
hopeless(retrying: number, inFlight: number): boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare function createReadTally(policy?: ReadPolicy): ReadTally;
|
|
33
|
+
/** What a load reader waits with (tests pass their own). */
|
|
34
|
+
export interface ReadClock {
|
|
35
|
+
wait(ms: number): Promise<void>;
|
|
36
|
+
/** A number in [0, 1). */
|
|
37
|
+
random(): number;
|
|
38
|
+
}
|
|
39
|
+
/** One load's reads (see {@link createLoadReader}). */
|
|
40
|
+
export interface LoadReader {
|
|
41
|
+
/** Runs `read` for `key` when a slot is free, trying it again after a server failure. */
|
|
42
|
+
run<T>(key: string, read: () => Promise<T>): Promise<T>;
|
|
43
|
+
/**
|
|
44
|
+
* Ends the load with `error`: reads not yet started fail. Returns the error
|
|
45
|
+
* to report: a refusal that came after a server failure is reported as that
|
|
46
|
+
* failure, which is what went wrong first.
|
|
47
|
+
*/
|
|
48
|
+
end(error: unknown): unknown;
|
|
49
|
+
}
|
|
50
|
+
export interface LoadReaderOptions {
|
|
51
|
+
policy?: ReadPolicy;
|
|
52
|
+
clock?: ReadClock;
|
|
53
|
+
}
|
|
54
|
+
/** Reads for one load: at most `concurrency` at once, server failures retried, given up when hopeless. */
|
|
55
|
+
export declare function createLoadReader({ policy, clock }?: LoadReaderOptions): LoadReader;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a project load reads its files through a flaky host: a cap on reads in
|
|
3
|
+
* flight, waits that grow between tries of a read the server failed, and the
|
|
4
|
+
* point where so much is failing that waiting longer is pointless.
|
|
5
|
+
*/
|
|
6
|
+
import { isArtifactError } from '@openartifactsai/runtime';
|
|
7
|
+
export const READ_POLICY = { concurrency: 64, retryWaits: [250, 500, 1000, 2000, 4000], window: 512 };
|
|
8
|
+
/** An HTTP status in a host error's message ("HTTP 503"), if any. */
|
|
9
|
+
function statusIn(error) {
|
|
10
|
+
const match = /\bHTTP (\d{3})\b/.exec(error.message);
|
|
11
|
+
return match ? Number(match[1]) : undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A read that may work if tried again: the server failed (5xx) or the
|
|
15
|
+
* network dropped. A refusal, a missing file or a bad request is final.
|
|
16
|
+
*/
|
|
17
|
+
export function isServerFailure(error) {
|
|
18
|
+
if (isArtifactError(error)) {
|
|
19
|
+
if (error.code !== 'unavailable')
|
|
20
|
+
return false;
|
|
21
|
+
const status = statusIn(error);
|
|
22
|
+
return status === undefined || status >= 500;
|
|
23
|
+
}
|
|
24
|
+
return error instanceof TypeError;
|
|
25
|
+
}
|
|
26
|
+
/** The host refused this viewer access to the files. */
|
|
27
|
+
export function isAccessRefused(error) {
|
|
28
|
+
if (!isArtifactError(error))
|
|
29
|
+
return false;
|
|
30
|
+
if (error.code === 'not_writer' || error.code === 'not_granted')
|
|
31
|
+
return true;
|
|
32
|
+
const status = error.code === 'unavailable' ? statusIn(error) : undefined;
|
|
33
|
+
return status === 401 || status === 403;
|
|
34
|
+
}
|
|
35
|
+
/** A wait of `step` ms spread into [step / 2, step] by `random` in [0, 1). */
|
|
36
|
+
export function spreadWait(step, random) {
|
|
37
|
+
return (step / 2) * (1 + random);
|
|
38
|
+
}
|
|
39
|
+
export function createReadTally(policy = READ_POLICY) {
|
|
40
|
+
const latest = [];
|
|
41
|
+
let failures = 0;
|
|
42
|
+
return {
|
|
43
|
+
note(failed) {
|
|
44
|
+
latest.push(failed);
|
|
45
|
+
if (failed)
|
|
46
|
+
failures++;
|
|
47
|
+
if (latest.length > policy.window && latest.shift())
|
|
48
|
+
failures--;
|
|
49
|
+
},
|
|
50
|
+
hopeless(retrying, inFlight) {
|
|
51
|
+
return retrying > policy.concurrency && failures * 2 >= latest.length + inFlight;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const REAL_CLOCK = {
|
|
56
|
+
wait: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
57
|
+
random: () => Math.random(),
|
|
58
|
+
};
|
|
59
|
+
/** Reads for one load: at most `concurrency` at once, server failures retried, given up when hopeless. */
|
|
60
|
+
export function createLoadReader({ policy = READ_POLICY, clock = REAL_CLOCK } = {}) {
|
|
61
|
+
const tally = createReadTally(policy);
|
|
62
|
+
const retrying = new Set();
|
|
63
|
+
const queue = [];
|
|
64
|
+
let inFlight = 0;
|
|
65
|
+
let ended = false;
|
|
66
|
+
let firstFailure = null;
|
|
67
|
+
/** Runs `read` in a slot: now if one is free, else when one frees. */
|
|
68
|
+
function inSlot(key, read) {
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
const start = () => {
|
|
71
|
+
if (ended) {
|
|
72
|
+
reject(new Error(`${key}: not read, because the load has ended`));
|
|
73
|
+
queue.shift()?.();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
inFlight++;
|
|
77
|
+
read()
|
|
78
|
+
.then(resolve, reject)
|
|
79
|
+
.finally(() => {
|
|
80
|
+
inFlight--;
|
|
81
|
+
queue.shift()?.();
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
if (inFlight < policy.concurrency && queue.length === 0)
|
|
85
|
+
start();
|
|
86
|
+
else
|
|
87
|
+
queue.push(start);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async function run(key, read) {
|
|
91
|
+
for (let tries = 0;; tries++) {
|
|
92
|
+
let failure;
|
|
93
|
+
try {
|
|
94
|
+
const value = await inSlot(key, read);
|
|
95
|
+
retrying.delete(key);
|
|
96
|
+
tally.note(false);
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
failure = error;
|
|
101
|
+
}
|
|
102
|
+
if (!isServerFailure(failure)) {
|
|
103
|
+
retrying.delete(key);
|
|
104
|
+
tally.note(false);
|
|
105
|
+
throw failure;
|
|
106
|
+
}
|
|
107
|
+
tally.note(true);
|
|
108
|
+
firstFailure ??= { error: failure };
|
|
109
|
+
const step = policy.retryWaits[tries];
|
|
110
|
+
if (ended || step === undefined)
|
|
111
|
+
throw failure;
|
|
112
|
+
retrying.add(key);
|
|
113
|
+
if (tally.hopeless(retrying.size, inFlight))
|
|
114
|
+
throw failure;
|
|
115
|
+
await clock.wait(spreadWait(step, clock.random()));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
run,
|
|
120
|
+
end(error) {
|
|
121
|
+
ended = true;
|
|
122
|
+
return isAccessRefused(error) && firstFailure ? firstFailure.error : error;
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dragging a canvas with the right mouse button. A right press that travels a
|
|
3
|
+
* few pixels pans the view and does nothing else: the selection stays as it
|
|
4
|
+
* was and no menu opens. One that does not travel is the right-click it always
|
|
5
|
+
* was: once the button is up, the press goes on to the editor and the menu
|
|
6
|
+
* opens where it was pressed.
|
|
7
|
+
*
|
|
8
|
+
* Browsers ask for the menu at different times: macOS and Linux send
|
|
9
|
+
* `contextmenu` right after the press, Windows after the release. So the press
|
|
10
|
+
* is held back until it is known which it is, the menu asked for meanwhile is
|
|
11
|
+
* stopped, and on a release without travel the press is sent again (and, when
|
|
12
|
+
* the menu was already asked for, the menu too). A `contextmenu` that follows
|
|
13
|
+
* a pan is stopped.
|
|
14
|
+
*
|
|
15
|
+
* {@link rightPressStep} is the decision, without a DOM; {@link installRightDragPan}
|
|
16
|
+
* wires it to a canvas element.
|
|
17
|
+
*/
|
|
18
|
+
/** Distance a right press travels before it pans, CSS px. */
|
|
19
|
+
export declare const RIGHT_PAN_THRESHOLD = 4;
|
|
20
|
+
/** How long after a pan a late `contextmenu` (Windows sends it after the release) is still stopped, ms. */
|
|
21
|
+
export declare const RIGHT_PAN_QUIET_MS = 500;
|
|
22
|
+
/** Where a right press stands. */
|
|
23
|
+
export type RightPress =
|
|
24
|
+
/** No right press. `quietUntil`: a pan ended, and a `contextmenu` before then is stopped. */
|
|
25
|
+
{
|
|
26
|
+
phase: 'idle';
|
|
27
|
+
quietUntil: number;
|
|
28
|
+
}
|
|
29
|
+
/** Pressed, not yet travelled. `menu`: the browser asked for the menu meanwhile. */
|
|
30
|
+
| {
|
|
31
|
+
phase: 'held';
|
|
32
|
+
id: number;
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
menu: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** Panning; (x, y) is where the pointer last was. */
|
|
38
|
+
| {
|
|
39
|
+
phase: 'panning';
|
|
40
|
+
id: number;
|
|
41
|
+
x: number;
|
|
42
|
+
y: number;
|
|
43
|
+
};
|
|
44
|
+
/** No right press. */
|
|
45
|
+
export declare const RIGHT_PRESS_IDLE: RightPress;
|
|
46
|
+
/** What reached the canvas. `time` is a clock in ms (the event's `timeStamp` or `performance.now()`). */
|
|
47
|
+
export type RightPressInput =
|
|
48
|
+
/** A right press on the canvas that may pan. */
|
|
49
|
+
{
|
|
50
|
+
type: 'press';
|
|
51
|
+
id: number;
|
|
52
|
+
x: number;
|
|
53
|
+
y: number;
|
|
54
|
+
time: number;
|
|
55
|
+
} | {
|
|
56
|
+
type: 'move';
|
|
57
|
+
id: number;
|
|
58
|
+
x: number;
|
|
59
|
+
y: number;
|
|
60
|
+
time: number;
|
|
61
|
+
} | {
|
|
62
|
+
type: 'release';
|
|
63
|
+
id: number;
|
|
64
|
+
x: number;
|
|
65
|
+
y: number;
|
|
66
|
+
time: number;
|
|
67
|
+
} | {
|
|
68
|
+
type: 'cancel';
|
|
69
|
+
id: number;
|
|
70
|
+
time: number;
|
|
71
|
+
}
|
|
72
|
+
/** The browser asks for the context menu. */
|
|
73
|
+
| {
|
|
74
|
+
type: 'menu';
|
|
75
|
+
time: number;
|
|
76
|
+
};
|
|
77
|
+
/** What to do with the event. */
|
|
78
|
+
export type RightPressEffect =
|
|
79
|
+
/** Not this press's: let it through. */
|
|
80
|
+
{
|
|
81
|
+
do: 'pass';
|
|
82
|
+
}
|
|
83
|
+
/** Stop it (a `contextmenu`'s default too). */
|
|
84
|
+
| {
|
|
85
|
+
do: 'stop';
|
|
86
|
+
}
|
|
87
|
+
/** Stop it and move the view by (dx, dy); `start` on the first move of a pan. */
|
|
88
|
+
| {
|
|
89
|
+
do: 'pan';
|
|
90
|
+
dx: number;
|
|
91
|
+
dy: number;
|
|
92
|
+
start: boolean;
|
|
93
|
+
}
|
|
94
|
+
/** Stop it: the pan is over. */
|
|
95
|
+
| {
|
|
96
|
+
do: 'end';
|
|
97
|
+
}
|
|
98
|
+
/** A right-click: send the press again, let the release through, and open the menu if it was already asked for. */
|
|
99
|
+
| {
|
|
100
|
+
do: 'click';
|
|
101
|
+
menu: boolean;
|
|
102
|
+
};
|
|
103
|
+
/** The next state of a right press, and what to do with the event that moved it there. */
|
|
104
|
+
export declare function rightPressStep(state: RightPress, input: RightPressInput): {
|
|
105
|
+
state: RightPress;
|
|
106
|
+
effect: RightPressEffect;
|
|
107
|
+
};
|
|
108
|
+
/** Options of {@link installRightDragPan}. */
|
|
109
|
+
export interface RightDragPanOptions {
|
|
110
|
+
/** Moves the view by a screen distance, CSS px: what is under the pointer follows it. */
|
|
111
|
+
pan(dx: number, dy: number): void;
|
|
112
|
+
/**
|
|
113
|
+
* Whether this right press on the canvas may pan. Presses in fields and text
|
|
114
|
+
* being typed never do (their own menu opens as usual); this narrows it further.
|
|
115
|
+
*/
|
|
116
|
+
accepts?(event: PointerEvent): boolean;
|
|
117
|
+
/** A pan starts (`true`) or ends (`false`). */
|
|
118
|
+
onPanning?(panning: boolean): void;
|
|
119
|
+
}
|
|
120
|
+
/** Set on `<html>` while a right-button pan runs; the grabbing cursor is drawn from it. */
|
|
121
|
+
export declare const RIGHT_PAN_ATTRIBUTE = "data-right-drag-pan";
|
|
122
|
+
/**
|
|
123
|
+
* Lets the right mouse button drag `surface` (see the top of this file).
|
|
124
|
+
* Listens on the window, in the capture phase, so the press is held back
|
|
125
|
+
* before the editor's own handlers (React's included) see it. Returns the
|
|
126
|
+
* uninstaller.
|
|
127
|
+
*/
|
|
128
|
+
export declare function installRightDragPan(surface: HTMLElement, options: RightDragPanOptions): () => void;
|