@tldraw/editor 3.8.0-canary.80294e641ad6 → 3.8.0-canary.811fed79c8ad

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.
Files changed (98) hide show
  1. package/dist-cjs/index.d.ts +257 -60
  2. package/dist-cjs/index.js +14 -8
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +2 -5
  5. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  6. package/dist-cjs/lib/config/TLSessionStateSnapshot.js.map +2 -2
  7. package/dist-cjs/lib/config/createTLStore.js +4 -2
  8. package/dist-cjs/lib/config/createTLStore.js.map +2 -2
  9. package/dist-cjs/lib/editor/Editor.js +85 -21
  10. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  11. package/dist-cjs/lib/editor/managers/SnapManager/BoundsSnaps.js.map +2 -2
  12. package/dist-cjs/lib/editor/managers/TextManager.js +1 -0
  13. package/dist-cjs/lib/editor/managers/TextManager.js.map +2 -2
  14. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
  15. package/dist-cjs/lib/editor/shapes/shared/resizeScaled.js +66 -0
  16. package/dist-cjs/lib/editor/shapes/shared/resizeScaled.js.map +7 -0
  17. package/dist-cjs/lib/editor/types/SvgExportContext.js.map +2 -2
  18. package/dist-cjs/lib/editor/types/emit-types.js.map +1 -1
  19. package/dist-cjs/lib/editor/types/external-content.js.map +1 -1
  20. package/dist-cjs/lib/editor/types/misc-types.js.map +1 -1
  21. package/dist-cjs/lib/exports/StyleEmbedder.js.map +2 -2
  22. package/dist-cjs/lib/exports/exportToSvg.js.map +2 -2
  23. package/dist-cjs/lib/exports/getSvgAsImage.js +83 -0
  24. package/dist-cjs/lib/exports/getSvgAsImage.js.map +7 -0
  25. package/dist-cjs/lib/exports/getSvgJsx.js +16 -3
  26. package/dist-cjs/lib/exports/getSvgJsx.js.map +2 -2
  27. package/dist-cjs/lib/hooks/useLocalStore.js +1 -1
  28. package/dist-cjs/lib/hooks/useLocalStore.js.map +2 -2
  29. package/dist-cjs/lib/hooks/usePassThroughWheelEvents.js +4 -0
  30. package/dist-cjs/lib/hooks/usePassThroughWheelEvents.js.map +3 -3
  31. package/dist-cjs/lib/options.js +3 -1
  32. package/dist-cjs/lib/options.js.map +2 -2
  33. package/dist-cjs/lib/utils/browserCanvasMaxSize.js +75 -0
  34. package/dist-cjs/lib/utils/browserCanvasMaxSize.js.map +7 -0
  35. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js +3 -1
  36. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js.map +2 -2
  37. package/dist-cjs/version.js +3 -3
  38. package/dist-cjs/version.js.map +1 -1
  39. package/dist-esm/index.d.mts +257 -60
  40. package/dist-esm/index.mjs +7 -1
  41. package/dist-esm/index.mjs.map +2 -2
  42. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +2 -5
  43. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  44. package/dist-esm/lib/config/TLSessionStateSnapshot.mjs.map +2 -2
  45. package/dist-esm/lib/config/createTLStore.mjs +4 -2
  46. package/dist-esm/lib/config/createTLStore.mjs.map +2 -2
  47. package/dist-esm/lib/editor/Editor.mjs +85 -21
  48. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  49. package/dist-esm/lib/editor/managers/SnapManager/BoundsSnaps.mjs.map +2 -2
  50. package/dist-esm/lib/editor/managers/TextManager.mjs +1 -0
  51. package/dist-esm/lib/editor/managers/TextManager.mjs.map +2 -2
  52. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
  53. package/dist-esm/lib/editor/shapes/shared/resizeScaled.mjs +46 -0
  54. package/dist-esm/lib/editor/shapes/shared/resizeScaled.mjs.map +7 -0
  55. package/dist-esm/lib/editor/types/SvgExportContext.mjs.map +2 -2
  56. package/dist-esm/lib/exports/StyleEmbedder.mjs.map +2 -2
  57. package/dist-esm/lib/exports/exportToSvg.mjs.map +2 -2
  58. package/dist-esm/lib/exports/getSvgAsImage.mjs +63 -0
  59. package/dist-esm/lib/exports/getSvgAsImage.mjs.map +7 -0
  60. package/dist-esm/lib/exports/getSvgJsx.mjs +16 -3
  61. package/dist-esm/lib/exports/getSvgJsx.mjs.map +2 -2
  62. package/dist-esm/lib/hooks/useLocalStore.mjs +1 -1
  63. package/dist-esm/lib/hooks/useLocalStore.mjs.map +2 -2
  64. package/dist-esm/lib/hooks/usePassThroughWheelEvents.mjs +4 -0
  65. package/dist-esm/lib/hooks/usePassThroughWheelEvents.mjs.map +3 -3
  66. package/dist-esm/lib/options.mjs +3 -1
  67. package/dist-esm/lib/options.mjs.map +2 -2
  68. package/dist-esm/lib/utils/browserCanvasMaxSize.mjs +45 -0
  69. package/dist-esm/lib/utils/browserCanvasMaxSize.mjs.map +7 -0
  70. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs +3 -1
  71. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs.map +2 -2
  72. package/dist-esm/version.mjs +3 -3
  73. package/dist-esm/version.mjs.map +1 -1
  74. package/editor.css +2 -1
  75. package/package.json +22 -20
  76. package/src/index.ts +19 -1
  77. package/src/lib/components/default-components/DefaultCanvas.tsx +2 -5
  78. package/src/lib/config/TLSessionStateSnapshot.ts +3 -1
  79. package/src/lib/config/createTLStore.ts +4 -2
  80. package/src/lib/editor/Editor.ts +134 -44
  81. package/src/lib/editor/managers/SnapManager/BoundsSnaps.ts +4 -4
  82. package/src/lib/editor/managers/TextManager.ts +1 -0
  83. package/src/lib/editor/shapes/ShapeUtil.ts +30 -1
  84. package/src/lib/editor/shapes/shared/resizeScaled.ts +61 -0
  85. package/src/lib/editor/types/SvgExportContext.tsx +21 -0
  86. package/src/lib/editor/types/emit-types.ts +1 -0
  87. package/src/lib/editor/types/external-content.ts +90 -50
  88. package/src/lib/editor/types/misc-types.ts +55 -2
  89. package/src/lib/exports/StyleEmbedder.ts +1 -1
  90. package/src/lib/exports/exportToSvg.tsx +2 -2
  91. package/src/lib/exports/getSvgAsImage.ts +92 -0
  92. package/src/lib/exports/getSvgJsx.tsx +17 -2
  93. package/src/lib/hooks/useLocalStore.ts +1 -1
  94. package/src/lib/hooks/usePassThroughWheelEvents.ts +7 -0
  95. package/src/lib/options.ts +11 -0
  96. package/src/lib/utils/browserCanvasMaxSize.ts +65 -0
  97. package/src/lib/utils/sync/TLLocalSyncClient.ts +3 -1
  98. package/src/version.ts +3 -3
