@ibodr/editor 0.2.18 → 0.2.20

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,274 @@ 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
+ }
4478
+
4479
+ // src/lib/editor/lod/lodPaintCache.ts
4480
+ var labels = /* @__PURE__ */ new WeakMap();
4481
+ function getLodLabelLayout(ctx, mesh) {
4482
+ if (labels.has(mesh)) return labels.get(mesh);
4483
+ const layout = measureLabel(ctx, mesh);
4484
+ labels.set(mesh, layout);
4485
+ return layout;
4486
+ }
4487
+ function measureLabel(ctx, mesh) {
4488
+ const text = mesh.label?.trim();
4489
+ if (!text || mesh.w < 4 || mesh.h < 4) return null;
4490
+ const pad = Math.min(mesh.w, mesh.h) * 0.08;
4491
+ const maxWidth = mesh.w - pad * 2, innerH = mesh.h - pad * 2;
4492
+ if (maxWidth < 4 || innerH < 4) return null;
4493
+ const preferred = mesh.labelSize > 0 ? mesh.labelSize : innerH * 0.4;
4494
+ const size = Math.min(Math.max(preferred, innerH * 0.22), innerH * 0.82);
4495
+ if (size < 1) return null;
4496
+ const font = `${size}px sans-serif`;
4497
+ const previousFont = ctx.font;
4498
+ ctx.font = font;
4499
+ const lines = [];
4500
+ let line = "";
4501
+ try {
4502
+ for (const word of text.split(/\s+/)) {
4503
+ const next = line ? `${line} ${word}` : word;
4504
+ if (line && ctx.measureText(next).width > maxWidth) {
4505
+ lines.push(line);
4506
+ line = word;
4507
+ } else line = next;
4508
+ }
4509
+ } finally {
4510
+ ctx.font = previousFont;
4511
+ }
4512
+ if (line) lines.push(line);
4513
+ const lineHeight = size * 1.15;
4514
+ const maxLines = Math.max(1, Math.floor(innerH / lineHeight));
4515
+ const fits = lines.length <= maxLines;
4516
+ const visible = fits ? lines : lines.slice(0, maxLines);
4517
+ const blockH = visible.length * lineHeight;
4518
+ return {
4519
+ font,
4520
+ maxWidth,
4521
+ lineHeight,
4522
+ lines: visible,
4523
+ x: mesh.x + mesh.w / 2,
4524
+ y: fits ? mesh.y + mesh.h / 2 - blockH / 2 + lineHeight / 2 : mesh.y + pad + lineHeight / 2
4525
+ };
4526
+ }
4527
+ var boundsCache = /* @__PURE__ */ new WeakMap();
4528
+ function getTransformedLodBounds(matrix, mesh) {
4529
+ const { a, b, c, d, e, f } = matrix;
4530
+ const cached = boundsCache.get(mesh);
4531
+ if (cached && cached.a === a && cached.b === b && cached.c === c && cached.d === d && cached.e === e && cached.f === f)
4532
+ return cached.bounds;
4533
+ const x0 = a * mesh.x + c * mesh.y + e, y0 = b * mesh.x + d * mesh.y + f;
4534
+ const dx = a * mesh.w, dy = b * mesh.w, ex = c * mesh.h, ey = d * mesh.h;
4535
+ const bounds = new Box(
4536
+ x0 + Math.min(0, dx) + Math.min(0, ex),
4537
+ y0 + Math.min(0, dy) + Math.min(0, ey),
4538
+ Math.abs(dx) + Math.abs(ex),
4539
+ Math.abs(dy) + Math.abs(ey)
4540
+ );
4541
+ boundsCache.set(mesh, { a, b, c, d, e, f, bounds });
4542
+ return bounds;
4543
+ }
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
+ }
4460
4728
 
4461
4729
  // src/lib/editor/lod/shapeRenderMode.ts
4462
4730
  var LOD_KEEP_SCREEN_PX = 36;
