mce 0.18.0 → 0.18.2

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/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export * from './mixin';
16
16
  export * from './mixins';
17
17
  export * from './plugin';
18
18
  export * from './plugins';
19
+ export * from './scene';
19
20
  export * from './types';
20
21
  export type { Camera2D, Element2D, Engine } from 'modern-canvas';
21
22
  export type { Document, Element, NormalizedElement } from 'modern-idoc';
package/dist/index.js CHANGED
@@ -393,10 +393,10 @@ var _0_config_default = defineMixin((editor) => {
393
393
  });
394
394
  onChange(async (files) => {
395
395
  const file = files?.[0];
396
- if (file) {
396
+ if (file) try {
397
397
  config.value = JSON.parse(await file.text());
398
398
  resolve(config.value);
399
- }
399
+ } catch {}
400
400
  });
401
401
  open();
402
402
  });
@@ -858,7 +858,7 @@ function signedArea(data, start, end, dim) {
858
858
  return sum;
859
859
  }
860
860
  //#endregion
861
- //#region ../../node_modules/.pnpm/modern-path2d@1.5.6/node_modules/modern-path2d/dist/index.mjs
861
+ //#region ../../node_modules/.pnpm/modern-path2d@1.6.0/node_modules/modern-path2d/dist/index.mjs
862
862
  function drawPoint(ctx, x, y, options = {}) {
863
863
  const { radius = 1 } = options;
864
864
  ctx.moveTo(x, y);
@@ -1027,9 +1027,7 @@ var Vector2 = class Vector2 {
1027
1027
  x: 0,
1028
1028
  y: 0
1029
1029
  }) {
1030
- const x = sx < 0 ? origin.x - this._x + origin.x : this._x;
1031
- const y = sy < 0 ? origin.y - this._y + origin.y : this._y;
1032
- return this.set(x * Math.abs(sx), y * Math.abs(sy));
1030
+ return this.set(origin.x + (this._x - origin.x) * sx, origin.y + (this._y - origin.y) * sy);
1033
1031
  }