@@ -87,6 +87,7 @@ var import_defaultBindings = require("../config/defaultBindings");
87
87
  var import_defaultShapes = require("../config/defaultShapes");
88
88
  var import_constants = require("../constants");
89
89
  var import_exportToSvg = require("../exports/exportToSvg");
90
+ var import_getSvgAsImage = require("../exports/getSvgAsImage");
90
91
  var import_environment = require("../globals/environment");
91
92
  var import_menus = require("../globals/menus");
92
93
  var import_time = require("../globals/time");
@@ -812,6 +813,10 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
812
813
  (0, import_utils.assert)(shapeUtil, `No shape util found for type "${type}"`);
813
814
  return shapeUtil;
814
815
  }
816
+ hasShapeUtil(arg) {
817
+ const type = typeof arg === "string" ? arg : arg.type;
818
+ return (0, import_utils.hasOwnProperty)(this.shapeUtils, type);
819
+ }
815
820
  getBindingUtil(arg) {
816
821
  const type = typeof arg === "string" ? arg : arg.type;
817
822
  const bindingUtil = (0, import_utils.getOwnProperty)(this.bindingUtils, type);
@@ -1159,8 +1164,8 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
1159
1164
  *
1160
1165
  * @example
1161
1166
  * ```ts
1162
- * state.getStateDescendant('select')
1163
- * state.getStateDescendant('select.brushing')
1167
+ * editor.getStateDescendant('select')
1168
+ * editor.getStateDescendant('select.brushing')
1164
1169
  * ```
1165
1170
  *
1166
1171
  * @param path - The descendant's path of state ids, separated by periods.
@@ -1214,7 +1219,7 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
1214
1219
  if (partial.isChangingStyle === true) {
1215
1220
  this._isChangingStyleTimeout = this.timers.setTimeout(() => {
1216
1221
  this._updateInstanceState({ isChangingStyle: false }, { history: "ignore" });
1217
- }, 2e3);
1222
+ }, 1e3);
1218
1223
  }
