@opentui/core 0.1.30 → 0.1.31

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
@@ -10,6 +10,7 @@ import {
10
10
  DataPathsManager,
11
11
  DebugOverlayCorner,
12
12
  Edge,
13
+ ExtmarksController,
13
14
  Gutter,
14
15
  InternalKeyHandler,
15
16
  KeyEvent,
@@ -62,6 +63,7 @@ import {
62
63
  convertGlobalToLocalSelection,
63
64
  coordinateToCharacterIndex,
64
65
  createCliRenderer,
66
+ createExtmarksController,
65
67
  createTextAttributes,
66
68
  cyan,
67
69
  delegate,
@@ -131,7 +133,7 @@ import {
131
133
  white,
132
134
  wrapWithDelegates,
133
135
  yellow
134
- } from "./index-0qmm1k4p.js";
136
+ } from "./index-91qheh74.js";
135
137
  // src/text-buffer-view.ts
136
138
  class TextBufferView {
137
139
  lib;
@@ -381,8 +383,8 @@ class EditBuffer extends EventEmitter {
381
383
  this.guard();
382
384
  const boundary = this.lib.editBufferGetNextWordBoundary(this.bufferPtr);
383
385
  return {
384
- line: boundary.row,
385
- visualColumn: boundary.col,
386
+ row: boundary.row,
387
+ col: boundary.col,
386
388
  offset: boundary.offset
387
389
  };
388
390
  }
@@ -390,8 +392,8 @@ class EditBuffer extends EventEmitter {
390
392
  this.guard();
391
393
  const boundary = this.lib.editBufferGetPrevWordBoundary(this.bufferPtr);
392
394
  return {
393
- line: boundary.row,
394
- visualColumn: boundary.col,
395
+ row: boundary.row,
396
+ col: boundary.col,
395
397
  offset: boundary.offset
396
398
  };
397
399
  }
@@ -399,11 +401,26 @@ class EditBuffer extends EventEmitter {
399
401
  this.guard();
400
402
  const boundary = this.lib.editBufferGetEOL(this.bufferPtr);
401
403
  return {
402
- line: boundary.row,
403
- visualColumn: boundary.col,
404
+ row: boundary.row,
405
+ col: boundary.col,
404
406
  offset: boundary.offset
405
407
  };
406
408
  }
409
+ offsetToPosition(offset) {
410
+ this.guard();
411
+ const result = this.lib.editBufferOffsetToPosition(this.bufferPtr, offset);
412
+ if (!result)
413
+ return null;
414
+ return { row: result.row, col: result.col };
415
+ }
416
+ positionToOffset(row, col) {
417
+ this.guard();
418
+ return this.lib.editBufferPositionToOffset(this.bufferPtr, row, col);
419
+ }
420
+ getLineStartOffset(row) {
421
+ this.guard();
422
+ return this.lib.editBufferGetLineStartOffset(this.bufferPtr, row);
423
+ }
407
424
  debugLogRope() {
408
425
  this.guard();
409
426
  this.lib.editBufferDebugLogRope(this.bufferPtr);
@@ -452,14 +469,6 @@ class EditBuffer extends EventEmitter {
452
469
  this.guard();
453
470
  this.lib.textBufferResetDefaults(this.textBufferPtr);
454
471
  }
455
- setPlaceholder(text) {
456
- this.guard();
457
- this.lib.editBufferSetPlaceholder(this.bufferPtr, text);
458
- }
459
- setPlaceholderColor(color) {
460
- this.guard();
461
- this.lib.editBufferSetPlaceholderColor(this.bufferPtr, color);
462
- }
463
472
  setSyntaxStyle(style) {
464
473
  this.guard();
465
474
  this._syntaxStyle = style ?? undefined;
@@ -493,6 +502,10 @@ class EditBuffer extends EventEmitter {
493
502
  this.guard();
494
503
  return this.lib.textBufferGetLineHighlights(this.textBufferPtr, lineIdx);
495
504
  }
505
+ clear() {
506
+ this.guard();
507
+ this.lib.editBufferClear(this.bufferPtr);
508
+ }
496
509
  destroy() {
497
510
  if (this._destroyed)
498
511
  return;
@@ -507,6 +520,7 @@ class EditorView {
507
520
  viewPtr;
508
521
  editBuffer;
509
522
  _destroyed = false;
523
+ _extmarksController;
510
524
  constructor(lib, ptr, editBuffer) {
511
525
  this.lib = lib;
512
526
  this.viewPtr = ptr;
@@ -627,17 +641,29 @@ class EditorView {
627
641
  }
628
642
  getLineInfo() {
629
643
  this.guard();
630
- const textBufferViewPtr = this.lib.editorViewGetTextBufferView(this.viewPtr);
631
- return this.lib.textBufferViewGetLineInfo(textBufferViewPtr);
644
+ return this.lib.editorViewGetLineInfo(this.viewPtr);
632
645
  }
633
646
  getLogicalLineInfo() {
634
647
  this.guard();
635
- const textBufferViewPtr = this.lib.editorViewGetTextBufferView(this.viewPtr);
636
- return this.lib.textBufferViewGetLogicalLineInfo(textBufferViewPtr);
648
+ return this.lib.editorViewGetLogicalLineInfo(this.viewPtr);
649
+ }
650
+ get extmarks() {
651
+ if (!this._extmarksController) {
652
+ this._extmarksController = createExtmarksController(this.editBuffer, this);
653
+ }
654
+ return this._extmarksController;
655
+ }
656
+ setPlaceholderStyledText(chunks) {
657
+ this.guard();
658
+ this.lib.editorViewSetPlaceholderStyledText(this.viewPtr, chunks);
637
659
  }
638
660
  destroy() {
639
661
  if (this._destroyed)
640
662
  return;
663
+ if (this._extmarksController) {
664
+ this._extmarksController.destroy();
665
+ this._extmarksController = undefined;
666
+ }
641
667
  this._destroyed = true;
642
668
  this.lib.destroyEditorView(this.viewPtr);
643
669
  }
@@ -2472,8 +2498,9 @@ class TextBufferRenderable extends Renderable {
2472
2498
  }
2473
2499
  setupMeasureFunc() {
2474
2500
  const measureFunc = (width, widthMode, height, heightMode) => {
2475
- if (this._wrapMode !== "none" && this.width !== width) {
2476
- this.updateWrapWidth(width);
2501
+ const effectiveWidth = isNaN(width) ? 1 : width;
2502
+ if (this._wrapMode !== "none" && this.width !== effectiveWidth) {
2503
+ this.updateWrapWidth(effectiveWidth);
2477
2504
  } else {
2478
2505
  this.updateLineInfo();
2479
2506
  }
@@ -5047,8 +5074,8 @@ class EditBufferRenderable extends Renderable {
5047
5074
  if (this._cursorChangeListener) {
5048
5075
  const cursor = this.editBuffer.getCursorPosition();
5049
5076
  this._cursorChangeListener({
5050
- line: cursor.line,
5051
- visualColumn: cursor.visualColumn
5077
+ line: cursor.row,
5078
+ visualColumn: cursor.col
5052
5079
  });
5053
5080
  }
5054
5081
  });
@@ -5063,7 +5090,7 @@ class EditBufferRenderable extends Renderable {
5063
5090
  get plainText() {
5064
5091
  return this.editBuffer.getText();
5065
5092
  }
5066
- get cursor() {
5093
+ get logicalCursor() {
5067
5094
  return this.editBuffer.getCursorPosition();
5068
5095
  }
5069
5096
  get visualCursor() {
@@ -5213,10 +5240,12 @@ class EditBufferRenderable extends Renderable {
5213
5240
  }
5214
5241
  setupMeasureFunc() {
5215
5242
  const measureFunc = (width, widthMode, height, heightMode) => {
5216
- if (this._wrapMode !== "none" && this.width !== width) {
5217
- this.editorView.setViewportSize(width, height);
5243
+ const effectiveHeight = isNaN(height) ? 1 : height;
5244
+ const effectiveWidth = isNaN(width) ? 1 : width;
5245
+ if (this._wrapMode !== "none" && this.width !== effectiveWidth) {
5246
+ this.editorView.setViewportSize(effectiveWidth, effectiveHeight);
5218
5247
  } else {
5219
- this.editorView.setViewportSize(width, height);
5248
+ this.editorView.setViewportSize(effectiveWidth, effectiveHeight);
5220
5249
  }
5221
5250
  const lineInfo = this.editorView.getLogicalLineInfo();
5222
5251
  const measuredWidth = lineInfo.maxLineWidth;
@@ -5317,6 +5346,27 @@ class EditBufferRenderable extends Renderable {
5317
5346
  getLineHighlights(lineIdx) {
5318
5347
  return this.editBuffer.getLineHighlights(lineIdx);
5319
5348
  }
5349
+ setText(text, opts) {
5350
+ this.editBuffer.setText(text, opts);
5351
+ this.yogaNode.markDirty();
5352
+ this.requestRender();
5353
+ }
5354
+ clear() {
5355
+ this.editBuffer.clear();
5356
+ this.editBuffer.clearAllHighlights();
5357
+ this.yogaNode.markDirty();
5358
+ this.requestRender();
5359
+ }
5360
+ deleteRange(startLine, startCol, endLine, endCol) {
5361
+ this.editBuffer.deleteRange(startLine, startCol, endLine, endCol);
5362
+ this.yogaNode.markDirty();
5363
+ this.requestRender();
5364
+ }
5365
+ insertText(text) {
5366
+ this.editBuffer.insertText(text);
5367
+ this.yogaNode.markDirty();
5368
+ this.requestRender();
5369
+ }
5320
5370
  }
5321
5371
 
5322
5372
  // src/lib/keymapping.ts
@@ -5366,6 +5416,8 @@ var defaultTextareaKeybindings = [
5366
5416
  { name: "delete", action: "delete" },
5367
5417
  { name: "return", action: "newline" },
5368
5418
  { name: "enter", action: "newline" },
5419
+ { name: "return", meta: true, action: "submit" },
5420
+ { name: "enter", meta: true, action: "submit" },
5369
5421
  { name: "z", ctrl: true, action: "undo" },
5370
5422
  { name: "Z", ctrl: true, shift: true, action: "redo" },
5371
5423
  { name: "y", ctrl: true, action: "redo" },
@@ -5388,17 +5440,16 @@ class TextareaRenderable extends EditBufferRenderable {
5388
5440
  _unfocusedTextColor;
5389
5441
  _focusedBackgroundColor;
5390
5442
  _focusedTextColor;
5391
- _placeholderColor;
5392
5443
  _keyBindingsMap;
5393
5444
  _actionHandlers;
5445
+ _initialValueSet = false;
5446
+ _submitListener = undefined;
5394
5447
  static defaults = {
5395
- value: "",
5396
5448
  backgroundColor: "transparent",
5397
5449
  textColor: "#FFFFFF",
5398
5450
  focusedBackgroundColor: "transparent",
5399
5451
  focusedTextColor: "#FFFFFF",
5400
- placeholder: null,
5401
- placeholderColor: "#666666"
5452
+ placeholder: null
5402
5453
  };
5403
5454
  constructor(ctx, options) {
5404
5455
  const defaults = TextareaRenderable.defaults;
@@ -5413,14 +5464,29 @@ class TextareaRenderable extends EditBufferRenderable {
5413
5464
  this._focusedBackgroundColor = parseColor(options.focusedBackgroundColor || options.backgroundColor || defaults.focusedBackgroundColor);
5414
5465
  this._focusedTextColor = parseColor(options.focusedTextColor || options.textColor || defaults.focusedTextColor);
5415
5466
  this._placeholder = options.placeholder ?? defaults.placeholder;
5416
- this._placeholderColor = parseColor(options.placeholderColor || defaults.placeholderColor);
5417
5467
  const mergedBindings = mergeKeyBindings(defaultTextareaKeybindings, options.keyBindings || []);
5418
5468
  this._keyBindingsMap = buildKeyBindingsMap(mergedBindings);
5419
5469
  this._actionHandlers = this.buildActionHandlers();
5420
- this.updateValue(options.value ?? defaults.value);
5470
+ this._submitListener = options.onSubmit;
5471
+ if (options.initialValue) {
5472
+ this.setText(options.initialValue);
5473
+ this._initialValueSet = true;
5474
+ }
5421
5475
  this.updateColors();
5422
- this.editBuffer.setPlaceholder(this._placeholder);
5423
- this.editBuffer.setPlaceholderColor(this._placeholderColor);
5476
+ this.applyPlaceholder(this._placeholder);
5477
+ }
5478
+ applyPlaceholder(placeholder) {
5479
+ if (placeholder === null) {
5480
+ this.editorView.setPlaceholderStyledText([]);
5481
+ return;
5482
+ }
5483
+ if (typeof placeholder === "string") {
5484
+ const defaultGray = fg("#666666");
5485
+ const chunks = [defaultGray(placeholder)];
5486
+ this.editorView.setPlaceholderStyledText(chunks);
5487
+ } else {
5488
+ this.editorView.setPlaceholderStyledText(placeholder.chunks);
5489
+ }
5424
5490
  }
5425
5491
  buildActionHandlers() {
5426
5492
  return new Map([
@@ -5450,11 +5516,12 @@ class TextareaRenderable extends EditBufferRenderable {
5450
5516
  ["select-word-forward", () => this.moveWordForward({ select: true })],
5451
5517
  ["select-word-backward", () => this.moveWordBackward({ select: true })],
5452
5518
  ["delete-word-forward", () => this.deleteWordForward()],
5453
- ["delete-word-backward", () => this.deleteWordBackward()]
5519
+ ["delete-word-backward", () => this.deleteWordBackward()],
5520
+ ["submit", () => this.submit()]
5454
5521
  ]);
5455
5522
  }
5456
- handlePaste(text) {
5457
- this.insertText(text);
5523
+ handlePaste(event) {
5524
+ this.insertText(event.text);
5458
5525
  }
5459
5526
  handleKeyPress(key) {
5460
5527
  const keyName = typeof key === "string" ? key : key.name;
@@ -5489,17 +5556,6 @@ class TextareaRenderable extends EditBufferRenderable {
5489
5556
  }
5490
5557
  return false;
5491
5558
  }
5492
- get value() {
5493
- return this.editBuffer.getText();
5494
- }
5495
- set value(value) {
5496
- this.updateValue(value);
5497
- }
5498
- updateValue(value) {
5499
- this.editBuffer.setText(value, { history: false });
5500
- this.yogaNode.markDirty();
5501
- this.requestRender();
5502
- }
5503
5559
  updateColors() {
5504
5560
  const effectiveBg = this._focused ? this._focusedBackgroundColor : this._unfocusedBackgroundColor;
5505
5561
  const effectiveFg = this._focused ? this._focusedTextColor : this._unfocusedTextColor;
@@ -5606,7 +5662,7 @@ class TextareaRenderable extends EditBufferRenderable {
5606
5662
  const select = options?.select ?? false;
5607
5663
  this.handleShiftSelection(select, true);
5608
5664
  const eol = this.editBuffer.getEOL();
5609
- this.editBuffer.setCursor(eol.line, eol.visualColumn);
5665
+ this.editBuffer.setCursor(eol.row, eol.col);
5610
5666
  this.handleShiftSelection(select, false);
5611
5667
  this.requestRender();
5612
5668
  return true;
@@ -5624,8 +5680,8 @@ class TextareaRenderable extends EditBufferRenderable {
5624
5680
  deleteToLineEnd() {
5625
5681
  const cursor = this.editorView.getCursor();
5626
5682
  const eol = this.editBuffer.getEOL();
5627
- if (eol.visualColumn > cursor.col) {
5628
- this.editBuffer.deleteRange(cursor.row, cursor.col, eol.line, eol.visualColumn);
5683
+ if (eol.col > cursor.col) {
5684
+ this.editBuffer.deleteRange(cursor.row, cursor.col, eol.row, eol.col);
5629
5685
  }
5630
5686
  this.requestRender();
5631
5687
  return true;
@@ -5668,7 +5724,7 @@ class TextareaRenderable extends EditBufferRenderable {
5668
5724
  const currentCursor = this.editBuffer.getCursorPosition();
5669
5725
  const nextWord = this.editBuffer.getNextWordBoundary();
5670
5726
  if (nextWord.offset > currentCursor.offset) {
5671
- this.editBuffer.deleteRange(currentCursor.line, currentCursor.visualColumn, nextWord.line, nextWord.visualColumn);
5727
+ this.editBuffer.deleteRange(currentCursor.row, currentCursor.col, nextWord.row, nextWord.col);
5672
5728
  }
5673
5729
  this._ctx.clearSelection();
5674
5730
  this.requestRender();
@@ -5682,7 +5738,7 @@ class TextareaRenderable extends EditBufferRenderable {
5682
5738
  const currentCursor = this.editBuffer.getCursorPosition();
5683
5739
  const prevWord = this.editBuffer.getPrevWordBoundary();
5684
5740
  if (prevWord.offset < currentCursor.offset) {
5685
- this.editBuffer.deleteRange(prevWord.line, prevWord.visualColumn, currentCursor.line, currentCursor.visualColumn);
5741
+ this.editBuffer.deleteRange(prevWord.row, prevWord.col, currentCursor.row, currentCursor.col);
5686
5742
  }
5687
5743
  this._ctx.clearSelection();
5688
5744
  this.requestRender();
@@ -5721,7 +5777,7 @@ class TextareaRenderable extends EditBufferRenderable {
5721
5777
  set placeholder(value) {
5722
5778
  if (this._placeholder !== value) {
5723
5779
  this._placeholder = value;
5724
- this.editBuffer.setPlaceholder(value);
5780
+ this.applyPlaceholder(value);
5725
5781
  this.requestRender();
5726
5782
  }
5727
5783
  }
@@ -5759,14 +5815,31 @@ class TextareaRenderable extends EditBufferRenderable {
5759
5815
  this.updateColors();
5760
5816
  }
5761
5817
  }
5762
- set placeholderColor(value) {
5763
- const newColor = parseColor(value ?? TextareaRenderable.defaults.placeholderColor);
5764
- if (this._placeholderColor !== newColor) {
5765
- this._placeholderColor = newColor;
5766
- this.editBuffer.setPlaceholderColor(newColor);
5767
- this.requestRender();
5818
+ set initialValue(value) {
5819
+ if (!this._initialValueSet) {
5820
+ this.setText(value);
5821
+ this._initialValueSet = true;
5768
5822
  }
5769
5823
  }
5824
+ submit() {
5825
+ if (this._submitListener) {
5826
+ this._submitListener({});
5827
+ }
5828
+ return true;
5829
+ }
5830
+ set onSubmit(handler) {
5831
+ this._submitListener = handler;
5832
+ }
5833
+ get onSubmit() {
5834
+ return this._submitListener;
5835
+ }
5836
+ set keyBindings(bindings) {
5837
+ const mergedBindings = mergeKeyBindings(defaultTextareaKeybindings, bindings);
5838
+ this._keyBindingsMap = buildKeyBindingsMap(mergedBindings);
5839
+ }
5840
+ get extmarks() {
5841
+ return this.editorView.extmarks;
5842
+ }
5770
5843
  }
5771
5844
  export {
5772
5845
  yellow,
@@ -5839,6 +5912,7 @@ export {
5839
5912
  cyan,
5840
5913
  createTimeline,
5841
5914
  createTextAttributes,
5915
+ createExtmarksController,
5842
5916
  createCliRenderer,
5843
5917
  coordinateToCharacterIndex,
5844
5918
  convertThemeToStyles,
@@ -5925,6 +5999,7 @@ export {
5925
5999
  Generic,
5926
6000
  FrameBufferRenderable,
5927
6001
  FrameBuffer,
6002
+ ExtmarksController,
5928
6003
  EditorView,
5929
6004
  EditBuffer,
5930
6005
  DistortionEffect,
@@ -5948,5 +6023,5 @@ export {
5948
6023
  ASCIIFont
5949
6024
  };
5950
6025
 
5951
- //# debugId=C45C47BFE87C68C464756E2164756E21
6026
+ //# debugId=F972C01B99F2CD0364756E2164756E21
5952
6027
  //# sourceMappingURL=index.js.map