@ibodr/editor 0.2.16 → 0.2.18

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
@@ -4463,13 +4463,11 @@ var LOD_KEEP_SCREEN_PX = 36;
4463
4463
  var LOD_KEEP_TEXT_PX = 7;
4464
4464
  var LOD_CULL_MARGIN = 0.75;
4465
4465
  var LOD_SNAPSHOT_SCREEN_PX = 180;
4466
- var LOD_OVERVIEW_ZOOM = 0.2;
4467
4466
  function getShapeRenderMode(input) {
4468
4467
  if (!input.hasLodStyle || input.interactive) return "dom";
4469
4468
  if (input.outsideViewport) return "unmounted";
4470
4469
  const screenMax = Math.max(input.pageWidth, input.pageHeight) * input.zoom;
4471
4470
  const textScreen = (input.labelSize ?? 0) * input.zoom;
4472
- if (input.zoom < LOD_OVERVIEW_ZOOM && !input.unmountWhenCulled) return "canvas";
4473
4471
  const snapshotBelow = input.snapshotBelowPx ?? 0;
4474
4472
  if (snapshotBelow > 0 && screenMax < snapshotBelow) {
4475
4473
  return input.hasSnapshot ? "canvas" : "dom";
@@ -4478,8 +4476,8 @@ function getShapeRenderMode(input) {
4478
4476
  if (screenMax >= LOD_KEEP_SCREEN_PX || textScreen >= LOD_KEEP_TEXT_PX) return "dom";
4479
4477
  return "canvas";
4480
4478
  }
4481
- var SNAPSHOT_MAX_EDGE = 768;
4482
- var MAX_SNAPSHOTS = 48;
4479
+ var SNAPSHOT_MAX_EDGE = 512;
4480
+ var MAX_SNAPSHOTS = 32;
4483
4481
  var MAX_CAPTURE_ATTEMPTS = 8;
4484
4482
  var snapshots = /* @__PURE__ */ new Map();
4485
4483
  var attempts = /* @__PURE__ */ new WeakMap();
@@ -4868,40 +4866,58 @@ function paintSnapshot(ctx, camera, dpr, matrix, mesh, bitmap, padPx = 0) {
4868
4866
  }
4869
4867
  function paintLodLabel(ctx, mesh) {
4870
4868
  const text = mesh.label?.trim();
4871
- if (!text) return;
4872
- const size = Math.max(10, mesh.labelSize || 16);
4873
- const pad = Math.max(8, size * 0.45);
4869
+ if (!text || mesh.w < 4 || mesh.h < 4) return;
4870
+ const pad = Math.min(mesh.w, mesh.h) * 0.08;
4874
4871
  const maxWidth = mesh.w - pad * 2;
4875
- const maxY = mesh.y + mesh.h - pad;
4876
- if (maxWidth < 8 || maxY <= mesh.y + pad) return;
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;
4877
4877
  ctx.save();
4878
4878
  ctx.beginPath();
4879
4879
  ctx.rect(mesh.x, mesh.y, mesh.w, mesh.h);
4880
4880
  ctx.clip();
4881
4881
  ctx.fillStyle = mesh.labelColor || "#1d1d1d";
4882
4882
  ctx.font = `${size}px sans-serif`;
4883
- ctx.textAlign = "left";
4884
- ctx.textBaseline = "top";
4883
+ ctx.textAlign = "center";
4884
+ ctx.textBaseline = "middle";
4885
4885
  const words = text.split(/\s+/);
4886
+ const lines = [];
4886
4887
  let line = "";
4887
- let y = mesh.y + pad;
4888
- const lineHeight = size * 1.25;
4889
4888
  for (const word of words) {
4890
- if (y > maxY) break;
4891
4889
  const next = line ? `${line} ${word}` : word;
4892
4890
  if (line && ctx.measureText(next).width > maxWidth) {
4893
- ctx.fillText(line, mesh.x + pad, y, maxWidth);
4891
+ lines.push(line);
4894
4892
  line = word;
4895
- y += lineHeight;
4896
4893
  } else {
4897
4894
  line = next;
4898
4895
  }
4899
4896
  }
4900
- if (line && y <= maxY) ctx.fillText(line, mesh.x + pad, y, maxWidth);
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;
4908
+ }
4901
4909
  ctx.restore();
4902
4910
  }
4903
- var LOD_OVERSCAN_SCREEN_PX = 192;
4904
4911
  var LOD_BITMAP_MAX_PX = 8192;
4912
+ var isMemoryConstrained = (() => {
4913
+ if (typeof navigator === "undefined") return false;
4914
+ const ua = navigator.userAgent || "";
4915
+ const ios = /iPad|iPhone|iPod/.test(ua) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1;
4916
+ const memory = navigator.deviceMemory;
4917
+ return ios || typeof memory === "number" && memory <= 4;
4918
+ })();
4919
+ var LOD_OVERSCAN_SCREEN_PX = isMemoryConstrained ? 96 : 192;
4920
+ var LOD_BITMAP_MAX_AREA = isMemoryConstrained ? 8e6 : 24e6;
4905
4921
  function lodBitmapCovers(bitmap, camera, viewport, dpr, shapes, frame, theme, colorMode) {
4906
4922
  if (bitmap.zoom !== camera.z || bitmap.dpr !== dpr) return false;
4907
4923
  if (bitmap.snapshotEpoch !== lodSnapshotEpoch.get()) return false;
@@ -4922,6 +4938,7 @@ function lodBitmapCovers(bitmap, camera, viewport, dpr, shapes, frame, theme, co
4922
4938
  return covered.contains(viewport);
4923
4939
  }
4924
4940
  function blitLodBitmap(ctx, width, height, bitmap, camera) {
4941
+ if (!bitmap.canvas) return;
4925
4942
  const z = camera.z || 1;
4926
4943
  const padPx = bitmap.pad * z * bitmap.dpr;
4927
4944
  ctx.globalAlpha = 1;
@@ -4933,13 +4950,16 @@ function blitLodBitmap(ctx, width, height, bitmap, camera) {
4933
4950
  (camera.y - bitmap.cameraY) * z * bitmap.dpr - padPx
4934
4951
  );
4935
4952
  }
4936
- function paintVisibleLodShapes(ctx, editor, camera, dpr, shapes, frame, paintBounds, padPx) {
4953
+ function collectLodPaintJobs(editor, camera, shapes, frame, paintBounds) {
4954
+ const jobs = [];
4955
+ if (frame.debug) return jobs;
4937
4956
  for (const shape of shapes) {
4938
4957
  if (editor.isShapeHidden(shape)) continue;
4939
4958
  const style = lodStyleFor(editor, shape);
4940
4959
  if (!style) continue;
4941
4960
  const mode = ownRenderMode(editor, shape, frame, false);
4942
- if (mode === "unmounted" || frame.debug) continue;
4961
+ if (mode === "unmounted") continue;
4962
+ if (mode === "dom" && !(style.snapshotBelowPx && style.snapshotBelowPx > 0)) continue;
4943
4963
  if (mode === "canvas" && coveredByDomBackground(editor, shape, frame)) continue;
4944
4964
  const mesh = meshFor(editor, shape, style);
4945
4965
  if (!mesh) continue;
@@ -4949,12 +4969,17 @@ function paintVisibleLodShapes(ctx, editor, camera, dpr, shapes, frame, paintBou
4949
4969
  const page = editor.getShapePageBounds(shape);
4950
4970
  const screenMax = page ? Math.max(page.w, page.h) * (camera.z || 1) : 0;
4951
4971
  const nearFreeze = (style.snapshotBelowPx ?? 0) > 0 && screenMax < (style.snapshotBelowPx ?? 0) * 1.35;
4952
- if (snapshot && (mode === "canvas" || nearFreeze)) {
4953
- paintSnapshot(ctx, camera, dpr, matrix, mesh, snapshot, padPx);
4954
- if (mode !== "dom") continue;
4955
- }
4956
- if (mode !== "canvas") continue;
4957
- paintShape(ctx, camera, dpr, matrix, mesh, padPx);
4972
+ const useSnapshot = Boolean(snapshot) && (mode === "canvas" || nearFreeze);
4973
+ const paintStandIn = mode === "canvas" && !useSnapshot;
4974
+ if (!useSnapshot && !paintStandIn) continue;
4975
+ jobs.push({ matrix, mesh, snapshot: useSnapshot ? snapshot : null, paintStandIn });
4976
+ }
4977
+ return jobs;
4978
+ }
4979
+ function paintLodJobs(ctx, camera, dpr, jobs, padPx) {
4980
+ for (const job of jobs) {
4981
+ if (job.snapshot) paintSnapshot(ctx, camera, dpr, job.matrix, job.mesh, job.snapshot, padPx);
4982
+ if (job.paintStandIn) paintShape(ctx, camera, dpr, job.matrix, job.mesh, padPx);
4958
4983
  }
4959
4984
  }
4960
4985
  function useMountedRenderingShapes() {
@@ -4993,7 +5018,7 @@ function useMountedRenderingShapes() {
4993
5018
  var CAPTURES_PER_PASS = 2;
4994
5019
  function captureDueSnapshots(editor) {
4995
5020
  const frame = readFrame(editor);
4996
- if (frame.moving || frame.debug || frame.zoom < LOD_OVERVIEW_ZOOM) return;
5021
+ if (frame.moving || frame.debug) return;
4997
5022
  let container;
4998
5023
  try {
4999
5024
  container = editor.getContainer();
@@ -5050,44 +5075,40 @@ function LodCanvas() {
5050
5075
  const dpr = window.devicePixelRatio || 1;
5051
5076
  const width = Math.max(1, Math.round((screen.w || canvas.clientWidth) * dpr));
5052
5077
  const height = Math.max(1, Math.round((screen.h || canvas.clientHeight) * dpr));
5053
- if (canvas.width !== width || canvas.height !== height) {
5054
- canvas.width = width;
5055
- canvas.height = height;
5056
- }
5057
5078
  const ctx = canvas.getContext("2d");
5058
5079
  if (!ctx) return;
5080
+ const sizeScreenCanvas = () => {
5081
+ if (canvas.width !== width || canvas.height !== height) {
5082
+ canvas.width = width;
5083
+ canvas.height = height;
5084
+ }
5085
+ };
5086
+ const releaseCanvases = (previous) => {
5087
+ if (previous?.canvas) {
5088
+ previous.canvas.width = 0;
5089
+ previous.canvas.height = 0;
5090
+ }
5091
+ if (canvas.width !== 1 || canvas.height !== 1) {
5092
+ canvas.width = 1;
5093
+ canvas.height = 1;
5094
+ }
5095
+ };
5059
5096
  unsafe__withoutCapture(() => {
5060
5097
  const cached = bitmapRef.current;
5061
5098
  if (!frame.debug && cached && lodBitmapCovers(cached, camera, frame.viewport, dpr, shapes, frame, theme, colorMode)) {
5062
- blitLodBitmap(ctx, width, height, cached, camera);
5063
- return;
5064
- }
5065
- ctx.setTransform(1, 0, 0, 1, 0, 0);
5066
- ctx.clearRect(0, 0, width, height);
5067
- if (frame.debug) {
5068
- bitmapRef.current = null;
5099
+ if (cached.canvas) {
5100
+ sizeScreenCanvas();
5101
+ blitLodBitmap(ctx, width, height, cached, camera);
5102
+ }
5069
5103
  return;
5070
5104
  }
5071
- const pad = LOD_OVERSCAN_SCREEN_PX / (camera.z || 1);
5105
+ const zoom = camera.z || 1;
5106
+ const pad = LOD_OVERSCAN_SCREEN_PX / zoom;
5072
5107
  const paintBounds = frame.viewport.clone().expandBy(pad);
5073
- const cacheW = Math.round((frame.viewport.w + pad * 2) * (camera.z || 1) * dpr);
5074
- const cacheH = Math.round((frame.viewport.h + pad * 2) * (camera.z || 1) * dpr);
5075
- const canCache = cacheW > 0 && cacheH > 0 && cacheW <= LOD_BITMAP_MAX_PX && cacheH <= LOD_BITMAP_MAX_PX;
5076
- const target = canCache ? cached?.canvas ?? document.createElement("canvas") : null;
5077
- if (target && (target.width !== cacheW || target.height !== cacheH)) {
5078
- target.width = cacheW;
5079
- target.height = cacheH;
5080
- }
5081
- const padPx = canCache ? pad * (camera.z || 1) * dpr : 0;
5082
- const paintCtx = target?.getContext("2d") ?? ctx;
5083
- if (target) {
5084
- paintCtx.globalAlpha = 1;
5085
- paintCtx.setTransform(1, 0, 0, 1, 0, 0);
5086
- paintCtx.clearRect(0, 0, cacheW, cacheH);
5087
- }
5088
- paintVisibleLodShapes(paintCtx, editor, camera, dpr, shapes, frame, paintBounds, padPx);
5089
- if (target) {
5090
- const bitmap = {
5108
+ const jobs = collectLodPaintJobs(editor, camera, shapes, frame, paintBounds);
5109
+ if (jobs.length === 0) {
5110
+ releaseCanvases(cached);
5111
+ bitmapRef.current = frame.debug ? null : {
5091
5112
  zoom: camera.z,
5092
5113
  dpr,
5093
5114
  cameraX: camera.x,
@@ -5100,12 +5121,55 @@ function LodCanvas() {
5100
5121
  editingId: frame.editingId,
5101
5122
  theme,
5102
5123
  colorMode,
5103
- canvas: target,
5124
+ canvas: null,
5104
5125
  snapshotEpoch: lodSnapshotEpoch.get()
5105
5126
  };
5106
- bitmapRef.current = bitmap;
5107
- blitLodBitmap(ctx, width, height, bitmap, camera);
5127
+ return;
5128
+ }
5129
+ sizeScreenCanvas();
5130
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
5131
+ ctx.clearRect(0, 0, width, height);
5132
+ const cacheW = Math.round((frame.viewport.w + pad * 2) * zoom * dpr);
5133
+ 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;
5135
+ if (!canCache) {
5136
+ if (cached?.canvas) {
5137
+ cached.canvas.width = 0;
5138
+ cached.canvas.height = 0;
5139
+ }
5140
+ bitmapRef.current = null;
5141
+ paintLodJobs(ctx, camera, dpr, jobs, 0);
5142
+ return;
5143
+ }
5144
+ const target = cached?.canvas ?? document.createElement("canvas");
5145
+ if (target.width !== cacheW || target.height !== cacheH) {
5146
+ target.width = cacheW;
5147
+ target.height = cacheH;
5108
5148
  }
5149
+ const paintCtx = target.getContext("2d");
5150
+ if (!paintCtx) return;
5151
+ paintCtx.globalAlpha = 1;
5152
+ paintCtx.setTransform(1, 0, 0, 1, 0, 0);
5153
+ paintCtx.clearRect(0, 0, cacheW, cacheH);
5154
+ paintLodJobs(paintCtx, camera, dpr, jobs, pad * zoom * dpr);
5155
+ const bitmap = {
5156
+ zoom: camera.z,
5157
+ dpr,
5158
+ cameraX: camera.x,
5159
+ cameraY: camera.y,
5160
+ pad,
5161
+ viewW: frame.viewport.w,
5162
+ viewH: frame.viewport.h,
5163
+ shapes,
5164
+ selectedIds: frame.selectedIds,
5165
+ editingId: frame.editingId,
5166
+ theme,
5167
+ colorMode,
5168
+ canvas: target,
5169
+ snapshotEpoch: lodSnapshotEpoch.get()
5170
+ };
5171
+ bitmapRef.current = bitmap;
5172
+ blitLodBitmap(ctx, width, height, bitmap, camera);
5109
5173
  });
5110
5174
  },
5111
5175
  [editor]
@@ -26550,7 +26614,7 @@ function getFontsFromRichText(editor, richText, initialState2) {
26550
26614
  // src/index.ts
26551
26615
  registerDrawLibraryVersion(
26552
26616
  "@ibodr/editor",
26553
- "0.2.16",
26617
+ "0.2.18",
26554
26618
  "esm"
26555
26619
  );
26556
26620
  /*!