@netless/forge-slide 1.1.4 → 1.2.0-beta.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/dist/slide.esm.js CHANGED
@@ -61535,14 +61535,33 @@ 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
+ }
61538
61556
  matrixedPoints() {
61539
61557
  const points = this.localPoints.length === 0 ? this.points : this.localPoints;
61540
61558
  const matrix = new this.scope.Matrix(this.pointsMatrix);
61541
61559
  const output = [];
61542
- for (let i = 0, len = points.length; i < len; i += 3) {
61560
+ const stride = this.pointStride(points);
61561
+ for (let i = 0, len = points.length; i + 1 < len; i += stride) {
61543
61562
  const p = new this.scope.Point(points[i], points[i + 1]);
61544
61563
  const tp = p.transform(matrix);
61545
- const pressure = points[i + 2] ?? 0;
61564
+ const pressure = stride === 3 ? points[i + 2] ?? 0 : 0;
61546
61565
  output.push([tp.x, tp.y, pressure]);
61547
61566
  }
61548
61567
  return output;
@@ -61618,11 +61637,13 @@ var CurveModel = class extends ElementModel {
61618
61637
  }
61619
61638
  liveCursorPoint() {
61620
61639
  const yArray = this.root.get(ElementModel.KEYS.points);
61621
- if (yArray.length < 3) {
61640
+ const points = yArray.toArray();
61641
+ const stride = this.pointStride(points);
61642
+ if (points.length < stride) {
61622
61643
  return null;
61623
61644
  }
61624
- const len = yArray.length;
61625
- const point = new this.scope.Point(yArray.get(len - 3), yArray.get(len - 2));
61645
+ const len = points.length;
61646
+ const point = new this.scope.Point(points[len - stride], points[len - stride + 1]);
61626
61647
  return point.transform(new this.scope.Matrix(this.pointsMatrix));
61627
61648
  }
61628
61649
  onStyleKeyUpdate(key) {
@@ -63245,6 +63266,7 @@ var ImageModel = class extends ElementModel {
63245
63266
  }
63246
63267
  if (!this.imageSets.querySelector(`[id='${this.uuid}']`)) {
63247
63268
  const img = document.createElement("img");
63269
+ img.crossOrigin = "anonymous";
63248
63270
  img.src = this.src;
63249
63271
  img.id = this.uuid;
63250
63272
  this.imageSets.appendChild(img);
@@ -63449,14 +63471,55 @@ var RenderableModel = class extends EventEmitter {
63449
63471
  const model = new ImageModel(yMap, this.scope, this.imageSets, this.liveCursor, this.isPerformanceMode);
63450
63472
  model.bindObserver();
63451
63473
  model.root.set("src", src);
63452
- const initMatrix = new this.scope.Matrix();
63474
+ model.ownerId = this.userManager.selfId;
63475
+ this.fitImageToViewport(src, model);
63476
+ }
63477
+ fitImageToViewport(src, model) {
63453
63478
  const center = this.scope.project.view.center;
63454
- initMatrix.translate({
63479
+ const fallbackMatrix = new this.scope.Matrix();
63480
+ fallbackMatrix.translate({
63455
63481
  x: center.x,
63456
63482
  y: center.y
63457
63483
  });
63458
- model.appendPointsMatrix(initMatrix);
63459
- model.ownerId = this.userManager.selfId;
63484
+ this.setElementPointsMatrix(model, fallbackMatrix);
63485
+ const img = document.createElement("img");
63486
+ img.crossOrigin = "anonymous";
63487
+ img.onload = () => {
63488
+ const naturalWidth = img.naturalWidth || img.width;
63489
+ const naturalHeight = img.naturalHeight || img.height;
63490
+ if (naturalWidth <= 0 || naturalHeight <= 0) {
63491
+ return;
63492
+ }
63493
+ const viewportBounds = this.scope.project.view.bounds;
63494
+ const maxWidth = viewportBounds.width * 2 / 3;
63495
+ const maxHeight = viewportBounds.height * 2 / 3;
63496
+ const fitScale = Math.min(maxWidth / naturalWidth, maxHeight / naturalHeight, 1);
63497
+ const nextMatrix = new this.scope.Matrix();
63498
+ nextMatrix.translate({
63499
+ x: center.x,
63500
+ y: center.y
63501
+ });
63502
+ if (fitScale > 0 && Number.isFinite(fitScale)) {
63503
+ nextMatrix.scale(fitScale);
63504
+ }
63505
+ this.setElementPointsMatrix(model, nextMatrix);
63506
+ };
63507
+ img.onerror = () => {
63508
+ log("[@netless/forge-whiteboard] failed to preload image for viewport fitting", {
63509
+ src
63510
+ }, "warn");
63511
+ };
63512
+ img.src = src;
63513
+ }
63514
+ setElementPointsMatrix(model, matrix) {
63515
+ const values = [matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty];
63516
+ if (model.root.doc) {
63517
+ model.root.doc.transact(() => {
63518
+ model.root.set(ElementModel.KEYS.pointsMatrix, values);
63519
+ }, elementsUndoOrigin);
63520
+ } else {
63521
+ model.root.set(ElementModel.KEYS.pointsMatrix, values);
63522
+ }
63460
63523
  }
63461
63524
  createCurve() {
63462
63525
  let shadow = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
@@ -68276,9 +68339,14 @@ var WhiteboardApplication = class _WhiteboardApplication extends AbstractApplica
68276
68339
  this.snapshotScope.view.matrix.scale(scale, this.paperScope.project.view.bounds.topLeft);
68277
68340
  this.snapshotScope.view.viewSize = this.paperScope.project.view.viewSize.clone().multiply(scale);
68278
68341
  }
68279
- return new Promise((resolve) => {
68342
+ return new Promise((resolve, reject) => {
68280
68343
  setTimeout(() => {
68281
- resolve(this.snapshotScope.view.element.toDataURL("image/png"));
68344
+ try {
68345
+ const res = this.snapshotScope.view.element.toDataURL("image/png");
68346
+ resolve(res);
68347
+ } catch (error) {
68348
+ reject(error);
68349
+ }
68282
68350
  }, 32);
68283
68351
  });
68284
68352
  }