@netless/forge-slide 1.1.5 → 1.2.0-beta.3

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/slide.esm.js CHANGED
@@ -58606,7 +58606,7 @@ var require_lodash = __commonJS2({
58606
58606
  result2.__values__ = wrapper.__values__;
58607
58607
  return result2;
58608
58608
  }
58609
- function chunk7(array, size2, guard) {
58609
+ function chunk8(array, size2, guard) {
58610
58610
  if (guard ? isIterateeCall(array, size2, guard) : size2 === undefined2) {
58611
58611
  size2 = 1;
58612
58612
  } else {
@@ -60478,7 +60478,7 @@ var require_lodash = __commonJS2({
60478
60478
  lodash.bindKey = bindKey;
60479
60479
  lodash.castArray = castArray;
60480
60480
  lodash.chain = chain;
60481
- lodash.chunk = chunk7;
60481
+ lodash.chunk = chunk8;
60482
60482
  lodash.compact = compact;
60483
60483
  lodash.concat = concat;
60484
60484
  lodash.cond = cond;
@@ -60956,6 +60956,7 @@ var require_lodash = __commonJS2({
60956
60956
  }
60957
60957
  });
60958
60958
  var import_paper = __toESM2(require_paper_full(), 1);
60959
+ var import_lodash = __toESM2(require_lodash(), 1);
60959
60960
  function $(e, t, u, x = (h) => h) {
60960
60961
  return e * x(0.5 - t * (0.5 - u));
60961
60962
  }
@@ -61503,7 +61504,7 @@ var CurveModel = class extends ElementModel {
61503
61504
  _defineProperty3(this, "debug", false);
61504
61505
  _defineProperty3(this, "clearLocalPointsWhenYPointsChange", false);
61505
61506
  _defineProperty3(this, "shouldUseLocalPoints", true);
61506
- _defineProperty3(this, "localPointsPick", 6);
61507
+ _defineProperty3(this, "localPointsPick", 4);
61507
61508
  if (!this.root.doc || !this.root.has("type")) {
61508
61509
  this.root.set("type", "curve");
61509
61510
  }
@@ -61515,7 +61516,6 @@ var CurveModel = class extends ElementModel {
61515
61516
  return (a2 + b2) / 2;
61516
61517
  }
61517
61518
  parsePoints(points) {
61518
- const hasRealPressure = points.some((p) => p.length >= 3 && p[2] > 0);
61519
61519
  const viewScale = this.scope.project.view.matrix.scaling.x || 1;
61520
61520
  const taper = this.strokeWidth * 5 / viewScale;
61521
61521
  const streamline = Math.min(0.7, 0.7 * viewScale);
@@ -61524,7 +61524,7 @@ var CurveModel = class extends ElementModel {
61524
61524
  smoothing: 0.7,
61525
61525
  thinning: 0.5,
61526
61526
  streamline,
61527
- simulatePressure: !hasRealPressure,
61527
+ simulatePressure: true,
61528
61528
  start: {
61529
61529
  taper,
61530
61530
  cap: true
@@ -61535,36 +61535,13 @@ var CurveModel = class extends ElementModel {
61535
61535
  }
61536
61536
  });
61537
61537
  }
61538
- isPressureValue(value) {
61539
- return typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1;
61540
- }
61541
- pointStride(points) {
61542
- if (points.length >= 3 && points.length % 3 === 0) {
61543
- let hasPressureSlot = false;
61544
- for (let i = 2; i < points.length; i += 3) {
61545
- if (!this.isPressureValue(points[i])) {
61546
- return 2;
61547
- }
61548
- hasPressureSlot = true;
61549
- }
61550
- if (hasPressureSlot) {
61551
- return 3;
61552
- }
61553
- }
61554
- return 2;
61555
- }
61556
61538
  matrixedPoints() {
61557
61539
  const points = this.localPoints.length === 0 ? this.points : this.localPoints;
61558
61540
  const matrix = new this.scope.Matrix(this.pointsMatrix);
61559
- const output = [];
61560
- const stride = this.pointStride(points);
61561
- for (let i = 0, len = points.length; i + 1 < len; i += stride) {
61562
- const p = new this.scope.Point(points[i], points[i + 1]);
61563
- const tp = p.transform(matrix);
61564
- const pressure = stride === 3 ? points[i + 2] ?? 0 : 0;
61565
- output.push([tp.x, tp.y, pressure]);
61566
- }
61567
- return output;
61541
+ return (0, import_lodash.chunk)(points, 2).filter((point) => point.length === 2).map((_ref) => {
61542
+ let [x, y] = _ref;
61543
+ return new this.scope.Point(x, y).transform(matrix);
61544
+ }).map((point) => [point.x, point.y]);
61568
61545
  }
61569
61546
  createPath(points) {
61570
61547
  const path = new this.scope.Path();
@@ -61638,12 +61615,11 @@ var CurveModel = class extends ElementModel {
61638
61615
  liveCursorPoint() {
61639
61616
  const yArray = this.root.get(ElementModel.KEYS.points);
61640
61617
  const points = yArray.toArray();
61641
- const stride = this.pointStride(points);
61642
- if (points.length < stride) {
61618
+ if (points.length < 2) {
61643
61619
  return null;
61644
61620
  }
61645
61621
  const len = points.length;
61646
- const point = new this.scope.Point(points[len - stride], points[len - stride + 1]);
61622
+ const point = new this.scope.Point(points[len - 2], points[len - 1]);
61647
61623
  return point.transform(new this.scope.Matrix(this.pointsMatrix));
61648
61624
  }
61649
61625
  onStyleKeyUpdate(key) {
@@ -61736,7 +61712,7 @@ var SelectorModel = class extends ElementModel {
61736
61712
  onStyleKeyUpdate(_key) {
61737
61713
  }
61738
61714
  };
61739
- var import_lodash = __toESM2(require_lodash(), 1);
61715
+ var import_lodash2 = __toESM2(require_lodash(), 1);
61740
61716
  function _defineProperty5(e, r, t) {
61741
61717
  return (r = _toPropertyKey5(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
61742
61718
  }
@@ -61769,7 +61745,7 @@ function serializePath(path) {
61769
61745
  }, []);
61770
61746
  }
61771
61747
  function deserializePath(points, scope, matrix) {
61772
- const segmentGroup = (0, import_lodash.chunk)(points, 6);
61748
+ const segmentGroup = (0, import_lodash2.chunk)(points, 6);
61773
61749
  const path = new scope.Path();
61774
61750
  path.segments = segmentGroup.map((v) => deserializeSegment(v, scope, matrix));
61775
61751
  return path;
@@ -61907,7 +61883,7 @@ var SegmentsModel = class extends ElementModel {
61907
61883
  onStyleKeyUpdate(_key) {
61908
61884
  }
61909
61885
  };
61910
- var import_lodash3 = __toESM2(require_lodash(), 1);
61886
+ var import_lodash4 = __toESM2(require_lodash(), 1);
61911
61887
  function _defineProperty7(e, r, t) {
61912
61888
  return (r = _toPropertyKey7(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
61913
61889
  }
@@ -62002,7 +61978,7 @@ var WhiteboardTool = class {
62002
61978
  this.tool.onMouseUp = this.onMouseUpSelf;
62003
61979
  }
62004
61980
  };
62005
- var import_lodash2 = __toESM2(require_lodash(), 1);
61981
+ var import_lodash3 = __toESM2(require_lodash(), 1);
62006
61982
  function _defineProperty8(e, r, t) {
62007
61983
  return (r = _toPropertyKey8(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
62008
61984
  }
@@ -62051,7 +62027,7 @@ var LineTool = class extends WhiteboardTool {
62051
62027
  const point = path.getPointAt(distance);
62052
62028
  return [point.x, point.y];
62053
62029
  });
62054
- this.elementModel.setPoints((0, import_lodash2.flattenDeep)(points));
62030
+ this.elementModel.setPoints((0, import_lodash3.flattenDeep)(points));
62055
62031
  }
62056
62032
  }
62057
62033
  onMouseUp(_event) {
@@ -62132,7 +62108,7 @@ var LineModel = class extends ElementModel {
62132
62108
  }
62133
62109
  renderLine() {
62134
62110
  const matrix = new this.scope.Matrix(this.pointsMatrix);
62135
- const papperPoints = (0, import_lodash3.chunk)(this.drawPoints, 2).map((item) => {
62111
+ const papperPoints = (0, import_lodash4.chunk)(this.drawPoints, 2).map((item) => {
62136
62112
  return new this.scope.Point(item[0], item[1]).transform(matrix);
62137
62113
  });
62138
62114
  const path = new this.scope.Path();
@@ -62268,7 +62244,7 @@ var LineControlPoint = class {
62268
62244
  const invertedPoint = point.transform(pointsMatrix.inverted());
62269
62245
  const points = this.model["drawPoints"];
62270
62246
  this.position = invertedPoint;
62271
- const clonedPoints = (0, import_lodash3.cloneDeep)(points);
62247
+ const clonedPoints = (0, import_lodash4.cloneDeep)(points);
62272
62248
  clonedPoints[this.options.index * 2] = invertedPoint.x;
62273
62249
  clonedPoints[this.options.index * 2 + 1] = invertedPoint.y;
62274
62250
  this.model.setPoints(clonedPoints);
@@ -62738,7 +62714,7 @@ var RectangleModel = class extends ElementModel {
62738
62714
  }
62739
62715
  };
62740
62716
  var elementsUndoOrigin = "elementsUndoOrigin";
62741
- var import_lodash4 = __toESM2(require_lodash(), 1);
62717
+ var import_lodash5 = __toESM2(require_lodash(), 1);
62742
62718
  function _defineProperty13(e, r, t) {
62743
62719
  return (r = _toPropertyKey13(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
62744
62720
  }
@@ -62806,7 +62782,7 @@ var EraserModel = class extends ElementModel {
62806
62782
  return path;
62807
62783
  }
62808
62784
  parsePoints(points) {
62809
- const groupPoints = (0, import_lodash4.chunk)(points, 2);
62785
+ const groupPoints = (0, import_lodash5.chunk)(points, 2);
62810
62786
  return ae(groupPoints, {
62811
62787
  size: this.strokeWidth,
62812
62788
  smoothing: 0.5,
@@ -62824,7 +62800,7 @@ var EraserModel = class extends ElementModel {
62824
62800
  });
62825
62801
  }
62826
62802
  matrixedPoints() {
62827
- const currentPoints = (0, import_lodash4.chunk)(this.drawPoints, 2).slice(this.sliceBegin);
62803
+ const currentPoints = (0, import_lodash5.chunk)(this.drawPoints, 2).slice(this.sliceBegin);
62828
62804
  return currentPoints.map((_ref) => {
62829
62805
  let [x, y] = _ref;
62830
62806
  return new this.scope.Point(x, y);
@@ -62887,7 +62863,7 @@ var EraserModel = class extends ElementModel {
62887
62863
  return point.transform(new this.scope.Matrix(this.pointsMatrix));
62888
62864
  }
62889
62865
  };
62890
- var import_lodash5 = __toESM2(require_lodash(), 1);
62866
+ var import_lodash6 = __toESM2(require_lodash(), 1);
62891
62867
  function _defineProperty14(e, r, t) {
62892
62868
  return (r = _toPropertyKey14(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
62893
62869
  }
@@ -63013,7 +62989,7 @@ var LaserPointerModel = class extends ElementModel {
63013
62989
  const matrix = new this.scope.Matrix(this.pointsMatrix);
63014
62990
  const points = this.cachedPoints || this.points;
63015
62991
  console.log("[][][] ,", this.points.length, this.cachedPoints?.length, this.localPoints.length);
63016
- const groupPoints = (0, import_lodash5.chunk)(points, 2).slice(this.sliceBegin);
62992
+ const groupPoints = (0, import_lodash6.chunk)(points, 2).slice(this.sliceBegin);
63017
62993
  return groupPoints.map((_ref) => {
63018
62994
  let [x, y] = _ref;
63019
62995
  return matrix.transform([x, y]);
@@ -63087,7 +63063,7 @@ var WhiteboardPermissions = class extends AbstractApplicationPermissions {
63087
63063
  return [WhiteboardPermissionFlag.draw, WhiteboardPermissionFlag.editSelf, WhiteboardPermissionFlag.editOthers, WhiteboardPermissionFlag.deleteSelf, WhiteboardPermissionFlag.deleteOthers, WhiteboardPermissionFlag.mainView, WhiteboardPermissionFlag.setOthersView].filter((v) => (v & value) !== 0);
63088
63064
  }
63089
63065
  };
63090
- var import_lodash6 = __toESM2(require_lodash(), 1);
63066
+ var import_lodash7 = __toESM2(require_lodash(), 1);
63091
63067
  function _defineProperty15(e, r, t) {
63092
63068
  return (r = _toPropertyKey15(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
63093
63069
  }
@@ -63146,7 +63122,7 @@ var StraightLineModel = class extends ElementModel {
63146
63122
  }
63147
63123
  renderLine() {
63148
63124
  const matrix = new this.scope.Matrix(this.pointsMatrix);
63149
- const papperPoints = (0, import_lodash6.chunk)(this.drawPoints, 2).map((item) => {
63125
+ const papperPoints = (0, import_lodash7.chunk)(this.drawPoints, 2).map((item) => {
63150
63126
  return new this.scope.Point(item[0], item[1]).transform(matrix);
63151
63127
  });
63152
63128
  const path = new this.scope.Path();
@@ -63266,6 +63242,7 @@ var ImageModel = class extends ElementModel {
63266
63242
  }
63267
63243
  if (!this.imageSets.querySelector(`[id='${this.uuid}']`)) {
63268
63244
  const img = document.createElement("img");
63245
+ img.crossOrigin = "anonymous";
63269
63246
  img.src = this.src;
63270
63247
  img.id = this.uuid;
63271
63248
  this.imageSets.appendChild(img);
@@ -63470,14 +63447,55 @@ var RenderableModel = class extends EventEmitter {
63470
63447
  const model = new ImageModel(yMap, this.scope, this.imageSets, this.liveCursor, this.isPerformanceMode);
63471
63448
  model.bindObserver();
63472
63449
  model.root.set("src", src);
63473
- const initMatrix = new this.scope.Matrix();
63450
+ model.ownerId = this.userManager.selfId;
63451
+ this.fitImageToViewport(src, model);
63452
+ }
63453
+ fitImageToViewport(src, model) {
63474
63454
  const center = this.scope.project.view.center;
63475
- initMatrix.translate({
63455
+ const fallbackMatrix = new this.scope.Matrix();
63456
+ fallbackMatrix.translate({
63476
63457
  x: center.x,
63477
63458
  y: center.y
63478
63459
  });
63479
- model.appendPointsMatrix(initMatrix);
63480
- model.ownerId = this.userManager.selfId;
63460
+ this.setElementPointsMatrix(model, fallbackMatrix);
63461
+ const img = document.createElement("img");
63462
+ img.crossOrigin = "anonymous";
63463
+ img.onload = () => {
63464
+ const naturalWidth = img.naturalWidth || img.width;
63465
+ const naturalHeight = img.naturalHeight || img.height;
63466
+ if (naturalWidth <= 0 || naturalHeight <= 0) {
63467
+ return;
63468
+ }
63469
+ const viewportBounds = this.scope.project.view.bounds;
63470
+ const maxWidth = viewportBounds.width * 2 / 3;
63471
+ const maxHeight = viewportBounds.height * 2 / 3;
63472
+ const fitScale = Math.min(maxWidth / naturalWidth, maxHeight / naturalHeight, 1);
63473
+ const nextMatrix = new this.scope.Matrix();
63474
+ nextMatrix.translate({
63475
+ x: center.x,
63476
+ y: center.y
63477
+ });
63478
+ if (fitScale > 0 && Number.isFinite(fitScale)) {
63479
+ nextMatrix.scale(fitScale);
63480
+ }
63481
+ this.setElementPointsMatrix(model, nextMatrix);
63482
+ };
63483
+ img.onerror = () => {
63484
+ log("[@netless/forge-whiteboard] failed to preload image for viewport fitting", {
63485
+ src
63486
+ }, "warn");
63487
+ };
63488
+ img.src = src;
63489
+ }
63490
+ setElementPointsMatrix(model, matrix) {
63491
+ const values = [matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty];
63492
+ if (model.root.doc) {
63493
+ model.root.doc.transact(() => {
63494
+ model.root.set(ElementModel.KEYS.pointsMatrix, values);
63495
+ }, elementsUndoOrigin);
63496
+ } else {
63497
+ model.root.set(ElementModel.KEYS.pointsMatrix, values);
63498
+ }
63481
63499
  }
63482
63500
  createCurve() {
63483
63501
  let shadow = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
@@ -63734,7 +63752,7 @@ var RenderableModel = class extends EventEmitter {
63734
63752
  if (shadow) {
63735
63753
  yMap.set(ElementModel.KEYS.shadow, "layer");
63736
63754
  }
63737
- yMap.set(ElementModel.KEYS.points, new Y12.Array());
63755
+ yMap.set(ElementModel.KEYS.points, Y12.Array.from([x, y]));
63738
63756
  yMap.set(ElementModel.KEYS.strokeWidth, this.toolbarModel.strokeWidth);
63739
63757
  yMap.set(ElementModel.KEYS.strokeColor, this.toolbarModel.strokeColor);
63740
63758
  yMap.set(ElementModel.KEYS.fillColor, this.toolbarModel.fillColor);
@@ -63742,7 +63760,6 @@ var RenderableModel = class extends EventEmitter {
63742
63760
  this.elements.set(uuid, yMap);
63743
63761
  }, elementsUndoOrigin);
63744
63762
  pointTextModel.bindObserver();
63745
- pointTextModel.setPoints([x, y]);
63746
63763
  pointTextModel.fontSize = this.toolbarModel.fontSize;
63747
63764
  pointTextModel.fontFamily = this.toolbarModel.fontFamily;
63748
63765
  this.initElement(pointTextModel);
@@ -63774,7 +63791,7 @@ var RenderableModel = class extends EventEmitter {
63774
63791
  });
63775
63792
  }
63776
63793
  };
63777
- var import_lodash7 = __toESM2(require_lodash(), 1);
63794
+ var import_lodash8 = __toESM2(require_lodash(), 1);
63778
63795
  function Point(x, y) {
63779
63796
  this.X = x;
63780
63797
  this.Y = y;
@@ -64017,7 +64034,7 @@ var Recognizer = class {
64017
64034
  let maxX = -Number.MAX_VALUE;
64018
64035
  let minY = Number.MAX_VALUE;
64019
64036
  let maxY = -Number.MAX_VALUE;
64020
- const result = this.dollar.Recognize((0, import_lodash7.chunk)(points, 3).map((v) => {
64037
+ const result = this.dollar.Recognize((0, import_lodash8.chunk)(points, 2).map((v) => {
64021
64038
  minX = Math.min(minX, v[0]);
64022
64039
  maxX = Math.max(maxX, v[0]);
64023
64040
  minY = Math.min(minY, v[1]);
@@ -64095,22 +64112,20 @@ var CurveTool = class extends WhiteboardTool {
64095
64112
  if (this.elementModel) {
64096
64113
  let lastX = 0;
64097
64114
  let lastY = 0;
64098
- if (this.pendingPoints.length >= 3) {
64099
- lastX = this.pendingPoints[this.pendingPoints.length - 3];
64100
- lastY = this.pendingPoints[this.pendingPoints.length - 2];
64115
+ if (this.pendingPoints.length >= 2) {
64116
+ lastX = this.pendingPoints[this.pendingPoints.length - 2];
64117
+ lastY = this.pendingPoints[this.pendingPoints.length - 1];
64101
64118
  } else {
64102
64119
  const len = this.elementModel.points.length;
64103
- if (len >= 3) {
64104
- lastX = this.elementModel.points[len - 3];
64105
- lastY = this.elementModel.points[len - 2];
64120
+ if (len >= 2) {
64121
+ lastX = this.elementModel.points[len - 2];
64122
+ lastY = this.elementModel.points[len - 1];
64106
64123
  }
64107
64124
  }
64108
64125
  const dist = Math.max(Math.abs(lastX - event.point.x), Math.abs(lastY - event.point.y));
64109
64126
  if (dist >= MIN_DISTANCE) {
64110
64127
  this.pointCount += 1;
64111
- const nativeEvent = event.event;
64112
- const pressure = nativeEvent.pointerType === "pen" && nativeEvent.pressure > 0 ? nativeEvent.pressure : 0;
64113
- this.pendingPoints.push(event.point.x, event.point.y, pressure);
64128
+ this.pendingPoints.push(event.point.x, event.point.y);
64114
64129
  if (!this.flushRafId) {
64115
64130
  this.flushRafId = requestAnimationFrame(this.flushPendingPoints);
64116
64131
  }
@@ -68297,9 +68312,14 @@ var WhiteboardApplication = class _WhiteboardApplication extends AbstractApplica
68297
68312
  this.snapshotScope.view.matrix.scale(scale, this.paperScope.project.view.bounds.topLeft);
68298
68313
  this.snapshotScope.view.viewSize = this.paperScope.project.view.viewSize.clone().multiply(scale);
68299
68314
  }
68300
- return new Promise((resolve) => {
68315
+ return new Promise((resolve, reject) => {
68301
68316
  setTimeout(() => {
68302
- resolve(this.snapshotScope.view.element.toDataURL("image/png"));
68317
+ try {
68318
+ const res = this.snapshotScope.view.element.toDataURL("image/png");
68319
+ resolve(res);
68320
+ } catch (error) {
68321
+ reject(error);
68322
+ }
68303
68323
  }, 32);
68304
68324
  });
68305
68325
  }