@mlightcad/mtext-input-box 0.2.3 → 0.2.4

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.
@@ -1,5 +1,6 @@
1
1
  import { type DebugVisibility } from '@mlightcad/text-box-cursor';
2
2
  import { type TextStyle } from '@mlightcad/mtext-renderer';
3
+ import * as THREE from 'three';
3
4
  import { defaultCharFormat, sameFormat } from './format';
4
5
  import type { CharFormat, CursorLayoutData, CursorDirection, EditorState, MTextInputBoxEvent, MTextInputBoxOptions, MTextToolbarTheme, SelectionDirection } from './types';
5
6
  type Handler = () => void;
@@ -21,6 +22,7 @@ export declare class MTextInputBox {
21
22
  private static readonly DEFAULT_TEXT_STYLE;
22
23
  private width;
23
24
  private position;
25
+ private mtextInsertionOffset;
24
26
  private enableWordWrap;
25
27
  private mtextString;
26
28
  private cursorIndex;
@@ -166,6 +168,8 @@ export declare class MTextInputBox {
166
168
  y: number;
167
169
  z: number;
168
170
  };
171
+ /** Returns the MTEXT insertion point that preserves the editor's visual top-left placement. */
172
+ getMTextInsertionPoint(): THREE.Vector3;
169
173
  /** Returns current internal state snapshot. */
170
174
  getState(): EditorState;
171
175
  private isActiveEditor;
@@ -34,6 +34,7 @@ export class MTextInputBox {
34
34
  };
35
35
  width;
36
36
  position;
37
+ mtextInsertionOffset = new THREE.Vector3(0, 0, 0);
37
38
  enableWordWrap;
38
39
  mtextString = '';
39
40
  cursorIndex = 0;
@@ -268,6 +269,9 @@ export class MTextInputBox {
268
269
  lineTolerance: 4
269
270
  });
270
271
  const cursorStyle = { ...(options.cursorStyle ?? {}) };
272
+ if (cursorStyle.height === undefined && cursorStyle.heightMode !== 'fixed') {
273
+ cursorStyle.height = 0;
274
+ }
271
275
  cursorStyle.color ??= '#ffffff';
272
276
  cursorStyle.glowColor ??= '#ffffff';
