@pixerate/editor 0.278.0 → 0.279.1
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/dist/animation-editor/index.cjs +59 -59
- package/dist/animation-editor/index.js +3 -3
- package/dist/canvas/editorDocument.d.cts +53 -0
- package/dist/canvas/editorDocument.d.ts +53 -0
- package/dist/canvas/index.cjs +169 -22
- package/dist/canvas/index.js +148 -1
- package/dist/chart/index.cjs +1 -1
- package/dist/chart/index.js +1 -1
- package/dist/{chunk-MITQGHN3.cjs → chunk-4BMRQSPB.cjs} +15 -1
- package/dist/{chunk-T2HUQBRC.cjs → chunk-DK33HLA2.cjs} +331 -10
- package/dist/{chunk-DWPFGWW5.js → chunk-FKO3CY2C.js} +1 -1
- package/dist/{chunk-N4MCWQZ7.js → chunk-HN57I7KT.js} +1 -1
- package/dist/{chunk-4NZTK7LM.cjs → chunk-K4ANWBF7.cjs} +35 -35
- package/dist/{chunk-JV7DKLMX.js → chunk-KBH6CM3U.js} +326 -5
- package/dist/{chunk-VNF7A2JA.cjs → chunk-MNHP3AQT.cjs} +23 -23
- package/dist/{chunk-FNIWHSLI.js → chunk-NJDEKU5D.js} +1 -1
- package/dist/{chunk-CTHWXW5M.js → chunk-V6LQND7D.js} +14 -0
- package/dist/{chunk-QBZEC2JJ.cjs → chunk-WCC4NXWY.cjs} +86 -86
- package/dist/{chunk-PEQWARCP.cjs → chunk-WZYRYYXN.cjs} +29 -25
- package/dist/{chunk-HHX7ST6Z.js → chunk-YOM76H2V.js} +7 -3
- package/dist/image-editor/index.cjs +783 -783
- package/dist/image-editor/index.js +4 -4
- package/dist/paint/index.cjs +2 -2
- package/dist/paint/index.js +1 -1
- package/dist/paint/wet/gpu.d.cts +2 -0
- package/dist/paint/wet/gpu.d.ts +2 -0
- package/dist/paint-editor/index.cjs +128 -128
- package/dist/paint-editor/index.js +3 -3
- package/dist/persistence/autosave.d.cts +15 -1
- package/dist/persistence/autosave.d.ts +15 -1
- package/dist/persistence/editorAutosave.d.cts +12 -1
- package/dist/persistence/editorAutosave.d.ts +12 -1
- package/dist/persistence/index.cjs +6 -2
- package/dist/persistence/index.d.cts +1 -0
- package/dist/persistence/index.d.ts +1 -0
- package/dist/persistence/index.js +5 -1
- package/dist/persistence/remoteSync.d.cts +152 -0
- package/dist/persistence/remoteSync.d.ts +152 -0
- package/dist/vector/index.cjs +997 -997
- package/dist/vector/index.js +4 -4
- package/package.json +1 -1
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
var
|
|
21
|
+
var _chunkWZYRYYXNcjs = require('./chunk-WZYRYYXN.cjs');
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
|
|
@@ -37,7 +37,7 @@ var lerpP = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
|
|
|
37
37
|
var toP = (v) => [v.x, v.y];
|
|
38
38
|
var mapP = (m, p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]];
|
|
39
39
|
var curvesOf = (c) => Math.max(0, (c.pts.length - 1) / 3) | 0;
|
|
40
|
-
function subpathToMorphContour(sp, m =
|
|
40
|
+
function subpathToMorphContour(sp, m = _chunkWZYRYYXNcjs.IDENTITY_MATRIX) {
|
|
41
41
|
const n = sp.anchors.length;
|
|
42
42
|
if (!n) return null;
|
|
43
43
|
const first = mapP(m, toP(sp.anchors[0]));
|
|
@@ -67,8 +67,8 @@ function isStraight(p0, p1, p2, p3) {
|
|
|
67
67
|
const u2 = along(p2);
|
|
68
68
|
return off(p1) < tol && off(p2) < tol && u1 >= -1e-9 && u1 <= 1 + 1e-9 && u2 >= -1e-9 && u2 <= 1 + 1e-9;
|
|
69
69
|
}
|
|
70
|
-
function morphContourToSubpath(c, m =
|
|
71
|
-
const pts =
|
|
70
|
+
function morphContourToSubpath(c, m = _chunkWZYRYYXNcjs.IDENTITY_MATRIX, closed = c.closed) {
|
|
71
|
+
const pts = _chunkWZYRYYXNcjs.isIdentityMatrix.call(void 0, m) ? c.pts : c.pts.map((p) => mapP(m, p));
|
|
72
72
|
const curves = curvesOf(c);
|
|
73
73
|
const dropLast = closed && c.closed && curves > 0;
|
|
74
74
|
const count = dropLast ? curves : curves + 1;
|
|
@@ -84,7 +84,7 @@ function morphContourToSubpath(c, m = _chunkPEQWARCPcjs.IDENTITY_MATRIX, closed
|
|
|
84
84
|
anchors[(k + 1) % count].in = { x: p2[0], y: p2[1] };
|
|
85
85
|
}
|
|
86
86
|
for (const a of anchors) {
|
|
87
|
-
const type =
|
|
87
|
+
const type = _chunkWZYRYYXNcjs.inferAnchorType.call(void 0, a);
|
|
88
88
|
if (type !== "corner") a.type = type;
|
|
89
89
|
}
|
|
90
90
|
const isClosed = closed && curves > 0;
|
|
@@ -267,7 +267,7 @@ function bestStart(fixed, free, useShapeContext) {
|
|
|
267
267
|
return best;
|
|
268
268
|
}
|
|
269
269
|
function placeIn(c, from, to) {
|
|
270
|
-
const b =
|
|
270
|
+
const b = _chunkWZYRYYXNcjs.unionBounds.call(void 0, [boundsOfPoints(c.pts)]);
|
|
271
271
|
const p = b ? [b.x + b.width / 2, b.y + b.height / 2] : c.pts[0];
|
|
272
272
|
if (!from || !to) return p;
|
|
273
273
|
const fx = from.width > 0 ? (p[0] - from.x) / from.width : 0.5;
|
|
@@ -456,7 +456,7 @@ function interpolateVectorColor(a, b, t) {
|
|
|
456
456
|
}
|
|
457
457
|
function resampleGradientStops(g, offsets) {
|
|
458
458
|
return offsets.map((offset) => {
|
|
459
|
-
const s =
|
|
459
|
+
const s = _chunkWZYRYYXNcjs.sampleVectorGradient.call(void 0, g, offset);
|
|
460
460
|
return s.opacity < 1 ? { offset, color: s.color, opacity: Math.round(s.opacity * 1e4) / 1e4 } : { offset, color: s.color };
|
|
461
461
|
});
|
|
462
462
|
}
|
|
@@ -467,11 +467,11 @@ function lerpAngle(a, b, t) {
|
|
|
467
467
|
var lerpPoint = (a, b, t) => ({ x: lerpN(a.x, b.x, t), y: lerpN(a.y, b.y, t) });
|
|
468
468
|
var sameColourModel = (a, b) => (_nullishCoalesce(a.interpolation, () => ( "srgb"))) === (_nullishCoalesce(b.interpolation, () => ( "srgb"))) && (_nullishCoalesce(a.smoothness, () => ( 0))) === (_nullishCoalesce(b.smoothness, () => ( 0)));
|
|
469
469
|
function lerpStops(a, b, t) {
|
|
470
|
-
let stopsA =
|
|
471
|
-
let stopsB =
|
|
470
|
+
let stopsA = _chunkWZYRYYXNcjs.sortedVectorGradientStops.call(void 0, a);
|
|
471
|
+
let stopsB = _chunkWZYRYYXNcjs.sortedVectorGradientStops.call(void 0, b);
|
|
472
472
|
const resampled = stopsA.length !== stopsB.length || !sameColourModel(a, b);
|
|
473
473
|
if (resampled) {
|
|
474
|
-
const offs = [...
|
|
474
|
+
const offs = [..._chunkWZYRYYXNcjs.expandVectorGradientStops.call(void 0, a), ..._chunkWZYRYYXNcjs.expandVectorGradientStops.call(void 0, b), ...stopsA, ...stopsB].map((s) => s.offset).filter(Number.isFinite).sort((x, y) => x - y);
|
|
475
475
|
const common = offs.filter((o, i) => i === 0 || o - offs[i - 1] > 1e-4);
|
|
476
476
|
stopsA = resampleGradientStops(a, common);
|
|
477
477
|
stopsB = resampleGradientStops(b, common);
|
|
@@ -498,8 +498,8 @@ function lerpGradient(a, b, t) {
|
|
|
498
498
|
}
|
|
499
499
|
if (near.spread && near.spread !== "pad") out.spread = near.spread;
|
|
500
500
|
if (near.strokeMode && near.strokeMode !== "within") out.strokeMode = near.strokeMode;
|
|
501
|
-
const ea =
|
|
502
|
-
const eb =
|
|
501
|
+
const ea = _chunkWZYRYYXNcjs.hasExplicitGradientGeometry.call(void 0, a);
|
|
502
|
+
const eb = _chunkWZYRYYXNcjs.hasExplicitGradientGeometry.call(void 0, b);
|
|
503
503
|
if (!ea && !eb) {
|
|
504
504
|
if (a.angle !== void 0 || b.angle !== void 0 || a.type === "linear" || a.type === "reflected") {
|
|
505
505
|
const fallback = a.type === "linear" || a.type === "reflected" ? 180 : 90;
|
|
@@ -512,10 +512,10 @@ function lerpGradient(a, b, t) {
|
|
|
512
512
|
}
|
|
513
513
|
return out;
|
|
514
514
|
}
|
|
515
|
-
const pa =
|
|
516
|
-
const pb =
|
|
515
|
+
const pa = _chunkWZYRYYXNcjs.vectorGradientPoints.call(void 0, a);
|
|
516
|
+
const pb = _chunkWZYRYYXNcjs.vectorGradientPoints.call(void 0, b);
|
|
517
517
|
if (pa.units !== pb.units) {
|
|
518
|
-
const src =
|
|
518
|
+
const src = _chunkWZYRYYXNcjs.explicitVectorGradient.call(void 0, near);
|
|
519
519
|
return { ...src, stops: out.stops, ...out.interpolation ? { interpolation: out.interpolation } : {}, ...out.smoothness ? { smoothness: out.smoothness } : {} };
|
|
520
520
|
}
|
|
521
521
|
out.start = lerpPoint(pa.start, pb.start, t);
|
|
@@ -535,14 +535,14 @@ function solidLike(color, like) {
|
|
|
535
535
|
function interpolateVectorPaint(a, b, t) {
|
|
536
536
|
if (t <= 0) return a;
|
|
537
537
|
if (t >= 1) return b;
|
|
538
|
-
const pa =
|
|
539
|
-
const pb =
|
|
538
|
+
const pa = _chunkWZYRYYXNcjs.hasPaint.call(void 0, a);
|
|
539
|
+
const pb = _chunkWZYRYYXNcjs.hasPaint.call(void 0, b);
|
|
540
540
|
if (!pa && !pb) return t < 0.5 ? a : b;
|
|
541
541
|
if (typeof a === "string" && pa && typeof b === "string" && pb) return interpolateVectorColor(a, b, t);
|
|
542
542
|
if (!pa && typeof b === "string") return interpolateVectorColor("transparent", b, t);
|
|
543
543
|
if (!pb && typeof a === "string") return interpolateVectorColor(a, "transparent", t);
|
|
544
|
-
const ga = pa &&
|
|
545
|
-
const gb = pb &&
|
|
544
|
+
const ga = pa && _chunkWZYRYYXNcjs.isVectorGradient.call(void 0, a) ? a : null;
|
|
545
|
+
const gb = pb && _chunkWZYRYYXNcjs.isVectorGradient.call(void 0, b) ? b : null;
|
|
546
546
|
if (ga && gb) return _nullishCoalesce(lerpGradient(ga, gb, t), () => ( (t < 0.5 ? a : b)));
|
|
547
547
|
if (gb) return _nullishCoalesce(lerpGradient(solidLike(pa ? a : null, gb), gb, t), () => ( b));
|
|
548
548
|
if (ga) return _nullishCoalesce(lerpGradient(ga, solidLike(pb ? b : null, ga), t), () => ( a));
|
|
@@ -553,8 +553,8 @@ function interpolateWidthProfiles(a, b, t) {
|
|
|
553
553
|
const at = [..._nullishCoalesce(a, () => ( [])), ..._nullishCoalesce(b, () => ( [])), { t: 0 }, { t: 1 }].map((p) => p.t).sort((x, y) => x - y);
|
|
554
554
|
const ts = at.filter((v, i) => i === 0 || v - at[i - 1] > 1e-6);
|
|
555
555
|
return ts.map((pos) => {
|
|
556
|
-
const x =
|
|
557
|
-
const y =
|
|
556
|
+
const x = _chunkWZYRYYXNcjs.widthProfileAt.call(void 0, a, pos);
|
|
557
|
+
const y = _chunkWZYRYYXNcjs.widthProfileAt.call(void 0, b, pos);
|
|
558
558
|
return { t: pos, left: lerpN(x.left, y.left, t), right: lerpN(x.right, y.right, t) };
|
|
559
559
|
});
|
|
560
560
|
}
|
|
@@ -597,7 +597,7 @@ function reversedStyle(s) {
|
|
|
597
597
|
out.endArrow = s.startArrow;
|
|
598
598
|
out.startArrowScale = s.endArrowScale;
|
|
599
599
|
out.endArrowScale = s.startArrowScale;
|
|
600
|
-
if (_optionalChain([s, 'access', _6 => _6.strokeWidthProfile, 'optionalAccess', _7 => _7.length])) out.strokeWidthProfile =
|
|
600
|
+
if (_optionalChain([s, 'access', _6 => _6.strokeWidthProfile, 'optionalAccess', _7 => _7.length])) out.strokeWidthProfile = _chunkWZYRYYXNcjs.reverseWidthProfile.call(void 0, s.strokeWidthProfile);
|
|
601
601
|
for (const k of ["startArrow", "endArrow", "startArrowScale", "endArrowScale"]) if (out[k] === void 0) delete out[k];
|
|
602
602
|
return out;
|
|
603
603
|
}
|
|
@@ -623,19 +623,19 @@ function styleOf(n) {
|
|
|
623
623
|
return out;
|
|
624
624
|
}
|
|
625
625
|
function contoursOf(node, m) {
|
|
626
|
-
return
|
|
626
|
+
return _chunkWZYRYYXNcjs.expandVectorCorners.call(void 0, node.subpaths).map((sp) => subpathToMorphContour(sp, m)).filter((c) => !!c);
|
|
627
627
|
}
|
|
628
|
-
function preparePathMorph(a, b, options, ma =
|
|
628
|
+
function preparePathMorph(a, b, options, ma = _chunkWZYRYYXNcjs.nodeMatrix.call(void 0, a), mb = _chunkWZYRYYXNcjs.nodeMatrix.call(void 0, b)) {
|
|
629
629
|
const corr = correspondMorphContours(contoursOf(a, ma), contoursOf(b, mb), options);
|
|
630
630
|
const sim = procrustes(corr.a, corr.b);
|
|
631
631
|
const styleA = styleOf(a);
|
|
632
632
|
const styleB = corr.reversed ? reversedStyle(styleOf(b)) : styleOf(b);
|
|
633
|
-
const rigid = !
|
|
633
|
+
const rigid = !_chunkWZYRYYXNcjs.isIdentityMatrix.call(void 0, ma) || !_chunkWZYRYYXNcjs.isIdentityMatrix.call(void 0, mb);
|
|
634
634
|
const id = _nullishCoalesce(options.id, () => ( a.id));
|
|
635
635
|
return (t) => {
|
|
636
636
|
const contours = interpolateMorphContours(corr.a, corr.b, t, { linear: options.linear, rigidity: options.rigidity, similarity: sim });
|
|
637
|
-
const m = rigid ? interpolateRigidMatrix(ma, mb, t) :
|
|
638
|
-
const back = rigid ?
|
|
637
|
+
const m = rigid ? interpolateRigidMatrix(ma, mb, t) : _chunkWZYRYYXNcjs.IDENTITY_MATRIX;
|
|
638
|
+
const back = rigid ? _chunkWZYRYYXNcjs.invertMatrix.call(void 0, m) : _chunkWZYRYYXNcjs.IDENTITY_MATRIX;
|
|
639
639
|
const subpaths = contours.map((c, k) => {
|
|
640
640
|
const closed = t <= 0 ? corr.a[k].closed : t >= 1 ? corr.b[k].closed : corr.a[k].closed && corr.b[k].closed;
|
|
641
641
|
return morphContourToSubpath({ closed: corr.a[k].closed && corr.b[k].closed, pts: c.pts }, back, closed);
|
|
@@ -643,14 +643,14 @@ function preparePathMorph(a, b, options, ma = _chunkPEQWARCPcjs.nodeMatrix.call(
|
|
|
643
643
|
const node = { id, type: "path", subpaths, ...interpolateVectorStyle(styleA, styleB, t) };
|
|
644
644
|
const opacity = lerpN(_nullishCoalesce(a.opacity, () => ( 1)), _nullishCoalesce(b.opacity, () => ( 1)), t);
|
|
645
645
|
if (opacity < 1) node.opacity = opacity;
|
|
646
|
-
if (rigid && !
|
|
646
|
+
if (rigid && !_chunkWZYRYYXNcjs.isIdentityMatrix.call(void 0, m)) node.transform = m;
|
|
647
647
|
return node;
|
|
648
648
|
};
|
|
649
649
|
}
|
|
650
|
-
function flattenPaths(node, parent =
|
|
651
|
-
const m =
|
|
650
|
+
function flattenPaths(node, parent = _chunkWZYRYYXNcjs.IDENTITY_MATRIX) {
|
|
651
|
+
const m = _chunkWZYRYYXNcjs.multiplyMatrices.call(void 0, _chunkWZYRYYXNcjs.nodeMatrix.call(void 0, node), parent);
|
|
652
652
|
if (node.type === "path") return [{ path: node, m, opacity: 1 }];
|
|
653
|
-
if (node.type === "text" || node.type === "instance") return flattenPaths(
|
|
653
|
+
if (node.type === "text" || node.type === "instance") return flattenPaths(_chunkWZYRYYXNcjs.expandVectorAppearance.call(void 0, node), parent);
|
|
654
654
|
const o = _nullishCoalesce(node.opacity, () => ( 1));
|
|
655
655
|
return node.children.filter((c) => c.visible !== false).flatMap((c) => flattenPaths(c, m)).map((e) => ({ ...e, opacity: e.opacity * o }));
|
|
656
656
|
}
|
|
@@ -672,8 +672,8 @@ function createVectorMorph(a, b, options = {}) {
|
|
|
672
672
|
}
|
|
673
673
|
const pa = flattenPaths(a);
|
|
674
674
|
const pb = flattenPaths(b);
|
|
675
|
-
const ba =
|
|
676
|
-
const bb =
|
|
675
|
+
const ba = _chunkWZYRYYXNcjs.unionBounds.call(void 0, pa.map((e) => placedBounds(e.path, e.m)));
|
|
676
|
+
const bb = _chunkWZYRYYXNcjs.unionBounds.call(void 0, pb.map((e) => placedBounds(e.path, e.m)));
|
|
677
677
|
const id = _nullishCoalesce(options.id, () => ( a.id));
|
|
678
678
|
const parts = [];
|
|
679
679
|
const opacities = [];
|
|
@@ -685,10 +685,10 @@ function createVectorMorph(a, b, options = {}) {
|
|
|
685
685
|
parts.push(preparePathMorph(x.path, y.path, sub, x.m, y.m));
|
|
686
686
|
opacities.push([x.opacity, y.opacity]);
|
|
687
687
|
} else if (x) {
|
|
688
|
-
parts.push(preparePathMorph(x.path, collapsedPath(x.path, x.m, ba, bb), sub, x.m,
|
|
688
|
+
parts.push(preparePathMorph(x.path, collapsedPath(x.path, x.m, ba, bb), sub, x.m, _chunkWZYRYYXNcjs.IDENTITY_MATRIX));
|
|
689
689
|
opacities.push([x.opacity, x.opacity]);
|
|
690
690
|
} else if (y) {
|
|
691
|
-
parts.push(preparePathMorph(collapsedPath(y.path, y.m, bb, ba), y.path, sub,
|
|
691
|
+
parts.push(preparePathMorph(collapsedPath(y.path, y.m, bb, ba), y.path, sub, _chunkWZYRYYXNcjs.IDENTITY_MATRIX, y.m));
|
|
692
692
|
opacities.push([y.opacity, y.opacity]);
|
|
693
693
|
}
|
|
694
694
|
}
|
|
@@ -473,6 +473,10 @@ function createAutosaveController(options) {
|
|
|
473
473
|
return rev;
|
|
474
474
|
};
|
|
475
475
|
const docName = () => typeof options.name === "function" ? options.name() : options.name;
|
|
476
|
+
const readBaseVersion = () => {
|
|
477
|
+
const v = typeof options.baseVersion === "function" ? options.baseVersion() : options.baseVersion;
|
|
478
|
+
return typeof v === "number" && Number.isFinite(v) ? v : void 0;
|
|
479
|
+
};
|
|
476
480
|
const run = (action, rev) => {
|
|
477
481
|
if (action.type === "writeSnapshot") {
|
|
478
482
|
setInfo({ status: "saving", nextAttemptAt: null });
|
|
@@ -482,11 +486,13 @@ function createAutosaveController(options) {
|
|
|
482
486
|
try {
|
|
483
487
|
const text = serialize(getSnapshot());
|
|
484
488
|
await storage.set(key, text);
|
|
489
|
+
const baseVersion = readBaseVersion();
|
|
485
490
|
const meta = {
|
|
486
491
|
dirty: true,
|
|
487
492
|
revision: action.revision,
|
|
488
493
|
time: wallClock(),
|
|
489
|
-
name: docName()
|
|
494
|
+
name: docName(),
|
|
495
|
+
...baseVersion !== void 0 ? { baseVersion } : {}
|
|
490
496
|
};
|
|
491
497
|
await storage.set(metaKey, JSON.stringify(meta));
|
|
492
498
|
ok = true;
|
|
@@ -607,7 +613,7 @@ function createAutosaveController(options) {
|
|
|
607
613
|
tick();
|
|
608
614
|
await whenIdle();
|
|
609
615
|
},
|
|
610
|
-
async checkRecovery() {
|
|
616
|
+
async checkRecovery(checkOptions) {
|
|
611
617
|
if (policy.ownsSnapshot) return null;
|
|
612
618
|
let meta;
|
|
613
619
|
try {
|
|
@@ -618,6 +624,12 @@ function createAutosaveController(options) {
|
|
|
618
624
|
return null;
|
|
619
625
|
}
|
|
620
626
|
if (!meta || meta.dirty !== true) return null;
|
|
627
|
+
const required = checkOptions?.minimumBaseVersion ?? readBaseVersion();
|
|
628
|
+
if (required !== void 0 && (typeof meta.baseVersion === "number" ? meta.baseVersion : 0) < required) {
|
|
629
|
+
await storage.remove(metaKey).catch(() => void 0);
|
|
630
|
+
await storage.remove(key).catch(() => void 0);
|
|
631
|
+
return null;
|
|
632
|
+
}
|
|
621
633
|
const text = await storage.get(key).catch(() => null);
|
|
622
634
|
if (text === null) return null;
|
|
623
635
|
try {
|
|
@@ -726,10 +738,36 @@ function createEditorAutosave(doc, options) {
|
|
|
726
738
|
savedRevision: revisions.savedRevision
|
|
727
739
|
});
|
|
728
740
|
const unsubscribe = revisions.subscribe(() => controller.notify());
|
|
741
|
+
const retireRecovery = async () => {
|
|
742
|
+
await controller.discardRecovery();
|
|
743
|
+
controller.ready();
|
|
744
|
+
await collect();
|
|
745
|
+
};
|
|
729
746
|
return {
|
|
730
747
|
controller,
|
|
731
748
|
revisions,
|
|
732
749
|
checkRecovery: () => controller.checkRecovery(),
|
|
750
|
+
async recoverIfNewer(minimumBaseVersion) {
|
|
751
|
+
const recovery = await controller.checkRecovery(
|
|
752
|
+
minimumBaseVersion !== void 0 ? { minimumBaseVersion } : void 0
|
|
753
|
+
);
|
|
754
|
+
if (!recovery) {
|
|
755
|
+
controller.ready();
|
|
756
|
+
return null;
|
|
757
|
+
}
|
|
758
|
+
if (!recovery.snapshot) {
|
|
759
|
+
await retireRecovery();
|
|
760
|
+
return null;
|
|
761
|
+
}
|
|
762
|
+
try {
|
|
763
|
+
const report = doc.loadDocument(recovery.snapshot, { history: "reset" });
|
|
764
|
+
controller.ready();
|
|
765
|
+
return report;
|
|
766
|
+
} catch {
|
|
767
|
+
await retireRecovery();
|
|
768
|
+
return null;
|
|
769
|
+
}
|
|
770
|
+
},
|
|
733
771
|
restore(recovery) {
|
|
734
772
|
if (!recovery.snapshot) throw recovery.error ?? new Error("The recovered snapshot could not be read.");
|
|
735
773
|
const report = doc.loadDocument(recovery.snapshot, { history: "reset" });
|
|
@@ -737,9 +775,7 @@ function createEditorAutosave(doc, options) {
|
|
|
737
775
|
return report;
|
|
738
776
|
},
|
|
739
777
|
async discard() {
|
|
740
|
-
await
|
|
741
|
-
controller.ready();
|
|
742
|
-
await collect();
|
|
778
|
+
await retireRecovery();
|
|
743
779
|
},
|
|
744
780
|
async markClean() {
|
|
745
781
|
revisions.markSaved();
|
|
@@ -754,6 +790,289 @@ function createEditorAutosave(doc, options) {
|
|
|
754
790
|
};
|
|
755
791
|
}
|
|
756
792
|
|
|
793
|
+
// src/persistence/remoteSync.ts
|
|
794
|
+
var EditorRemoteConflictError = class extends Error {
|
|
795
|
+
constructor(serverVersion) {
|
|
796
|
+
super(
|
|
797
|
+
serverVersion === void 0 ? "The document changed on the server since it was loaded; local edits were not saved." : `The document is at version ${serverVersion} on the server; local edits were not saved.`
|
|
798
|
+
);
|
|
799
|
+
this.serverVersion = serverVersion;
|
|
800
|
+
this.name = "EditorRemoteConflictError";
|
|
801
|
+
}
|
|
802
|
+
serverVersion;
|
|
803
|
+
};
|
|
804
|
+
var cloneSnapshot = (value) => {
|
|
805
|
+
try {
|
|
806
|
+
return JSON.parse(JSON.stringify(value));
|
|
807
|
+
} catch {
|
|
808
|
+
return value;
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
function createEditorRemoteSync(doc, options) {
|
|
812
|
+
const debounceMs = options.debounceMs ?? 500;
|
|
813
|
+
const now = options.now ?? Date.now;
|
|
814
|
+
let localVersion = options.initialVersion ?? 0;
|
|
815
|
+
let confirmedVersion = localVersion;
|
|
816
|
+
let localSequence = 0;
|
|
817
|
+
let savedSequence = 0;
|
|
818
|
+
let baselineSnapshot = cloneSnapshot(doc.getDocument());
|
|
819
|
+
let savedSnapshot = baselineSnapshot;
|
|
820
|
+
let state = "idle";
|
|
821
|
+
let lastSavedAt = null;
|
|
822
|
+
let lastError = void 0;
|
|
823
|
+
let isApplyingRemote = false;
|
|
824
|
+
let conflictRetries = 0;
|
|
825
|
+
let destroyed = false;
|
|
826
|
+
let debounceTimer = null;
|
|
827
|
+
let inFlightPromise = null;
|
|
828
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
829
|
+
const getStatus = () => ({
|
|
830
|
+
status: state,
|
|
831
|
+
version: localVersion,
|
|
832
|
+
savedVersion: confirmedVersion,
|
|
833
|
+
dirty: localSequence > savedSequence,
|
|
834
|
+
lastSavedAt,
|
|
835
|
+
...lastError !== void 0 ? { error: lastError } : {}
|
|
836
|
+
});
|
|
837
|
+
const emit = () => {
|
|
838
|
+
const currentStatus = getStatus();
|
|
839
|
+
listeners.forEach((listener) => {
|
|
840
|
+
try {
|
|
841
|
+
listener(currentStatus);
|
|
842
|
+
} catch (error) {
|
|
843
|
+
console.error("[remoteSync] Error in status listener:", error);
|
|
844
|
+
}
|
|
845
|
+
});
|
|
846
|
+
};
|
|
847
|
+
const clearTimer = () => {
|
|
848
|
+
if (debounceTimer !== null) {
|
|
849
|
+
clearTimeout(debounceTimer);
|
|
850
|
+
debounceTimer = null;
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
const hasChanged = (current, baseline) => {
|
|
854
|
+
if (options.diff) {
|
|
855
|
+
const diff = options.diff(current, baseline);
|
|
856
|
+
const empty = options.isEmptyDiff ? options.isEmptyDiff(diff, current, baseline) : JSON.stringify(current) === JSON.stringify(baseline);
|
|
857
|
+
return { changed: !empty, diff };
|
|
858
|
+
}
|
|
859
|
+
return { changed: JSON.stringify(current) !== JSON.stringify(baseline) };
|
|
860
|
+
};
|
|
861
|
+
const suppressed = (fn) => {
|
|
862
|
+
const wasApplying = isApplyingRemote;
|
|
863
|
+
isApplyingRemote = true;
|
|
864
|
+
try {
|
|
865
|
+
return fn();
|
|
866
|
+
} finally {
|
|
867
|
+
isApplyingRemote = wasApplying;
|
|
868
|
+
}
|
|
869
|
+
};
|
|
870
|
+
const executeSave = async () => {
|
|
871
|
+
if (inFlightPromise) {
|
|
872
|
+
await inFlightPromise;
|
|
873
|
+
if (localSequence > savedSequence) {
|
|
874
|
+
return executeSave();
|
|
875
|
+
}
|
|
876
|
+
return true;
|
|
877
|
+
}
|
|
878
|
+
if (localSequence <= savedSequence) {
|
|
879
|
+
return true;
|
|
880
|
+
}
|
|
881
|
+
clearTimer();
|
|
882
|
+
const targetSequence = localSequence;
|
|
883
|
+
const snapshotToSave = cloneSnapshot(baselineSnapshot);
|
|
884
|
+
const diffToSave = options.diff ? options.diff(snapshotToSave, savedSnapshot) : void 0;
|
|
885
|
+
const versionToSave = Math.max(localVersion, confirmedVersion + 1);
|
|
886
|
+
localVersion = versionToSave;
|
|
887
|
+
state = "saving";
|
|
888
|
+
lastError = void 0;
|
|
889
|
+
emit();
|
|
890
|
+
const run = (async () => {
|
|
891
|
+
try {
|
|
892
|
+
const result = await options.save({
|
|
893
|
+
document: snapshotToSave,
|
|
894
|
+
version: versionToSave,
|
|
895
|
+
baseVersion: confirmedVersion,
|
|
896
|
+
diff: diffToSave
|
|
897
|
+
});
|
|
898
|
+
if (result && typeof result === "object" && result.conflict) {
|
|
899
|
+
const serverVersion = typeof result.version === "number" ? result.version : void 0;
|
|
900
|
+
if (!options.onConflict || conflictRetries >= (options.maxConflictRetries ?? 3)) {
|
|
901
|
+
conflictRetries = 0;
|
|
902
|
+
throw new EditorRemoteConflictError(serverVersion);
|
|
903
|
+
}
|
|
904
|
+
conflictRetries++;
|
|
905
|
+
const resolution = await options.onConflict({
|
|
906
|
+
document: snapshotToSave,
|
|
907
|
+
base: savedSnapshot,
|
|
908
|
+
serverVersion,
|
|
909
|
+
apply: (fn) => suppressed(fn)
|
|
910
|
+
});
|
|
911
|
+
confirmedVersion = resolution.serverVersion;
|
|
912
|
+
localVersion = Math.max(localVersion, confirmedVersion);
|
|
913
|
+
if (resolution.serverDocument !== void 0) savedSnapshot = cloneSnapshot(resolution.serverDocument);
|
|
914
|
+
baselineSnapshot = cloneSnapshot(doc.getDocument());
|
|
915
|
+
localSequence++;
|
|
916
|
+
state = "pending";
|
|
917
|
+
emit();
|
|
918
|
+
return true;
|
|
919
|
+
}
|
|
920
|
+
conflictRetries = 0;
|
|
921
|
+
const returnedVersion = typeof result === "number" ? result : result && typeof result === "object" && typeof result.version === "number" ? result.version : versionToSave;
|
|
922
|
+
confirmedVersion = Math.max(confirmedVersion, versionToSave, returnedVersion);
|
|
923
|
+
if (returnedVersion > localVersion) {
|
|
924
|
+
localVersion = returnedVersion;
|
|
925
|
+
}
|
|
926
|
+
savedSequence = Math.max(savedSequence, targetSequence);
|
|
927
|
+
savedSnapshot = snapshotToSave;
|
|
928
|
+
lastSavedAt = now();
|
|
929
|
+
lastError = void 0;
|
|
930
|
+
if (localSequence <= savedSequence) {
|
|
931
|
+
state = "saved";
|
|
932
|
+
try {
|
|
933
|
+
await options.autosave?.markClean();
|
|
934
|
+
} catch (error) {
|
|
935
|
+
console.error("[remoteSync] autosave.markClean failed:", error);
|
|
936
|
+
}
|
|
937
|
+
} else {
|
|
938
|
+
state = "pending";
|
|
939
|
+
}
|
|
940
|
+
options.onSaved?.({ document: snapshotToSave, version: confirmedVersion });
|
|
941
|
+
emit();
|
|
942
|
+
return true;
|
|
943
|
+
} catch (error) {
|
|
944
|
+
lastError = error;
|
|
945
|
+
state = "error";
|
|
946
|
+
options.onError?.(error);
|
|
947
|
+
emit();
|
|
948
|
+
return false;
|
|
949
|
+
}
|
|
950
|
+
})();
|
|
951
|
+
inFlightPromise = run;
|
|
952
|
+
let ok;
|
|
953
|
+
try {
|
|
954
|
+
ok = await run;
|
|
955
|
+
} finally {
|
|
956
|
+
if (inFlightPromise === run) inFlightPromise = null;
|
|
957
|
+
}
|
|
958
|
+
if (ok && localSequence > savedSequence && debounceTimer === null) {
|
|
959
|
+
return executeSave();
|
|
960
|
+
}
|
|
961
|
+
return ok;
|
|
962
|
+
};
|
|
963
|
+
const scheduleSave = () => {
|
|
964
|
+
clearTimer();
|
|
965
|
+
if (debounceMs <= 0) {
|
|
966
|
+
void executeSave();
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
debounceTimer = setTimeout(() => {
|
|
970
|
+
debounceTimer = null;
|
|
971
|
+
if (!destroyed) {
|
|
972
|
+
void executeSave();
|
|
973
|
+
}
|
|
974
|
+
}, debounceMs);
|
|
975
|
+
};
|
|
976
|
+
const notify = () => {
|
|
977
|
+
if (isApplyingRemote || destroyed) return false;
|
|
978
|
+
const current = cloneSnapshot(doc.getDocument());
|
|
979
|
+
const { changed, diff } = hasChanged(current, baselineSnapshot);
|
|
980
|
+
if (!changed) return false;
|
|
981
|
+
const previousBaseline = baselineSnapshot;
|
|
982
|
+
baselineSnapshot = current;
|
|
983
|
+
localSequence++;
|
|
984
|
+
localVersion = Math.max(localVersion, confirmedVersion) + 1;
|
|
985
|
+
if (state !== "saving") {
|
|
986
|
+
state = "pending";
|
|
987
|
+
}
|
|
988
|
+
emit();
|
|
989
|
+
options.onLocalChange?.({
|
|
990
|
+
document: current,
|
|
991
|
+
baseline: previousBaseline,
|
|
992
|
+
diff,
|
|
993
|
+
version: localVersion
|
|
994
|
+
});
|
|
995
|
+
scheduleSave();
|
|
996
|
+
return true;
|
|
997
|
+
};
|
|
998
|
+
const flush = async () => {
|
|
999
|
+
clearTimer();
|
|
1000
|
+
notify();
|
|
1001
|
+
if (localSequence <= savedSequence && !inFlightPromise) {
|
|
1002
|
+
return true;
|
|
1003
|
+
}
|
|
1004
|
+
return executeSave();
|
|
1005
|
+
};
|
|
1006
|
+
const unsubscribeDoc = doc.subscribe(() => {
|
|
1007
|
+
notify();
|
|
1008
|
+
});
|
|
1009
|
+
return {
|
|
1010
|
+
version: () => localVersion,
|
|
1011
|
+
savedVersion: () => confirmedVersion,
|
|
1012
|
+
dirty: () => localSequence > savedSequence,
|
|
1013
|
+
status: getStatus,
|
|
1014
|
+
baseline: () => baselineSnapshot,
|
|
1015
|
+
notify,
|
|
1016
|
+
applyRemote(fn, remoteVersion) {
|
|
1017
|
+
const wasApplying = isApplyingRemote;
|
|
1018
|
+
const wasDirty = localSequence > savedSequence;
|
|
1019
|
+
isApplyingRemote = true;
|
|
1020
|
+
try {
|
|
1021
|
+
const result = fn();
|
|
1022
|
+
baselineSnapshot = cloneSnapshot(doc.getDocument());
|
|
1023
|
+
if (typeof remoteVersion === "number" && Number.isFinite(remoteVersion)) {
|
|
1024
|
+
confirmedVersion = Math.max(confirmedVersion, remoteVersion);
|
|
1025
|
+
localVersion = Math.max(localVersion, remoteVersion);
|
|
1026
|
+
}
|
|
1027
|
+
if (!wasDirty) {
|
|
1028
|
+
savedSequence = localSequence;
|
|
1029
|
+
savedSnapshot = baselineSnapshot;
|
|
1030
|
+
}
|
|
1031
|
+
emit();
|
|
1032
|
+
return result;
|
|
1033
|
+
} finally {
|
|
1034
|
+
isApplyingRemote = wasApplying;
|
|
1035
|
+
}
|
|
1036
|
+
},
|
|
1037
|
+
resetBaseline(remoteVersion) {
|
|
1038
|
+
clearTimer();
|
|
1039
|
+
baselineSnapshot = cloneSnapshot(doc.getDocument());
|
|
1040
|
+
savedSnapshot = baselineSnapshot;
|
|
1041
|
+
if (typeof remoteVersion === "number" && Number.isFinite(remoteVersion)) {
|
|
1042
|
+
localVersion = remoteVersion;
|
|
1043
|
+
confirmedVersion = remoteVersion;
|
|
1044
|
+
}
|
|
1045
|
+
savedSequence = localSequence;
|
|
1046
|
+
lastError = void 0;
|
|
1047
|
+
state = "idle";
|
|
1048
|
+
emit();
|
|
1049
|
+
},
|
|
1050
|
+
flush,
|
|
1051
|
+
async whenIdle() {
|
|
1052
|
+
while (debounceTimer !== null || inFlightPromise) {
|
|
1053
|
+
if (debounceTimer !== null) {
|
|
1054
|
+
await flush();
|
|
1055
|
+
} else if (inFlightPromise) {
|
|
1056
|
+
await inFlightPromise.catch(() => false);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
},
|
|
1060
|
+
subscribe(listener) {
|
|
1061
|
+
listeners.add(listener);
|
|
1062
|
+
listener(getStatus());
|
|
1063
|
+
return () => {
|
|
1064
|
+
listeners.delete(listener);
|
|
1065
|
+
};
|
|
1066
|
+
},
|
|
1067
|
+
destroy() {
|
|
1068
|
+
destroyed = true;
|
|
1069
|
+
clearTimer();
|
|
1070
|
+
unsubscribeDoc();
|
|
1071
|
+
listeners.clear();
|
|
1072
|
+
}
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
|
|
757
1076
|
// src/persistence/documentStore.ts
|
|
758
1077
|
function slugifyDocumentName(name) {
|
|
759
1078
|
const slug = name.normalize("NFKD").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 80);
|
|
@@ -1067,6 +1386,8 @@ export {
|
|
|
1067
1386
|
createAutosaveController,
|
|
1068
1387
|
createRevisionTracker,
|
|
1069
1388
|
createEditorAutosave,
|
|
1389
|
+
EditorRemoteConflictError,
|
|
1390
|
+
createEditorRemoteSync,
|
|
1070
1391
|
slugifyDocumentName,
|
|
1071
1392
|
createEditorDocumentStore,
|
|
1072
1393
|
createDocumentToolProvider
|