1219
1224
  }
1220
1225
  return this;
@@ -3165,11 +3170,14 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
3165
3170
  if (!assetId) return null;
3166
3171
  const asset = this.getAsset(assetId);
3167
3172
  if (!asset) return null;
3168
- const { screenScale = 1, shouldResolveToOriginal = false } = context;
3173
+ const {
3174
+ screenScale = 1,
3175
+ shouldResolveToOriginal = false,
3176
+ dpr = this.getInstanceState().devicePixelRatio
3177
+ } = context;
3169
3178
  const zoomStepFunction = (zoom) => Math.pow(2, Math.ceil(Math.log2(zoom)));
3170
- const steppedScreenScale = Math.max(0.125, zoomStepFunction(screenScale));
3179
+ const steppedScreenScale = zoomStepFunction(screenScale);
3171
3180
  const networkEffectiveType = "connection" in navigator ? navigator.connection.effectiveType : null;
3172
- const dpr = this.getInstanceState().devicePixelRatio;
3173
3181
  return await this.store.props.assets.resolve(asset, {
3174
3182
  screenScale: screenScale || 1,
3175
3183
  steppedScreenScale,
@@ -5090,6 +5098,7 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5090
5098
  scale = new import_Vec.Vec(Math.sign(scale.x) * Math.abs(scale.y), scale.y);
5091
5099
  }
5092
5100
  }
5101
+ let didResize = false;
5093
5102
  if (util.onResize && util.canResize(initialShape)) {
5094
5103
  const newPagePoint = this._scalePagePoint(
5095
5104
  import_Mat.Mat.applyToPoint(pageTransform, new import_Vec.Vec(0, 0)),
@@ -5114,24 +5123,28 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5114
5123
  util.onResizeStart?.(initialShape) ?? void 0
5115
5124
  );
5116
5125
  }
5126
+ const resizedShape = util.onResize(
5127
+ { ...initialShape, x, y },
5128
+ {
5129
+ newPoint: newLocalPoint,
5130
+ handle: opts.dragHandle ?? "bottom_right",
5131
+ // don't set isSingle to true for children
5132
+ mode: opts.mode ?? "scale_shape",
5133
+ scaleX: myScale.x,
5134
+ scaleY: myScale.y,
5135
+ initialBounds,
5136
+ initialShape
5137
+ }
5138
+ );
5139
+ if (resizedShape) {
5140
+ didResize = true;
5141
+ }
5117
5142
  workingShape = applyPartialToRecordWithProps(workingShape, {
5118
5143
  id,
5119
5144
  type: initialShape.type,
5120
5145
  x: newLocalPoint.x,
5121
5146
  y: newLocalPoint.y,
5122
- ...util.onResize(
5123
- { ...initialShape, x, y },
5124
- {
5125
- newPoint: newLocalPoint,
5126
- handle: opts.dragHandle ?? "bottom_right",
5127
- // don't set isSingle to true for children
5128
- mode: opts.mode ?? "scale_shape",
5129
- scaleX: myScale.x,
5130
- scaleY: myScale.y,
5131
- initialBounds,
5132
- initialShape
5133
- }
5134
- )
5147
+ ...resizedShape
5135
5148
  });
