@ibodr/editor 0.2.19 → 0.2.21

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/index.mjs CHANGED
@@ -15,10 +15,10 @@ import React__default, { createContext, useRef, useLayoutEffect, memo, useEffect
15
15
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
16
16
  import classNames from 'classnames';
17
17
  import { flushSync, createPortal } from 'react-dom';
18
+ import RBush from 'rbush';
18
19
  import { openDB, deleteDB } from 'idb';
19
20
  import EventEmitter from 'eventemitter3';
20
21
  import { createRoot } from 'react-dom/client';
21
- import RBush from 'rbush';
22
22
  import { getSchema } from '@tiptap/core';
23
23
  import { Node as Node$1 } from '@tiptap/pm/model';
24
24
 
@@ -4457,6 +4457,24 @@ function drawGeometryStroke(ctx, geometry) {
4457
4457
  if (geometry.isClosed) ctx.closePath();
4458
4458
  ctx.stroke();
4459
4459
  }
4460
+ function createLodContentCache(readShapes, prepare) {
4461
+ let entries = /* @__PURE__ */ new Map();
4462
+ return computed("lod scene content", () => {
4463
+ const nextEntries = /* @__PURE__ */ new Map();
4464
+ const result = /* @__PURE__ */ new Map();
4465
+ for (const shape of readShapes()) {
4466
+ const previous = entries.get(shape.id);
4467
+ const entry = previous?.shape === shape ? previous : {
4468
+ shape,
4469
+ value: computed("lod content " + shape.id, () => prepare(shape))
4470
+ };
4471
+ nextEntries.set(shape.id, entry);
4472
+ result.set(shape.id, entry.value.get());
4473
+ }
4474
+ entries = nextEntries;
4475
+ return result;
4476
+ });
4477
+ }
4460
4478
 
4461
4479
  // src/lib/editor/lod/lodPaintCache.ts
4462
4480
  var labels = /* @__PURE__ */ new WeakMap();
@@ -4524,6 +4542,190 @@ function getTransformedLodBounds(matrix, mesh) {
4524
4542
  return bounds;
4525
4543
  }
4526
4544
 
4545
+ // src/lib/editor/lod/lodPanMountCache.ts
4546
+ function createLodPanMountCache() {
4547
+ let source;
4548
+ let inputs = [];
4549
+ let mounted2 = [];
4550
+ let mountedIds = /* @__PURE__ */ new Set();
4551
+ let byId = /* @__PURE__ */ new Map();
4552
+ let rank = /* @__PURE__ */ new Map();
4553
+ return {
4554
+ reset(rendering, key, result) {
4555
+ source = rendering;
4556
+ inputs = key;
4557
+ mounted2 = result;
4558
+ mountedIds = new Set(result.map((entry) => entry.id));
4559
+ byId = new Map(rendering.map((entry) => [entry.id, entry]));
4560
+ rank = new Map(rendering.map((entry, index) => [entry.id, index]));
4561
+ },
4562
+ extend(rendering, key, candidates, shouldMount) {
4563
+ if (source !== rendering || inputs.length !== key.length || inputs.some((value, i) => value !== key[i]))
4564
+ return void 0;
4565
+ let next;
4566
+ for (const id of candidates()) {
4567
+ if (mountedIds.has(id)) continue;
4568
+ const entry = byId.get(id);
4569
+ if (!entry || !shouldMount(entry)) continue;
4570
+ if (!next) next = mounted2.slice();
4571
+ next.push(entry);
4572
+ mountedIds.add(id);
4573
+ }
4574
+ if (next) {
4575
+ next.sort((a, b) => rank.get(a.id) - rank.get(b.id));
4576
+ mounted2 = next;
4577
+ }
4578
+ return mounted2;
4579
+ }
4580
+ };
4581
+ }
4582
+ var DrawRBush = class extends RBush {
4583
+ };
4584
+ var RBushIndex = class {
4585
+ rBush;
4586
+ elementsInTree;
4587
+ constructor() {
4588
+ this.rBush = new DrawRBush();
4589
+ this.elementsInTree = /* @__PURE__ */ new Map();
4590
+ }
4591
+ /**
4592
+ * Search for shapes within the given bounds.
4593
+ * Returns set of shape IDs that intersect with the bounds.
4594
+ */
4595
+ search(bounds) {
4596
+ const results = this.rBush.search({
4597
+ minX: bounds.minX,
4598
+ minY: bounds.minY,
4599
+ maxX: bounds.maxX,
4600
+ maxY: bounds.maxY
4601
+ });
4602
+ return new Set(results.map((e) => e.id));
4603
+ }
4604
+ /**
4605
+ * Remove a shape from the spatial index.
4606
+ */
4607
+ remove(id) {
4608
+ const element = this.elementsInTree.get(id);
4609
+ if (element) {
4610
+ this.rBush.remove(element);
4611
+ this.elementsInTree.delete(id);
4612
+ }
4613
+ }
4614
+ /**
4615
+ * Apply the removals and upserts collected during one incremental update.
4616
+ * Mutations are applied per item: bulk-rebuilding the whole tree instead
4617
+ * was measured slower even with ~10% of the index in the batch.
4618
+ */
4619
+ applyBatch(removes, upserts) {
4620
+ for (const id of removes) {
4621
+ this.remove(id);
4622
+ }
4623
+ for (const element of upserts) {
4624
+ const existing = this.elementsInTree.get(element.id);
4625
+ if (existing) {
4626
+ this.rBush.remove(existing);
4627
+ }
4628
+ this.rBush.insert(element);
4629
+ this.elementsInTree.set(element.id, element);
4630
+ }
4631
+ }
4632
+ /**
4633
+ * Bulk load elements into the spatial index.
4634
+ * More efficient than individual inserts for initial loading.
4635
+ */
4636
+ bulkLoad(elements) {
4637
+ this.rBush.load(elements);
4638
+ for (const element of elements) {
4639
+ this.elementsInTree.set(element.id, element);
4640
+ }
4641
+ }
4642
+ /**
4643
+ * Clear all elements from the spatial index.
4644
+ */
4645
+ clear() {
4646
+ this.rBush.clear();
4647
+ this.elementsInTree.clear();
4648
+ }
4649
+ /**
4650
+ * Check if a shape is in the spatial index.
4651
+ */
4652
+ has(id) {
4653
+ return this.elementsInTree.has(id);
4654
+ }
4655
+ /**
4656
+ * Get the number of elements in the spatial index.
4657
+ */
4658
+ getSize() {
4659
+ return this.elementsInTree.size;
4660
+ }
4661
+ /**
4662
+ * Get the raw stored element for a shape, without allocating a Box.
4663
+ * Use when you only need to read the indexed bounds for comparison.
4664
+ *
4665
+ * @internal
4666
+ */
4667
+ getElement(id) {
4668
+ return this.elementsInTree.get(id);
4669
+ }
4670
+ /**
4671
+ * Dispose of the spatial index.
4672
+ * Clears all data structures to prevent memory leaks.
4673
+ */
4674
+ dispose() {
4675
+ this.clear();
4676
+ }
4677
+ };
4678
+
4679
+ // src/lib/editor/lod/lodSpatialIndex.ts
4680
+ function createLodSpatialIndex(getBounds) {
4681
+ const tree = new RBushIndex();
4682
+ let previous = /* @__PURE__ */ new Map();
4683
+ let ranks = /* @__PURE__ */ new Map();
4684
+ let fallback = /* @__PURE__ */ new Set();
4685
+ return {
4686
+ update(content) {
4687
+ if (content === previous) return;
4688
+ const removes = /* @__PURE__ */ new Set();
4689
+ const upserts = [];
4690
+ const nextFallback = /* @__PURE__ */ new Set();
4691
+ const nextRanks = /* @__PURE__ */ new Map();
4692
+ for (const id of previous.keys()) if (!content.has(id)) removes.add(id);
4693
+ let rank = 0;
4694
+ for (const [key, value] of content) {
4695
+ const id = key;
4696
+ nextRanks.set(id, rank++);
4697
+ if (previous.has(id) && previous.get(id) === value) {
4698
+ if (fallback.has(id)) nextFallback.add(id);
4699
+ continue;
4700
+ }
4701
+ const bounds = getBounds(value);
4702
+ if (!bounds || !bounds.isValid()) {
4703
+ removes.add(id);
4704
+ nextFallback.add(id);
4705
+ } else {
4706
+ upserts.push({
4707
+ id,
4708
+ minX: bounds.minX,
4709
+ minY: bounds.minY,
4710
+ maxX: bounds.maxX,
4711
+ maxY: bounds.maxY
4712
+ });
4713
+ }
4714
+ }
4715
+ if (previous.size === 0) tree.bulkLoad(upserts);
4716
+ else tree.applyBatch(removes, upserts);
4717
+ previous = content;
4718
+ ranks = nextRanks;
4719
+ fallback = nextFallback;
4720
+ },
4721
+ query(bounds) {
4722
+ const ids = tree.search(bounds);
4723
+ for (const id of fallback) ids.add(id);
4724
+ return [...ids].sort((a, b) => ranks.get(a) - ranks.get(b)).map((id) => previous.get(id));
4725
+ }
4726
+ };
4727
+ }
4728
+
4527
4729
  // src/lib/editor/lod/shapeRenderMode.ts
4528
4730
  var LOD_KEEP_SCREEN_PX = 36;
4529
4731
  var LOD_KEEP_TEXT_PX = 7;
@@ -4690,6 +4892,48 @@ function lodStyleFor(editor, shape, frame) {
4690
4892
  styleCache.set(shape, { theme, colorMode, style });
4691
4893
  return style;
4692
4894
  }
4895
+ var contentCaches = /* @__PURE__ */ new WeakMap();
4896
+ function makeContentCache(editor) {
4897
+ return createLodContentCache(
4898
+ () => editor.getCurrentPageShapesSorted(),
4899
+ (shape) => {
4900
+ const hidden = editor.isShapeHidden(shape);
4901
+ const style = lodStyleFor(editor, shape, {
4902
+ theme: editor.getCurrentTheme(),
4903
+ colorMode: editor.getColorMode()
4904
+ });
4905
+ return {
4906
+ shape,
4907
+ hidden,
4908
+ style,
4909
+ mesh: style ? meshFor(editor, shape, style) : null,
4910
+ matrix: editor.getShapePageTransform(shape),
4911
+ bounds: editor.getShapePageBounds(shape)
4912
+ };
4913
+ }
4914
+ );
4915
+ }
4916
+ var spatialCaches = /* @__PURE__ */ new WeakMap();
4917
+ function readCandidates(editor, content) {
4918
+ let index = spatialCaches.get(editor);
4919
+ if (!index) {
4920
+ index = createLodSpatialIndex((entry) => {
4921
+ const meshBounds = entry.mesh ? getTransformedLodBounds(entry.matrix, entry.mesh) : void 0;
4922
+ return meshBounds && entry.bounds ? entry.bounds.clone().union(meshBounds) : meshBounds ?? entry.bounds;
4923
+ });
4924
+ spatialCaches.set(editor, index);
4925
+ }
4926
+ index.update(content);
4927
+ return index;
4928
+ }
4929
+ function readContent(editor) {
4930
+ let cache = contentCaches.get(editor);
4931
+ if (!cache) {
4932
+ cache = makeContentCache(editor);
4933
+ contentCaches.set(editor, cache);
4934
+ }
4935
+ return cache.get();
4936
+ }
4693
4937
  function decimate(vertices) {
4694
4938
  const count = vertices.length;
4695
4939
  if (count === 0) return new Float32Array(0);
@@ -4750,10 +4994,13 @@ function isInteractive(editor, shape, selected, editingId) {
4750
4994
  return false;
4751
4995
  }
4752
4996
  function readFrame(editor) {
4997
+ const content = readContent(editor);
4753
4998
  const viewport = editor.getViewportPageBounds();
4754
4999
  const pad = Math.max(viewport.w, viewport.h) * LOD_CULL_MARGIN;
4755
5000
  const selectedIds = editor.getSelectedShapeIds();
4756
5001
  return {
5002
+ candidates: readCandidates(editor, content),
5003
+ content,
4757
5004
  zoom: editor.getZoomLevel(),
4758
5005
  theme: editor.getCurrentTheme(),
4759
5006
  colorMode: editor.getColorMode(),
@@ -4771,17 +5018,17 @@ function outsideSet(editor, frame, shapes) {
4771
5018
  const live = /* @__PURE__ */ new Set();
4772
5019
  if (!frame.debug) {
4773
5020
  for (const shape of shapes) {
4774
- if (editor.isShapeHidden(shape)) continue;
4775
- const style = lodStyleFor(editor, shape, frame);
5021
+ if (frame.content.get(shape.id)?.hidden) continue;
5022
+ const style = frame.content.get(shape.id)?.style ?? null;
4776
5023
  if (!style) continue;
4777
5024
  if (style.unmountWhenCulled) {
4778
- const bounds = editor.getShapePageBounds(shape);
5025
+ const bounds = frame.content.get(shape.id)?.bounds;
4779
5026
  if (bounds && !frame.expanded.collides(bounds)) live.add(shape.id);
4780
5027
  continue;
4781
5028
  }
4782
- const mesh = meshFor(editor, shape, style);
5029
+ const mesh = frame.content.get(shape.id)?.mesh;
4783
5030
  if (!mesh) continue;
4784
- const matrix = editor.getShapePageTransform(shape);
5031
+ const matrix = frame.content.get(shape.id).matrix;
4785
5032
  if (!frame.expanded.collides(getTransformedLodBounds(matrix, mesh))) live.add(shape.id);
4786
5033
  }
4787
5034
  }
@@ -4800,10 +5047,10 @@ function outsideSet(editor, frame, shapes) {
4800
5047
  return retained;
4801
5048
  }
4802
5049
  function ownRenderMode(editor, shape, frame, outsideViewport) {
4803
- if (frame.debug || editor.isShapeHidden(shape)) return "dom";
4804
- const style = lodStyleFor(editor, shape, frame);
4805
- const mesh = style && !style.unmountWhenCulled ? meshFor(editor, shape, style) : null;
4806
- const pageBounds = editor.getShapePageBounds(shape);
5050
+ if (frame.debug || frame.content.get(shape.id)?.hidden) return "dom";
5051
+ const style = frame.content.get(shape.id)?.style ?? null;
5052
+ const mesh = style && !style.unmountWhenCulled ? frame.content.get(shape.id)?.mesh : null;
5053
+ const pageBounds = frame.content.get(shape.id)?.bounds;
4807
5054
  const pageWidth = pageBounds?.w ?? mesh?.w ?? 0;
4808
5055
  const pageHeight = pageBounds?.h ?? mesh?.h ?? 0;
4809
5056
  Math.max(pageWidth, pageHeight) * frame.zoom;
@@ -4949,6 +5196,7 @@ var isMemoryConstrained = (() => {
4949
5196
  var LOD_OVERSCAN_SCREEN_PX = isMemoryConstrained ? 96 : 192;
4950
5197
  var LOD_BITMAP_MAX_AREA = isMemoryConstrained ? 8e6 : 24e6;
4951
5198
  function lodBitmapCovers(bitmap, camera, viewport, dpr, shapes, frame, theme, colorMode) {
5199
+ if (bitmap.content !== frame.content) return false;
4952
5200
  if (bitmap.zoom !== camera.z || bitmap.dpr !== dpr) return false;
4953
5201
  if (bitmap.snapshotEpoch !== lodSnapshotEpoch.get()) return false;
4954
5202
  if (bitmap.shapes !== shapes || bitmap.selectedIds !== frame.selectedIds) return false;
@@ -4980,23 +5228,23 @@ function blitLodBitmap(ctx, width, height, bitmap, camera) {
4980
5228
  (camera.y - bitmap.cameraY) * z * bitmap.dpr - padPx
4981
5229
  );
4982
5230
  }
4983
- function collectLodPaintJobs(editor, camera, shapes, frame, paintBounds) {
5231
+ function collectLodPaintJobs(editor, camera, frame, paintBounds) {
4984
5232
  const jobs = [];
4985
5233
  if (frame.debug) return jobs;
4986
- for (const shape of shapes) {
4987
- if (editor.isShapeHidden(shape)) continue;
4988
- const style = lodStyleFor(editor, shape, frame);
5234
+ for (const { shape } of frame.candidates.query(paintBounds)) {
5235
+ if (frame.content.get(shape.id)?.hidden) continue;
5236
+ const style = frame.content.get(shape.id)?.style ?? null;
4989
5237
  if (!style) continue;
4990
5238
  const mode = ownRenderMode(editor, shape, frame, false);
4991
5239
  if (mode === "unmounted") continue;
4992
5240
  if (mode === "dom" && !(style.snapshotBelowPx && style.snapshotBelowPx > 0)) continue;
4993
5241
  if (mode === "canvas" && coveredByDomBackground(editor, shape, frame)) continue;
4994
- const mesh = meshFor(editor, shape, style);
5242
+ const mesh = frame.content.get(shape.id)?.mesh;
4995
5243
  if (!mesh) continue;
4996
- const matrix = editor.getShapePageTransform(shape);
5244
+ const matrix = frame.content.get(shape.id).matrix;
4997
5245
  if (!paintBounds.collides(getTransformedLodBounds(matrix, mesh))) continue;
4998
5246
  const snapshot = getLodSnapshot(shape);
4999
- const page = (style.snapshotBelowPx ?? 0) > 0 ? editor.getShapePageBounds(shape) : void 0;
5247
+ const page = (style.snapshotBelowPx ?? 0) > 0 ? frame.content.get(shape.id)?.bounds : void 0;
5000
5248
  const screenMax = page ? Math.max(page.w, page.h) * (camera.z || 1) : 0;
5001
5249
  const nearFreeze = (style.snapshotBelowPx ?? 0) > 0 && screenMax < (style.snapshotBelowPx ?? 0) * 1.35;
5002
5250
  const useSnapshot = Boolean(snapshot) && (mode === "canvas" || nearFreeze);
@@ -5015,15 +5263,57 @@ function paintLodJobs(ctx, camera, dpr, jobs, padPx) {
5015
5263
  function useMountedRenderingShapes() {
5016
5264
  const editor = useEditor();
5017
5265
  const mountedRef = useRef([]);
5266
+ const panCacheRef = useRef(
5267
+ null
5268
+ );
5269
+ if (!panCacheRef.current) panCacheRef.current = createLodPanMountCache();
5018
5270
  return useValue(
5019
5271
  "mounted rendering shapes",
5020
5272
  () => {
5021
- lodSnapshotEpoch.get();
5273
+ const snapshotEpoch = lodSnapshotEpoch.get();
5274
+ const framePreview = readFrame(editor);
5275
+ if (isMemoryConstrained && framePreview.moving && mountedRef.current.length > 0) {
5276
+ return mountedRef.current;
5277
+ }
5022
5278
  const rendering = editor.getRenderingShapes();
5023
5279
  const shapes = editor.getCurrentPageShapesSorted();
5024
5280
  editor.getCurrentTheme();
5025
5281
  editor.getColorMode();
5026
- const frame = readFrame(editor);
5282
+ const frame = framePreview;
5283
+ const key = [
5284
+ frame.content,
5285
+ frame.zoom,
5286
+ frame.lodZoom,
5287
+ frame.debug,
5288
+ frame.selectedIds,
5289
+ frame.editingId,
5290
+ snapshotEpoch
5291
+ ];
5292
+ if (frame.moving && !frame.debug) {
5293
+ const extended = unsafe__withoutCapture(
5294
+ () => panCacheRef.current.extend(
5295
+ rendering,
5296
+ key,
5297
+ () => {
5298
+ const candidates = frame.candidates.query(frame.expanded);
5299
+ const retained = retainedOutside.get(editor);
5300
+ for (const entry of candidates) {
5301
+ const bounds = entry.style?.unmountWhenCulled ? entry.bounds : entry.mesh ? getTransformedLodBounds(entry.matrix, entry.mesh) : void 0;
5302
+ if (!bounds || frame.expanded.collides(bounds)) retained?.delete(entry.shape.id);
5303
+ }
5304
+ return candidates.map((entry) => entry.shape.id);
5305
+ },
5306
+ (entry) => {
5307
+ if (modeFor(editor, entry.shape, frame, /* @__PURE__ */ new Set()) !== "dom") return false;
5308
+ return true;
5309
+ }
5310
+ )
5311
+ );
5312
+ if (extended) {
5313
+ mountedRef.current = extended;
5314
+ return extended;
5315
+ }
5316
+ }
5027
5317
  const outside = unsafe__withoutCapture(() => outsideSet(editor, frame, shapes));
5028
5318
  const next = [];
5029
5319
  for (const renderingShape of rendering) {
@@ -5037,8 +5327,10 @@ function useMountedRenderingShapes() {
5037
5327
  const other = next[index];
5038
5328
  return entry.id === other.id && entry.shape === other.shape && entry.index === other.index && entry.backgroundIndex === other.backgroundIndex && entry.opacity === other.opacity;
5039
5329
  })) {
5330
+ panCacheRef.current.reset(rendering, key, prev);
5040
5331
  return prev;
5041
5332
  }
5333
+ panCacheRef.current.reset(rendering, key, next);
5042
5334
  mountedRef.current = next;
5043
5335
  return next;
5044
5336
  },
@@ -5058,13 +5350,13 @@ function captureDueSnapshots(editor) {
5058
5350
  let captured = 0;
5059
5351
  let deferred = false;
5060
5352
  let changed = false;
5061
- for (const shape of editor.getCurrentPageShapesSorted()) {
5062
- if (editor.isShapeHidden(shape)) continue;
5063
- const style = lodStyleFor(editor, shape, frame);
5353
+ for (const { shape } of frame.candidates.query(frame.expanded)) {
5354
+ if (frame.content.get(shape.id)?.hidden) continue;
5355
+ const style = frame.content.get(shape.id)?.style ?? null;
5064
5356
  const below = style?.snapshotBelowPx ?? 0;
5065
5357
  if (below <= 0) continue;
5066
5358
  if (isInteractive(editor, shape, frame.selected, frame.editingId)) continue;
5067
- const bounds = editor.getShapePageBounds(shape);
5359
+ const bounds = frame.content.get(shape.id)?.bounds;
5068
5360
  if (!bounds || !frame.expanded.collides(bounds)) continue;
5069
5361
  const screenMax = Math.max(bounds.w, bounds.h) * frame.zoom;
5070
5362
  if (screenMax < 8 || screenMax > below * 1.35) continue;
@@ -5110,6 +5402,7 @@ function LodCanvas() {
5110
5402
  const height = Math.max(1, Math.round((screen.h || canvas.clientHeight) * dpr));
5111
5403
  const ctx = canvas.getContext("2d");
5112
5404
  if (!ctx) return;
5405
+ if (isMemoryConstrained && frame.moving) return;
5113
5406
  const sizeScreenCanvas = () => {
5114
5407
  if (canvas.width !== width || canvas.height !== height) {
5115
5408
  canvas.width = width;
@@ -5138,10 +5431,11 @@ function LodCanvas() {
5138
5431
  const zoom = camera.z || 1;
5139
5432
  const pad = LOD_OVERSCAN_SCREEN_PX / zoom;
5140
5433
  const paintBounds = frame.viewport.clone().expandBy(pad);
5141
- const jobs = collectLodPaintJobs(editor, camera, shapes, frame, paintBounds);
5434
+ const jobs = collectLodPaintJobs(editor, camera, frame, paintBounds);
5142
5435
  if (jobs.length === 0) {
5143
5436
  releaseCanvases(cached);
5144
5437
  bitmapRef.current = frame.debug ? null : {
5438
+ content: frame.content,
5145
5439
  zoom: camera.z,
5146
5440
  dpr,
5147
5441
  cameraX: camera.x,
@@ -5186,6 +5480,7 @@ function LodCanvas() {
5186
5480
  paintCtx.clearRect(0, 0, cacheW, cacheH);
5187
5481
  paintLodJobs(paintCtx, camera, dpr, jobs, pad * zoom * dpr);
5188
5482
  const bitmap = {
5483
+ content: frame.content,
5189
5484
  zoom: camera.z,
5190
5485
  dpr,
5191
5486
  cameraX: camera.x,
@@ -14270,104 +14565,6 @@ __decorateClass([
14270
14565
  __decorateClass([
14271
14566
  computed
14272
14567
  ], SnapManager.prototype, "getCurrentCommonAncestor");
14273
- var DrawRBush = class extends RBush {
14274
- };
14275
- var RBushIndex = class {
14276
- rBush;
14277
- elementsInTree;
14278
- constructor() {
14279
- this.rBush = new DrawRBush();
14280
- this.elementsInTree = /* @__PURE__ */ new Map();
14281
- }
14282
- /**
14283
- * Search for shapes within the given bounds.
14284
- * Returns set of shape IDs that intersect with the bounds.
14285
- */
14286
- search(bounds) {
14287
- const results = this.rBush.search({
14288
- minX: bounds.minX,
14289
- minY: bounds.minY,
14290
- maxX: bounds.maxX,
14291
- maxY: bounds.maxY
14292
- });
14293
- return new Set(results.map((e) => e.id));
14294
- }
14295
- /**
14296
- * Remove a shape from the spatial index.
14297
- */
14298
- remove(id) {
14299
- const element = this.elementsInTree.get(id);
14300
- if (element) {
14301
- this.rBush.remove(element);
14302
- this.elementsInTree.delete(id);
14303
- }
14304
- }
14305
- /**
14306
- * Apply the removals and upserts collected during one incremental update.
14307
- * Mutations are applied per item: bulk-rebuilding the whole tree instead
14308
- * was measured slower even with ~10% of the index in the batch.
14309
- */
14310
- applyBatch(removes, upserts) {
14311
- for (const id of removes) {
14312
- this.remove(id);
14313
- }
14314
- for (const element of upserts) {
14315
- const existing = this.elementsInTree.get(element.id);
14316
- if (existing) {
14317
- this.rBush.remove(existing);
14318
- }
14319
- this.rBush.insert(element);
14320
- this.elementsInTree.set(element.id, element);
14321
- }
14322
- }
14323
- /**
14324
- * Bulk load elements into the spatial index.
14325
- * More efficient than individual inserts for initial loading.
14326
- */
14327
- bulkLoad(elements) {
14328
- this.rBush.load(elements);
14329
- for (const element of elements) {
14330
- this.elementsInTree.set(element.id, element);
14331
- }
14332
- }
14333
- /**
14334
- * Clear all elements from the spatial index.
14335
- */
14336
- clear() {
14337
- this.rBush.clear();
14338
- this.elementsInTree.clear();
14339
- }
14340
- /**
14341
- * Check if a shape is in the spatial index.
14342
- */
14343
- has(id) {
14344
- return this.elementsInTree.has(id);
14345
- }
14346
- /**
14347
- * Get the number of elements in the spatial index.
14348
- */
14349
- getSize() {
14350
- return this.elementsInTree.size;
14351
- }
14352
- /**
14353
- * Get the raw stored element for a shape, without allocating a Box.
14354
- * Use when you only need to read the indexed bounds for comparison.
14355
- *
14356
- * @internal
14357
- */
14358
- getElement(id) {
14359
- return this.elementsInTree.get(id);
14360
- }
14361
- /**
14362
- * Dispose of the spatial index.
14363
- * Clears all data structures to prevent memory leaks.
14364
- */
14365
- dispose() {
14366
- this.clear();
14367
- }
14368
- };
14369
-
14370
- // src/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.ts
14371
14568
  var SpatialIndexManager = class {
14372
14569
  constructor(editor) {
14373
14570
  this.editor = editor;
@@ -17885,6 +18082,9 @@ var Editor = class extends EventEmitter {
17885
18082
  _setCamera(point, opts) {
17886
18083
  const currentCamera = this.getCamera();
17887
18084
  const { x, y, z } = this.getConstrainedCamera(point, opts);
18085
+ if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(z)) {
18086
+ return this;
18087
+ }
17888
18088
  if (currentCamera.x === x && currentCamera.y === y && currentCamera.z === z) {
17889
18089
  return this;
17890
18090
  }
@@ -17970,7 +18170,11 @@ var Editor = class extends EventEmitter {
17970
18170
  const { isLocked } = this.getCameraOptions();
17971
18171
  if (isLocked && !opts?.force) return this;
17972
18172
  const { width: pw, height: ph } = this.getViewportPageBounds();
17973
- this.setCamera(new Vec(-(point.x - pw / 2), -(point.y - ph / 2), this.getCamera().z), opts);
18173
+ const x = -(point.x - pw / 2);
18174
+ const y = -(point.y - ph / 2);
18175
+ const z = this.getCamera().z;
18176
+ if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(z)) return this;
18177
+ this.setCamera(new Vec(x, y, z), opts);
17974
18178
  return this;
17975
18179
  }
17976
18180
  /**
@@ -26647,7 +26851,7 @@ function getFontsFromRichText(editor, richText, initialState2) {
26647
26851
  // src/index.ts
26648
26852
  registerDrawLibraryVersion(
26649
26853
  "@ibodr/editor",
26650
- "0.2.19",
26854
+ "0.2.21",
26651
26855
  "esm"
26652
26856
  );
26653
26857
  /*!