@mlightcad/mtext-input-box 0.2.0 → 0.2.1

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
@@ -30,6 +30,7 @@ pnpm add @mlightcad/mtext-input-box three @mlightcad/mtext-parser @mlightcad/mte
30
30
  ```ts
31
31
  import * as THREE from 'three';
32
32
  import { MTextInputBox } from '@mlightcad/mtext-input-box';
33
+ import { MTextColor } from '@mlightcad/mtext-renderer';
33
34
 
34
35
  const scene = new THREE.Scene();
35
36
  const camera = new THREE.OrthographicCamera(0, 1000, 600, 0, -1000, 1000);
@@ -51,8 +52,12 @@ const editor = new MTextInputBox({
51
52
  textGenerationFlag: 0,
52
53
  lastHeight: 24,
53
54
  font: 'simkai',
54
- bigFont: '',
55
- color: 0xffffff
55
+ bigFont: ''
56
+ },
57
+ colorSettings: {
58
+ byLayerColor: 0xffffff,
59
+ byBlockColor: 0xffffff,
60
+ color: new MTextColor(256)
56
61
  },
57
62
  toolbar: {
58
63
  enabled: true,
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { MTextInputBox, MTextEditor, defaultCharFormat, sameFormat } from './viewer';
1
+ export { DEFAULT_FONT_FAMILY, MTextInputBox, MTextEditor, defaultCharFormat, sameFormat } from './viewer';
2
2
  export { MTextDocument, parseMTextToAst, buildMTextFromAst, insertTextInAst, deleteRangeInAst, replaceRangeInAst } from './model';
3
3
  export { EditorUiAdapter } from './controller';
4
4
  export { MTextToolbar } from './ui';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { MTextInputBox, MTextEditor, defaultCharFormat, sameFormat } from './viewer';
1
+ export { DEFAULT_FONT_FAMILY, MTextInputBox, MTextEditor, defaultCharFormat, sameFormat } from './viewer';
2
2
  export { MTextDocument, parseMTextToAst, buildMTextFromAst, insertTextInAst, deleteRangeInAst, replaceRangeInAst } from './model';
3
3
  export { EditorUiAdapter } from './controller';
4
4
  export { MTextToolbar } from './ui';
@@ -1,2 +1,3 @@
1
+ export { DEFAULT_FONT_FAMILY } from './format';
1
2
  export { MTextInputBox, MTextEditor, defaultCharFormat, sameFormat } from './viewer';
2
3
  export type { CharFormat, CharScript, EditorState, LayoutCharBox, RendererContainerBox, RendererLineInfo, MTextRendererOutput, CursorLayoutData, MTextBoundingBoxStyle, MTextInputBoxOptions, MTextToolbarColorPickerContext, MTextToolbarColorPickerFactory, MTextToolbarColorPickerInstance, MTextToolbarOptions, MTextToolbarTheme, CursorDirection, SelectionDirection, MTextInputBoxEvent, MTextEditorEvent, MTextEditorOptions } from './types';
@@ -1 +1,2 @@
1
+ export { DEFAULT_FONT_FAMILY } from './format';
1
2
  export { MTextInputBox, MTextEditor, defaultCharFormat, sameFormat } from './viewer';
@@ -204,7 +204,7 @@ export declare class MTextInputBox {
204
204
  private normalizeColorNumber;
205
205
  private resolveAciColor;
206
206
  private resolveBaseColor;
207
- private resolveFormatColorNumber;
207
+ private resolveBaseFormatColor;
208
208
  private createBaseFormatFromTextStyle;
209
209
  private createMTextData;
210
210
  private getFallbackLineAdvance;
@@ -1,5 +1,5 @@
1
1
  import { CursorRenderer, TextBoxCursor, ThreeJsRendererAdapter } from '@mlightcad/text-box-cursor';
2
- import { getColorByIndex, UnifiedRenderer, MTextAttachmentPoint, MTextFlowDirection, MText } from '@mlightcad/mtext-renderer';
2
+ import { getColorByIndex, UnifiedRenderer, MTextAttachmentPoint, MTextFlowDirection, MText, MTextColor } from '@mlightcad/mtext-renderer';
3
3
  import * as THREE from 'three';
4
4
  import { MTextContext, MTextLineAlignment } from '@mlightcad/mtext-renderer';
5
5
  import { DEFAULT_FONT_FAMILY, defaultCharFormat, sameFormat } from './format';
@@ -30,8 +30,7 @@ export class MTextInputBox {
30
30
  textGenerationFlag: 0,
31
31
  lastHeight: 24,
32
32
  font: DEFAULT_FONT_FAMILY,
33
- bigFont: '',
34
- color: 0xffffff
33
+ bigFont: ''
35
34
  };
36
35
  width;
37
36
  position;
@@ -240,9 +239,6 @@ export class MTextInputBox {
240
239
  ? textStyle.fixedTextHeight
241
240
  : MTextInputBox.DEFAULT_TEXT_STYLE.fixedTextHeight);
242
241
  const lastHeight = Math.max(1, Number.isFinite(textStyle.lastHeight) ? textStyle.lastHeight : fixedTextHeight);
243
- const color = Number.isFinite(textStyle.color)
244
- ? textStyle.color
245
- : MTextInputBox.DEFAULT_TEXT_STYLE.color;
246
242
  this.defaultTextStyle = {
247
243
  ...textStyle,
248
244
  fixedTextHeight,
@@ -252,10 +248,9 @@ export class MTextInputBox {
252
248
  textGenerationFlag: Number.isFinite(textStyle.textGenerationFlag) ? textStyle.textGenerationFlag : 0,
253
249
  standardFlag: Number.isFinite(textStyle.standardFlag) ? textStyle.standardFlag : 0,
254
250
  font: textStyle.font || MTextInputBox.DEFAULT_TEXT_STYLE.font,
255
- bigFont: textStyle.bigFont ?? '',
256
- color: this.normalizeColorNumber(color)
251
+ bigFont: textStyle.bigFont ?? ''
257
252
  };
258
- this.baseFormat = this.createBaseFormatFromTextStyle(this.defaultTextStyle);
253
+ this.baseFormat = this.createBaseFormatFromTextStyle(this.defaultTextStyle, this.resolveRenderColorSettings());
259
254
  this.currentFormat = { ...this.baseFormat };
260
255
  this.document = new MTextDocument();
261
256
  this.uiAdapter = new EditorUiAdapter(this.document);
@@ -1397,19 +1392,30 @@ export class MTextInputBox {
1397
1392
  }
1398
1393
  return 0xffffff;
1399
1394
  }
1400
- resolveFormatColorNumber(format) {
1401
- return this.isExplicitAci(format.aci)
1402
- ? this.resolveAciColor(format.aci)
1403
- : format.rgb !== null
1404
- ? this.normalizeColorNumber(format.rgb)
1405
- : 0xffffff;
1395
+ resolveBaseFormatColor(colorSettings) {
1396
+ const color = colorSettings.color;
1397
+ if (color.isRgb && color.rgbValue !== null) {
1398
+ return { aci: null, rgb: this.normalizeColorNumber(color.rgbValue) };
1399
+ }
1400
+ if (color.isAci && color.aci !== null) {
1401
+ const aci = color.aci;
1402
+ if (Number.isInteger(aci) && aci >= 1 && aci <= 255) {
1403
+ return { aci, rgb: null };
1404
+ }
1405
+ if (aci === 0) {
1406
+ return { aci: null, rgb: this.normalizeColorNumber(colorSettings.byBlockColor) };
1407
+ }
1408
+ return { aci: null, rgb: this.normalizeColorNumber(colorSettings.byLayerColor) };
1409
+ }
1410
+ return { aci: null, rgb: this.normalizeColorNumber(colorSettings.byLayerColor) };
1406
1411
  }
1407
- createBaseFormatFromTextStyle(style) {
1412
+ createBaseFormatFromTextStyle(style, colorSettings) {
1408
1413
  const base = defaultCharFormat();
1409
1414
  base.fontFamily = style.font || base.fontFamily;
1410
1415
  base.fontSize = Math.max(1, style.fixedTextHeight);
1411
- base.rgb = this.normalizeColorNumber(style.color);
1412
- base.aci = null;
1416
+ const color = this.resolveBaseFormatColor(colorSettings);
1417
+ base.aci = color.aci;
1418
+ base.rgb = color.rgb;
1413
1419
  return base;
1414
1420
  }
1415
1421
  createMTextData() {
@@ -1439,7 +1445,7 @@ export class MTextInputBox {
1439
1445
  return this.colorSettings;
1440
1446
  }
1441
1447
  const base = this.toolbarTheme === 'light' ? 0x000000 : 0xffffff;
1442
- return { byLayerColor: base, byBlockColor: base };
1448
+ return { byLayerColor: base, byBlockColor: base, color: new MTextColor(256) };
1443
1449
  }
1444
1450
  focusImeInput() {
1445
1451
  if (!this.imeInput)
@@ -1736,7 +1742,7 @@ export class MTextInputBox {
1736
1742
  syncDocumentFromUiState() {
1737
1743
  const selection = this.getSelectionRange();
1738
1744
  if (!selection.isCollapsed) {
1739
- const start = this.toDocumentIndexFromLogicalIndex(selection.start, false);
1745
+ const start = this.toDocumentIndexFromLogicalIndex(selection.start, true);
1740
1746
  const end = this.toDocumentIndexFromLogicalIndex(selection.end, false);
1741
1747
  this.document.setSelection(start, end);
1742
1748
  return;
@@ -1765,7 +1771,7 @@ export class MTextInputBox {
1765
1771
  return;
1766
1772
  this.commitHistoryEdit(() => {
1767
1773
  this.syncDocumentFromUiState();
1768
- const start = this.toDocumentIndexFromLogicalIndex(selection.start, false);
1774
+ const start = this.toDocumentIndexFromLogicalIndex(selection.start, true);
1769
1775
  const end = this.toDocumentIndexFromLogicalIndex(selection.end, false);
1770
1776
  if (end <= start)
1771
1777
  return;
@@ -1819,7 +1825,7 @@ export class MTextInputBox {
1819
1825
  let changed = false;
1820
1826
  this.commitHistoryEdit(() => {
1821
1827
  this.syncDocumentFromUiState();
1822
- const start = this.toDocumentIndexFromLogicalIndex(selection.start, false);
1828
+ const start = this.toDocumentIndexFromLogicalIndex(selection.start, true);
1823
1829
  const end = this.toDocumentIndexFromLogicalIndex(selection.end, false);
1824
1830
  if (end <= start)
1825
1831
  return;
@@ -2006,7 +2012,7 @@ export class MTextInputBox {
2006
2012
  toolbar.setStackActive(false);
2007
2013
  return;
2008
2014
  }
2009
- const start = this.toDocumentIndexFromLogicalIndex(selection.start, false);
2015
+ const start = this.toDocumentIndexFromLogicalIndex(selection.start, true);
2010
2016
  const end = this.toDocumentIndexFromLogicalIndex(selection.end, false);
2011
2017
  const selectedNodes = this.document.ast.nodes.slice(start, end);
2012
2018
  const active = selectedNodes.length === 1 &&
@@ -2037,6 +2043,10 @@ export class MTextInputBox {
2037
2043
  const node = this.getNodeByLogicalCharIndex(anchorIndex);
2038
2044
  if (!node)
2039
2045
  return null;
2046
+ if (node.type === 'stack' && this.isScriptOnlyStack(node)) {
2047
+ const script = node.numerator.trim().length > 0 ? 'superscript' : 'subscript';
2048
+ return { ...this.toCharFormat(node.style), script };
2049
+ }
2040
2050
  return this.toCharFormat(node.style);
2041
2051
  }
2042
2052
  getNodeByLogicalCharIndex(index) {
@@ -2109,11 +2119,16 @@ export class MTextInputBox {
2109
2119
  };
2110
2120
  }
2111
2121
  toScriptFromStyle(style, baseSize, capHeight) {
2112
- if (style.script && style.script !== 'normal') {
2113
- return style.script;
2114
- }
2115
2122
  const reducedThreshold = Math.max(1, baseSize) * 0.9;
2116
2123
  const isReduced = capHeight <= reducedThreshold;
2124
+ if (style.script && style.script !== 'normal') {
2125
+ // Guard against styles that inherit baseline alignment (BOTTOM/TOP) but
2126
+ // are not actual superscript/subscript runs. Only treat non-normal script
2127
+ // as active when the cap height is reduced.
2128
+ if (isReduced)
2129
+ return style.script;
2130
+ return 'normal';
2131
+ }
2117
2132
  if (isReduced && style.align === MTextLineAlignment.TOP) {
2118
2133
  return 'superscript';
2119
2134
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlightcad/mtext-input-box",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
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.4",
20
+ "@mlightcad/mtext-renderer": "^0.10.5",
21
21
  "three": "^0.172.0"
22
22
  },
23
23
  "devDependencies": {