@mlightcad/mtext-input-box 0.2.3 → 0.2.5

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/README.md CHANGED
@@ -147,7 +147,7 @@ toolbar: {
147
147
  app.unmount();
148
148
  }
149
149
  };
150
- }
150
+ };
151
151
  }
152
152
  ```
153
153
 
@@ -8,7 +8,9 @@ export class MTextDocument {
8
8
  _selection = null;
9
9
  constructor(astOrMText = { nodes: [] }) {
10
10
  this._ast =
11
- typeof astOrMText === 'string' ? parseMTextToAst(astOrMText) : MTextDocument.cloneAst(astOrMText);
11
+ typeof astOrMText === 'string'
12
+ ? parseMTextToAst(astOrMText)
13
+ : MTextDocument.cloneAst(astOrMText);
12
14
  this._cursor = this._ast.nodes.length;
13
15
  }
14
16
  static fromMText(mtext, ctx) {
@@ -2,5 +2,5 @@ import { MTextContext } from '@mlightcad/mtext-renderer';
2
2
  import type { MTextAstCharNode, MTextAstColumnBreakNode, MTextAstNode, MTextAstParagraphBreakNode, MTextAstStackNode, MTextAstWrapAtDimLineNode, MTextStyle } from './types';
3
3
  export declare function contextToStyle(ctx: MTextContext): MTextStyle;
4
4
  export declare function cloneStyle(style: MTextStyle): MTextStyle;
5
- export declare function hasNodeStyle(node: MTextAstNode): node is (MTextAstCharNode | MTextAstStackNode | MTextAstParagraphBreakNode | MTextAstColumnBreakNode | MTextAstWrapAtDimLineNode);
5
+ export declare function hasNodeStyle(node: MTextAstNode): node is MTextAstCharNode | MTextAstStackNode | MTextAstParagraphBreakNode | MTextAstColumnBreakNode | MTextAstWrapAtDimLineNode;
6
6
  export declare function serializeStyleDelta(prev: MTextStyle | null, next: MTextStyle): string;
@@ -113,7 +113,8 @@ export function serializeStyleDelta(prev, next) {
113
113
  if (from.align !== nextWithScript.align) {
114
114
  out += `\\A${nextWithScript.align};`;
115
115
  }
116
- if (from.aci !== nextWithScript.aci || JSON.stringify(from.rgb) !== JSON.stringify(nextWithScript.rgb)) {
116
+ if (from.aci !== nextWithScript.aci ||
117
+ JSON.stringify(from.rgb) !== JSON.stringify(nextWithScript.rgb)) {
117
118
  if (nextWithScript.rgb) {
118
119
  out += `\\c${rgb2int(nextWithScript.rgb)};`;
119
120
  }
package/dist/ui/icons.js CHANGED
@@ -6,5 +6,5 @@ export const toolbarIcons = {
6
6
  strike: '<svg viewBox="0 0 24 24"><g fill-rule="evenodd"><path d="M15.6 8.5c-.5-.7-1-1.1-1.3-1.3-.6-.4-1.3-.6-2-.6-2.7 0-2.8 1.7-2.8 2.1 0 1.6 1.8 2 3.2 2.3 4.4.9 4.6 2.8 4.6 3.9 0 1.4-.7 4.1-5 4.1A6.2 6.2 0 0 1 7 16.4l1.5-1.1c.4.6 1.6 2 3.7 2 1.6 0 2.5-.4 3-1.2.4-.8.3-2-.8-2.6-.7-.4-1.6-.7-2.9-1-1-.2-3.9-.8-3.9-3.6C7.6 6 10.3 5 12.4 5c2.9 0 4.2 1.6 4.7 2.4l-1.5 1.1Z"></path><path d="M5 11h14a1 1 0 0 1 0 2H5a1 1 0 0 1 0-2Z" fill-rule="nonzero"></path></g></svg>',
7
7
  superscript: '<svg viewBox="0 0 24 24"><path d="M15 9.4 10.4 14l4.6 4.6-1.4 1.4L9 15.4 4.4 20 3 18.6 7.6 14 3 9.4 4.4 8 9 12.6 13.6 8 15 9.4Zm5.9 1.6h-5v-1l1-.8 1.7-1.6c.3-.5.5-.9.5-1.3 0-.3 0-.5-.2-.7-.2-.2-.5-.3-.9-.3l-.8.2-.7.4-.4-1.2c.2-.2.5-.4 1-.5.3-.2.8-.2 1.2-.2.8 0 1.4.2 1.8.6.4.4.6 1 .6 1.6 0 .5-.2 1-.5 1.5l-1.3 1.4-.6.5h2.6V11Z" fill-rule="nonzero"></path></svg>',
8
8
  subscript: '<svg viewBox="0 0 24 24"><path d="m10.4 10 4.6 4.6-1.4 1.4L9 11.4 4.4 16 3 14.6 7.6 10 3 5.4 4.4 4 9 8.6 13.6 4 15 5.4 10.4 10ZM21 19h-5v-1l1-.8 1.7-1.6c.3-.4.5-.8.5-1.2 0-.3 0-.6-.2-.7-.2-.2-.5-.3-.9-.3a2 2 0 0 0-.8.2l-.7.3-.4-1.1 1-.6 1.2-.2c.8 0 1.4.3 1.8.7.4.4.6.9.6 1.5s-.2 1.1-.5 1.6a8 8 0 0 1-1.3 1.3l-.6.6h2.6V19Z" fill-rule="nonzero"></path></svg>',
9
- stack: '<svg viewBox="0 0 24 24"><path d="M12.4065 5.44832C12.8151 5.44832 13.1994 5.56542 13.5595 5.7996C13.9265 6.0267 14.2208 6.36024 14.4424 6.80022C14.664 7.23312 14.7748 7.74407 14.7748 8.33309C14.7748 8.9292 14.664 9.44725 14.4424 9.88724C14.2208 10.3201 13.9265 10.6501 13.5595 10.8772C13.1994 11.1043 12.8151 11.2179 12.4065 11.2179C12.0464 11.2179 11.7037 11.1291 11.3782 10.9517C11.0527 10.7743 10.7931 10.5082 10.5992 10.1534C10.5715 10.4585 10.5507 10.7743 10.5368 11.1008H9.75781V3H10.6407V6.4383C10.8415 6.11185 11.0977 5.86702 11.4094 5.7038C11.721 5.53348 12.0534 5.44832 12.4065 5.44832ZM12.2923 10.3663C12.7631 10.3663 13.1405 10.1888 13.4245 9.83402C13.7084 9.47919 13.8503 8.97888 13.8503 8.33309C13.8503 7.69439 13.7084 7.19763 13.4245 6.8428C13.1405 6.48797 12.7631 6.31056 12.2923 6.31056C11.8422 6.31056 11.4544 6.47378 11.1289 6.80022C10.8034 7.12667 10.6407 7.63762 10.6407 8.33309C10.6407 8.80856 10.7203 9.19887 10.8796 9.50402C11.0389 9.80918 11.2432 10.0292 11.4925 10.164C11.7417 10.2988 12.0083 10.3663 12.2923 10.3663Z"></path><path d="M12.0949 14.9531C12.7043 14.9531 13.2029 15.1447 13.5906 15.5279C13.9784 15.9041 14.1723 16.4434 14.1723 17.146V20.6056H13.3933C13.3725 20.2436 13.3587 20.0094 13.3517 19.903C13.1856 20.1727 12.9605 20.3785 12.6766 20.5204C12.3996 20.6552 12.0811 20.7227 11.721 20.7227C11.3747 20.7227 11.0493 20.6588 10.7446 20.531C10.4468 20.3962 10.2079 20.2011 10.0279 19.9456C9.84783 19.683 9.75781 19.3708 9.75781 19.0088C9.75781 18.4624 9.97248 18.033 10.4018 17.7208C10.8381 17.4014 11.399 17.2418 12.0845 17.2418C12.4723 17.2418 12.8739 17.295 13.2894 17.4014V17.3056C13.2894 16.7876 13.1856 16.4044 12.9778 16.156C12.777 15.9005 12.4688 15.7728 12.0534 15.7728C11.721 15.7728 11.444 15.8437 11.2224 15.9857C11.0008 16.1276 10.8138 16.337 10.6615 16.6137L9.98633 16.2199C10.1525 15.8367 10.4226 15.5315 10.7965 15.3044C11.1774 15.0702 11.6102 14.9531 12.0949 14.9531ZM13.2894 18.1572C12.8532 18.0437 12.455 17.9869 12.0949 17.9869C11.6448 17.9869 11.2951 18.0756 11.0458 18.253C10.8034 18.4305 10.6823 18.6788 10.6823 18.9982C10.6823 19.2962 10.7861 19.5304 10.9939 19.7007C11.2016 19.864 11.4717 19.9456 11.8041 19.9456C12.2126 19.9456 12.5623 19.8249 12.8532 19.5836C13.144 19.3353 13.2894 18.9946 13.2894 18.5617V18.1572Z""></path><rect x="6.02344" y="12.7119" width="11.9532" height="0.747077"></rect></svg>',
9
+ stack: '<svg viewBox="0 0 24 24"><path d="M12.4065 5.44832C12.8151 5.44832 13.1994 5.56542 13.5595 5.7996C13.9265 6.0267 14.2208 6.36024 14.4424 6.80022C14.664 7.23312 14.7748 7.74407 14.7748 8.33309C14.7748 8.9292 14.664 9.44725 14.4424 9.88724C14.2208 10.3201 13.9265 10.6501 13.5595 10.8772C13.1994 11.1043 12.8151 11.2179 12.4065 11.2179C12.0464 11.2179 11.7037 11.1291 11.3782 10.9517C11.0527 10.7743 10.7931 10.5082 10.5992 10.1534C10.5715 10.4585 10.5507 10.7743 10.5368 11.1008H9.75781V3H10.6407V6.4383C10.8415 6.11185 11.0977 5.86702 11.4094 5.7038C11.721 5.53348 12.0534 5.44832 12.4065 5.44832ZM12.2923 10.3663C12.7631 10.3663 13.1405 10.1888 13.4245 9.83402C13.7084 9.47919 13.8503 8.97888 13.8503 8.33309C13.8503 7.69439 13.7084 7.19763 13.4245 6.8428C13.1405 6.48797 12.7631 6.31056 12.2923 6.31056C11.8422 6.31056 11.4544 6.47378 11.1289 6.80022C10.8034 7.12667 10.6407 7.63762 10.6407 8.33309C10.6407 8.80856 10.7203 9.19887 10.8796 9.50402C11.0389 9.80918 11.2432 10.0292 11.4925 10.164C11.7417 10.2988 12.0083 10.3663 12.2923 10.3663Z"></path><path d="M12.0949 14.9531C12.7043 14.9531 13.2029 15.1447 13.5906 15.5279C13.9784 15.9041 14.1723 16.4434 14.1723 17.146V20.6056H13.3933C13.3725 20.2436 13.3587 20.0094 13.3517 19.903C13.1856 20.1727 12.9605 20.3785 12.6766 20.5204C12.3996 20.6552 12.0811 20.7227 11.721 20.7227C11.3747 20.7227 11.0493 20.6588 10.7446 20.531C10.4468 20.3962 10.2079 20.2011 10.0279 19.9456C9.84783 19.683 9.75781 19.3708 9.75781 19.0088C9.75781 18.4624 9.97248 18.033 10.4018 17.7208C10.8381 17.4014 11.399 17.2418 12.0845 17.2418C12.4723 17.2418 12.8739 17.295 13.2894 17.4014V17.3056C13.2894 16.7876 13.1856 16.4044 12.9778 16.156C12.777 15.9005 12.4688 15.7728 12.0534 15.7728C11.721 15.7728 11.444 15.8437 11.2224 15.9857C11.0008 16.1276 10.8138 16.337 10.6615 16.6137L9.98633 16.2199C10.1525 15.8367 10.4226 15.5315 10.7965 15.3044C11.1774 15.0702 11.6102 14.9531 12.0949 14.9531ZM13.2894 18.1572C12.8532 18.0437 12.455 17.9869 12.0949 17.9869C11.6448 17.9869 11.2951 18.0756 11.0458 18.253C10.8034 18.4305 10.6823 18.6788 10.6823 18.9982C10.6823 19.2962 10.7861 19.5304 10.9939 19.7007C11.2016 19.864 11.4717 19.9456 11.8041 19.9456C12.2126 19.9456 12.5623 19.8249 12.8532 19.5836C13.144 19.3353 13.2894 18.9946 13.2894 18.5617V18.1572Z""></path><rect x="6.02344" y="12.7119" width="11.9532" height="0.747077"></rect></svg>'
10
10
  };
@@ -367,7 +367,9 @@ export class MTextToolbar {
367
367
  this.onFormatChange({ italic: !this.controls.italicBtn.classList.contains('is-active') });
368
368
  });
369
369
  this.controls.underlineBtn.addEventListener('click', () => {
370
- this.onFormatChange({ underline: !this.controls.underlineBtn.classList.contains('is-active') });
370
+ this.onFormatChange({
371
+ underline: !this.controls.underlineBtn.classList.contains('is-active')
372
+ });
371
373
  });
372
374
  this.controls.overlineBtn.addEventListener('click', () => {
373
375
  this.onFormatChange({ overline: !this.controls.overlineBtn.classList.contains('is-active') });
@@ -425,7 +427,9 @@ export class MTextToolbar {
425
427
  const host = document.createElement('div');
426
428
  host.className = 'ml-mtext-toolbar__color';
427
429
  host.dataset.mlMtextToolbarInteractive = 'true';
428
- const initialColor = initialFormat ? formatToMTextColor(initialFormat) : hexToMTextColor('#ffffff');
430
+ const initialColor = initialFormat
431
+ ? formatToMTextColor(initialFormat)
432
+ : hexToMTextColor('#ffffff');
429
433
  const instance = factory({
430
434
  container: host,
431
435
  theme: this.theme,
@@ -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;
@@ -130,6 +132,8 @@ export declare class MTextInputBox {
130
132
  setCurrentFormat(format: Partial<CharFormat>): void;
131
133
  /** Returns current insertion format. */
132
134
  getCurrentFormat(): CharFormat;
135
+ /** Toggles selected alphabetic text between upper and lower case. */
136
+ toggleCase(): void;
133
137
  /** Returns renderer default text style defined by `textStyle`. */
134
138
  getDefaultTextStyle(): TextStyle;
135
139
  /** Keyboard event handler. Returns true when consumed. */
@@ -166,6 +170,8 @@ export declare class MTextInputBox {
166
170
  y: number;
167
171
  z: number;
168
172
  };
173
+ /** Returns the MTEXT insertion point supplied when the editor was opened. */
174
+ getMTextInsertionPoint(): THREE.Vector3;
169
175
  /** Returns current internal state snapshot. */
170
176
  getState(): EditorState;
171
177
  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;
@@ -245,7 +246,9 @@ export class MTextInputBox {
245
246
  lastHeight,
246
247
  widthFactor: Number.isFinite(textStyle.widthFactor) ? textStyle.widthFactor : 1,
247
248
  obliqueAngle: Number.isFinite(textStyle.obliqueAngle) ? textStyle.obliqueAngle : 0,
248
- textGenerationFlag: Number.isFinite(textStyle.textGenerationFlag) ? textStyle.textGenerationFlag : 0,
249
+ textGenerationFlag: Number.isFinite(textStyle.textGenerationFlag)
250
+ ? textStyle.textGenerationFlag
251
+ : 0,
249
252
  standardFlag: Number.isFinite(textStyle.standardFlag) ? textStyle.standardFlag : 0,
250
253
  font: textStyle.font || MTextInputBox.DEFAULT_TEXT_STYLE.font,
251
254
  bigFont: textStyle.bigFont ?? ''
@@ -257,7 +260,7 @@ export class MTextInputBox {
257
260
  this.mtextRenderer = new UnifiedRenderer('main', {
258
261
  workerUrl: typeof options.workerUrl === 'string'
259
262
  ? options.workerUrl
260
- : options.workerUrl?.toString() ?? './assets/mtext-renderer-worker.js'
263
+ : (options.workerUrl?.toString() ?? './assets/mtext-renderer-worker.js')
261
264
  });
262
265
  if (options.fontUrl) {
263
266
  this.mtextRenderer.setFontUrl(options.fontUrl);
@@ -268,6 +271,9 @@ export class MTextInputBox {
268
271
  lineTolerance: 4
269
272
  });
270
273
  const cursorStyle = { ...(options.cursorStyle ?? {}) };
274
+ if (cursorStyle.height === undefined && cursorStyle.heightMode !== 'fixed') {
275
+ cursorStyle.height = 0;
276
+ }
271
277
  cursorStyle.color ??= '#ffffff';
272
278
  cursorStyle.glowColor ??= '#ffffff';
273
279
  this.cursorRenderer = new CursorRenderer({
@@ -276,10 +282,17 @@ export class MTextInputBox {
276
282
  selectionStyle: options.selectionStyle,
277
283
  enableDebug: false,
278
284
  enableSelection: true,
279
- debugYAxisUp: this.camera instanceof THREE.OrthographicCamera ? this.camera.top >= this.camera.bottom : true,
285
+ debugYAxisUp: this.camera instanceof THREE.OrthographicCamera
286
+ ? this.camera.top >= this.camera.bottom
287
+ : true,
280
288
  debugVisibility: this.debugVisibility
281
289
  });
282
- this.cursorRenderer.setViewTransform({ x: this.position.x, y: this.position.y, scaleX: 1, scaleY: 1 });
290
+ this.cursorRenderer.setViewTransform({
291
+ x: this.position.x,
292
+ y: this.position.y,
293
+ scaleX: 1,
294
+ scaleY: 1
295
+ });
283
296
  this.createBoundingBox(options.showBoundingBox ?? true, options.boundingBoxStyle);
284
297
  this.setText(options.initialText ?? '');
285
298
  this.attachIme(options.imeTarget);
@@ -559,6 +572,47 @@ export class MTextInputBox {
559
572
  getCurrentFormat() {
560
573
  return { ...this.currentFormat };
561
574
  }
575
+ /** Toggles selected alphabetic text between upper and lower case. */
576
+ toggleCase() {
577
+ const selection = this.getSelectionRange();
578
+ if (selection.isCollapsed)
579
+ return;
580
+ this.commitHistoryEdit(() => {
581
+ this.syncDocumentFromUiState();
582
+ const start = this.toDocumentIndexFromLogicalIndex(selection.start, true);
583
+ const end = this.toDocumentIndexFromLogicalIndex(selection.end, false);
584
+ if (end <= start)
585
+ return;
586
+ const selectedNodes = this.document.ast.nodes.slice(start, end);
587
+ const selectedText = selectedNodes
588
+ .map((node) => {
589
+ if (node.type === 'char')
590
+ return node.value;
591
+ if (node.type === 'stack')
592
+ return `${node.numerator}${node.denominator}`;
593
+ return '';
594
+ })
595
+ .join('');
596
+ if (!/[a-zA-Z]/.test(selectedText))
597
+ return;
598
+ const hasLowercase = /[a-z]/.test(selectedText);
599
+ const transform = (value) => hasLowercase ? value.toUpperCase() : value.toLowerCase();
600
+ this.document.transaction(() => {
601
+ for (const node of selectedNodes) {
602
+ if (node.type === 'char') {
603
+ node.value = transform(node.value);
604
+ }
605
+ else if (node.type === 'stack') {
606
+ node.numerator = transform(node.numerator);
607
+ node.denominator = transform(node.denominator);
608
+ }
609
+ }
610
+ this.document.setSelection(start, end);
611
+ });
612
+ this.syncUiStateFromDocument();
613
+ this.relayout();
614
+ });
615
+ }
562
616
  /** Returns renderer default text style defined by `textStyle`. */
563
617
  getDefaultTextStyle() {
564
618
  return { ...this.defaultTextStyle };
@@ -796,13 +850,18 @@ export class MTextInputBox {
796
850
  }
797
851
  /** Returns cursor world position for IME/caret anchoring. */
798
852
  getCursorWorldPosition() {
799
- const cursor = this.cursorLogic.getCursorState().position;
853
+ const cursorState = this.cursorLogic.getCursorState();
854
+ const cursor = this.getActiveCursorRenderState(cursorState.position, cursorState.lineInfo.height).position;
800
855
  return {
801
856
  x: this.position.x + cursor.x,
802
857
  y: this.position.y + cursor.y,
803
858
  z: this.position.z
804
859
  };
805
860
  }
861
+ /** Returns the MTEXT insertion point supplied when the editor was opened. */
862
+ getMTextInsertionPoint() {
863
+ return this.position.clone();
864
+ }
806
865
  /** Returns current internal state snapshot. */
807
866
  getState() {
808
867
  return {
@@ -976,8 +1035,8 @@ export class MTextInputBox {
976
1035
  let maxY = Number.NEGATIVE_INFINITY;
977
1036
  for (const corner of corners) {
978
1037
  const projected = corner.clone().project(this.camera);
979
- const sx = rect.left + ((projected.x + 1) * 0.5) * rect.width;
980
- const sy = rect.top + ((1 - projected.y) * 0.5) * rect.height;
1038
+ const sx = rect.left + (projected.x + 1) * 0.5 * rect.width;
1039
+ const sy = rect.top + (1 - projected.y) * 0.5 * rect.height;
981
1040
  minX = Math.min(minX, sx);
982
1041
  maxX = Math.max(maxX, sx);
983
1042
  minY = Math.min(minY, sy);
@@ -1058,6 +1117,7 @@ export class MTextInputBox {
1058
1117
  }
1059
1118
  relayout() {
1060
1119
  if (!this.rendererReady) {
1120
+ this.mtextInsertionOffset.set(0, 0, 0);
1061
1121
  const fallback = this.createFallbackCharBoxes();
1062
1122
  this.layoutContainer = fallback.containerBox;
1063
1123
  this.updateCursorData(fallback.charBoxes, fallback.lineBreakIndices, fallback.lineLayouts);
@@ -1072,6 +1132,7 @@ export class MTextInputBox {
1072
1132
  const object = this.mtextRenderer.syncRenderMText(mtextData, style, colorSettings);
1073
1133
  this.replaceRenderedObject(object);
1074
1134
  const rendered = this.extractBoxesFromRenderedObject(object);
1135
+ this.mtextInsertionOffset.set(0, 0, 0);
1075
1136
  this.normalizeRenderedTopAlignment(object, rendered);
1076
1137
  this.layoutContainer = rendered.containerBox;
1077
1138
  this.updateCursorData(rendered.charBoxes, rendered.lineBreakIndices, rendered.lineLayouts);
@@ -1079,6 +1140,7 @@ export class MTextInputBox {
1079
1140
  }
1080
1141
  catch (error) {
1081
1142
  console.error('[mtext-input-box] Failed to sync render MTEXT', error);
1143
+ this.mtextInsertionOffset.set(0, 0, 0);
1082
1144
  const fallback = this.createFallbackCharBoxes();
1083
1145
  this.layoutContainer = fallback.containerBox;
1084
1146
  this.updateCursorData(fallback.charBoxes, fallback.lineBreakIndices, fallback.lineLayouts);
@@ -1089,7 +1151,10 @@ export class MTextInputBox {
1089
1151
  if (this.pendingFontLoad)
1090
1152
  return;
1091
1153
  const fonts = this.collectFontNamesForText();
1092
- const key = fonts.map((font) => font.toLowerCase()).sort().join('|');
1154
+ const key = fonts
1155
+ .map((font) => font.toLowerCase())
1156
+ .sort()
1157
+ .join('|');
1093
1158
  if (!key || key === this.lastFontKey)
1094
1159
  return;
1095
1160
  this.lastFontKey = key;
@@ -1141,7 +1206,9 @@ export class MTextInputBox {
1141
1206
  this.cursorLogic.updateData(this.layoutContainer, charBoxes, lineBreakIndices, lineLayouts);
1142
1207
  const nextIndex = Math.min(this.cursorIndex, this.charCount());
1143
1208
  const maxLineIndex = Math.max(0, this.cursorLogic.getLineCount() - 1);
1144
- const pendingLineHint = this.pendingCursorLineHint === null ? null : Math.max(0, Math.min(this.pendingCursorLineHint, maxLineIndex));
1209
+ const pendingLineHint = this.pendingCursorLineHint === null
1210
+ ? null
1211
+ : Math.max(0, Math.min(this.pendingCursorLineHint, maxLineIndex));
1145
1212
  this.pendingCursorLineHint = null;
1146
1213
  this.cursorLogic.moveTo(nextIndex, pendingLineHint);
1147
1214
  if (this.selectionStart !== this.selectionEnd) {
@@ -1205,9 +1272,11 @@ export class MTextInputBox {
1205
1272
  };
1206
1273
  }
1207
1274
  normalizeRenderedTopAlignment(object, rendered) {
1208
- const dy = -rendered.containerBox.y;
1275
+ const topY = rendered.containerBox.y + rendered.containerBox.height;
1276
+ const dy = -topY;
1209
1277
  if (!Number.isFinite(dy) || Math.abs(dy) < 1e-8)
1210
1278
  return;
1279
+ this.mtextInsertionOffset.set(0, dy, 0);
1211
1280
  object.position.y += dy;
1212
1281
  object.updateMatrixWorld(true);
1213
1282
  rendered.containerBox.y += dy;
@@ -1238,34 +1307,35 @@ export class MTextInputBox {
1238
1307
  const lineBreakIndices = [];
1239
1308
  const lineLayouts = [];
1240
1309
  let x = 0;
1241
- let y = 0;
1242
- let maxY = lineHeight;
1243
- lineLayouts.push({ y: y + lineHeight / 2, height: lineHeight });
1310
+ let lineTop = 0;
1311
+ let minY = -lineHeight;
1312
+ lineLayouts.push({ y: lineTop - lineHeight / 2, height: lineHeight });
1244
1313
  for (const char of this.getChars()) {
1245
1314
  if (char === '\n') {
1246
1315
  lineBreakIndices.push(charBoxes.length);
1247
1316
  x = 0;
1248
- y += lineHeight;
1249
- maxY = Math.max(maxY, y + lineHeight);
1250
- lineLayouts.push({ y: y + lineHeight / 2, height: lineHeight });
1317
+ lineTop -= lineHeight;
1318
+ minY = Math.min(minY, lineTop - lineHeight);
1319
+ lineLayouts.push({ y: lineTop - lineHeight / 2, height: lineHeight });
1251
1320
  continue;
1252
1321
  }
1253
1322
  const width = Math.max(1, this.currentFormat.fontSize * 0.6);
1254
1323
  if (this.enableWordWrap && x > 0 && x + width > this.width) {
1255
1324
  lineBreakIndices.push(charBoxes.length);
1256
1325
  x = 0;
1257
- y += lineHeight;
1326
+ lineTop -= lineHeight;
1327
+ minY = Math.min(minY, lineTop - lineHeight);
1328
+ lineLayouts.push({ y: lineTop - lineHeight / 2, height: lineHeight });
1258
1329
  }
1259
- charBoxes.push({ x, y: y + lineHeight / 2, width, height: lineHeight });
1330
+ charBoxes.push({ x, y: lineTop - lineHeight / 2, width, height: lineHeight });
1260
1331
  x += width;
1261
- maxY = Math.max(maxY, y + lineHeight);
1262
1332
  }
1263
1333
  return {
1264
1334
  containerBox: {
1265
1335
  x: 0,
1266
- y: 0,
1336
+ y: minY,
1267
1337
  width: this.width,
1268
- height: Math.max(1, maxY)
1338
+ height: Math.max(1, -minY)
1269
1339
  },
1270
1340
  charBoxes,
1271
1341
  lineBreakIndices,
@@ -1337,13 +1407,13 @@ export class MTextInputBox {
1337
1407
  : []);
1338
1408
  for (const mat of materials) {
1339
1409
  const m = mat;
1340
- if (typeof m.side !== "undefined")
1410
+ if (typeof m.side !== 'undefined')
1341
1411
  m.side = THREE.DoubleSide;
1342
- if (typeof m.depthTest !== "undefined")
1412
+ if (typeof m.depthTest !== 'undefined')
1343
1413
  m.depthTest = false;
1344
- if (typeof m.depthWrite !== "undefined")
1414
+ if (typeof m.depthWrite !== 'undefined')
1345
1415
  m.depthWrite = false;
1346
- if (typeof m.transparent !== "undefined" && typeof m.opacity !== "undefined") {
1416
+ if (typeof m.transparent !== 'undefined' && typeof m.opacity !== 'undefined') {
1347
1417
  m.transparent = m.opacity < 1;
1348
1418
  }
1349
1419
  m.needsUpdate = true;
@@ -1470,8 +1540,8 @@ export class MTextInputBox {
1470
1540
  const world = new THREE.Vector3(cursorWorld.x, cursorWorld.y, cursorWorld.z);
1471
1541
  const projected = world.project(this.camera);
1472
1542
  const rect = this.imeTarget.getBoundingClientRect();
1473
- const x = rect.left + ((projected.x + 1) * 0.5) * rect.width;
1474
- const y = rect.top + ((1 - projected.y) * 0.5) * rect.height;
1543
+ const x = rect.left + (projected.x + 1) * 0.5 * rect.width;
1544
+ const y = rect.top + (1 - projected.y) * 0.5 * rect.height;
1475
1545
  this.imeInput.style.left = `${Math.round(x)}px`;
1476
1546
  this.imeInput.style.top = `${Math.round(y)}px`;
1477
1547
  }
@@ -1535,7 +1605,9 @@ export class MTextInputBox {
1535
1605
  chars.push('');
1536
1606
  continue;
1537
1607
  }
1538
- if (node.type === 'paragraphBreak' || node.type === 'columnBreak' || node.type === 'wrapAtDimLine') {
1608
+ if (node.type === 'paragraphBreak' ||
1609
+ node.type === 'columnBreak' ||
1610
+ node.type === 'wrapAtDimLine') {
1539
1611
  chars.push('\n');
1540
1612
  }
1541
1613
  }
@@ -1568,7 +1640,7 @@ export class MTextInputBox {
1568
1640
  // Best source: renderer-provided line layout already represents
1569
1641
  // the actual first-line center in editor local coordinates.
1570
1642
  position: { x: fallbackPosition.x, y: firstLine.y },
1571
- height: Math.max(1, lineHeight * 0.8)
1643
+ height: lineHeight
1572
1644
  };
1573
1645
  }
1574
1646
  const inferredLineHeight = Math.max(1, this.getFallbackLineAdvance());
@@ -1576,12 +1648,12 @@ export class MTextInputBox {
1576
1648
  position: {
1577
1649
  x: fallbackPosition.x,
1578
1650
  // When explicit line layout is unavailable, infer first-line center
1579
- // from top-left container coordinates:
1580
- // lineCenterY = containerTopY + lineHeight / 2
1651
+ // from the container's lower edge in y-up coordinates:
1652
+ // lineCenterY = containerBottomY + lineHeight / 2
1581
1653
  // This keeps caret aligned with the top row for empty MTEXT.
1582
1654
  y: this.layoutContainer.y + inferredLineHeight / 2
1583
1655
  },
1584
- height: Math.max(1, inferredLineHeight * 0.8)
1656
+ height: inferredLineHeight
1585
1657
  };
1586
1658
  }
1587
1659
  // At a line-start gap (including wrapped/paragraph boundaries), rely on
@@ -1598,7 +1670,7 @@ export class MTextInputBox {
1598
1670
  };
1599
1671
  }
1600
1672
  }
1601
- const emptyLineHeight = (line.height ?? fallbackHeight) * 0.8;
1673
+ const emptyLineHeight = line.height ?? fallbackHeight;
1602
1674
  return {
1603
1675
  position: { x: fallbackPosition.x, y: line.y ?? fallbackPosition.y },
1604
1676
  height: Math.max(1, emptyLineHeight)
@@ -1618,13 +1690,13 @@ export class MTextInputBox {
1618
1690
  }
1619
1691
  getNodeLogicalSpan(node) {
1620
1692
  switch (node.type) {
1621
- case "char":
1693
+ case 'char':
1622
1694
  return 1;
1623
- case "stack":
1695
+ case 'stack':
1624
1696
  return 1;
1625
- case "paragraphBreak":
1626
- case "columnBreak":
1627
- case "wrapAtDimLine":
1697
+ case 'paragraphBreak':
1698
+ case 'columnBreak':
1699
+ case 'wrapAtDimLine':
1628
1700
  // Structural line-break nodes do not have a visual char box, so
1629
1701
  // they must not consume logical cursor positions.
1630
1702
  return 0;
@@ -1683,7 +1755,9 @@ export class MTextInputBox {
1683
1755
  const minCandidateLine = candidates[0]?.lineIndex ?? 0;
1684
1756
  const maxCandidateLine = candidates[candidates.length - 1]?.lineIndex ?? 0;
1685
1757
  if (lineIndexHint < minCandidateLine || lineIndexHint > maxCandidateLine) {
1686
- return preferAfterZeroSpan ? candidates[candidates.length - 1].index : candidates[0].index;
1758
+ return preferAfterZeroSpan
1759
+ ? candidates[candidates.length - 1].index
1760
+ : candidates[0].index;
1687
1761
  }
1688
1762
  let best = candidates[0];
1689
1763
  let bestDist = Math.abs(best.lineIndex - lineIndexHint);
@@ -1864,7 +1938,8 @@ export class MTextInputBox {
1864
1938
  return;
1865
1939
  const scriptStyle = this.cloneDocumentStyle(first.style);
1866
1940
  scriptStyle.script = script;
1867
- scriptStyle.align = script === 'superscript' ? MTextLineAlignment.TOP : MTextLineAlignment.BOTTOM;
1941
+ scriptStyle.align =
1942
+ script === 'superscript' ? MTextLineAlignment.TOP : MTextLineAlignment.BOTTOM;
1868
1943
  const stackNode = {
1869
1944
  type: 'stack',
1870
1945
  numerator: script === 'superscript' ? plainText : '',
@@ -1928,7 +2003,8 @@ export class MTextInputBox {
1928
2003
  if (!!snapshot.selection !== !!selection)
1929
2004
  return false;
1930
2005
  if (snapshot.selection && selection) {
1931
- if (snapshot.selection.start !== selection.start || snapshot.selection.end !== selection.end) {
2006
+ if (snapshot.selection.start !== selection.start ||
2007
+ snapshot.selection.end !== selection.end) {
1932
2008
  return false;
1933
2009
  }
1934
2010
  }
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.5",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",