@@ -4611,9 +4879,8 @@ var MAX_LOD_POINTS = 48;
4611
4879
  var meshCache = /* @__PURE__ */ new WeakMap();
4612
4880
  var styleCache = /* @__PURE__ */ new WeakMap();
4613
4881
  var retainedOutside = /* @__PURE__ */ new WeakMap();
4614
- function lodStyleFor(editor, shape) {
4615
- const theme = editor.getCurrentTheme();
4616
- const colorMode = editor.getColorMode();
4882
+ function lodStyleFor(editor, shape, frame) {
4883
+ const { theme, colorMode } = frame;
4617
4884
  const cached = styleCache.get(shape);
4618
4885
  if (cached && cached.theme === theme && cached.colorMode === colorMode) return cached.style;
4619
4886
  let style = null;
@@ -4625,6 +4892,48 @@ function lodStyleFor(editor, shape) {
4625
4892
  styleCache.set(shape, { theme, colorMode, style });
4626
4893
  return style;
4627
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
+ }
4628
4937
  function decimate(vertices) {
4629
4938
  const count = vertices.length;
4630
4939
  if (count === 0) return new Float32Array(0);
@@ -4671,24 +4980,13 @@ function meshFor(editor, shape, style) {
4671
4980
  meshCache.set(shape, { style, mesh });
4672
4981
  return mesh;
4673
4982
  }
4674
- function transformedBounds(matrix, mesh) {
4675
- const x0 = mesh.x;
4676
- const y0 = mesh.y;
4677
- const x1 = mesh.x + mesh.w;
4678
- const y1 = mesh.y + mesh.h;
4679
- return Box.FromPoints([
4680
- matrix.applyToPoint({ x: x0, y: y0 }),
4681
- matrix.applyToPoint({ x: x1, y: y0 }),
4682
- matrix.applyToPoint({ x: x1, y: y1 }),
4683
- matrix.applyToPoint({ x: x0, y: y1 })
4684
- ]);
4685
- }
4686
4983
  function isInteractive(editor, shape, selected, editingId) {
4984
+ if (selected.size === 0 && editingId === null) return false;
4687
4985
  let current = shape;
4688
4986
  for (let depth = 0; depth < 32 && current; depth++) {
4689
4987
  if (current.id === editingId || selected.has(current.id)) return true;
4690
4988
  const parentId = current.parentId;
4691
- if (!editor.store.has(parentId)) return false;
4989
+ if (!parentId.startsWith("shape:") || !editor.store.has(parentId)) return false;
4692
4990
  const parent = editor.store.unsafeGetWithoutCapture(parentId);
4693
4991
  if (!parent || parent.typeName !== "shape") return false;
4694
4992
  current = parent;
@@ -4696,11 +4994,16 @@ function isInteractive(editor, shape, selected, editingId) {
4696
4994
  return false;
4697
4995
  }
4698
4996
  function readFrame(editor) {
4997
+ const content = readContent(editor);
4699
4998
  const viewport = editor.getViewportPageBounds();
4700
4999
  const pad = Math.max(viewport.w, viewport.h) * LOD_CULL_MARGIN;
4701
5000
  const selectedIds = editor.getSelectedShapeIds();
4702
5001
  return {
5002
+ candidates: readCandidates(editor, content),
5003
+ content,
4703
5004
  zoom: editor.getZoomLevel(),
5005
+ theme: editor.getCurrentTheme(),
5006
+ colorMode: editor.getColorMode(),
4704
5007
  lodZoom: editor.options.lodZoom,
4705
5008
  debug: debugFlags.debugSvg.get() && editor.getInstanceState().isDebugMode,
4706
5009
  moving: editor.getCameraState() === "moving",
@@ -4715,18 +5018,18 @@ function outsideSet(editor, frame, shapes) {
4715
5018
  const live = /* @__PURE__ */ new Set();
4716
5019
  if (!frame.debug) {
4717
5020
  for (const shape of shapes) {
4718
- if (editor.isShapeHidden(shape)) continue;
4719
- const style = lodStyleFor(editor, shape);
5021
+ if (frame.content.get(shape.id)?.hidden) continue;
5022
+ const style = frame.content.get(shape.id)?.style ?? null;
4720
5023
  if (!style) continue;
4721
5024
  if (style.unmountWhenCulled) {
4722
- const bounds = editor.getShapePageBounds(shape);
5025
+ const bounds = frame.content.get(shape.id)?.bounds;
4723
5026
  if (bounds && !frame.expanded.collides(bounds)) live.add(shape.id);
4724
5027
  continue;
4725
5028
  }
4726
- const mesh = meshFor(editor, shape, style);
5029
+ const mesh = frame.content.get(shape.id)?.mesh;
4727
5030
  if (!mesh) continue;
4728
- const matrix = editor.getShapePageTransform(shape);
4729
- if (!frame.expanded.collides(transformedBounds(matrix, mesh))) live.add(shape.id);
5031
+ const matrix = frame.content.get(shape.id).matrix;
5032
+ if (!frame.expanded.collides(getTransformedLodBounds(matrix, mesh))) live.add(shape.id);
4730
5033
  }
4731
5034
  }
4732
5035
  if (!frame.moving) {
@@ -4744,10 +5047,10 @@ function outsideSet(editor, frame, shapes) {
4744
5047
  return retained;
4745
5048
  }
4746
5049
  function ownRenderMode(editor, shape, frame, outsideViewport) {
4747
- if (frame.debug || editor.isShapeHidden(shape)) return "dom";
4748
- const style = lodStyleFor(editor, shape);
4749
- const mesh = style && !style.unmountWhenCulled ? meshFor(editor, shape, style) : null;
4750
- 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;
4751
5054
  const pageWidth = pageBounds?.w ?? mesh?.w ?? 0;
4752
5055
  const pageHeight = pageBounds?.h ?? mesh?.h ?? 0;
4753
5056
  Math.max(pageWidth, pageHeight) * frame.zoom;
@@ -4764,7 +5067,7 @@ function ownRenderMode(editor, shape, frame, outsideViewport) {
4764
5067
  pageHeight,
4765
5068
  labelSize: style?.labelSize ?? 0,
4766
5069
  snapshotBelowPx: style?.snapshotBelowPx,
4767
- hasSnapshot: snapshotCoversShape(shape)
5070
+ hasSnapshot: (style?.snapshotBelowPx ?? 0) > 0 && snapshotCoversShape(shape)
4768
5071
  });
4769
5072
  }
4770
5073
  function snapshotCoversShape(shape, screenMax) {
@@ -4773,7 +5076,7 @@ function snapshotCoversShape(shape, screenMax) {
4773
5076
  function coveredByDomBackground(editor, shape, frame) {
4774
5077
  let parentId = shape.parentId;
4775
5078
  for (let depth = 0; depth < 32; depth++) {
4776
- if (!editor.store.has(parentId)) return false;
5079
+ if (!parentId.startsWith("shape:") || !editor.store.has(parentId)) return false;
4777
5080
  const parent = editor.store.unsafeGetWithoutCapture(parentId);
4778
5081
  if (!parent || parent.typeName !== "shape") return false;
4779
5082
  const parentShape = parent;
@@ -4865,46 +5168,20 @@ function paintSnapshot(ctx, camera, dpr, matrix, mesh, bitmap, padPx = 0) {
4865
5168
  ctx.restore();
4866
5169
  }
4867
5170
  function paintLodLabel(ctx, mesh) {
4868
- const text = mesh.label?.trim();
4869
- if (!text || mesh.w < 4 || mesh.h < 4) return;
4870
- const pad = Math.min(mesh.w, mesh.h) * 0.08;
4871
- const maxWidth = mesh.w - pad * 2;
4872
- const innerH = mesh.h - pad * 2;
4873
- if (maxWidth < 4 || innerH < 4) return;
4874
- const preferred = mesh.labelSize > 0 ? mesh.labelSize : innerH * 0.4;
4875
- const size = Math.min(Math.max(preferred, innerH * 0.22), innerH * 0.82);
4876
- if (size < 1) return;
5171
+ const layout = getLodLabelLayout(ctx, mesh);
5172
+ if (!layout) return;
4877
5173
  ctx.save();
4878
5174
  ctx.beginPath();
4879
5175
  ctx.rect(mesh.x, mesh.y, mesh.w, mesh.h);
4880
5176
  ctx.clip();
4881
5177
  ctx.fillStyle = mesh.labelColor || "#1d1d1d";
4882
- ctx.font = `${size}px sans-serif`;
5178
+ ctx.font = layout.font;
4883
5179
  ctx.textAlign = "center";
4884
5180
  ctx.textBaseline = "middle";
4885
- const words = text.split(/\s+/);
4886
- const lines = [];
4887
- let line = "";
4888
- for (const word of words) {
4889
- const next = line ? `${line} ${word}` : word;
4890
- if (line && ctx.measureText(next).width > maxWidth) {
4891
- lines.push(line);
4892
- line = word;
4893
- } else {
4894
- line = next;
4895
- }
4896
- }
4897
- if (line) lines.push(line);
4898
- const lineHeight = size * 1.15;
4899
- const maxLines = Math.max(1, Math.floor(innerH / lineHeight));
4900
- const fits = lines.length <= maxLines;
4901
- const visible = fits ? lines : lines.slice(0, maxLines);
4902
- const blockH = visible.length * lineHeight;
4903
- let y = fits ? mesh.y + mesh.h / 2 - blockH / 2 + lineHeight / 2 : mesh.y + pad + lineHeight / 2;
4904
- const x = mesh.x + mesh.w / 2;
4905
- for (const row of visible) {
4906
- ctx.fillText(row, x, y, maxWidth);
4907
- y += lineHeight;
5181
+ let y = layout.y;
5182
+ for (const row of layout.lines) {
5183
+ ctx.fillText(row, layout.x, y, layout.maxWidth);
5184
+ y += layout.lineHeight;
4908
5185
  }
4909
5186
  ctx.restore();
4910
5187
  }
@@ -4919,6 +5196,7 @@ var isMemoryConstrained = (() => {
4919
5196
  var LOD_OVERSCAN_SCREEN_PX = isMemoryConstrained ? 96 : 192;
4920
5197
  var LOD_BITMAP_MAX_AREA = isMemoryConstrained ? 8e6 : 24e6;
4921
5198
  function lodBitmapCovers(bitmap, camera, viewport, dpr, shapes, frame, theme, colorMode) {
5199
+ if (bitmap.content !== frame.content) return false;
4922
5200
  if (bitmap.zoom !== camera.z || bitmap.dpr !== dpr) return false;
4923
5201
  if (bitmap.snapshotEpoch !== lodSnapshotEpoch.get()) return false;
4924
5202
  if (bitmap.shapes !== shapes || bitmap.selectedIds !== frame.selectedIds) return false;
@@ -4950,23 +5228,23 @@ function blitLodBitmap(ctx, width, height, bitmap, camera) {
4950
5228
  (camera.y - bitmap.cameraY) * z * bitmap.dpr - padPx
4951
5229
  );
4952
5230
  }
4953
- function collectLodPaintJobs(editor, camera, shapes, frame, paintBounds) {
5231
+ function collectLodPaintJobs(editor, camera, frame, paintBounds) {
4954
5232
  const jobs = [];
4955
5233
  if (frame.debug) return jobs;
4956
- for (const shape of shapes) {
4957
- if (editor.isShapeHidden(shape)) continue;
4958
- const style = lodStyleFor(editor, shape);
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;
4959
5237
  if (!style) continue;
4960
5238
  const mode = ownRenderMode(editor, shape, frame, false);
4961
5239
  if (mode === "unmounted") continue;
4962
5240
  if (mode === "dom" && !(style.snapshotBelowPx && style.snapshotBelowPx > 0)) continue;
4963
5241
  if (mode === "canvas" && coveredByDomBackground(editor, shape, frame)) continue;
4964
- const mesh = meshFor(editor, shape, style);
5242
+ const mesh = frame.content.get(shape.id)?.mesh;
4965
5243
  if (!mesh) continue;
4966
- const matrix = editor.getShapePageTransform(shape);
4967
- if (!paintBounds.collides(transformedBounds(matrix, mesh))) continue;
5244
+ const matrix = frame.content.get(shape.id).matrix;
5245
+ if (!paintBounds.collides(getTransformedLodBounds(matrix, mesh))) continue;
4968
5246
  const snapshot = getLodSnapshot(shape);
4969
- const page = editor.getShapePageBounds(shape);
5247
+ const page = (style.snapshotBelowPx ?? 0) > 0 ? frame.content.get(shape.id)?.bounds : void 0;
4970
5248
  const screenMax = page ? Math.max(page.w, page.h) * (camera.z || 1) : 0;
4971
5249
  const nearFreeze = (style.snapshotBelowPx ?? 0) > 0 && screenMax < (style.snapshotBelowPx ?? 0) * 1.35;
4972
5250
  const useSnapshot = Boolean(snapshot) && (mode === "canvas" || nearFreeze);
@@ -4985,15 +5263,57 @@ function paintLodJobs(ctx, camera, dpr, jobs, padPx) {
4985
5263
  function useMountedRenderingShapes() {
4986
5264
  const editor = useEditor();
4987
5265
  const mountedRef = useRef([]);
5266
+ const panCacheRef = useRef(
5267
+ null
5268
+ );
5269
+ if (!panCacheRef.current) panCacheRef.current = createLodPanMountCache();
4988
5270
  return useValue(
4989
5271
  "mounted rendering shapes",
4990
5272
  () => {
4991
- 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
+ }
4992
5278
  const rendering = editor.getRenderingShapes();
4993
5279
  const shapes = editor.getCurrentPageShapesSorted();
4994
5280
  editor.getCurrentTheme();
4995
5281
  editor.getColorMode();
4996
- 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
+ }
4997
5317
  const outside = unsafe__withoutCapture(() => outsideSet(editor, frame, shapes));
4998
5318
  const next = [];
4999
5319
  for (const renderingShape of rendering) {
@@ -5007,8 +5327,10 @@ function useMountedRenderingShapes() {
5007
5327
  const other = next[index];
5008
5328
  return entry.id === other.id && entry.shape === other.shape && entry.index === other.index && entry.backgroundIndex === other.backgroundIndex && entry.opacity === other.opacity;
5009
5329
  })) {
5330
+ panCacheRef.current.reset(rendering, key, prev);
5010
5331
  return prev;
5011
5332
  }
5333
+ panCacheRef.current.reset(rendering, key, next);
5012
5334
  mountedRef.current = next;
5013
5335
  return next;
5014
5336
  },
@@ -5028,13 +5350,13 @@ function captureDueSnapshots(editor) {
5028
5350
  let captured = 0;
5029
5351
  let deferred = false;
5030
5352
  let changed = false;
5031
- for (const shape of editor.getCurrentPageShapesSorted()) {
5032
- if (editor.isShapeHidden(shape)) continue;
5033
- const style = lodStyleFor(editor, shape);
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;
5034
5356
  const below = style?.snapshotBelowPx ?? 0;
5035
5357
  if (below <= 0) continue;
5036
5358
  if (isInteractive(editor, shape, frame.selected, frame.editingId)) continue;
5037
- const bounds = editor.getShapePageBounds(shape);
5359
+ const bounds = frame.content.get(shape.id)?.bounds;
5038
5360
  if (!bounds || !frame.expanded.collides(bounds)) continue;
5039
5361
  const screenMax = Math.max(bounds.w, bounds.h) * frame.zoom;
5040
5362
  if (screenMax < 8 || screenMax > below * 1.35) continue;
@@ -5060,6 +5382,7 @@ function LodCanvas() {
5060
5382
  const editor = useEditor();
5061
5383
  const canvasRef = useRef(null);
5062
5384
  const bitmapRef = useRef(null);
5385
+ const lastPaintZoom = useRef(null);
5063
5386
  useQuickReactor(
5064
5387
  "lod canvas",
5065
5388
  () => {
@@ -5067,6 +5390,8 @@ function LodCanvas() {
5067
5390
  const canvas = canvasRef.current;
5068
5391
  if (!canvas) return;
5069
5392
  const camera = editor.getCamera();
5393
+ const zoomChanged = lastPaintZoom.current !== null && lastPaintZoom.current !== camera.z;
5394
+ lastPaintZoom.current = camera.z;
5070
5395
  const screen = editor.getViewportScreenBounds();
5071
5396
  const shapes = editor.getCurrentPageShapesSorted();
5072
5397
  const theme = editor.getCurrentTheme();
@@ -5077,6 +5402,7 @@ function LodCanvas() {
5077
5402
  const height = Math.max(1, Math.round((screen.h || canvas.clientHeight) * dpr));
5078
5403
  const ctx = canvas.getContext("2d");
5079
5404
  if (!ctx) return;
5405
+ if (isMemoryConstrained && frame.moving) return;
5080
5406
  const sizeScreenCanvas = () => {
5081
5407
  if (canvas.width !== width || canvas.height !== height) {
5082
5408
  canvas.width = width;
@@ -5105,10 +5431,11 @@ function LodCanvas() {
5105
5431
  const zoom = camera.z || 1;
5106
5432
  const pad = LOD_OVERSCAN_SCREEN_PX / zoom;
5107
5433
  const paintBounds = frame.viewport.clone().expandBy(pad);
5108
- const jobs = collectLodPaintJobs(editor, camera, shapes, frame, paintBounds);
5434
+ const jobs = collectLodPaintJobs(editor, camera, frame, paintBounds);
5109
5435
  if (jobs.length === 0) {
5110
5436
  releaseCanvases(cached);
5111
5437
  bitmapRef.current = frame.debug ? null : {
5438
+ content: frame.content,
5112
5439
  zoom: camera.z,
5113
5440
  dpr,
5114
5441
  cameraX: camera.x,
@@ -5131,7 +5458,7 @@ function LodCanvas() {
5131
5458
  ctx.clearRect(0, 0, width, height);
5132
5459
  const cacheW = Math.round((frame.viewport.w + pad * 2) * zoom * dpr);
5133
5460
  const cacheH = Math.round((frame.viewport.h + pad * 2) * zoom * dpr);
5134
- const canCache = cacheW > 0 && cacheH > 0 && cacheW <= LOD_BITMAP_MAX_PX && cacheH <= LOD_BITMAP_MAX_PX && cacheW * cacheH <= LOD_BITMAP_MAX_AREA;
5461
+ const canCache = !zoomChanged && cacheW > 0 && cacheH > 0 && cacheW <= LOD_BITMAP_MAX_PX && cacheH <= LOD_BITMAP_MAX_PX && cacheW * cacheH <= LOD_BITMAP_MAX_AREA;
5135
5462
  if (!canCache) {
5136
5463
  if (cached?.canvas) {
5137
5464
  cached.canvas.width = 0;
@@ -5153,6 +5480,7 @@ function LodCanvas() {
5153
5480
  paintCtx.clearRect(0, 0, cacheW, cacheH);
5154
5481
  paintLodJobs(paintCtx, camera, dpr, jobs, pad * zoom * dpr);
5155
5482
  const bitmap = {
5483
+ content: frame.content,
5156
5484
  zoom: camera.z,
5157
5485
  dpr,
5158
5486
  cameraX: camera.x,
@@ -14237,104 +14565,6 @@ __decorateClass([
14237
14565
  __decorateClass([
14238
14566
  computed
14239
14567
  ], SnapManager.prototype, "getCurrentCommonAncestor");
14240
- var DrawRBush = class extends RBush {
14241
- };
14242
- var RBushIndex = class {
14243
- rBush;
14244
- elementsInTree;
14245
- constructor() {
14246
- this.rBush = new DrawRBush();
14247
- this.elementsInTree = /* @__PURE__ */ new Map();
14248
- }
14249
- /**
14250
- * Search for shapes within the given bounds.
14251
- * Returns set of shape IDs that intersect with the bounds.
14252
- */
14253
- search(bounds) {
14254
- const results = this.rBush.search({
14255
- minX: bounds.minX,
14256
- minY: bounds.minY,
14257
- maxX: bounds.maxX,
14258
- maxY: bounds.maxY
14259
- });
14260
- return new Set(results.map((e) => e.id));
14261
- }
14262
- /**
14263
- * Remove a shape from the spatial index.
14264
- */
14265
- remove(id) {
14266
- const element = this.elementsInTree.get(id);
14267
- if (element) {
14268
- this.rBush.remove(element);
14269
- this.elementsInTree.delete(id);
14270
- }
14271
- }
14272
- /**
14273
- * Apply the removals and upserts collected during one incremental update.
14274
- * Mutations are applied per item: bulk-rebuilding the whole tree instead
14275
- * was measured slower even with ~10% of the index in the batch.
14276
- */
14277
- applyBatch(removes, upserts) {
14278
- for (const id of removes) {
14279
- this.remove(id);
14280
- }
14281
- for (const element of upserts) {
14282
- const existing = this.elementsInTree.get(element.id);
14283
- if (existing) {
14284
- this.rBush.remove(existing);
14285
- }
14286
- this.rBush.insert(element);
14287
- this.elementsInTree.set(element.id, element);
14288
- }
14289
- }
14290
- /**
14291
- * Bulk load elements into the spatial index.
14292
- * More efficient than individual inserts for initial loading.
14293
- */
14294
- bulkLoad(elements) {
14295
- this.rBush.load(elements);
14296
- for (const element of elements) {
14297
- this.elementsInTree.set(element.id, element);
14298
- }
14299
- }
14300
- /**
14301
- * Clear all elements from the spatial index.
14302
- */
14303
- clear() {
14304
- this.rBush.clear();
14305
- this.elementsInTree.clear();
14306
- }
14307
- /**
14308
- * Check if a shape is in the spatial index.
14309
- */
14310
- has(id) {
14311
- return this.elementsInTree.has(id);
14312
- }
14313
- /**
14314
- * Get the number of elements in the spatial index.
14315
- */
14316
- getSize() {
14317
- return this.elementsInTree.size;
14318
- }
14319
- /**
14320
- * Get the raw stored element for a shape, without allocating a Box.
14321
- * Use when you only need to read the indexed bounds for comparison.
14322
- *
14323
- * @internal
14324
- */
14325
- getElement(id) {
14326
- return this.elementsInTree.get(id);
14327
- }
14328
- /**
14329
- * Dispose of the spatial index.
14330
- * Clears all data structures to prevent memory leaks.
14331
- */
14332
- dispose() {
14333
- this.clear();
14334
- }
14335
- };
14336
-
14337
- // src/lib/editor/managers/SpatialIndexManager/SpatialIndexManager.ts
14338
14568
  var SpatialIndexManager = class {
14339
14569
  constructor(editor) {
14340
14570
  this.editor = editor;
@@ -26614,7 +26844,7 @@ function getFontsFromRichText(editor, richText, initialState2) {
26614
26844
  // src/index.ts
26615
26845
  registerDrawLibraryVersion(
26616
26846
  "@ibodr/editor",
26617
- "0.2.18",
26847
+ "0.2.20",
26618
26848
  "esm"
26619
26849
  );
26620
26850
  /*!