1034
1032
  skew(ax, ay = 0, origin = {
1035
1033
  x: 0,
@@ -2342,8 +2340,11 @@ function getReflection(a, b) {
2342
2340
  function svgPathCommandsAddToPath2D(commands, path) {
2343
2341
  const current = new Vector2();
2344
2342
  const control = new Vector2();
2343
+ let prevType = "";
2345
2344
  for (let i = 0, l = commands.length; i < l; i++) {
2346
2345
  const cmd = commands[i];
2346
+ if ((cmd.type === "s" || cmd.type === "S") && !"CcSs".includes(prevType)) control.copyFrom(current);
2347
+ else if ((cmd.type === "t" || cmd.type === "T") && !"QqTt".includes(prevType)) control.copyFrom(current);
2347
2348
  if (cmd.type === "m" || cmd.type === "M") {
2348
2349
  if (cmd.type === "m") current.add(cmd);
2349
2350
  else current.copyFrom(cmd);
@@ -2424,6 +2425,7 @@ function svgPathCommandsAddToPath2D(commands, path) {
2424
2425
  if (path.startPoint) current.copyFrom(path.startPoint);
2425
2426
  path.closePath();
2426
2427
  } else console.warn("Unsupported commands", cmd);
2428
+ prevType = cmd.type;
2427
2429
  }
2428
2430
  }
2429
2431
  function svgPathCommandsToData(commands) {
@@ -3194,8 +3196,11 @@ function eigenDecomposition(A, B, C) {
3194
3196
  rt1 = .5 * (sm + rt);
3195
3197
  t = 1 / rt1;
3196
3198
  rt2 = A * t * C - B * t * B;
3197
- } else if (sm < 0) rt2 = .5 * (sm - rt);
3198
- else {
3199
+ } else if (sm < 0) {
3200
+ rt2 = .5 * (sm - rt);
3201
+ t = 1 / rt2;
3202
+ rt1 = A * t * C - B * t * B;
3203
+ } else {
3199
3204
  rt1 = .5 * rt;
3200
3205
  rt2 = -.5 * rt;
3201
3206
  }
@@ -3339,17 +3344,19 @@ var CompositeCurve = class CompositeCurve extends Curve {
3339
3344
  getPoint(t, output = new Vector2()) {
3340
3345
  const d = t * this.getLength();
3341
3346
  const lengths = this.getLengths();
3342
- let i = 0;
3343
- while (i < lengths.length) {
3344
- if (lengths[i] >= d) {
3345
- const diff = lengths[i] - d;
3346
- const curve = this.curves[i];
3347
- const length = curve.getLength();
3348
- return curve.getPointAt(length === 0 ? 0 : 1 - diff / length, output);
3349
- }
3350
- i++;
3351
- }
3352
- return output;
3347
+ const n = lengths.length;
3348
+ if (n === 0) return output;
3349
+ let lo = 0;
3350
+ let hi = n - 1;
3351
+ while (lo < hi) {
3352
+ const mid = lo + hi >>> 1;
3353
+ if (lengths[mid] < d) lo = mid + 1;
3354
+ else hi = mid;
3355
+ }
3356
+ const diff = lengths[lo] - d;
3357
+ const curve = this.curves[lo];
3358
+ const length = curve.getLength();
3359
+ return curve.getPointAt(length === 0 ? 0 : 1 - diff / length, output);
3353
3360
  }
3354
3361
  getLengths() {
3355
3362
  if (this._lengths.length !== this.curves.length) this.updateLengths();
@@ -3467,6 +3474,11 @@ var CubicBezierCurve = class CubicBezierCurve extends Curve {
3467
3474
  ];
3468
3475
  }
3469
3476
  _solveQuadratic(a, b, c) {
3477
+ if (Math.abs(a) < 1e-12) {
3478
+ if (Math.abs(b) < 1e-12) return [];
3479
+ const t = -c / b;
3480
+ return t >= 0 && t <= 1 ? [t] : [];
3481
+ }
3470
3482
  const discriminant = b * b - 4 * a * c;
3471
3483
  if (discriminant < 0) return [];
3472
3484
  const sqrtDiscriminant = Math.sqrt(discriminant);
@@ -3474,26 +3486,21 @@ var CubicBezierCurve = class CubicBezierCurve extends Curve {
3474
3486
  }
3475
3487
  getMinMax(min = Vector2.MAX, max = Vector2.MIN) {
3476
3488
  const { p1, cp1, cp2, p2 } = this;
3477
- const dxRoots = this._solveQuadratic(3 * (cp1.x - p1.x), 6 * (cp2.x - cp1.x), 3 * (p2.x - cp2.x));
3478
- const dyRoots = this._solveQuadratic(3 * (cp1.y - p1.y), 6 * (cp2.y - cp1.y), 3 * (p2.y - cp2.y));
3489
+ const dxRoots = this._solveQuadratic(3 * (-p1.x + 3 * cp1.x - 3 * cp2.x + p2.x), 6 * (p1.x - 2 * cp1.x + cp2.x), 3 * (cp1.x - p1.x));
3490
+ const dyRoots = this._solveQuadratic(3 * (-p1.y + 3 * cp1.y - 3 * cp2.y + p2.y), 6 * (p1.y - 2 * cp1.y + cp2.y), 3 * (cp1.y - p1.y));
3479
3491
  const tValues = [
3480
3492
  0,
3481
3493
  1,
3482
3494
  ...dxRoots,
3483
3495
  ...dyRoots
3484
3496
  ];
3485
- const samplePoints = (tValues2, precision) => {
3486
- for (const t of tValues2) for (let i = 0; i <= precision; i++) {
3487
- const delta = i / precision - .5;
3488
- const refinedT = Math.min(1, Math.max(0, t + delta));
3489
- const point = this.getPoint(refinedT);
3490
- min.x = Math.min(min.x, point.x);
3491
- min.y = Math.min(min.y, point.y);
3492
- max.x = Math.max(max.x, point.x);
3493
- max.y = Math.max(max.y, point.y);
3494
- }
3495
- };
3496
- samplePoints(tValues, 10);
3497
+ for (const t of tValues) {
3498
+ const point = this.getPoint(t);
3499
+ min.x = Math.min(min.x, point.x);
3500
+ min.y = Math.min(min.y, point.y);
3501
+ max.x = Math.max(max.x, point.x);
3502
+ max.y = Math.max(max.y, point.y);
3503
+ }
3497
3504
  return {
3498
3505
  min: min.finite(),
3499
3506
  max: max.finite()
@@ -3539,7 +3546,7 @@ var EllipseCurve = class extends RoundCurve {
3539
3546
  return this;
3540
3547
  }
3541
3548
  };
3542
- var PloygonCurve = class extends CompositeCurve {};
3549
+ var PolygonCurve = class extends CompositeCurve {};
3543
3550
  var QuadraticBezierCurve = class QuadraticBezierCurve extends Curve {
3544
3551
  constructor(p1 = new Vector2(), cp = new Vector2(), p2 = new Vector2()) {
3545
3552
  super();
@@ -3567,14 +3574,22 @@ var QuadraticBezierCurve = class QuadraticBezierCurve extends Curve {
3567
3574
  }
3568
3575
  getMinMax(min = Vector2.MAX, max = Vector2.MIN) {
3569
3576
  const { p1, cp, p2 } = this;
3570
- const x1 = .5 * (p1.x + cp.x);
3571
- const y1 = .5 * (p1.y + cp.y);
3572
- const x2 = .5 * (p1.x + p2.x);
3573
- const y2 = .5 * (p1.y + p2.y);
3574
- min.x = Math.min(min.x, p1.x, p2.x, x1, x2);
3575
- min.y = Math.min(min.y, p1.y, p2.y, y1, y2);
3576
- max.x = Math.max(max.x, p1.x, p2.x, x1, x2);
3577
- max.y = Math.max(max.y, p1.y, p2.y, y1, y2);
3577
+ const extrema = (a, b, c) => {
3578
+ const denom = a - 2 * b + c;
3579
+ if (Math.abs(denom) < 1e-12) return null;
3580
+ const t = (a - b) / denom;
3581
+ return t > 0 && t < 1 ? t : null;
3582
+ };
3583
+ const tx = extrema(p1.x, cp.x, p2.x);
3584
+ const ty = extrema(p1.y, cp.y, p2.y);
3585
+ const xs = [p1.x, p2.x];
3586
+ const ys = [p1.y, p2.y];
3587
+ if (tx !== null) xs.push(quadraticBezier(tx, p1.x, cp.x, p2.x));
3588
+ if (ty !== null) ys.push(quadraticBezier(ty, p1.y, cp.y, p2.y));
3589
+ min.x = Math.min(min.x, ...xs);
3590
+ min.y = Math.min(min.y, ...ys);
3591
+ max.x = Math.max(max.x, ...xs);
3592
+ max.y = Math.max(max.y, ...ys);
3578
3593
  return {
3579
3594
  min: min.finite(),
3580
3595
  max: max.finite()
@@ -3608,7 +3623,7 @@ var QuadraticBezierCurve = class QuadraticBezierCurve extends Curve {
3608
3623
  return this;
3609
3624
  }
3610
3625
  };
3611
- var RectangleCurve = class extends PloygonCurve {
3626
+ var RectangleCurve = class extends PolygonCurve {
3612
3627
  constructor(x = 0, y = 0, width = 0, height = 0) {
3613
3628
  super();
3614
3629
  this.x = x;
@@ -3950,16 +3965,11 @@ var Path2D = class Path2D extends CompositeCurve {
3950
3965
  return this;
3951
3966
  }
3952
3967
  moveTo(x, y) {
3953
- if (!this.currentCurve.currentPoint?.equals({
3954
- x,
3955
- y
3956
- })) {
3957
- if (this.currentCurve.curves.length) {
3958
- this.currentCurve = new CurvePath();
3959
- this.curves.push(this.currentCurve);
3960
- }
3961
- this.currentCurve.moveTo(x, y);
3968
+ if (this.currentCurve.curves.length) {
3969
+ this.currentCurve = new CurvePath();
3970
+ this.curves.push(this.currentCurve);
3962
3971
  }
3972
+ this.currentCurve.moveTo(x, y);
3963
3973
  return this;
3964
3974
  }
3965
3975
  lineTo(x, y) {
@@ -4366,6 +4376,8 @@ function storeState(idbPersistence, forceStore = true) {
4366
4376
  });
4367
4377
  }
4368
4378
  var IndexeddbProvider = class extends Observable {
4379
+ name;
4380
+ doc;
4369
4381
  db = null;
4370
4382
  synced = false;
4371
4383
  whenSynced;
@@ -4383,7 +4395,10 @@ var IndexeddbProvider = class extends Observable {
4383
4395
  this._db = openDB(name, (db) => {
4384
4396
  createStores(db, [["updates", { autoIncrement: true }], ["custom"]]);
4385
4397
  });
4386
- this.whenSynced = new Promise((resolve) => this.on("synced", () => resolve(this)));
4398
+ this.whenSynced = new Promise((resolve, reject) => {
4399
+ this.on("synced", () => resolve(this));
4400
+ this._db.catch(reject);
4401
+ });
4387
4402
  this._db.then((db) => {
4388
4403
  this.db = db;
4389
4404
  fetchUpdates(this, (updatesStore) => {
@@ -4393,7 +4408,7 @@ var IndexeddbProvider = class extends Observable {
4393
4408
  this.synced = true;
4394
4409
  this.emit("synced", this);
4395
4410
  });
4396
- });
4411
+ }).catch(() => {});
4397
4412
  this._storeUpdate = (update, origin) => {
4398
4413
  if (this.db && origin !== this) {
4399
4414
  const [updatesStore] = transact(this.db, [updatesStoreName]);
@@ -4447,6 +4462,7 @@ var IndexeddbProvider = class extends Observable {
4447
4462
  //#endregion
4448
4463
  //#region src/crdt/YDoc.ts
4449
4464
  var YDoc = class extends Observable {
4465
+ id;
4450
4466
  _transacting;
4451
4467
  _yDoc;
4452
4468
  _yProps;
@@ -6701,7 +6717,11 @@ var clipboard_default = definePlugin(() => {
6701
6717
  },
6702
6718
  load: async (doc) => {
6703
6719
  const mce = doc.querySelector("mce-clipboard");
6704
- if (mce) return JSON.parse(mce.textContent);
6720
+ if (mce) try {
6721
+ return JSON.parse(mce.textContent);
6722
+ } catch {
6723
+ return [];
6724
+ }
6705
6725
  return [];
6706
6726
  }
6707
6727
  }]
@@ -7932,7 +7952,9 @@ var frame_default = definePlugin((editor) => {
7932
7952
  }],
7933
7953
  setup: () => {
7934
7954
  const { on, off, inEditorIs, snapshot, captureFrameScreenshot, frames, frameThumbs } = editor;
7955
+ let cleanupDoc = null;
7935
7956
  function onSetDoc(doc) {
7957
+ cleanupDoc?.();
7936
7958
  if (config.value.thumbnail) snapshot();
7937
7959
  function onAddChild(node, _newIndex) {
7938
7960
  if (config.value.thumbnail && inEditorIs(node, "Frame")) {
@@ -7951,12 +7973,17 @@ var frame_default = definePlugin((editor) => {
7951
7973
  }
7952
7974
  doc.on("addChild", onAddChild);
7953
7975
  doc.on("removeChild", onRemoveChild);
7976
+ cleanupDoc = () => {
7977
+ doc.off("addChild", onAddChild);
7978
+ doc.off("removeChild", onRemoveChild);
7979
+ };
7954
7980
  }
7955
7981
  onBeforeMount(() => {
7956
7982
  on("docSet", onSetDoc);
7957
7983
  });
7958
7984
  onScopeDispose(() => {
7959
7985
  off("docSet", onSetDoc);
7986
+ cleanupDoc?.();
7960
7987
  });
7961
7988
  }
7962
7989
  };
@@ -8062,8 +8089,8 @@ var html_default = definePlugin((editor) => {
8062
8089
  const dom = new DOMParser().parseFromString(await source.text(), "text/html");
8063
8090
  try {
8064
8091
  return await load(dom);
8065
- } catch (_err) {
8066
- console.log(dom);
8092
+ } catch (err) {
8093
+ console.error(err);
8067
8094
  return [];
8068
8095
  }
8069
8096
  }
@@ -9554,42 +9581,46 @@ var pen_default = definePlugin((editor) => {
9554
9581
  currentPath.currentCurve.addCurve(currentLine);
9555
9582
  update();
9556
9583
  }
9557
- } else {
9558
- el = addElement({
9559
- name: "pen",
9560
- style: {
9561
- width: 1,
9562
- height: 1
9563
- },
9564
- outline: {
9565
- color: "#d9d9d9",
9566
- width: 5,
9567
- lineCap: "round",
9568
- lineJoin: "round"
9569
- },
9570
- meta: { inPptIs: "Shape" }
9571
- }, { position: start });
9572
- currentPath = new Path2D();
9573
- currentLine = new LineCurve(new Vector2(start.x, start.y), new Vector2(start.x, start.y));
9574
- currentPath.currentCurve.addCurve(currentLine);
9575
- update();
9576
- const onMove = () => {
9577
- const move = getGlobalPointer();
9578
- if (currentLine && move) {
9579
- currentLine.p2.x = move.x;
9580
- currentLine.p2.y = move.y;
9581
- update();
9582
- }
9583
- };
9584
- renderEngine.value.on("pointermove", onMove);
9585
- const stop = watch([state, activeTool], () => {
9586
- renderEngine.value.off("pointermove", onMove);
9587
- stop();
9588
- el = void 0;
9589
- currentPath = void 0;
9590
- currentLine = void 0;
9591
- });
9584
+ return;
9592
9585
  }
9586
+ el = addElement({
9587
+ name: "pen",
9588
+ style: {
9589
+ width: 1,
9590
+ height: 1
9591
+ },
9592
+ outline: {
9593
+ color: "#d9d9d9",
9594
+ width: 5,
9595
+ lineCap: "round",
9596
+ lineJoin: "round"
9597
+ },
9598
+ meta: { inPptIs: "Shape" }
9599
+ }, { position: start });
9600
+ currentPath = new Path2D();
9601
+ currentLine = new LineCurve(new Vector2(start.x, start.y), new Vector2(start.x, start.y));
9602
+ currentPath.currentCurve.addCurve(currentLine);
9603
+ update();
9604
+ const onMove = () => {
9605
+ const move = getGlobalPointer();
9606
+ if (currentLine && move) {
9607
+ currentLine.p2.x = move.x;
9608
+ currentLine.p2.y = move.y;
9609
+ update();
9610
+ }
9611
+ };
9612
+ renderEngine.value.on("pointermove", onMove);
9613
+ let stopWatch;
9614
+ const cleanup = () => {
9615
+ renderEngine.value.off("pointermove", onMove);
9616
+ stopWatch?.();
9617
+ stopWatch = void 0;
9618
+ el = void 0;
9619
+ currentPath = void 0;
9620
+ currentLine = void 0;
9621
+ };
9622
+ stopWatch = watch([state, activeTool], cleanup);
9623
+ return { end: cleanup };
9593
9624
  }
9594
9625
  }, {
9595
9626
  name: "pencil",
@@ -14828,12 +14859,12 @@ var TextEditor_default = /* @__PURE__ */ defineComponent({
14828
14859
  }
14829
14860
  });
14830
14861
  //#endregion
14831
- //#region \0@oxc-project+runtime@0.128.0/helpers/decorateMetadata.js
14862
+ //#region \0@oxc-project+runtime@0.130.0/helpers/decorateMetadata.js
14832
14863
  function __decorateMetadata(k, v) {
14833
14864
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
14834
14865
  }
14835
14866
  //#endregion
14836
- //#region \0@oxc-project+runtime@0.128.0/helpers/decorate.js
14867
+ //#region \0@oxc-project+runtime@0.130.0/helpers/decorate.js
14837
14868
  function __decorate(decorators, target, key, desc) {
14838
14869
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
14839
14870
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -15221,7 +15252,10 @@ var TextEditor = class extends HTMLElement {
15221
15252
  }
15222
15253
  _findNearest(options) {
15223
15254
  const isVertical = this.text.isVertical;
15224
- const { x, y, xWeight = 1, yWeight = 1 } = options;
15255
+ const { boundingBox } = this.text;
15256
+ const { xWeight = 1, yWeight = 1 } = options;
15257
+ const x = options.x + boundingBox.left;
15258
+ const y = options.y + boundingBox.top;
15225
15259
  const char = this._chars.reduce((prev, current, index) => {
15226
15260
  const diff = Math.abs(current.left + current.width / 2 - x) * xWeight + Math.abs(current.top + current.height / 2 - y) * yWeight;
15227
15261
  if (diff < prev.diff) return {
@@ -17041,4 +17075,4 @@ var Dialog_default = /* @__PURE__ */ defineComponent({
17041
17075
  }
17042
17076
  });
17043
17077
  //#endregion
17044
- export { Cropper_default as Cropper, Dialog_default as Dialog, Editor, Layers_default as EditorLayers, EditorLayout_default as EditorLayout, LayoutItem_default as EditorLayoutItem, Toolbelt_default as EditorToolbelt, Menu_default as Menu, Ruler_default as Ruler, Scrollbar_default as Scrollbar, Transform_default as Transform, defineMixin, definePlugin, mixins, plugins, useEditor };
17078
+ export { Cropper_default as Cropper, Dialog_default as Dialog, Doc, Editor, Layers_default as EditorLayers, EditorLayout_default as EditorLayout, LayoutItem_default as EditorLayoutItem, Toolbelt_default as EditorToolbelt, Menu_default as Menu, Ruler_default as Ruler, Scrollbar_default as Scrollbar, Transform_default as Transform, defineMixin, definePlugin, mixins, plugins, useEditor };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mce",
3
3
  "type": "module",
4
- "version": "0.18.0",
4
+ "version": "0.18.2",
5
5
  "description": "A headless infinite canvas editor framework built on WebGL rendering, supports exporting to image, video, and PPT. Only the ESM.",
6
6
  "author": "wxm",
7
7
  "license": "MIT",
@@ -60,10 +60,10 @@
60
60
  "@vueuse/core": "^14.3.0",
61
61
  "diff": "^9.0.0",
62
62
  "lodash-es": "^4.18.1",
63
- "modern-canvas": "^0.17.1",
63
+ "modern-canvas": "^0.17.2",
64
64
  "modern-font": "^0.5.0",
65
- "modern-idoc": "^0.11.3",
66
- "modern-text": "^1.11.0",
65
+ "modern-idoc": "^0.11.4",
66
+ "modern-text": "^1.11.1",
67
67
  "y-protocols": "^1.0.7",
68
68
  "yjs": "^13.6.30"
69
69
  },
@@ -86,7 +86,7 @@
86
86
  },
87
87
  "devDependencies": {
88
88
  "@types/lodash-es": "^4.17.12",
89
- "@vitejs/plugin-vue": "^6.0.6",
89
+ "@vitejs/plugin-vue": "^6.0.7",
90
90
  "jiti": "^2.7.0",
91
91
  "sass-embedded": "^1.99.0",
92
92
  "typedoc": "^0.28.19",