@opentui/core 0.1.19 → 0.1.21

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/index.js CHANGED
@@ -66,6 +66,7 @@ import {
66
66
  getBorderSides,
67
67
  getCharacterPositions,
68
68
  getKeyHandler,
69
+ getObjectsInViewport,
69
70
  green,
70
71
  h,
71
72
  hastToStyledText,
@@ -81,6 +82,7 @@ import {
81
82
  isPositionTypeType,
82
83
  isRenderable,
83
84
  isSizeType,
85
+ isStyledText,
84
86
  isVNode,
85
87
  isValidPercentage,
86
88
  italic,
@@ -118,7 +120,7 @@ import {
118
120
  white,
119
121
  wrapWithDelegates,
120
122
  yellow
121
- } from "./index-0h2r5adk.js";
123
+ } from "./index-8sac0sgm.js";
122
124
  // src/post/filters.ts
123
125
  function applyScanlines(buffer, strength = 0.8, step = 2) {
124
126
  const width = buffer.width;
@@ -1449,6 +1451,17 @@ var BrandedTextNodeRenderable = Symbol.for("@opentui/core/TextNodeRenderable");
1449
1451
  function isTextNodeRenderable(obj) {
1450
1452
  return !!obj?.[BrandedTextNodeRenderable];
1451
1453
  }
1454
+ function styledTextToTextNodes(styledText) {
1455
+ return styledText.chunks.map((chunk) => {
1456
+ const node = new TextNodeRenderable({
1457
+ fg: chunk.fg,
1458
+ bg: chunk.bg,
1459
+ attributes: chunk.attributes
1460
+ });
1461
+ node.add(chunk.text);
1462
+ return node;
1463
+ });
1464
+ }
1452
1465
 
1453
1466
  class TextNodeRenderable extends BaseRenderable {
1454
1467
  [BrandedTextNodeRenderable] = true;
@@ -1480,12 +1493,11 @@ class TextNodeRenderable extends BaseRenderable {
1480
1493
  this._children.splice(index, 0, obj);
1481
1494
  this.requestRender();
1482
1495
  return index;
1483
- } else {
1484
- const insertIndex = this._children.length;
1485
- this._children.push(obj);
1486
- this.requestRender();
1487
- return insertIndex;
1488
1496
  }
1497
+ const insertIndex = this._children.length;
1498
+ this._children.push(obj);
1499
+ this.requestRender();
1500
+ return insertIndex;
1489
1501
  }
1490
1502
  if (isTextNodeRenderable(obj)) {
1491
1503
  if (index !== undefined) {
@@ -1493,24 +1505,48 @@ class TextNodeRenderable extends BaseRenderable {
1493
1505
  obj.parent = this;
1494
1506
  this.requestRender();
1495
1507
  return index;
1496
- } else {
1497
- const insertIndex = this._children.length;
1498
- this._children.push(obj);
1499
- obj.parent = this;
1508
+ }
1509
+ const insertIndex = this._children.length;
1510
+ this._children.push(obj);
1511
+ obj.parent = this;
1512
+ this.requestRender();
1513
+ return insertIndex;
1514
+ }
1515
+ if (isStyledText(obj)) {
1516
+ const textNodes = styledTextToTextNodes(obj);
1517
+ if (index !== undefined) {
1518
+ this._children.splice(index, 0, ...textNodes);
1519
+ textNodes.forEach((node) => node.parent = this);
1500
1520
  this.requestRender();
1501
- return insertIndex;
1521
+ return index;
1502
1522
  }
1523
+ const insertIndex = this._children.length;
1524
+ this._children.push(...textNodes);
1525
+ textNodes.forEach((node) => node.parent = this);
1526
+ this.requestRender();
1527
+ return insertIndex;
1503
1528
  }
1504
- throw new Error("TextNodeRenderable only accepts strings or other TextNodeRenderable instances");
1529
+ throw new Error("TextNodeRenderable only accepts strings, TextNodeRenderable instances, or StyledText instances");
1505
1530
  }
1506
1531
  insertBefore(child, anchorNode) {
1532
+ if (!anchorNode || !isTextNodeRenderable(anchorNode)) {
1533
+ throw new Error("Anchor must be a TextNodeRenderable");
1534
+ }
1507
1535
  const anchorIndex = this._children.indexOf(anchorNode);
1508
1536
  if (anchorIndex === -1) {
1509
1537
  throw new Error("Anchor node not found in children");
1510
1538
  }
1511
- this._children.splice(anchorIndex, 0, child);
1512
- if (typeof child !== "string") {
1539
+ if (typeof child === "string") {
1540
+ this._children.splice(anchorIndex, 0, child);
1541
+ } else if (isTextNodeRenderable(child)) {
1542
+ this._children.splice(anchorIndex, 0, child);
1513
1543
  child.parent = this;
1544
+ } else if (child instanceof StyledText) {
1545
+ const textNodes = styledTextToTextNodes(child);
1546
+ this._children.splice(anchorIndex, 0, ...textNodes);
1547
+ textNodes.forEach((node) => node.parent = this);
1548
+ } else {
1549
+ throw new Error("Child must be a string, TextNodeRenderable, or StyledText instance");
1514
1550
  }
1515
1551
  this.requestRender();
1516
1552
  return this;
@@ -1802,7 +1838,6 @@ class TextRenderable extends Renderable {
1802
1838
  }
1803
1839
  updateTextFromNodes() {
1804
1840
  if (this.rootTextNode.isDirty) {
1805
- const startTime = performance.now();
1806
1841
  const chunks = this.rootTextNode.gatherWithInheritedStyle({
1807
1842
  fg: this._defaultFg,
1808
1843
  bg: this._defaultBg,
@@ -1810,14 +1845,9 @@ class TextRenderable extends Renderable {
1810
1845
  });
1811
1846
  this.textBuffer.setStyledText(new StyledText(chunks));
1812
1847
  this.updateTextInfo();
1813
- const endTime = performance.now();
1814
- console.log(`updateTextFromNodes took ${endTime - startTime}ms`);
1815
1848
  }
1816
1849
  }
1817
1850
  add(obj, index) {
1818
- if (!isTextNodeRenderable(obj)) {
1819
- throw new Error("TextRenderable only accepts TextNodeRenderables. Use add() method.");
1820
- }
1821
1851
  return this.rootTextNode.add(obj, index);
1822
1852
  }
1823
1853
  remove(id) {
@@ -1827,12 +1857,6 @@ class TextRenderable extends Renderable {
1827
1857
  }
1828
1858
  }
1829
1859
  insertBefore(obj, anchor) {
1830
- if (!isTextNodeRenderable(obj)) {
1831
- throw new Error("TextRenderable insertBefore only accepts TextNodeRenderables");
1832
- }
1833
- if (!anchor || !isTextNodeRenderable(anchor)) {
1834
- throw new Error("Anchor must be a TextNodeRenderable");
1835
- }
1836
1860
  this.rootTextNode.insertBefore(obj, anchor);
1837
1861
  return this.rootTextNode.children.indexOf(obj);
1838
1862
  }
@@ -1870,12 +1894,12 @@ class TextRenderable extends Renderable {
1870
1894
  getSelection() {
1871
1895
  return this.textBuffer.getSelection();
1872
1896
  }
1897
+ onUpdate(deltaTime) {
1898
+ this.updateTextFromNodes();
1899
+ }
1873
1900
  render(buffer, deltaTime) {
1874
1901
  if (!this.visible)
1875
1902
  return;
1876
- this.onUpdate(deltaTime);
1877
- this.updateFromLayout();
1878
- this.updateTextFromNodes();
1879
1903
  this.markClean();
1880
1904
  this._ctx.addToHitGrid(this.x, this.y, this.width, this.height, this.num);
1881
1905
  this.renderSelf(buffer);
@@ -3395,7 +3419,7 @@ class ContentRenderable extends BoxRenderable {
3395
3419
  this.viewport = viewport;
3396
3420
  }
3397
3421
  _getChildren() {
3398
- return this.getChildrenInViewport(this.viewport);
3422
+ return getObjectsInViewport(this.viewport, this.getChildrenSortedByPrimaryAxis(), this.primaryAxis);
3399
3423
  }
3400
3424
  }
3401
3425
 
@@ -3845,6 +3869,7 @@ export {
3845
3869
  isValidPercentage,
3846
3870
  isVNode,
3847
3871
  isTextNodeRenderable,
3872
+ isStyledText,
3848
3873
  isSizeType,
3849
3874
  isRenderable,
3850
3875
  isPositionTypeType,
@@ -3964,5 +3989,5 @@ export {
3964
3989
  ASCIIFont
3965
3990
  };
3966
3991
 
3967
- //# debugId=AFF1AC494BCD215264756E2164756E21
3992
+ //# debugId=8F19C97D909538BC64756E2164756E21
3968
3993
  //# sourceMappingURL=index.js.map