@ibodr/editor 0.2.18 → 0.2.19

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
@@ -4458,6 +4458,72 @@ function drawGeometryStroke(ctx, geometry) {
4458
4458
  ctx.stroke();
4459
4459
  }
4460
4460
 
4461
+ // src/lib/editor/lod/lodPaintCache.ts
4462
+ var labels = /* @__PURE__ */ new WeakMap();
4463
+ function getLodLabelLayout(ctx, mesh) {
4464
+ if (labels.has(mesh)) return labels.get(mesh);
4465
+ const layout = measureLabel(ctx, mesh);
4466
+ labels.set(mesh, layout);
4467
+ return layout;
4468
+ }
4469
+ function measureLabel(ctx, mesh) {
4470
+ const text = mesh.label?.trim();
4471
+ if (!text || mesh.w < 4 || mesh.h < 4) return null;
4472
+ const pad = Math.min(mesh.w, mesh.h) * 0.08;
4473
+ const maxWidth = mesh.w - pad * 2, innerH = mesh.h - pad * 2;
4474
+ if (maxWidth < 4 || innerH < 4) return null;
4475
+ const preferred = mesh.labelSize > 0 ? mesh.labelSize : innerH * 0.4;
4476
+ const size = Math.min(Math.max(preferred, innerH * 0.22), innerH * 0.82);
4477
+ if (size < 1) return null;
4478
+ const font = `${size}px sans-serif`;
4479
+ const previousFont = ctx.font;
4480
+ ctx.font = font;
4481
+ const lines = [];
4482
+ let line = "";
4483
+ try {
4484
+ for (const word of text.split(/\s+/)) {
4485
+ const next = line ? `${line} ${word}` : word;
4486
+ if (line && ctx.measureText(next).width > maxWidth) {
4487
+ lines.push(line);
4488
+ line = word;
4489
+ } else line = next;
4490
+ }
4491
+ } finally {
4492
+ ctx.font = previousFont;
4493
+ }
4494
+ if (line) lines.push(line);
4495
+ const lineHeight = size * 1.15;
4496
+ const maxLines = Math.max(1, Math.floor(innerH / lineHeight));
4497
+ const fits = lines.length <= maxLines;
4498
+ const visible = fits ? lines : lines.slice(0, maxLines);
4499
+ const blockH = visible.length * lineHeight;
4500
+ return {
4501
+ font,
4502
+ maxWidth,
4503
+ lineHeight,
4504
+ lines: visible,
4505
+ x: mesh.x + mesh.w / 2,
4506
+ y: fits ? mesh.y + mesh.h / 2 - blockH / 2 + lineHeight / 2 : mesh.y + pad + lineHeight / 2
4507
+ };
4508
+ }
4509
+ var boundsCache = /* @__PURE__ */ new WeakMap();
4510
+ function getTransformedLodBounds(matrix, mesh) {
4511
+ const { a, b, c, d, e, f } = matrix;
4512
+ const cached = boundsCache.get(mesh);
4513
+ if (cached && cached.a === a && cached.b === b && cached.c === c && cached.d === d && cached.e === e && cached.f === f)
4514
+ return cached.bounds;
4515
+ const x0 = a * mesh.x + c * mesh.y + e, y0 = b * mesh.x + d * mesh.y + f;
4516
+ const dx = a * mesh.w, dy = b * mesh.w, ex = c * mesh.h, ey = d * mesh.h;
4517
+ const bounds = new Box(
4518
+ x0 + Math.min(0, dx) + Math.min(0, ex),
4519
+ y0 + Math.min(0, dy) + Math.min(0, ey),
4520
+ Math.abs(dx) + Math.abs(ex),
4521
+ Math.abs(dy) + Math.abs(ey)
4522
+ );
4523
+ boundsCache.set(mesh, { a, b, c, d, e, f, bounds });
4524
+ return bounds;
4525
+ }
4526
+
4461
4527
  // src/lib/editor/lod/shapeRenderMode.ts
4462
4528
  var LOD_KEEP_SCREEN_PX = 36;
4463
4529
  var LOD_KEEP_TEXT_PX = 7;
@@ -4611,9 +4677,8 @@ var MAX_LOD_POINTS = 48;
4611
4677
  var meshCache = /* @__PURE__ */ new WeakMap();
4612
4678
  var styleCache = /* @__PURE__ */ new WeakMap();
4613
4679
  var retainedOutside = /* @__PURE__ */ new WeakMap();