5136
5149
  if (!opts.skipStartAndEndCallbacks) {
5137
5150
  workingShape = applyPartialToRecordWithProps(
@@ -5140,7 +5153,8 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
5140
5153
  );
5141
5154
  }
5142
5155
  this.updateShapes([workingShape]);
5143
- } else {
5156
+ }
5157
+ if (!didResize) {
5144
5158
  const initialPageCenter = import_Mat.Mat.applyToPoint(pageTransform, initialBounds.center);
5145
5159
  const newPageCenter = this._scalePagePoint(
5146
5160
  initialPageCenter,
@@ -6287,7 +6301,7 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
6287
6301
  * @public
6288
6302
  */
6289
6303
  async getSvgElement(shapes, opts = {}) {
6290
- const ids = typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
6304
+ const ids = shapes.length === 0 ? this.getCurrentPageShapeIdsSorted() : typeof shapes[0] === "string" ? shapes : shapes.map((s) => s.id);
6291
6305
  if (ids.length === 0) return void 0;
6292
6306
  return (0, import_exportToSvg.exportToSvg)(this, ids, opts);
6293
6307
  }
@@ -6317,6 +6331,55 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
6317
6331
  if (!result) return void 0;
6318
6332
  return result.svg;
6319
6333
  }
6334
+ /**
6335
+ * Get an exported image of the given shapes.
6336
+ *
6337
+ * @param shapes - The shapes (or shape ids) to export.
6338
+ * @param opts - Options for the export.
6339
+ *
6340
+ * @returns A blob of the image.
6341
+ * @public
6342
+ */
6343
+ async toImage(shapes, opts = {}) {
6344
+ const withDefaults = {
6345
+ format: "png",
6346
+ scale: 1,
6347
+ pixelRatio: opts.format === "svg" ? void 0 : 2,
6348
+ ...opts
6349
+ };
6350
+ const result = await this.getSvgString(shapes, withDefaults);
6351
+ if (!result) throw new Error("Could not create SVG");
6352
+ switch (withDefaults.format) {
6353
+ case "svg":
6354
+ return {
6355
+ blob: new Blob([result.svg], { type: "text/plain" }),
6356
+ width: result.width,
6357
+ height: result.height
6358
+ };
6359
+ case "jpeg":
6360
+ case "png":
6361
+ case "webp": {
6362
+ const blob = await (0, import_getSvgAsImage.getSvgAsImage)(result.svg, {
6363
+ type: withDefaults.format,
6364
+ quality: withDefaults.quality,
6365
+ pixelRatio: withDefaults.pixelRatio,
6366
+ width: result.width,
6367
+ height: result.height
6368
+ });
6369
+ if (!blob) {
6370
+ throw new Error("Could not construct image.");
6371
+ }
6372
+ return {
6373
+ blob,
6374
+ width: result.width,
6375
+ height: result.height
6376
+ };
6377
+ }
6378
+ default: {
6379
+ (0, import_utils.exhaustiveSwitchError)(withDefaults.format);
6380
+ }
6381
+ }
6382
+ }
6320
6383
  /**
6321
6384
  * Update the input points from a pointer, pinch, or wheel event.
6322
6385
  *
@@ -6810,6 +6873,7 @@ class Editor extends (_a = import_eventemitter3.default, _getIsShapeHiddenCache_
6810
6873
  }
6811
6874
  _flushEventForTick(info) {
6812
6875
  if (this.getCrashingError()) return this;
6876
+ this.emit("before-event", info);
6813
6877
  const { inputs } = this;
6814
6878
  const { type } = info;
6815
6879
  if (info.type === "misc") {