@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.js CHANGED
@@ -63275,6 +63275,7 @@ var ImageModel = class extends ElementModel {
63275
63275
  }
63276
63276
  if (!this.imageSets.querySelector(`[id='${this.uuid}']`)) {
63277
63277
  const img = document.createElement("img");
63278
+ img.crossOrigin = "anonymous";
63278
63279
  img.src = this.src;
63279
63280
  img.id = this.uuid;
63280
63281
  this.imageSets.appendChild(img);
@@ -63479,14 +63480,55 @@ var RenderableModel = class extends import_eventemitter3.default {
63479
63480
  const model = new ImageModel(yMap, this.scope, this.imageSets, this.liveCursor, this.isPerformanceMode);
63480
63481
  model.bindObserver();
63481
63482
  model.root.set("src", src);
63482
- const initMatrix = new this.scope.Matrix();
63483
+ model.ownerId = this.userManager.selfId;
63484
+ this.fitImageToViewport(src, model);
63485
+ }
63486
+ fitImageToViewport(src, model) {
63483
63487
  const center = this.scope.project.view.center;
63484
- initMatrix.translate({
63488
+ const fallbackMatrix = new this.scope.Matrix();
63489
+ fallbackMatrix.translate({
63485
63490
  x: center.x,
63486
63491
  y: center.y
63487
63492
  });
63488
- model.appendPointsMatrix(initMatrix);
63489
- model.ownerId = this.userManager.selfId;
63493
+ this.setElementPointsMatrix(model, fallbackMatrix);
63494
+ const img = document.createElement("img");
63495
+ img.crossOrigin = "anonymous";
63496
+ img.onload = () => {
63497
+ const naturalWidth = img.naturalWidth || img.width;
63498
+ const naturalHeight = img.naturalHeight || img.height;
63499
+ if (naturalWidth <= 0 || naturalHeight <= 0) {
63500
+ return;
63501
+ }
63502
+ const viewportBounds = this.scope.project.view.bounds;
63503
+ const maxWidth = viewportBounds.width * 2 / 3;
63504
+ const maxHeight = viewportBounds.height * 2 / 3;
63505
+ const fitScale = Math.min(maxWidth / naturalWidth, maxHeight / naturalHeight, 1);
63506
+ const nextMatrix = new this.scope.Matrix();
63507
+ nextMatrix.translate({
63508
+ x: center.x,
63509
+ y: center.y
63510
+ });
63511
+ if (fitScale > 0 && Number.isFinite(fitScale)) {
63512
+ nextMatrix.scale(fitScale);
63513
+ }
63514
+ this.setElementPointsMatrix(model, nextMatrix);
63515
+ };
63516
+ img.onerror = () => {
63517
+ (0, import_forge_room2.log)("[@netless/forge-whiteboard] failed to preload image for viewport fitting", {
63518
+ src
63519
+ }, "warn");
63520
+ };
63521
+ img.src = src;
63522
+ }
63523
+ setElementPointsMatrix(model, matrix) {
63524
+ const values = [matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty];
63525
+ if (model.root.doc) {
63526
+ model.root.doc.transact(() => {
63527
+ model.root.set(ElementModel.KEYS.pointsMatrix, values);
63528
+ }, elementsUndoOrigin);
63529
+ } else {
63530
+ model.root.set(ElementModel.KEYS.pointsMatrix, values);
63531
+ }
63490
63532
  }
63491
63533
  createCurve() {
63492
63534
  let shadow = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
@@ -68306,9 +68348,14 @@ var WhiteboardApplication = class _WhiteboardApplication extends import_forge_ro
68306
68348
  this.snapshotScope.view.matrix.scale(scale, this.paperScope.project.view.bounds.topLeft);
68307
68349
  this.snapshotScope.view.viewSize = this.paperScope.project.view.viewSize.clone().multiply(scale);
68308
68350
  }
68309
- return new Promise((resolve) => {
68351
+ return new Promise((resolve, reject) => {
68310
68352
  setTimeout(() => {
68311
- resolve(this.snapshotScope.view.element.toDataURL("image/png"));
68353
+ try {
68354
+ const res = this.snapshotScope.view.element.toDataURL("image/png");
68355
+ resolve(res);
68356
+ } catch (error) {
68357
+ reject(error);
68358
+ }
68312
68359
  }, 32);
68313
68360
  });
68314
68361
  }