4614
- function lodStyleFor(editor, shape) {
4615
- const theme = editor.getCurrentTheme();
4616
- const colorMode = editor.getColorMode();
4680
+ function lodStyleFor(editor, shape, frame) {
4681
+ const { theme, colorMode } = frame;
4617
4682
  const cached = styleCache.get(shape);
4618
4683
  if (cached && cached.theme === theme && cached.colorMode === colorMode) return cached.style;
4619
4684
  let style = null;
@@ -4671,24 +4736,13 @@ function meshFor(editor, shape, style) {
4671
4736
  meshCache.set(shape, { style, mesh });
4672
4737
  return mesh;
4673
4738
  }
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
4739
  function isInteractive(editor, shape, selected, editingId) {
4740
+ if (selected.size === 0 && editingId === null) return false;
4687
4741
  let current = shape;
4688
4742
  for (let depth = 0; depth < 32 && current; depth++) {
4689
4743
  if (current.id === editingId || selected.has(current.id)) return true;
4690
4744
  const parentId = current.parentId;
4691
- if (!editor.store.has(parentId)) return false;
4745
+ if (!parentId.startsWith("shape:") || !editor.store.has(parentId)) return false;
4692
4746
  const parent = editor.store.unsafeGetWithoutCapture(parentId);
4693
4747
  if (!parent || parent.typeName !== "shape") return false;
4694
4748
  current = parent;
@@ -4701,6 +4755,8 @@ function readFrame(editor) {
4701
4755
  const selectedIds = editor.getSelectedShapeIds();
4702
4756
  return {
4703
4757
  zoom: editor.getZoomLevel(),
4758
+ theme: editor.getCurrentTheme(),
4759
+ colorMode: editor.getColorMode(),
4704
4760
  lodZoom: editor.options.lodZoom,
4705
4761
  debug: debugFlags.debugSvg.get() && editor.getInstanceState().isDebugMode,
4706
4762
  moving: editor.getCameraState() === "moving",
@@ -4716,7 +4772,7 @@ function outsideSet(editor, frame, shapes) {
4716
4772
  if (!frame.debug) {
4717
4773
  for (const shape of shapes) {
4718
4774
  if (editor.isShapeHidden(shape)) continue;
4719
- const style = lodStyleFor(editor, shape);
4775
+ const style = lodStyleFor(editor, shape, frame);
4720
4776
  if (!style) continue;
4721
4777
  if (style.unmountWhenCulled) {
4722
4778
  const bounds = editor.getShapePageBounds(shape);
@@ -4726,7 +4782,7 @@ function outsideSet(editor, frame, shapes) {
4726
4782
  const mesh = meshFor(editor, shape, style);
4727
4783
  if (!mesh) continue;
4728
4784
  const matrix = editor.getShapePageTransform(shape);
4729
- if (!frame.expanded.collides(transformedBounds(matrix, mesh))) live.add(shape.id);
4785
+ if (!frame.expanded.collides(getTransformedLodBounds(matrix, mesh))) live.add(shape.id);
4730
4786
  }
4731
4787
  }
4732
4788
  if (!frame.moving) {
@@ -4745,7 +4801,7 @@ function outsideSet(editor, frame, shapes) {
4745
4801
  }
4746
4802
  function ownRenderMode(editor, shape, frame, outsideViewport) {
4747
4803
  if (frame.debug || editor.isShapeHidden(shape)) return "dom";
4748
- const style = lodStyleFor(editor, shape);
4804
+ const style = lodStyleFor(editor, shape, frame);
4749
4805
  const mesh = style && !style.unmountWhenCulled ? meshFor(editor, shape, style) : null;
4750
4806
  const pageBounds = editor.getShapePageBounds(shape);
4751
4807
  const pageWidth = pageBounds?.w ?? mesh?.w ?? 0;
@@ -4764,7 +4820,7 @@ function ownRenderMode(editor, shape, frame, outsideViewport) {
4764
4820
  pageHeight,
4765
4821
  labelSize: style?.labelSize ?? 0,
4766
4822
  snapshotBelowPx: style?.snapshotBelowPx,
4767
- hasSnapshot: snapshotCoversShape(shape)
4823
+ hasSnapshot: (style?.snapshotBelowPx ?? 0) > 0 && snapshotCoversShape(shape)
4768
4824
  });
4769
4825
  }
4770
4826
  function snapshotCoversShape(shape, screenMax) {
@@ -4773,7 +4829,7 @@ function snapshotCoversShape(shape, screenMax) {
4773
4829
  function coveredByDomBackground(editor, shape, frame) {
4774
4830
  let parentId = shape.parentId;
4775
4831
  for (let depth = 0; depth < 32; depth++) {
4776
- if (!editor.store.has(parentId)) return false;
4832
+ if (!parentId.startsWith("shape:") || !editor.store.has(parentId)) return false;
4777
4833
  const parent = editor.store.unsafeGetWithoutCapture(parentId);
4778
4834
  if (!parent || parent.typeName !== "shape") return false;
4779
4835
  const parentShape = parent;
@@ -4865,46 +4921,20 @@ function paintSnapshot(ctx, camera, dpr, matrix, mesh, bitmap, padPx = 0) {
4865
4921
  ctx.restore();
4866
4922
  }
4867
4923
  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;
4924
+ const layout = getLodLabelLayout(ctx, mesh);
4925
+ if (!layout) return;
4877
4926
  ctx.save();
4878
4927
  ctx.beginPath();
4879
4928
  ctx.rect(mesh.x, mesh.y, mesh.w, mesh.h);
4880
4929
  ctx.clip();
4881
4930
  ctx.fillStyle = mesh.labelColor || "#1d1d1d";
4882
- ctx.font = `${size}px sans-serif`;
4931
+ ctx.font = layout.font;
4883
4932
  ctx.textAlign = "center";
4884
4933
  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;
4934
+ let y = layout.y;
4935
+ for (const row of layout.lines) {
4936
+ ctx.fillText(row, layout.x, y, layout.maxWidth);
4937
+ y += layout.lineHeight;
4908
4938
  }
4909
4939
  ctx.restore();
4910
4940
  }
@@ -4955,7 +4985,7 @@ function collectLodPaintJobs(editor, camera, shapes, frame, paintBounds) {
4955
4985
  if (frame.debug) return jobs;
4956
4986
  for (const shape of shapes) {
4957
4987
  if (editor.isShapeHidden(shape)) continue;
4958
- const style = lodStyleFor(editor, shape);
4988
+ const style = lodStyleFor(editor, shape, frame);
4959
4989
  if (!style) continue;
4960
4990
  const mode = ownRenderMode(editor, shape, frame, false);
4961
4991
  if (mode === "unmounted") continue;
@@ -4964,9 +4994,9 @@ function collectLodPaintJobs(editor, camera, shapes, frame, paintBounds) {
4964
4994
  const mesh = meshFor(editor, shape, style);
4965
4995
  if (!mesh) continue;
4966
4996
  const matrix = editor.getShapePageTransform(shape);
4967
- if (!paintBounds.collides(transformedBounds(matrix, mesh))) continue;
4997
+ if (!paintBounds.collides(getTransformedLodBounds(matrix, mesh))) continue;
4968
4998
  const snapshot = getLodSnapshot(shape);
4969
- const page = editor.getShapePageBounds(shape);
4999
+ const page = (style.snapshotBelowPx ?? 0) > 0 ? editor.getShapePageBounds(shape) : void 0;
4970
5000
  const screenMax = page ? Math.max(page.w, page.h) * (camera.z || 1) : 0;
4971
5001
  const nearFreeze = (style.snapshotBelowPx ?? 0) > 0 && screenMax < (style.snapshotBelowPx ?? 0) * 1.35;
4972
5002
  const useSnapshot = Boolean(snapshot) && (mode === "canvas" || nearFreeze);
@@ -5030,7 +5060,7 @@ function captureDueSnapshots(editor) {
5030
5060
  let changed = false;
5031
5061
  for (const shape of editor.getCurrentPageShapesSorted()) {
5032
5062
  if (editor.isShapeHidden(shape)) continue;
5033
- const style = lodStyleFor(editor, shape);
5063
+ const style = lodStyleFor(editor, shape, frame);
5034
5064
  const below = style?.snapshotBelowPx ?? 0;
5035
5065
  if (below <= 0) continue;
5036
5066
  if (isInteractive(editor, shape, frame.selected, frame.editingId)) continue;
@@ -5060,6 +5090,7 @@ function LodCanvas() {
5060
5090
  const editor = useEditor();
5061
5091
  const canvasRef = useRef(null);
5062
5092
  const bitmapRef = useRef(null);
5093
+ const lastPaintZoom = useRef(null);
5063
5094
  useQuickReactor(
5064
5095
  "lod canvas",
5065
5096
  () => {
@@ -5067,6 +5098,8 @@ function LodCanvas() {
5067
5098
  const canvas = canvasRef.current;
5068
5099
  if (!canvas) return;
5069
5100
  const camera = editor.getCamera();
5101
+ const zoomChanged = lastPaintZoom.current !== null && lastPaintZoom.current !== camera.z;
5102
+ lastPaintZoom.current = camera.z;
5070
5103
  const screen = editor.getViewportScreenBounds();
5071
5104
  const shapes = editor.getCurrentPageShapesSorted();
5072
5105
  const theme = editor.getCurrentTheme();
@@ -5131,7 +5164,7 @@ function LodCanvas() {
5131
5164
  ctx.clearRect(0, 0, width, height);
5132
5165
  const cacheW = Math.round((frame.viewport.w + pad * 2) * zoom * dpr);
5133
5166
  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;
5167
+ const canCache = !zoomChanged && cacheW > 0 && cacheH > 0 && cacheW <= LOD_BITMAP_MAX_PX && cacheH <= LOD_BITMAP_MAX_PX && cacheW * cacheH <= LOD_BITMAP_MAX_AREA;
5135
5168
  if (!canCache) {
5136
5169
  if (cached?.canvas) {
5137
5170
  cached.canvas.width = 0;
@@ -26614,7 +26647,7 @@ function getFontsFromRichText(editor, richText, initialState2) {
26614
26647
  // src/index.ts
26615
26648
  registerDrawLibraryVersion(
26616
26649
  "@ibodr/editor",
26617
- "0.2.18",
26650
+ "0.2.19",
26618
26651
  "esm"
26619
26652
  );
26620
26653
  /*!