@mlightcad/mtext-input-box 0.2.8 → 0.2.9

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.
@@ -221,6 +221,13 @@ export declare class MTextInputBox {
221
221
  private ensureFontsLoaded;
222
222
  private collectFontNamesForText;
223
223
  private updateCursorData;
224
+ /**
225
+ * Editor vertical bounds for the selection box / bounding overlay.
226
+ * Renderer `layout.lines` strips are often taller than actual glyphs (leading
227
+ * above the first row, uniform line-height slots, etc.). When a line has
228
+ * glyph boxes, use those for that row; keep the strip only for empty lines.
229
+ */
230
+ private computeEditorVerticalBounds;
224
231
  private extractBoxesFromRenderedObject;
225
232
  private normalizeRenderedTopAlignment;
226
233
  private toLocalBox;
@@ -1299,6 +1299,84 @@ export class MTextInputBox {
1299
1299
  }
1300
1300
  this.syncStateFromCursor();
1301
1301
  }
1302
+ /**
1303
+ * Editor vertical bounds for the selection box / bounding overlay.
1304
+ * Renderer `layout.lines` strips are often taller than actual glyphs (leading
1305
+ * above the first row, uniform line-height slots, etc.). When a line has
1306
+ * glyph boxes, use those for that row; keep the strip only for empty lines.
1307
+ */
1308
+ computeEditorVerticalBounds(lineLayouts, charBoxes, objectLocal) {
1309
+ const fallback = () => ({
1310
+ minY: objectLocal.y - objectLocal.height / 2,
1311
+ maxY: objectLocal.y + objectLocal.height / 2
1312
+ });
1313
+ const overlapY = (a0, a1, b0, b1) => a0 < b1 + 1e-4 && a1 > b0 - 1e-4;
1314
+ if (lineLayouts.length === 0 && charBoxes.length === 0) {
1315
+ return fallback();
1316
+ }
1317
+ if (lineLayouts.length === 0) {
1318
+ let minY = Number.POSITIVE_INFINITY;
1319
+ let maxY = Number.NEGATIVE_INFINITY;
1320
+ for (const b of charBoxes) {
1321
+ minY = Math.min(minY, b.y - b.height / 2);
1322
+ maxY = Math.max(maxY, b.y + b.height / 2);
1323
+ }
1324
+ return Number.isFinite(minY) && Number.isFinite(maxY) && maxY >= minY
1325
+ ? { minY, maxY }
1326
+ : fallback();
1327
+ }
1328
+ if (charBoxes.length === 0) {
1329
+ let minY = Number.POSITIVE_INFINITY;
1330
+ let maxY = Number.NEGATIVE_INFINITY;
1331
+ for (const line of lineLayouts) {
1332
+ const lo = line.y - line.height / 2;
1333
+ const hi = line.y + line.height / 2;
1334
+ minY = Math.min(minY, lo);
1335
+ maxY = Math.max(maxY, hi);
1336
+ }
1337
+ return Number.isFinite(minY) && Number.isFinite(maxY) && maxY >= minY
1338
+ ? { minY, maxY }
1339
+ : fallback();
1340
+ }
1341
+ let minY = Number.POSITIVE_INFINITY;
1342
+ let maxY = Number.NEGATIVE_INFINITY;
1343
+ for (const line of lineLayouts) {
1344
+ const lo = line.y - line.height / 2;
1345
+ const hi = line.y + line.height / 2;
1346
+ const onLine = charBoxes.filter((b) => {
1347
+ const c0 = b.y - b.height / 2;
1348
+ const c1 = b.y + b.height / 2;
1349
+ return overlapY(c0, c1, lo, hi);
1350
+ });
1351
+ if (onLine.length === 0) {
1352
+ minY = Math.min(minY, lo);
1353
+ maxY = Math.max(maxY, hi);
1354
+ }
1355
+ else {
1356
+ for (const b of onLine) {
1357
+ minY = Math.min(minY, b.y - b.height / 2);
1358
+ maxY = Math.max(maxY, b.y + b.height / 2);
1359
+ }
1360
+ }
1361
+ }
1362
+ for (const b of charBoxes) {
1363
+ const c0 = b.y - b.height / 2;
1364
+ const c1 = b.y + b.height / 2;
1365
+ const overlapsAnyLine = lineLayouts.some((line) => {
1366
+ const lo = line.y - line.height / 2;
1367
+ const hi = line.y + line.height / 2;
1368
+ return overlapY(c0, c1, lo, hi);
1369
+ });
1370
+ if (!overlapsAnyLine) {
1371
+ minY = Math.min(minY, c0);
1372
+ maxY = Math.max(maxY, c1);
1373
+ }
1374
+ }
1375
+ if (!Number.isFinite(minY) || !Number.isFinite(maxY) || maxY < minY) {
1376
+ return fallback();
1377
+ }
1378
+ return { minY, maxY };
1379
+ }
1302
1380
  extractBoxesFromRenderedObject(object) {
1303
1381
  const layout = object.createLayoutData();
1304
1382
  const charBoxes = [];
@@ -1325,16 +1403,7 @@ export class MTextInputBox {
1325
1403
  .filter((value) => Number.isInteger(value))
1326
1404
  .filter((value) => value >= 0 && value <= charBoxes.length);
1327
1405
  const local = this.toLocalBox(object.box);
1328
- let containerTop = local.y - local.height / 2;
1329
- let containerBottom = local.y + local.height / 2;
1330
- if (lineLayouts.length > 0) {
1331
- for (const line of lineLayouts) {
1332
- const top = line.y - line.height / 2;
1333
- const bottom = line.y + line.height / 2;
1334
- containerTop = Math.min(containerTop, top);
1335
- containerBottom = Math.max(containerBottom, bottom);
1336
- }
1337
- }
1406
+ const { minY: containerTop, maxY: containerBottom } = this.computeEditorVerticalBounds(lineLayouts, charBoxes, local);
1338
1407
  const containerBox = {
1339
1408
  x: local.x,
1340
1409
  y: containerTop,
@@ -1343,7 +1412,12 @@ export class MTextInputBox {
1343
1412
  };
1344
1413
  containerBox.x = 0;
1345
1414
  containerBox.width = this.width;
1346
- containerBox.height = Math.max(containerBox.height, this.getFallbackLineAdvance());
1415
+ const minHeight = this.getFallbackLineAdvance();
1416
+ if (containerBox.height < minHeight) {
1417
+ const delta = minHeight - containerBox.height;
1418
+ containerBox.y -= delta;
1419
+ containerBox.height = minHeight;
1420
+ }
1347
1421
  return {
1348
1422
  containerBox,
1349
1423
  charBoxes,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlightcad/mtext-input-box",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
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.13",
20
+ "@mlightcad/mtext-renderer": "^0.10.14",
21
21
  "three": "^0.172.0"
22
22
  },
23
23
  "devDependencies": {
@@ -28,6 +28,7 @@
28
28
  "@typescript-eslint/parser": "^8.41.0",
29
29
  "eslint": "^9.34.0",
30
30
  "eslint-config-prettier": "^10.1.8",
31
+ "rollup-plugin-peer-deps-external": "^2.2.4",
31
32
  "typescript": "^5.9.2",
32
33
  "typescript-eslint": "^8.41.0",
33
34
  "vitest": "^3.2.4"
@@ -35,6 +36,6 @@
35
36
  "scripts": {
36
37
  "build": "tsc -p tsconfig.build.json",
37
38
  "lint": "eslint src tests",
38
- "test": "vitest run --config ./vitest.config.ts"
39
+ "test": "vitest run"
39
40
  }
40
41
  }