@netless/forge-slide 1.1.5 → 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
@@ -63266,6 +63266,7 @@ var ImageModel = class extends ElementModel {
63266
63266
  }
63267
63267
  if (!this.imageSets.querySelector(`[id='${this.uuid}']`)) {
63268
63268
  const img = document.createElement("img");
63269
+ img.crossOrigin = "anonymous";
63269
63270
  img.src = this.src;
63270
63271
  img.id = this.uuid;
63271
63272
  this.imageSets.appendChild(img);
@@ -63470,14 +63471,55 @@ var RenderableModel = class extends EventEmitter {
63470
63471
  const model = new ImageModel(yMap, this.scope, this.imageSets, this.liveCursor, this.isPerformanceMode);
63471
63472
  model.bindObserver();
63472
63473
  model.root.set("src", src);
63473
- const initMatrix = new this.scope.Matrix();
63474
+ model.ownerId = this.userManager.selfId;
63475
+ this.fitImageToViewport(src, model);
63476
+ }
63477
+ fitImageToViewport(src, model) {
63474
63478
  const center = this.scope.project.view.center;
63475
- initMatrix.translate({
63479
+ const fallbackMatrix = new this.scope.Matrix();
63480
+ fallbackMatrix.translate({
63476
63481
  x: center.x,
63477
63482
  y: center.y
63478
63483
  });
63479
- model.appendPointsMatrix(initMatrix);
63480
- 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
+ }
63481
63523
  }
63482
63524
  createCurve() {
63483
63525
  let shadow = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
@@ -68297,9 +68339,14 @@ var WhiteboardApplication = class _WhiteboardApplication extends AbstractApplica
68297
68339
  this.snapshotScope.view.matrix.scale(scale, this.paperScope.project.view.bounds.topLeft);
68298
68340
  this.snapshotScope.view.viewSize = this.paperScope.project.view.viewSize.clone().multiply(scale);
68299
68341
  }
68300
- return new Promise((resolve) => {
68342
+ return new Promise((resolve, reject) => {
68301
68343
  setTimeout(() => {
68302
- 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
+ }
68303
68350
  }, 32);
68304
68351
  });
68305
68352
  }