@mlightcad/mtext-input-box 0.2.10 → 0.2.12

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.
@@ -245,7 +245,20 @@ export declare class MTextInputBox {
245
245
  private updateBoundingBoxGeometry;
246
246
  private disposeBoundingBox;
247
247
  private replaceRenderedObject;
248
+ /**
249
+ * Removes any previous MTEXT scene roots tagged for this input box. Defensive
250
+ * cleanup when a prior root was not fully detached before the next relayout.
251
+ */
252
+ private removeStrayInputBoxSceneRootsExcept;
248
253
  private forceVisibleMaterialState;
254
+ private disposeDetachedThreeSubtree;
255
+ /**
256
+ * Keeps a single layout root on the MTEXT object. {@link MText.syncDraw} only calls
257
+ * `add()` for the new layout; multiple draws on the same instance would otherwise
258
+ * stack several full copies of the text (e.g. after attachment-point changes).
259
+ */
260
+ private pruneExtraMTextLayoutRoots;
261
+ private disposeMTextRootResources;
249
262
  private disposeRenderedObject;
250
263
  private isExplicitAci;
251
264
  private normalizeColorNumber;
@@ -24,6 +24,8 @@ function attachmentColumnMinLocal(width, attachment) {
24
24
  return -w;
25
25
  return 0;
26
26
  }
27
+ /** Marks MTEXT roots added by {@link MTextInputBox} so stray scene objects can be scavenged. */
28
+ const MTEXT_INPUT_BOX_SCENE_ROOT_KEY = '__mlightcadMTextInputBoxSceneRoot';
27
29
  /**
28
30
  * Three.js based MText editor component with core text editing interaction.
29
31
  */
@@ -669,7 +671,13 @@ export class MTextInputBox {
669
671
  // Keep the on-screen text frame fixed: DXF insertion point is the selected
670
672
  // attachment location on the bounding box, so changing attachment must move
671
673
  // `position` by the delta between the old and new anchor on the same box.
672
- if (this.rendererReady && Number.isFinite(this.layoutContainer.width)) {
674
+ //
675
+ // Do not gate this on `rendererReady`: while fonts load, `relayout()` still
676
+ // fills `layoutContainer` from the fallback layout. Skipping the move here
677
+ // left `position` at the top-left anchor even after the user chose middle
678
+ // center, so committed MTEXT used the wrong insertion vs attachment (DXF 71).
679
+ if (Number.isFinite(this.layoutContainer.width) &&
680
+ Number.isFinite(this.layoutContainer.height)) {
673
681
  const left = this.position.x + this.layoutContainer.x;
674
682
  const right = left + this.layoutContainer.width;
675
683
  const bottom = this.position.y + this.layoutContainer.y;
@@ -1262,6 +1270,10 @@ export class MTextInputBox {
1262
1270
  const style = this.createTextStyle();
1263
1271
  const colorSettings = this.resolveRenderColorSettings();
1264
1272
  const object = this.mtextRenderer.syncRenderMText(mtextData, style, colorSettings);
1273
+ // `MText.syncDraw()` appends a fresh layout group without clearing prior children.
1274
+ // If the renderer ever reuses one root for multiple draws, prune stale roots so
1275
+ // attachment / justify changes cannot leave duplicate glyph trees in the scene.
1276
+ this.pruneExtraMTextLayoutRoots(object);
1265
1277
  this.replaceRenderedObject(object);
1266
1278
  const rendered = this.extractBoxesFromRenderedObject(object);
1267
1279
  this.mtextInsertionOffset.set(0, 0, 0);
@@ -1648,10 +1660,30 @@ export class MTextInputBox {
1648
1660
  }
1649
1661
  replaceRenderedObject(object) {
1650
1662
  this.disposeRenderedObject(this.renderedObject);
1663
+ this.removeStrayInputBoxSceneRootsExcept(object);
1651
1664
  this.forceVisibleMaterialState(object);
1665
+ object.userData[MTEXT_INPUT_BOX_SCENE_ROOT_KEY] = this;
1652
1666
  this.renderedObject = object;
1653
1667
  this.scene.add(object);
1654
1668
  }
1669
+ /**
1670
+ * Removes any previous MTEXT scene roots tagged for this input box. Defensive
1671
+ * cleanup when a prior root was not fully detached before the next relayout.
1672
+ */
1673
+ removeStrayInputBoxSceneRootsExcept(keep) {
1674
+ const keepObj = keep;
1675
+ for (let i = this.scene.children.length - 1; i >= 0; i--) {
1676
+ const ch = this.scene.children[i];
1677
+ if (!ch || ch === keepObj)
1678
+ continue;
1679
+ const owner = ch.userData[MTEXT_INPUT_BOX_SCENE_ROOT_KEY];
1680
+ if (owner === this) {
1681
+ this.scene.remove(ch);
1682
+ this.disposeMTextRootResources(ch);
1683
+ delete ch.userData[MTEXT_INPUT_BOX_SCENE_ROOT_KEY];
1684
+ }
1685
+ }
1686
+ }
1655
1687
  forceVisibleMaterialState(object) {
1656
1688
  object.traverse((child) => {
1657
1689
  const meshLike = child;
@@ -1675,16 +1707,8 @@ export class MTextInputBox {
1675
1707
  }
1676
1708
  });
1677
1709
  }
1678
- disposeRenderedObject(object) {
1679
- if (!object)
1680
- return;
1681
- object.removeFromParent();
1682
- const withDispose = object;
1683
- if (typeof withDispose.dispose === 'function') {
1684
- withDispose.dispose();
1685
- return;
1686
- }
1687
- object.traverse((child) => {
1710
+ disposeDetachedThreeSubtree(root) {
1711
+ root.traverse((child) => {
1688
1712
  const mesh = child;
1689
1713
  if (mesh.geometry) {
1690
1714
  mesh.geometry.dispose();
@@ -1698,6 +1722,35 @@ export class MTextInputBox {
1698
1722
  }
1699
1723
  });
1700
1724
  }
1725
+ /**
1726
+ * Keeps a single layout root on the MTEXT object. {@link MText.syncDraw} only calls
1727
+ * `add()` for the new layout; multiple draws on the same instance would otherwise
1728
+ * stack several full copies of the text (e.g. after attachment-point changes).
1729
+ */
1730
+ pruneExtraMTextLayoutRoots(object) {
1731
+ while (object.children.length > 1) {
1732
+ const stale = object.children[0];
1733
+ if (!stale)
1734
+ break;
1735
+ object.remove(stale);
1736
+ this.disposeDetachedThreeSubtree(stale);
1737
+ }
1738
+ }
1739
+ disposeMTextRootResources(object) {
1740
+ const withDispose = object;
1741
+ if (typeof withDispose.dispose === 'function') {
1742
+ withDispose.dispose();
1743
+ return;
1744
+ }
1745
+ this.disposeDetachedThreeSubtree(object);
1746
+ }
1747
+ disposeRenderedObject(object) {
1748
+ if (!object)
1749
+ return;
1750
+ object.removeFromParent();
1751
+ delete object.userData[MTEXT_INPUT_BOX_SCENE_ROOT_KEY];
1752
+ this.disposeMTextRootResources(object);
1753
+ }
1701
1754
  isExplicitAci(aci) {
1702
1755
  return aci !== null && Number.isInteger(aci) && aci > 0 && aci < 256;
1703
1756
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlightcad/mtext-input-box",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -17,7 +17,7 @@
17
17
  "@mlightcad/text-box-cursor": "0.1.1"
18
18
  },
19
19
  "peerDependencies": {
20
- "@mlightcad/mtext-renderer": "^0.10.14",
20
+ "@mlightcad/mtext-renderer": "^0.10.15",
21
21
  "three": "^0.172.0"
22
22
  },
23
23
  "devDependencies": {