@veltdev/ace-crdt-react 1.0.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/README.md +65 -0
- package/cjs/index.js +745 -0
- package/cjs/index.js.map +1 -0
- package/esm/index.js +710 -0
- package/esm/types/ace-crdt-editor.d.ts +5 -0
- package/esm/types/ace-crdt-editor.d.ts.map +1 -0
- package/esm/types/context.d.ts +15 -0
- package/esm/types/context.d.ts.map +1 -0
- package/esm/types/index.d.ts +6 -0
- package/esm/types/index.d.ts.map +1 -0
- package/esm/types/types.d.ts +139 -0
- package/esm/types/types.d.ts.map +1 -0
- package/esm/types/use-collaboration.d.ts +3 -0
- package/esm/types/use-collaboration.d.ts.map +1 -0
- package/esm/types/utils.d.ts +6 -0
- package/esm/types/utils.d.ts.map +1 -0
- package/index.d.ts +161 -0
- package/package.json +42 -0
package/esm/index.js
ADDED
|
@@ -0,0 +1,710 @@
|
|
|
1
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
+
import { useState, useRef, useCallback, useEffect, useMemo, createContext, useContext } from 'react';
|
|
3
|
+
import { useVeltClient, useVeltInitState } from '@veltdev/react';
|
|
4
|
+
import { createCollaboration } from '@veltdev/ace-crdt';
|
|
5
|
+
export { CollaborationManager, createCollaboration, indexToPosition, normalizeText, offsetsToRange, positionToIndex, rangeToOffsets } from '@veltdev/ace-crdt';
|
|
6
|
+
import AceEditor from 'react-ace';
|
|
7
|
+
import 'ace-builds/src-noconflict/mode-javascript';
|
|
8
|
+
import 'ace-builds/src-noconflict/theme-textmate';
|
|
9
|
+
|
|
10
|
+
// POLICY: Every function/method in this file MUST be wrapped in try/catch.
|
|
11
|
+
// The package must NEVER throw an exception to the consumer's code.
|
|
12
|
+
const LOG_PREFIX = '[VeltAceCRDTReact]';
|
|
13
|
+
/**
|
|
14
|
+
* Log an error/warning — only visible when debug mode is enabled
|
|
15
|
+
* via sessionStorage.setItem('forceCrdtDebugMode', 'true').
|
|
16
|
+
*/
|
|
17
|
+
function logError(message, error) {
|
|
18
|
+
try {
|
|
19
|
+
if (typeof sessionStorage !== 'undefined' &&
|
|
20
|
+
sessionStorage.getItem('forceCrdtDebugMode')) {
|
|
21
|
+
console.warn(`${LOG_PREFIX} ${message}`, error !== undefined ? error : '');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// sessionStorage may not be available (SSR / iframe restrictions)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// POLICY: Every function/method in this file MUST be wrapped in try/catch.
|
|
30
|
+
// The package must NEVER throw an exception to the consumer's code.
|
|
31
|
+
const EMPTY_VERSIONS = [];
|
|
32
|
+
const EMPTY_SELECTIONS = [];
|
|
33
|
+
function toError(value) {
|
|
34
|
+
try {
|
|
35
|
+
return value instanceof Error ? value : new Error(String(value));
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return new Error('Unknown error');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function readPrimitives(manager) {
|
|
42
|
+
try {
|
|
43
|
+
return manager?.getCollaborationPrimitives() ?? null;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
logError('Failed to read Ace collaboration primitives', error);
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function useCollaboration(config) {
|
|
51
|
+
const { client } = useVeltClient();
|
|
52
|
+
const veltInitState = useVeltInitState();
|
|
53
|
+
const [capturedEditor, setCapturedEditor] = useState(config.editor ?? null);
|
|
54
|
+
const [manager, setManager] = useState(null);
|
|
55
|
+
const [editor, setEditor] = useState(config.editor ?? null);
|
|
56
|
+
const [primitives, setPrimitives] = useState(null);
|
|
57
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
58
|
+
const [synced, setSynced] = useState(false);
|
|
59
|
+
const [status, setStatus] = useState('connecting');
|
|
60
|
+
const [error, setError] = useState(null);
|
|
61
|
+
const [value, setValueState] = useState('');
|
|
62
|
+
const [versions, setVersions] = useState(EMPTY_VERSIONS);
|
|
63
|
+
const [remoteSelections, setRemoteSelections] = useState(EMPTY_SELECTIONS);
|
|
64
|
+
const [stats, setStats] = useState(null);
|
|
65
|
+
const configRef = useRef(config);
|
|
66
|
+
const managerRef = useRef(null);
|
|
67
|
+
const unsubscribersRef = useRef([]);
|
|
68
|
+
const activeClientRef = useRef(null);
|
|
69
|
+
const activeEditorIdRef = useRef(null);
|
|
70
|
+
const activeEditorRef = useRef(null);
|
|
71
|
+
configRef.current = config;
|
|
72
|
+
const veltClient = config.veltClient ?? client;
|
|
73
|
+
const resolvedEditor = config.editor ?? capturedEditor;
|
|
74
|
+
const enabled = config.enabled ?? true;
|
|
75
|
+
const refreshSnapshots = useCallback(() => {
|
|
76
|
+
try {
|
|
77
|
+
const current = managerRef.current;
|
|
78
|
+
if (!current)
|
|
79
|
+
return;
|
|
80
|
+
setEditor(current.getEditor());
|
|
81
|
+
setPrimitives(readPrimitives(current));
|
|
82
|
+
setValueState(current.getValue());
|
|
83
|
+
setRemoteSelections(current.getRemoteSelections());
|
|
84
|
+
setStats(current.getStats());
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
logError('Failed to refresh Ace snapshots', err);
|
|
88
|
+
}
|
|
89
|
+
}, []);
|
|
90
|
+
const cleanupActiveManager = useCallback(() => {
|
|
91
|
+
try {
|
|
92
|
+
for (const unsubscribe of unsubscribersRef.current.splice(0)) {
|
|
93
|
+
try {
|
|
94
|
+
unsubscribe();
|
|
95
|
+
}
|
|
96
|
+
catch { /* ignore */ }
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
managerRef.current?.destroy();
|
|
100
|
+
}
|
|
101
|
+
catch { /* ignore */ }
|
|
102
|
+
managerRef.current = null;
|
|
103
|
+
activeClientRef.current = null;
|
|
104
|
+
activeEditorIdRef.current = null;
|
|
105
|
+
activeEditorRef.current = null;
|
|
106
|
+
setManager(null);
|
|
107
|
+
setEditor(null);
|
|
108
|
+
setPrimitives(null);
|
|
109
|
+
setIsLoading(true);
|
|
110
|
+
setSynced(false);
|
|
111
|
+
setStatus('connecting');
|
|
112
|
+
setError(null);
|
|
113
|
+
setValueState('');
|
|
114
|
+
setVersions(EMPTY_VERSIONS);
|
|
115
|
+
setRemoteSelections(EMPTY_SELECTIONS);
|
|
116
|
+
setStats(null);
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
logError('Failed to clean up Ace collaboration manager', err);
|
|
120
|
+
}
|
|
121
|
+
}, []);
|
|
122
|
+
const editorRef = useCallback((nextEditor) => {
|
|
123
|
+
try {
|
|
124
|
+
setCapturedEditor(nextEditor ?? null);
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
logError('Failed to capture Ace editor instance', err);
|
|
128
|
+
}
|
|
129
|
+
}, []);
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
try {
|
|
132
|
+
if (enabled)
|
|
133
|
+
return;
|
|
134
|
+
cleanupActiveManager();
|
|
135
|
+
setIsLoading(false);
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
logError('Failed to disable Ace collaboration', err);
|
|
139
|
+
}
|
|
140
|
+
}, [enabled, cleanupActiveManager]);
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
try {
|
|
143
|
+
if (!enabled || !veltClient || !veltInitState || !config.editorId)
|
|
144
|
+
return;
|
|
145
|
+
if (!resolvedEditor && !config.initializeWithoutEditor)
|
|
146
|
+
return;
|
|
147
|
+
let user;
|
|
148
|
+
try {
|
|
149
|
+
user = veltClient.getUser?.();
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
user = null;
|
|
153
|
+
}
|
|
154
|
+
if (!user)
|
|
155
|
+
return;
|
|
156
|
+
if (managerRef.current
|
|
157
|
+
&& activeClientRef.current === veltClient
|
|
158
|
+
&& activeEditorIdRef.current === config.editorId
|
|
159
|
+
&& activeEditorRef.current === resolvedEditor) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
cleanupActiveManager();
|
|
163
|
+
let mounted = true;
|
|
164
|
+
const init = async () => {
|
|
165
|
+
try {
|
|
166
|
+
setIsLoading(true);
|
|
167
|
+
setError(null);
|
|
168
|
+
const latest = configRef.current;
|
|
169
|
+
const created = await createCollaboration({
|
|
170
|
+
editorId: latest.editorId,
|
|
171
|
+
veltClient,
|
|
172
|
+
editor: resolvedEditor ?? undefined,
|
|
173
|
+
initialContent: latest.initialContent,
|
|
174
|
+
cursorData: latest.cursorData,
|
|
175
|
+
rangeFactory: latest.rangeFactory,
|
|
176
|
+
debounceMs: latest.debounceMs,
|
|
177
|
+
forceResetInitialContent: latest.forceResetInitialContent,
|
|
178
|
+
onError: (err) => {
|
|
179
|
+
try {
|
|
180
|
+
if (mounted)
|
|
181
|
+
setError(err);
|
|
182
|
+
configRef.current.onError?.(err);
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
// Ignore consumer callback failures.
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
if (!mounted) {
|
|
190
|
+
try {
|
|
191
|
+
created.destroy();
|
|
192
|
+
}
|
|
193
|
+
catch { /* ignore */ }
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
managerRef.current = created;
|
|
197
|
+
activeClientRef.current = veltClient;
|
|
198
|
+
activeEditorIdRef.current = latest.editorId;
|
|
199
|
+
activeEditorRef.current = resolvedEditor ?? null;
|
|
200
|
+
setManager(created);
|
|
201
|
+
setSynced(created.synced);
|
|
202
|
+
setStatus(created.status);
|
|
203
|
+
setEditor(created.getEditor());
|
|
204
|
+
setPrimitives(readPrimitives(created));
|
|
205
|
+
setValueState(created.getValue());
|
|
206
|
+
setRemoteSelections(created.getRemoteSelections());
|
|
207
|
+
setStats(created.getStats());
|
|
208
|
+
const unsubscribeStatus = created.onStatusChange((nextStatus) => {
|
|
209
|
+
try {
|
|
210
|
+
if (!mounted)
|
|
211
|
+
return;
|
|
212
|
+
setStatus(nextStatus);
|
|
213
|
+
refreshSnapshots();
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
// Ignore listener errors.
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
const unsubscribeSynced = created.onSynced((nextSynced) => {
|
|
220
|
+
try {
|
|
221
|
+
if (!mounted)
|
|
222
|
+
return;
|
|
223
|
+
setSynced(nextSynced);
|
|
224
|
+
refreshSnapshots();
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// Ignore listener errors.
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
const unsubscribeSelections = created.onRemoteSelectionsChange((selections) => {
|
|
231
|
+
try {
|
|
232
|
+
if (!mounted)
|
|
233
|
+
return;
|
|
234
|
+
setRemoteSelections(selections);
|
|
235
|
+
setStats(created.getStats());
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
// Ignore listener errors.
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
unsubscribersRef.current = [unsubscribeStatus, unsubscribeSynced, unsubscribeSelections];
|
|
242
|
+
try {
|
|
243
|
+
const list = await created.getVersions();
|
|
244
|
+
if (mounted)
|
|
245
|
+
setVersions(list);
|
|
246
|
+
}
|
|
247
|
+
catch (err) {
|
|
248
|
+
logError('Failed to load initial Ace versions', err);
|
|
249
|
+
}
|
|
250
|
+
if (mounted) {
|
|
251
|
+
setIsLoading(false);
|
|
252
|
+
setError(null);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
catch (err) {
|
|
256
|
+
logError('Failed to initialize Ace collaboration', err);
|
|
257
|
+
if (mounted) {
|
|
258
|
+
setError(toError(err));
|
|
259
|
+
setIsLoading(false);
|
|
260
|
+
}
|
|
261
|
+
configRef.current.onError?.(toError(err));
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
void init();
|
|
265
|
+
return () => {
|
|
266
|
+
try {
|
|
267
|
+
mounted = false;
|
|
268
|
+
cleanupActiveManager();
|
|
269
|
+
}
|
|
270
|
+
catch (err) {
|
|
271
|
+
logError('Failed to clean up Ace hook effect', err);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
catch (err) {
|
|
276
|
+
logError('Error in useCollaboration effect', err);
|
|
277
|
+
}
|
|
278
|
+
}, [
|
|
279
|
+
cleanupActiveManager,
|
|
280
|
+
refreshSnapshots,
|
|
281
|
+
enabled,
|
|
282
|
+
veltClient,
|
|
283
|
+
veltInitState,
|
|
284
|
+
config.editorId,
|
|
285
|
+
config.initializeWithoutEditor,
|
|
286
|
+
resolvedEditor,
|
|
287
|
+
]);
|
|
288
|
+
const attachEditor = useCallback((nextEditor, options) => {
|
|
289
|
+
try {
|
|
290
|
+
const attached = managerRef.current?.attachEditor(nextEditor, options) ?? false;
|
|
291
|
+
if (attached) {
|
|
292
|
+
activeEditorRef.current = nextEditor ?? null;
|
|
293
|
+
setEditor(nextEditor ?? null);
|
|
294
|
+
}
|
|
295
|
+
refreshSnapshots();
|
|
296
|
+
return attached;
|
|
297
|
+
}
|
|
298
|
+
catch (err) {
|
|
299
|
+
logError('Failed to attach Ace editor', err);
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
}, [refreshSnapshots]);
|
|
303
|
+
const detachEditor = useCallback(() => {
|
|
304
|
+
try {
|
|
305
|
+
managerRef.current?.detachEditor();
|
|
306
|
+
activeEditorRef.current = null;
|
|
307
|
+
setEditor(null);
|
|
308
|
+
refreshSnapshots();
|
|
309
|
+
}
|
|
310
|
+
catch (err) {
|
|
311
|
+
logError('Failed to detach Ace editor', err);
|
|
312
|
+
}
|
|
313
|
+
}, [refreshSnapshots]);
|
|
314
|
+
const getValue = useCallback(() => {
|
|
315
|
+
try {
|
|
316
|
+
return managerRef.current?.getValue() ?? '';
|
|
317
|
+
}
|
|
318
|
+
catch (err) {
|
|
319
|
+
logError('Failed to get Ace value', err);
|
|
320
|
+
return '';
|
|
321
|
+
}
|
|
322
|
+
}, []);
|
|
323
|
+
const setValue = useCallback((nextValue, reason) => {
|
|
324
|
+
try {
|
|
325
|
+
const ok = managerRef.current?.setValue(nextValue, reason) ?? false;
|
|
326
|
+
refreshSnapshots();
|
|
327
|
+
return ok;
|
|
328
|
+
}
|
|
329
|
+
catch (err) {
|
|
330
|
+
logError('Failed to set Ace value', err);
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
}, [refreshSnapshots]);
|
|
334
|
+
const insertText = useCallback((nextValue, index) => {
|
|
335
|
+
try {
|
|
336
|
+
const ok = managerRef.current?.insertText(nextValue, index) ?? false;
|
|
337
|
+
refreshSnapshots();
|
|
338
|
+
return ok;
|
|
339
|
+
}
|
|
340
|
+
catch (err) {
|
|
341
|
+
logError('Failed to insert Ace text', err);
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
}, [refreshSnapshots]);
|
|
345
|
+
const flushEditorToStore = useCallback((reason) => {
|
|
346
|
+
try {
|
|
347
|
+
const ok = managerRef.current?.flushEditorToStore(reason) ?? false;
|
|
348
|
+
refreshSnapshots();
|
|
349
|
+
return ok;
|
|
350
|
+
}
|
|
351
|
+
catch (err) {
|
|
352
|
+
logError('Failed to flush Ace editor to Store', err);
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
}, [refreshSnapshots]);
|
|
356
|
+
const forceReset = useCallback(async (nextInitialContent) => {
|
|
357
|
+
try {
|
|
358
|
+
const ok = (await managerRef.current?.forceReset(nextInitialContent)) ?? false;
|
|
359
|
+
refreshSnapshots();
|
|
360
|
+
return ok;
|
|
361
|
+
}
|
|
362
|
+
catch (err) {
|
|
363
|
+
logError('Failed to force reset Ace content', err);
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
}, [refreshSnapshots]);
|
|
367
|
+
const publishCurrentSelection = useCallback((kind) => {
|
|
368
|
+
try {
|
|
369
|
+
const ok = managerRef.current?.publishCurrentSelection(kind) ?? false;
|
|
370
|
+
refreshSnapshots();
|
|
371
|
+
return ok;
|
|
372
|
+
}
|
|
373
|
+
catch (err) {
|
|
374
|
+
logError('Failed to publish Ace selection', err);
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
}, [refreshSnapshots]);
|
|
378
|
+
const setLocalSelection = useCallback((selection) => {
|
|
379
|
+
try {
|
|
380
|
+
const ok = managerRef.current?.setLocalSelection(selection) ?? false;
|
|
381
|
+
refreshSnapshots();
|
|
382
|
+
return ok;
|
|
383
|
+
}
|
|
384
|
+
catch (err) {
|
|
385
|
+
logError('Failed to set Ace local selection', err);
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
}, [refreshSnapshots]);
|
|
389
|
+
const addHighlight = useCallback((range, color) => {
|
|
390
|
+
try {
|
|
391
|
+
const ok = managerRef.current?.addHighlight(range, color) ?? false;
|
|
392
|
+
refreshSnapshots();
|
|
393
|
+
return ok;
|
|
394
|
+
}
|
|
395
|
+
catch (err) {
|
|
396
|
+
logError('Failed to add Ace highlight', err);
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
}, [refreshSnapshots]);
|
|
400
|
+
const renderRemoteSelections = useCallback(() => {
|
|
401
|
+
try {
|
|
402
|
+
const count = managerRef.current?.renderRemoteSelections() ?? 0;
|
|
403
|
+
refreshSnapshots();
|
|
404
|
+
return count;
|
|
405
|
+
}
|
|
406
|
+
catch (err) {
|
|
407
|
+
logError('Failed to render Ace remote selections', err);
|
|
408
|
+
return 0;
|
|
409
|
+
}
|
|
410
|
+
}, [refreshSnapshots]);
|
|
411
|
+
const clearRemoteMarkers = useCallback(() => {
|
|
412
|
+
try {
|
|
413
|
+
managerRef.current?.clearRemoteMarkers();
|
|
414
|
+
refreshSnapshots();
|
|
415
|
+
}
|
|
416
|
+
catch (err) {
|
|
417
|
+
logError('Failed to clear Ace remote markers', err);
|
|
418
|
+
}
|
|
419
|
+
}, [refreshSnapshots]);
|
|
420
|
+
const undo = useCallback(() => {
|
|
421
|
+
try {
|
|
422
|
+
const undoManager = managerRef.current?.getUndoManager();
|
|
423
|
+
if (!undoManager)
|
|
424
|
+
return false;
|
|
425
|
+
undoManager.undo();
|
|
426
|
+
managerRef.current?.flushEditorToStore('undo');
|
|
427
|
+
refreshSnapshots();
|
|
428
|
+
return true;
|
|
429
|
+
}
|
|
430
|
+
catch (err) {
|
|
431
|
+
logError('Failed to undo Ace content', err);
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
}, [refreshSnapshots]);
|
|
435
|
+
const redo = useCallback(() => {
|
|
436
|
+
try {
|
|
437
|
+
const undoManager = managerRef.current?.getUndoManager();
|
|
438
|
+
if (!undoManager)
|
|
439
|
+
return false;
|
|
440
|
+
undoManager.redo();
|
|
441
|
+
managerRef.current?.flushEditorToStore('redo');
|
|
442
|
+
refreshSnapshots();
|
|
443
|
+
return true;
|
|
444
|
+
}
|
|
445
|
+
catch (err) {
|
|
446
|
+
logError('Failed to redo Ace content', err);
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
}, [refreshSnapshots]);
|
|
450
|
+
const refreshVersions = useCallback(async () => {
|
|
451
|
+
try {
|
|
452
|
+
const list = (await managerRef.current?.getVersions()) ?? [];
|
|
453
|
+
setVersions(list);
|
|
454
|
+
return list;
|
|
455
|
+
}
|
|
456
|
+
catch (err) {
|
|
457
|
+
logError('Failed to refresh Ace versions', err);
|
|
458
|
+
return [];
|
|
459
|
+
}
|
|
460
|
+
}, []);
|
|
461
|
+
const saveVersion = useCallback(async (name) => {
|
|
462
|
+
try {
|
|
463
|
+
const id = (await managerRef.current?.saveVersion(name)) ?? '';
|
|
464
|
+
await refreshVersions();
|
|
465
|
+
refreshSnapshots();
|
|
466
|
+
return id;
|
|
467
|
+
}
|
|
468
|
+
catch (err) {
|
|
469
|
+
logError('Failed to save Ace version', err);
|
|
470
|
+
return '';
|
|
471
|
+
}
|
|
472
|
+
}, [refreshSnapshots, refreshVersions]);
|
|
473
|
+
const getVersions = useCallback(async () => {
|
|
474
|
+
try {
|
|
475
|
+
return (await managerRef.current?.getVersions()) ?? [];
|
|
476
|
+
}
|
|
477
|
+
catch (err) {
|
|
478
|
+
logError('Failed to get Ace versions', err);
|
|
479
|
+
return [];
|
|
480
|
+
}
|
|
481
|
+
}, []);
|
|
482
|
+
const getVersionById = useCallback(async (versionId) => {
|
|
483
|
+
try {
|
|
484
|
+
return (await managerRef.current?.getVersionById(versionId)) ?? null;
|
|
485
|
+
}
|
|
486
|
+
catch (err) {
|
|
487
|
+
logError('Failed to get Ace version by id', err);
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
}, []);
|
|
491
|
+
const restoreVersion = useCallback(async (versionId) => {
|
|
492
|
+
try {
|
|
493
|
+
const ok = (await managerRef.current?.restoreVersion(versionId)) ?? false;
|
|
494
|
+
await refreshVersions();
|
|
495
|
+
refreshSnapshots();
|
|
496
|
+
return ok;
|
|
497
|
+
}
|
|
498
|
+
catch (err) {
|
|
499
|
+
logError('Failed to restore Ace version', err);
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
}, [refreshSnapshots, refreshVersions]);
|
|
503
|
+
const setStateFromVersion = useCallback(async (version) => {
|
|
504
|
+
try {
|
|
505
|
+
await managerRef.current?.setStateFromVersion(version);
|
|
506
|
+
refreshSnapshots();
|
|
507
|
+
}
|
|
508
|
+
catch (err) {
|
|
509
|
+
logError('Failed to set Ace state from version', err);
|
|
510
|
+
}
|
|
511
|
+
}, [refreshSnapshots]);
|
|
512
|
+
const destroy = useCallback(() => {
|
|
513
|
+
try {
|
|
514
|
+
cleanupActiveManager();
|
|
515
|
+
setStatus('disconnected');
|
|
516
|
+
setIsLoading(false);
|
|
517
|
+
}
|
|
518
|
+
catch (err) {
|
|
519
|
+
logError('Failed to destroy Ace collaboration', err);
|
|
520
|
+
}
|
|
521
|
+
}, [cleanupActiveManager]);
|
|
522
|
+
// Stable return identity: all callbacks are memoized useCallbacks, so the
|
|
523
|
+
// object only changes when a state slice changes. This keeps context
|
|
524
|
+
// consumers (AceCrdtProvider) from re-rendering on every provider render.
|
|
525
|
+
return useMemo(() => ({
|
|
526
|
+
editorRef,
|
|
527
|
+
manager,
|
|
528
|
+
editor,
|
|
529
|
+
primitives,
|
|
530
|
+
isLoading,
|
|
531
|
+
synced,
|
|
532
|
+
isSynced: synced,
|
|
533
|
+
status,
|
|
534
|
+
error,
|
|
535
|
+
value,
|
|
536
|
+
versions,
|
|
537
|
+
remoteSelections,
|
|
538
|
+
stats,
|
|
539
|
+
refreshSnapshots,
|
|
540
|
+
attachEditor,
|
|
541
|
+
detachEditor,
|
|
542
|
+
getValue,
|
|
543
|
+
setValue,
|
|
544
|
+
insertText,
|
|
545
|
+
flushEditorToStore,
|
|
546
|
+
forceReset,
|
|
547
|
+
publishCurrentSelection,
|
|
548
|
+
setLocalSelection,
|
|
549
|
+
addHighlight,
|
|
550
|
+
renderRemoteSelections,
|
|
551
|
+
clearRemoteMarkers,
|
|
552
|
+
undo,
|
|
553
|
+
redo,
|
|
554
|
+
saveVersion,
|
|
555
|
+
getVersions,
|
|
556
|
+
getVersionById,
|
|
557
|
+
refreshVersions,
|
|
558
|
+
restoreVersion,
|
|
559
|
+
setStateFromVersion,
|
|
560
|
+
destroy,
|
|
561
|
+
}), [
|
|
562
|
+
editorRef,
|
|
563
|
+
manager,
|
|
564
|
+
editor,
|
|
565
|
+
primitives,
|
|
566
|
+
isLoading,
|
|
567
|
+
synced,
|
|
568
|
+
status,
|
|
569
|
+
error,
|
|
570
|
+
value,
|
|
571
|
+
versions,
|
|
572
|
+
remoteSelections,
|
|
573
|
+
stats,
|
|
574
|
+
refreshSnapshots,
|
|
575
|
+
attachEditor,
|
|
576
|
+
detachEditor,
|
|
577
|
+
getValue,
|
|
578
|
+
setValue,
|
|
579
|
+
insertText,
|
|
580
|
+
flushEditorToStore,
|
|
581
|
+
forceReset,
|
|
582
|
+
publishCurrentSelection,
|
|
583
|
+
setLocalSelection,
|
|
584
|
+
addHighlight,
|
|
585
|
+
renderRemoteSelections,
|
|
586
|
+
clearRemoteMarkers,
|
|
587
|
+
undo,
|
|
588
|
+
redo,
|
|
589
|
+
saveVersion,
|
|
590
|
+
getVersions,
|
|
591
|
+
getVersionById,
|
|
592
|
+
refreshVersions,
|
|
593
|
+
restoreVersion,
|
|
594
|
+
setStateFromVersion,
|
|
595
|
+
destroy,
|
|
596
|
+
]);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const CollaborationContext = createContext(null);
|
|
600
|
+
/**
|
|
601
|
+
* Context provider that runs useCollaboration once and shares the hook result
|
|
602
|
+
* with descendants. The context value is the hook's memoized return object, so
|
|
603
|
+
* consumers only re-render when collaboration state actually changes.
|
|
604
|
+
*/
|
|
605
|
+
function AceCrdtProvider({ config, children }) {
|
|
606
|
+
const value = useCollaboration(config);
|
|
607
|
+
try {
|
|
608
|
+
return (jsx(CollaborationContext.Provider, { value: value, children: typeof children === 'function' ? children(value) : children }));
|
|
609
|
+
}
|
|
610
|
+
catch (error) {
|
|
611
|
+
logError('Failed to render AceCrdtProvider', error);
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
const CollaborationProvider = AceCrdtProvider;
|
|
616
|
+
/**
|
|
617
|
+
* Read the collaboration value shared by the nearest AceCrdtProvider.
|
|
618
|
+
* Returns null (never throws) when called outside a provider.
|
|
619
|
+
*/
|
|
620
|
+
function useCollaborationContext() {
|
|
621
|
+
try {
|
|
622
|
+
return useContext(CollaborationContext);
|
|
623
|
+
}
|
|
624
|
+
catch (error) {
|
|
625
|
+
logError('Failed to read Ace CRDT context', error);
|
|
626
|
+
return null;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function AceCrdtEditor(props) {
|
|
631
|
+
const { editorId, documentId, veltClient, initialContent, forceResetInitialContent, cursorData, rangeFactory, debounceMs, enabled, onError, onStatusChange, onManagerReady, onEditorReady, onLoad, children, mode = 'javascript', theme = 'textmate', name = 'velt-ace-crdt-editor', width = '100%', height = '100%', setOptions, ...aceProps } = props;
|
|
632
|
+
const resolvedEditorId = editorId ?? documentId ?? 'velt-ace-crdt-document';
|
|
633
|
+
const value = useCollaboration({
|
|
634
|
+
editorId: resolvedEditorId,
|
|
635
|
+
veltClient,
|
|
636
|
+
initialContent,
|
|
637
|
+
forceResetInitialContent,
|
|
638
|
+
cursorData,
|
|
639
|
+
rangeFactory,
|
|
640
|
+
debounceMs,
|
|
641
|
+
enabled,
|
|
642
|
+
onError,
|
|
643
|
+
});
|
|
644
|
+
const lastStatusRef = useRef(null);
|
|
645
|
+
useEffect(() => {
|
|
646
|
+
try {
|
|
647
|
+
if (value.status !== lastStatusRef.current) {
|
|
648
|
+
lastStatusRef.current = value.status;
|
|
649
|
+
onStatusChange?.(value.status);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
catch (error) {
|
|
653
|
+
logError('Error in onStatusChange callback', error);
|
|
654
|
+
}
|
|
655
|
+
}, [value.status, onStatusChange]);
|
|
656
|
+
const notifiedManagerRef = useRef(null);
|
|
657
|
+
useEffect(() => {
|
|
658
|
+
try {
|
|
659
|
+
if (value.manager && value.manager !== notifiedManagerRef.current) {
|
|
660
|
+
notifiedManagerRef.current = value.manager;
|
|
661
|
+
onManagerReady?.(value.manager);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
catch (error) {
|
|
665
|
+
logError('Error in onManagerReady callback', error);
|
|
666
|
+
}
|
|
667
|
+
}, [value.manager, onManagerReady]);
|
|
668
|
+
// react-ace types onLoad against its own Ace.Editor; the base package works
|
|
669
|
+
// against the structural AceEditorLike. Accept unknown at the boundary and
|
|
670
|
+
// narrow once, instead of erasing the whole handler with `any`.
|
|
671
|
+
const handleLoad = (editorInstance) => {
|
|
672
|
+
try {
|
|
673
|
+
const editor = editorInstance;
|
|
674
|
+
value.editorRef(editor);
|
|
675
|
+
try {
|
|
676
|
+
onEditorReady?.(editor);
|
|
677
|
+
}
|
|
678
|
+
catch (error) {
|
|
679
|
+
logError('Error in onEditorReady callback', error);
|
|
680
|
+
}
|
|
681
|
+
try {
|
|
682
|
+
onLoad?.(editor);
|
|
683
|
+
}
|
|
684
|
+
catch (error) {
|
|
685
|
+
logError('Error in Ace onLoad callback', error);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
catch (error) {
|
|
689
|
+
logError('Failed to capture Ace editor from onLoad', error);
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
try {
|
|
693
|
+
return (jsxs(Fragment, { children: [jsx(AceEditor, { ...aceProps, mode: mode, theme: theme, name: name, width: width, height: height, defaultValue: "", onLoad: handleLoad, setOptions: {
|
|
694
|
+
fontSize: 14,
|
|
695
|
+
tabSize: 2,
|
|
696
|
+
useSoftTabs: true,
|
|
697
|
+
showPrintMargin: false,
|
|
698
|
+
highlightActiveLine: true,
|
|
699
|
+
wrap: true,
|
|
700
|
+
useWorker: false,
|
|
701
|
+
...setOptions,
|
|
702
|
+
} }), children?.(value)] }));
|
|
703
|
+
}
|
|
704
|
+
catch (error) {
|
|
705
|
+
logError('Failed to render AceCrdtEditor', error);
|
|
706
|
+
return null;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
export { AceCrdtEditor, AceCrdtProvider, CollaborationContext, CollaborationProvider, useCollaboration, useCollaborationContext };
|