@link-foundation/ui-devmode 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +353 -2
- package/package.json +78 -4
- package/src/activation.js +235 -0
- package/src/adapter.js +92 -0
- package/src/agent-instruction.d.ts +6 -0
- package/src/agent-instruction.js +17 -0
- package/src/agent-instruction.txt +8 -0
- package/src/apply-shared.js +135 -0
- package/src/comments/commands.js +77 -0
- package/src/comments/geometry.js +65 -0
- package/src/comments/index.d.ts +93 -0
- package/src/comments/index.js +205 -0
- package/src/comments/model.js +359 -0
- package/src/comments/picking.js +154 -0
- package/src/comments/style.js +36 -0
- package/src/comments/view.js +250 -0
- package/src/comments/voice.js +64 -0
- package/src/defaults-dialog.js +120 -0
- package/src/details.js +159 -0
- package/src/editors.js +194 -0
- package/src/export.js +50 -0
- package/src/foreign/adapter.js +166 -0
- package/src/foreign/apply.js +89 -0
- package/src/foreign/entry.js +173 -0
- package/src/foreign/index.d.ts +32 -0
- package/src/foreign/injected.js +6 -0
- package/src/foreign/persist.js +54 -0
- package/src/foreign/properties.js +126 -0
- package/src/foreign/runtime.js +67 -0
- package/src/index.d.ts +204 -0
- package/src/index.js +201 -0
- package/src/inspector-css.js +106 -0
- package/src/inspector.js +342 -0
- package/src/inspector.lino +50 -0
- package/src/messages.js +113 -0
- package/src/model.d.ts +101 -0
- package/src/model.js +473 -0
- package/src/overlay.js +91 -0
- package/src/policy.js +222 -0
- package/src/read-asset.browser.js +8 -0
- package/src/read-asset.js +10 -0
- package/src/session.js +555 -0
- package/src/share.js +23 -0
- package/src/validate.js +170 -0
- package/src/views.js +328 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import type { UiDocument } from '@link-foundation/ui-core';
|
|
3
|
+
export interface CommentTask {
|
|
4
|
+
id: string;
|
|
5
|
+
target: string;
|
|
6
|
+
kind: string;
|
|
7
|
+
revision: string;
|
|
8
|
+
instruction: string;
|
|
9
|
+
input: 'text' | 'voice';
|
|
10
|
+
status: 'open' | 'resolved';
|
|
11
|
+
}
|
|
12
|
+
export interface CommentRevision {
|
|
13
|
+
id: string;
|
|
14
|
+
source: string;
|
|
15
|
+
}
|
|
16
|
+
export interface CommentEvent extends CommentTask {
|
|
17
|
+
action: 'create' | 'update' | 'remove';
|
|
18
|
+
}
|
|
19
|
+
export interface CommentData {
|
|
20
|
+
revisions: CommentRevision[];
|
|
21
|
+
tasks: CommentTask[];
|
|
22
|
+
events: CommentEvent[];
|
|
23
|
+
}
|
|
24
|
+
export interface CommentTree {
|
|
25
|
+
get(id: string): object | undefined;
|
|
26
|
+
roots(): string[];
|
|
27
|
+
children(id: string): string[];
|
|
28
|
+
toDocument(): UiDocument;
|
|
29
|
+
subscribe?(listener: () => void): () => void;
|
|
30
|
+
}
|
|
31
|
+
export interface CommentModel {
|
|
32
|
+
tasks(): CommentTask[];
|
|
33
|
+
revisions(): CommentRevision[];
|
|
34
|
+
events(): CommentEvent[];
|
|
35
|
+
export(): string;
|
|
36
|
+
import(text: string): void;
|
|
37
|
+
subscribe(listener: () => void): () => void;
|
|
38
|
+
add(
|
|
39
|
+
ids: Iterable<string>,
|
|
40
|
+
instruction: string,
|
|
41
|
+
input?: 'text' | 'voice'
|
|
42
|
+
): CommentTask[];
|
|
43
|
+
update(
|
|
44
|
+
id: string,
|
|
45
|
+
patch: Partial<Pick<CommentTask, 'instruction' | 'status'>>
|
|
46
|
+
): void;
|
|
47
|
+
remove(id: string): void;
|
|
48
|
+
state(id: string): 'current' | 'outdated' | 'missing';
|
|
49
|
+
}
|
|
50
|
+
export const COMMENT_LIMITS: Readonly<{
|
|
51
|
+
size: number;
|
|
52
|
+
tasks: number;
|
|
53
|
+
revisions: number;
|
|
54
|
+
events: number;
|
|
55
|
+
instruction: number;
|
|
56
|
+
}>;
|
|
57
|
+
export function readComments(text: string): CommentData;
|
|
58
|
+
export function commentDocument(tree: CommentTree): UiDocument;
|
|
59
|
+
export function createComments(options: {
|
|
60
|
+
getDocument: () => UiDocument;
|
|
61
|
+
text?: string;
|
|
62
|
+
trace?: (event: string, detail: CommentEvent) => void;
|
|
63
|
+
}): CommentModel;
|
|
64
|
+
export interface MountCommentsOptions {
|
|
65
|
+
root: Element;
|
|
66
|
+
tree: CommentTree;
|
|
67
|
+
nodeFor(id: string): Element | null;
|
|
68
|
+
observe?(listener: () => void): () => void;
|
|
69
|
+
container?: Node;
|
|
70
|
+
storage?: Pick<Storage, 'getItem' | 'setItem'> | null;
|
|
71
|
+
storageKey?: string;
|
|
72
|
+
text?: string;
|
|
73
|
+
messages?: Record<string, string>;
|
|
74
|
+
trace?: (event: string, detail: CommentEvent) => void;
|
|
75
|
+
onClose?: () => void;
|
|
76
|
+
}
|
|
77
|
+
export function mountComments(options: MountCommentsOptions): {
|
|
78
|
+
host: HTMLElement;
|
|
79
|
+
model: CommentModel;
|
|
80
|
+
tree: CommentTree;
|
|
81
|
+
select(ids: Iterable<string>): void;
|
|
82
|
+
selection(): string[];
|
|
83
|
+
refresh(): void;
|
|
84
|
+
destroy(): void;
|
|
85
|
+
};
|
|
86
|
+
export interface Point {
|
|
87
|
+
x: number;
|
|
88
|
+
y: number;
|
|
89
|
+
}
|
|
90
|
+
export function lasso(
|
|
91
|
+
rects: (Point & { id: string; width: number; height: number })[],
|
|
92
|
+
polygon: Point[]
|
|
93
|
+
): string[];
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { commentDocument, createComments } from './model.js';
|
|
2
|
+
import {
|
|
3
|
+
createCommentView,
|
|
4
|
+
renderObjects,
|
|
5
|
+
renderTasks,
|
|
6
|
+
renderMarks,
|
|
7
|
+
} from './view.js';
|
|
8
|
+
import { commentCommands } from './commands.js';
|
|
9
|
+
import { installCommentPicking } from './picking.js';
|
|
10
|
+
export {
|
|
11
|
+
createComments,
|
|
12
|
+
readComments,
|
|
13
|
+
commentDocument,
|
|
14
|
+
COMMENT_LIMITS,
|
|
15
|
+
} from './model.js';
|
|
16
|
+
export { lasso } from './geometry.js';
|
|
17
|
+
|
|
18
|
+
/** Mount reusable comments over a stable-ID tree and its rendered objects. */
|
|
19
|
+
export function mountComments(options) {
|
|
20
|
+
const { root, tree, nodeFor, onClose } = options;
|
|
21
|
+
const document = root.ownerDocument;
|
|
22
|
+
const window = document.defaultView;
|
|
23
|
+
const restoreFocus = activeElement(document);
|
|
24
|
+
const t = (text) => options.messages?.[text] ?? text;
|
|
25
|
+
const storageKey =
|
|
26
|
+
options.storageKey ?? `ui-comments:${window.location.pathname}`;
|
|
27
|
+
const getDocument = () => commentDocument(tree);
|
|
28
|
+
const { model, storage, restoreError } = restoreModel(
|
|
29
|
+
options,
|
|
30
|
+
getDocument,
|
|
31
|
+
window,
|
|
32
|
+
storageKey
|
|
33
|
+
);
|
|
34
|
+
const view = createCommentView({
|
|
35
|
+
document,
|
|
36
|
+
container: options.container ?? document.body,
|
|
37
|
+
t,
|
|
38
|
+
});
|
|
39
|
+
const cleanups = [];
|
|
40
|
+
const listen = commentListeners(cleanups);
|
|
41
|
+
const nodes = () =>
|
|
42
|
+
[...getDocument().byId.values()]
|
|
43
|
+
.map((node) => ({ ...node, node: nodeFor(node.id) }))
|
|
44
|
+
.filter(({ node }) => node?.isConnected);
|
|
45
|
+
let selected = new Set();
|
|
46
|
+
let closed = false;
|
|
47
|
+
const announce = (text) => {
|
|
48
|
+
view.status.textContent = text;
|
|
49
|
+
};
|
|
50
|
+
const run = (callback) => {
|
|
51
|
+
try {
|
|
52
|
+
callback();
|
|
53
|
+
} catch (error) {
|
|
54
|
+
announce(error.message);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const marks = () => renderMarks(view, nodes(), selected, model);
|
|
58
|
+
const refresh = () => {
|
|
59
|
+
const available = nodes();
|
|
60
|
+
selected = new Set(
|
|
61
|
+
[...selected].filter((id) => available.some((node) => node.id === id))
|
|
62
|
+
);
|
|
63
|
+
renderObjects(view, available, selected);
|
|
64
|
+
for (const task of model.tasks()) {
|
|
65
|
+
const card = [...view.tasks.children].find(
|
|
66
|
+
(node) => node.dataset.task === task.id
|
|
67
|
+
);
|
|
68
|
+
const meta = card?.querySelector('.meta');
|
|
69
|
+
if (meta) {
|
|
70
|
+
meta.textContent = `${task.revision} · ${t(model.state(task.id))} · ${task.input}`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
marks();
|
|
74
|
+
};
|
|
75
|
+
const select = (ids) => {
|
|
76
|
+
selected = new Set(ids);
|
|
77
|
+
refresh();
|
|
78
|
+
};
|
|
79
|
+
const selection = () => [...selected];
|
|
80
|
+
function destroy() {
|
|
81
|
+
if (closed) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
closed = true;
|
|
85
|
+
actions.destroy();
|
|
86
|
+
picking.destroy();
|
|
87
|
+
cleanups.splice(0).forEach((cleanup) => cleanup());
|
|
88
|
+
view.host.remove();
|
|
89
|
+
onClose?.();
|
|
90
|
+
if (restoreFocus?.isConnected) {
|
|
91
|
+
restoreFocus.focus?.();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const picking = installCommentPicking({
|
|
95
|
+
window,
|
|
96
|
+
root,
|
|
97
|
+
view,
|
|
98
|
+
nodes,
|
|
99
|
+
select,
|
|
100
|
+
selection,
|
|
101
|
+
listen,
|
|
102
|
+
close: destroy,
|
|
103
|
+
});
|
|
104
|
+
const saveError = (error) => {
|
|
105
|
+
if (!closed) {
|
|
106
|
+
announce(
|
|
107
|
+
`${t('Comments remain available in this tab. Export LiNo to keep them.')} ${error.message}`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const persist = () => {
|
|
112
|
+
renderTasks(view, model, run);
|
|
113
|
+
refresh();
|
|
114
|
+
try {
|
|
115
|
+
Promise.resolve(storage?.setItem(storageKey, model.export())).then(() => {
|
|
116
|
+
if (!closed) {
|
|
117
|
+
announce(t('Comments saved.'));
|
|
118
|
+
}
|
|
119
|
+
}, saveError);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
saveError(error);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
cleanups.push(model.subscribe(persist));
|
|
125
|
+
const actions = commentCommands({
|
|
126
|
+
window,
|
|
127
|
+
view,
|
|
128
|
+
model,
|
|
129
|
+
picking,
|
|
130
|
+
selection,
|
|
131
|
+
select,
|
|
132
|
+
destroy,
|
|
133
|
+
announce,
|
|
134
|
+
isClosed: () => closed,
|
|
135
|
+
});
|
|
136
|
+
listen(view.shadow, 'click', (event) => {
|
|
137
|
+
const action = event.target.closest?.('[data-action]')?.dataset.action;
|
|
138
|
+
if (action) {
|
|
139
|
+
run(() => actions.commands[action]?.());
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
listen(view.objects, 'change', () =>
|
|
143
|
+
select(
|
|
144
|
+
[...view.objects.querySelectorAll('input:checked')].map(
|
|
145
|
+
(node) => node.dataset.target
|
|
146
|
+
)
|
|
147
|
+
)
|
|
148
|
+
);
|
|
149
|
+
listen(window, 'scroll', marks, true);
|
|
150
|
+
listen(window, 'resize', marks);
|
|
151
|
+
for (const observe of new Set(
|
|
152
|
+
[options.observe, tree.subscribe].filter(Boolean)
|
|
153
|
+
)) {
|
|
154
|
+
cleanups.push(observe(refresh));
|
|
155
|
+
}
|
|
156
|
+
if (window.ResizeObserver) {
|
|
157
|
+
const observer = new window.ResizeObserver(marks);
|
|
158
|
+
observer.observe(root);
|
|
159
|
+
cleanups.push(() => observer.disconnect());
|
|
160
|
+
}
|
|
161
|
+
renderTasks(view, model, run);
|
|
162
|
+
refresh();
|
|
163
|
+
if (restoreError) {
|
|
164
|
+
announce(`${t('Saved comments could not be restored.')} ${restoreError}`);
|
|
165
|
+
}
|
|
166
|
+
view.pick.focus();
|
|
167
|
+
return { host: view.host, model, tree, select, selection, refresh, destroy };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function restoreModel(options, getDocument, window, storageKey) {
|
|
171
|
+
let storage = options.storage;
|
|
172
|
+
let saved = options.text;
|
|
173
|
+
let restoreError = '';
|
|
174
|
+
try {
|
|
175
|
+
if (storage === undefined) {
|
|
176
|
+
storage = window.localStorage;
|
|
177
|
+
}
|
|
178
|
+
saved ??= storage?.getItem(storageKey);
|
|
179
|
+
} catch (error) {
|
|
180
|
+
restoreError = error.message;
|
|
181
|
+
}
|
|
182
|
+
let model;
|
|
183
|
+
try {
|
|
184
|
+
model = createComments({ getDocument, text: saved, trace: options.trace });
|
|
185
|
+
} catch (error) {
|
|
186
|
+
restoreError = error.message;
|
|
187
|
+
model = createComments({ getDocument, trace: options.trace });
|
|
188
|
+
}
|
|
189
|
+
return { model, storage, restoreError };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function commentListeners(cleanups) {
|
|
193
|
+
return (target, name, handler, capture = false) => {
|
|
194
|
+
target.addEventListener(name, handler, capture);
|
|
195
|
+
cleanups.push(() => target.removeEventListener(name, handler, capture));
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function activeElement(document) {
|
|
200
|
+
let node = document.activeElement;
|
|
201
|
+
while (node?.shadowRoot?.activeElement) {
|
|
202
|
+
node = node.shadowRoot.activeElement;
|
|
203
|
+
}
|
|
204
|
+
return node;
|
|
205
|
+
}
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
/** Versioned annotations are data, never executable application operations. */
|
|
2
|
+
import {
|
|
3
|
+
format,
|
|
4
|
+
formatLinks,
|
|
5
|
+
parse,
|
|
6
|
+
parseLinks,
|
|
7
|
+
} from '@link-foundation/ui-core';
|
|
8
|
+
|
|
9
|
+
export const COMMENT_LIMITS = Object.freeze({
|
|
10
|
+
size: 1048576,
|
|
11
|
+
tasks: 500,
|
|
12
|
+
revisions: 100,
|
|
13
|
+
events: 2000,
|
|
14
|
+
instruction: 4000,
|
|
15
|
+
});
|
|
16
|
+
const TASK_FIELDS = [
|
|
17
|
+
'target',
|
|
18
|
+
'kind',
|
|
19
|
+
'revision',
|
|
20
|
+
'instruction',
|
|
21
|
+
'input',
|
|
22
|
+
'status',
|
|
23
|
+
];
|
|
24
|
+
const fail = (message) => {
|
|
25
|
+
throw new Error(message);
|
|
26
|
+
};
|
|
27
|
+
const clone = (items) => items.map((item) => ({ ...item }));
|
|
28
|
+
|
|
29
|
+
/** Name anonymous objects with their actual adapter IDs in review snapshots. */
|
|
30
|
+
export function commentDocument(tree) {
|
|
31
|
+
const byId = new Map();
|
|
32
|
+
const walk = (id) => {
|
|
33
|
+
const node = { ...tree.get(id), id, children: tree.children(id).map(walk) };
|
|
34
|
+
byId.set(id, node);
|
|
35
|
+
return node;
|
|
36
|
+
};
|
|
37
|
+
return { ...tree.toDocument(), roots: tree.roots().map(walk), byId };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function fields(values, allowed) {
|
|
41
|
+
const result = {};
|
|
42
|
+
for (const value of values) {
|
|
43
|
+
if (
|
|
44
|
+
!Array.isArray(value) ||
|
|
45
|
+
value.length !== 2 ||
|
|
46
|
+
!allowed.includes(value[0]) ||
|
|
47
|
+
typeof value[1] !== 'string' ||
|
|
48
|
+
Object.hasOwn(result, value[0])
|
|
49
|
+
) {
|
|
50
|
+
fail('Unknown, duplicate or invalid comment field');
|
|
51
|
+
}
|
|
52
|
+
result[value[0]] = value[1];
|
|
53
|
+
}
|
|
54
|
+
if (allowed.some((name) => !Object.hasOwn(result, name))) {
|
|
55
|
+
fail('Missing comment field');
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function instructionText(text) {
|
|
61
|
+
if (
|
|
62
|
+
typeof text !== 'string' ||
|
|
63
|
+
!text.trim() ||
|
|
64
|
+
text.length > COMMENT_LIMITS.instruction
|
|
65
|
+
) {
|
|
66
|
+
fail('Instruction must contain 1–4000 characters');
|
|
67
|
+
}
|
|
68
|
+
return text;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function checkTask(task, revisions) {
|
|
72
|
+
instructionText(task.instruction);
|
|
73
|
+
if (!['text', 'voice'].includes(task.input)) {
|
|
74
|
+
fail('Invalid input method');
|
|
75
|
+
}
|
|
76
|
+
if (!['open', 'resolved'].includes(task.status)) {
|
|
77
|
+
fail('Invalid status');
|
|
78
|
+
}
|
|
79
|
+
const document = revisions.get(task.revision);
|
|
80
|
+
if (
|
|
81
|
+
!document?.byId.has(task.target) ||
|
|
82
|
+
document.byId.get(task.target).kind !== task.kind
|
|
83
|
+
) {
|
|
84
|
+
fail('Invalid target or revision');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function snapshot(source) {
|
|
89
|
+
const document = parse(source, { checks: false });
|
|
90
|
+
if (
|
|
91
|
+
document.diagnostics.some((d) => d.severity === 'error') ||
|
|
92
|
+
!document.roots.length
|
|
93
|
+
) {
|
|
94
|
+
fail('Invalid revision snapshot');
|
|
95
|
+
}
|
|
96
|
+
return document;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const isReview = (roots) =>
|
|
100
|
+
roots.length === 1 &&
|
|
101
|
+
Boolean(roots[0]?.id) &&
|
|
102
|
+
roots[0]?.values?.[0] === 'ui-comments';
|
|
103
|
+
|
|
104
|
+
/** Strict bounded reader for the ui-comments/1 interchange format. */
|
|
105
|
+
export function readComments(text) {
|
|
106
|
+
const roots = parseLinks(text, {
|
|
107
|
+
maxInputSize: COMMENT_LIMITS.size,
|
|
108
|
+
maxDepth: 80,
|
|
109
|
+
});
|
|
110
|
+
const root = roots[0];
|
|
111
|
+
if (!isReview(roots)) {
|
|
112
|
+
fail('Expected one ui-comments link');
|
|
113
|
+
}
|
|
114
|
+
const result = { revisions: [], tasks: [], events: [] };
|
|
115
|
+
const seen = new Set(['ui-review', root.id]);
|
|
116
|
+
let version = false;
|
|
117
|
+
for (const value of root.values.slice(1)) {
|
|
118
|
+
if (Array.isArray(value) && value[0] === 'version') {
|
|
119
|
+
if (version || value.length !== 2 || value[1] !== '1') {
|
|
120
|
+
fail('Invalid comments version');
|
|
121
|
+
}
|
|
122
|
+
version = true;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (!value?.id || seen.has(value.id)) {
|
|
126
|
+
fail('Invalid or duplicate comment field');
|
|
127
|
+
}
|
|
128
|
+
seen.add(value.id);
|
|
129
|
+
readEntry(result, value);
|
|
130
|
+
}
|
|
131
|
+
if (!version) {
|
|
132
|
+
fail('Missing comments version');
|
|
133
|
+
}
|
|
134
|
+
validateData(result);
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function readEntry(result, value) {
|
|
139
|
+
const [kind, ...values] = value.values ?? [];
|
|
140
|
+
if (kind === 'revision') {
|
|
141
|
+
const [source] = values;
|
|
142
|
+
if (
|
|
143
|
+
values.length !== 1 ||
|
|
144
|
+
!Array.isArray(source) ||
|
|
145
|
+
source[0] !== 'source' ||
|
|
146
|
+
source.length < 2
|
|
147
|
+
) {
|
|
148
|
+
fail('Invalid revision source');
|
|
149
|
+
}
|
|
150
|
+
result.revisions.push({
|
|
151
|
+
id: value.id,
|
|
152
|
+
source: formatLinks(source.slice(1)),
|
|
153
|
+
});
|
|
154
|
+
} else if (kind === 'task') {
|
|
155
|
+
result.tasks.push({ id: value.id, ...fields(values, TASK_FIELDS) });
|
|
156
|
+
} else if (kind === 'event') {
|
|
157
|
+
const { task, action, ...data } = fields(values, [
|
|
158
|
+
'task',
|
|
159
|
+
'action',
|
|
160
|
+
...TASK_FIELDS,
|
|
161
|
+
]);
|
|
162
|
+
result.events.push({ id: task, action, ...data });
|
|
163
|
+
} else {
|
|
164
|
+
fail('Unknown comment field');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const freshId = (prefix, used) => {
|
|
168
|
+
let i = 1;
|
|
169
|
+
while (used.has(`${prefix}-${i}`)) {
|
|
170
|
+
i += 1;
|
|
171
|
+
}
|
|
172
|
+
const id = `${prefix}-${i}`;
|
|
173
|
+
used.add(id);
|
|
174
|
+
return id;
|
|
175
|
+
};
|
|
176
|
+
const idsOf = (data) =>
|
|
177
|
+
new Set([
|
|
178
|
+
'ui-review',
|
|
179
|
+
...data.revisions.map((r) => r.id),
|
|
180
|
+
...data.events.map((e) => e.id),
|
|
181
|
+
]);
|
|
182
|
+
|
|
183
|
+
function validateData(data) {
|
|
184
|
+
for (const name of ['tasks', 'revisions', 'events']) {
|
|
185
|
+
if (data[name].length > COMMENT_LIMITS[name]) {
|
|
186
|
+
fail(`Comment ${name} limit exceeded`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const revisions = new Map(
|
|
190
|
+
data.revisions.map((r) => [r.id, snapshot(r.source)])
|
|
191
|
+
);
|
|
192
|
+
data.tasks.forEach((task) => checkTask(task, revisions));
|
|
193
|
+
const replayed = new Map();
|
|
194
|
+
const created = new Set();
|
|
195
|
+
for (const { action, ...task } of data.events) {
|
|
196
|
+
checkTask(task, revisions);
|
|
197
|
+
const previous = replayed.get(task.id);
|
|
198
|
+
if (
|
|
199
|
+
previous &&
|
|
200
|
+
['target', 'kind', 'revision', 'input'].some(
|
|
201
|
+
(key) => previous[key] !== task[key]
|
|
202
|
+
)
|
|
203
|
+
) {
|
|
204
|
+
fail('Immutable task history field');
|
|
205
|
+
}
|
|
206
|
+
if (action === 'create' && !created.has(task.id)) {
|
|
207
|
+
created.add(task.id);
|
|
208
|
+
replayed.set(task.id, task);
|
|
209
|
+
} else if (action === 'update' && replayed.has(task.id)) {
|
|
210
|
+
replayed.set(task.id, task);
|
|
211
|
+
} else if (action === 'remove' && replayed.has(task.id)) {
|
|
212
|
+
if (JSON.stringify(task) !== JSON.stringify(previous)) {
|
|
213
|
+
fail('Removed task differs from history');
|
|
214
|
+
}
|
|
215
|
+
replayed.delete(task.id);
|
|
216
|
+
} else {
|
|
217
|
+
fail('Invalid comment event history');
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (JSON.stringify([...replayed.values()]) !== JSON.stringify(data.tasks)) {
|
|
221
|
+
fail('Comment history does not match tasks');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const props = (object, names) => names.map((name) => [name, object[name]]);
|
|
226
|
+
function write(data) {
|
|
227
|
+
const used = idsOf(data);
|
|
228
|
+
return formatLinks([
|
|
229
|
+
{
|
|
230
|
+
id: 'ui-review',
|
|
231
|
+
values: [
|
|
232
|
+
'ui-comments',
|
|
233
|
+
['version', '1'],
|
|
234
|
+
...data.revisions.map((r) => ({
|
|
235
|
+
id: r.id,
|
|
236
|
+
values: ['revision', ['source', ...parseLinks(r.source)]],
|
|
237
|
+
})),
|
|
238
|
+
...data.tasks.map((t) => ({
|
|
239
|
+
id: t.id,
|
|
240
|
+
values: ['task', ...props(t, TASK_FIELDS)],
|
|
241
|
+
})),
|
|
242
|
+
...data.events.map((e) => ({
|
|
243
|
+
id: freshId('event', used),
|
|
244
|
+
values: [
|
|
245
|
+
'event',
|
|
246
|
+
['task', e.id],
|
|
247
|
+
['action', e.action],
|
|
248
|
+
...props(e, TASK_FIELDS),
|
|
249
|
+
],
|
|
250
|
+
})),
|
|
251
|
+
],
|
|
252
|
+
},
|
|
253
|
+
]);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** A deterministic model: batches validate completely before becoming visible. */
|
|
257
|
+
export function createComments({ getDocument, text, trace } = {}) {
|
|
258
|
+
let data = text
|
|
259
|
+
? readComments(text)
|
|
260
|
+
: { revisions: [], tasks: [], events: [] };
|
|
261
|
+
const listeners = new Set();
|
|
262
|
+
const commit = (next) => {
|
|
263
|
+
validateData(next);
|
|
264
|
+
if (write(next).length > COMMENT_LIMITS.size) {
|
|
265
|
+
fail('Comment size limit exceeded');
|
|
266
|
+
}
|
|
267
|
+
const events = next.events.slice(data.events.length);
|
|
268
|
+
data = next;
|
|
269
|
+
for (const event of events) {
|
|
270
|
+
trace?.('comment', { ...event });
|
|
271
|
+
}
|
|
272
|
+
listeners.forEach((listener) => listener());
|
|
273
|
+
};
|
|
274
|
+
const taskFor = (id) =>
|
|
275
|
+
data.tasks.find((t) => t.id === id) ?? fail('Unknown task');
|
|
276
|
+
return {
|
|
277
|
+
tasks: () => clone(data.tasks),
|
|
278
|
+
revisions: () => clone(data.revisions),
|
|
279
|
+
events: () => clone(data.events),
|
|
280
|
+
export: () => write(data),
|
|
281
|
+
subscribe(listener) {
|
|
282
|
+
listeners.add(listener);
|
|
283
|
+
return () => listeners.delete(listener);
|
|
284
|
+
},
|
|
285
|
+
add(ids, instruction, input = 'text') {
|
|
286
|
+
instructionText(instruction);
|
|
287
|
+
const document = getDocument();
|
|
288
|
+
const targets = [...new Set(ids)];
|
|
289
|
+
if (!targets.length || targets.some((id) => !document.byId.has(id))) {
|
|
290
|
+
fail('Select valid targets');
|
|
291
|
+
}
|
|
292
|
+
const source = format(document);
|
|
293
|
+
const used = idsOf(data);
|
|
294
|
+
const revision = data.revisions.find((r) => r.source === source) ?? {
|
|
295
|
+
id: freshId('revision', used),
|
|
296
|
+
source,
|
|
297
|
+
};
|
|
298
|
+
const tasks = targets.map((target) => ({
|
|
299
|
+
id: freshId('task', used),
|
|
300
|
+
target,
|
|
301
|
+
kind: document.byId.get(target).kind,
|
|
302
|
+
revision: revision.id,
|
|
303
|
+
instruction,
|
|
304
|
+
input,
|
|
305
|
+
status: 'open',
|
|
306
|
+
}));
|
|
307
|
+
commit({
|
|
308
|
+
revisions: data.revisions.includes(revision)
|
|
309
|
+
? data.revisions
|
|
310
|
+
: [...data.revisions, revision],
|
|
311
|
+
tasks: [...data.tasks, ...tasks],
|
|
312
|
+
events: [
|
|
313
|
+
...data.events,
|
|
314
|
+
...tasks.map((task) => ({ ...task, action: 'create' })),
|
|
315
|
+
],
|
|
316
|
+
});
|
|
317
|
+
return clone(tasks);
|
|
318
|
+
},
|
|
319
|
+
update(id, patch) {
|
|
320
|
+
if (
|
|
321
|
+
Object.keys(patch).some(
|
|
322
|
+
(key) => !['instruction', 'status'].includes(key)
|
|
323
|
+
)
|
|
324
|
+
) {
|
|
325
|
+
fail('Invalid task field');
|
|
326
|
+
}
|
|
327
|
+
const next = { ...taskFor(id), ...patch };
|
|
328
|
+
commit({
|
|
329
|
+
...data,
|
|
330
|
+
tasks: data.tasks.map((t) => (t.id === id ? next : t)),
|
|
331
|
+
events: [...data.events, { ...next, action: 'update' }],
|
|
332
|
+
});
|
|
333
|
+
},
|
|
334
|
+
remove(id) {
|
|
335
|
+
const task = taskFor(id);
|
|
336
|
+
commit({
|
|
337
|
+
...data,
|
|
338
|
+
tasks: data.tasks.filter((t) => t.id !== id),
|
|
339
|
+
events: [...data.events, { ...task, action: 'remove' }],
|
|
340
|
+
});
|
|
341
|
+
},
|
|
342
|
+
state(id) {
|
|
343
|
+
const task = taskFor(id);
|
|
344
|
+
const document = getDocument();
|
|
345
|
+
if (!document.byId.has(task.target)) {
|
|
346
|
+
return 'missing';
|
|
347
|
+
}
|
|
348
|
+
return format(document) ===
|
|
349
|
+
data.revisions.find((r) => r.id === task.revision).source
|
|
350
|
+
? 'current'
|
|
351
|
+
: 'outdated';
|
|
352
|
+
},
|
|
353
|
+
import(text) {
|
|
354
|
+
const next = readComments(text);
|
|
355
|
+
data = next;
|
|
356
|
+
listeners.forEach((listener) => listener());
|
|
357
|
+
},
|
|
358
|
+
};
|
|
359
|
+
}
|