273
277
  this.cursorRenderer = new CursorRenderer({
@@ -796,13 +800,18 @@ export class MTextInputBox {
796
800
  }
797
801
  /** Returns cursor world position for IME/caret anchoring. */
798
802
  getCursorWorldPosition() {
799
- const cursor = this.cursorLogic.getCursorState().position;
803
+ const cursorState = this.cursorLogic.getCursorState();
804
+ const cursor = this.getActiveCursorRenderState(cursorState.position, cursorState.lineInfo.height).position;
800
805
  return {
801
806
  x: this.position.x + cursor.x,
802
807
  y: this.position.y + cursor.y,
803
808
  z: this.position.z
804
809
  };
805
810
  }
811
+ /** Returns the MTEXT insertion point that preserves the editor's visual top-left placement. */
812
+ getMTextInsertionPoint() {
813
+ return this.position.clone().add(this.mtextInsertionOffset);
814
+ }
806
815
  /** Returns current internal state snapshot. */
807
816
  getState() {
808
817
  return {
@@ -1058,6 +1067,7 @@ export class MTextInputBox {
1058
1067
  }
1059
1068
  relayout() {
1060
1069
  if (!this.rendererReady) {
1070
+ this.mtextInsertionOffset.set(0, 0, 0);
1061
1071
  const fallback = this.createFallbackCharBoxes();
1062
1072
  this.layoutContainer = fallback.containerBox;
1063
1073
  this.updateCursorData(fallback.charBoxes, fallback.lineBreakIndices, fallback.lineLayouts);
@@ -1072,6 +1082,7 @@ export class MTextInputBox {
1072
1082
  const object = this.mtextRenderer.syncRenderMText(mtextData, style, colorSettings);
1073
1083
  this.replaceRenderedObject(object);
1074
1084
  const rendered = this.extractBoxesFromRenderedObject(object);
1085
+ this.mtextInsertionOffset.set(0, 0, 0);
1075
1086
  this.normalizeRenderedTopAlignment(object, rendered);
1076
1087
  this.layoutContainer = rendered.containerBox;
1077
1088
  this.updateCursorData(rendered.charBoxes, rendered.lineBreakIndices, rendered.lineLayouts);
@@ -1079,6 +1090,7 @@ export class MTextInputBox {
1079
1090
  }
1080
1091
  catch (error) {
1081
1092
  console.error('[mtext-input-box] Failed to sync render MTEXT', error);
1093
+ this.mtextInsertionOffset.set(0, 0, 0);
1082
1094
  const fallback = this.createFallbackCharBoxes();
1083
1095
  this.layoutContainer = fallback.containerBox;
1084
1096
  this.updateCursorData(fallback.charBoxes, fallback.lineBreakIndices, fallback.lineLayouts);
@@ -1205,9 +1217,11 @@ export class MTextInputBox {
1205
1217
  };
1206
1218
  }
1207
1219
  normalizeRenderedTopAlignment(object, rendered) {
1208
- const dy = -rendered.containerBox.y;
1220
+ const topY = rendered.containerBox.y + rendered.containerBox.height;
1221
+ const dy = -topY;
1209
1222
  if (!Number.isFinite(dy) || Math.abs(dy) < 1e-8)
1210
1223
  return;
1224
+ this.mtextInsertionOffset.set(0, dy, 0);
1211
1225
  object.position.y += dy;
1212
1226
  object.updateMatrixWorld(true);
1213
1227
  rendered.containerBox.y += dy;
@@ -1238,34 +1252,35 @@ export class MTextInputBox {
1238
1252
  const lineBreakIndices = [];
1239
1253
  const lineLayouts = [];
1240
1254
  let x = 0;
1241
- let y = 0;
1242
- let maxY = lineHeight;
1243
- lineLayouts.push({ y: y + lineHeight / 2, height: lineHeight });
1255
+ let lineTop = 0;
1256
+ let minY = -lineHeight;
1257
+ lineLayouts.push({ y: lineTop - lineHeight / 2, height: lineHeight });
1244
1258
  for (const char of this.getChars()) {
1245
1259
  if (char === '\n') {
1246
1260
  lineBreakIndices.push(charBoxes.length);
1247
1261
  x = 0;
1248
- y += lineHeight;
1249
- maxY = Math.max(maxY, y + lineHeight);
1250
- lineLayouts.push({ y: y + lineHeight / 2, height: lineHeight });
1262
+ lineTop -= lineHeight;
1263
+ minY = Math.min(minY, lineTop - lineHeight);
1264
+ lineLayouts.push({ y: lineTop - lineHeight / 2, height: lineHeight });
1251
1265
  continue;
1252
1266
  }
1253
1267
  const width = Math.max(1, this.currentFormat.fontSize * 0.6);
1254
1268
  if (this.enableWordWrap && x > 0 && x + width > this.width) {
1255
1269
  lineBreakIndices.push(charBoxes.length);
1256
1270
  x = 0;
1257
- y += lineHeight;
1271
+ lineTop -= lineHeight;
1272
+ minY = Math.min(minY, lineTop - lineHeight);
1273
+ lineLayouts.push({ y: lineTop - lineHeight / 2, height: lineHeight });
1258
1274
  }
1259
- charBoxes.push({ x, y: y + lineHeight / 2, width, height: lineHeight });
1275
+ charBoxes.push({ x, y: lineTop - lineHeight / 2, width, height: lineHeight });
1260
1276
  x += width;
1261
- maxY = Math.max(maxY, y + lineHeight);
1262
1277
  }
1263
1278
  return {
1264
1279
  containerBox: {
1265
1280
  x: 0,
1266
- y: 0,
1281
+ y: minY,
1267
1282
  width: this.width,
1268
- height: Math.max(1, maxY)
1283
+ height: Math.max(1, -minY)
1269
1284
  },
1270
1285
  charBoxes,
1271
1286
  lineBreakIndices,
@@ -1568,7 +1583,7 @@ export class MTextInputBox {
1568
1583
  // Best source: renderer-provided line layout already represents
1569
1584
  // the actual first-line center in editor local coordinates.
1570
1585
  position: { x: fallbackPosition.x, y: firstLine.y },
1571
- height: Math.max(1, lineHeight * 0.8)
1586
+ height: lineHeight
1572
1587
  };
1573
1588
  }
1574
1589
  const inferredLineHeight = Math.max(1, this.getFallbackLineAdvance());
@@ -1576,12 +1591,12 @@ export class MTextInputBox {
1576
1591
  position: {
1577
1592
  x: fallbackPosition.x,
1578
1593
  // When explicit line layout is unavailable, infer first-line center
1579
- // from top-left container coordinates:
1580
- // lineCenterY = containerTopY + lineHeight / 2
1594
+ // from the container's lower edge in y-up coordinates:
1595
+ // lineCenterY = containerBottomY + lineHeight / 2
1581
1596
  // This keeps caret aligned with the top row for empty MTEXT.
1582
1597
  y: this.layoutContainer.y + inferredLineHeight / 2
1583
1598
  },
1584
- height: Math.max(1, inferredLineHeight * 0.8)
1599
+ height: inferredLineHeight
1585
1600
  };
1586
1601
  }
1587
1602
  // At a line-start gap (including wrapped/paragraph boundaries), rely on
@@ -1598,7 +1613,7 @@ export class MTextInputBox {
1598
1613
  };
1599
1614
  }
1600
1615
  }
1601
- const emptyLineHeight = (line.height ?? fallbackHeight) * 0.8;
1616
+ const emptyLineHeight = line.height ?? fallbackHeight;
1602
1617
  return {
1603
1618
  position: { x: fallbackPosition.x, y: line.y ?? fallbackPosition.y },
1604
1619
  height: Math.max(1, emptyLineHeight)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlightcad/mtext-input-box",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",