@cascadetui/core 0.1.5 → 0.1.7

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
@@ -153,7 +153,7 @@ import {
153
153
  white,
154
154
  wrapWithDelegates,
155
155
  yellow
156
- } from "./index-jx194wn1.js";
156
+ } from "./index-r7vqrd61.js";
157
157
  // src/text-buffer-view.ts
158
158
  class TextBufferView {
159
159
  lib;
@@ -2730,6 +2730,24 @@ class BoxRenderable extends Renderable {
2730
2730
  this.requestRender();
2731
2731
  }
2732
2732
  }
2733
+ clear() {
2734
+ const trace = this.ctx.trace;
2735
+ const traceEnabled = trace?.enabled === true;
2736
+ const traceStart = traceEnabled ? trace.now() : 0;
2737
+ const children = this.getChildren();
2738
+ this.beginRenderBatch();
2739
+ try {
2740
+ for (const child of children) {
2741
+ this.remove(child.id);
2742
+ child.destroyRecursively();
2743
+ }
2744
+ } finally {
2745
+ this.endRenderBatch();
2746
+ }
2747
+ if (traceEnabled) {
2748
+ trace.write(`trace.container.clear id=${this.id} count=${children.length} ms=${(trace.now() - traceStart).toFixed(3)}`);
2749
+ }
2750
+ }
2733
2751
  }
2734
2752
  // src/renderables/TextBufferRenderable.ts
2735
2753
  class TextBufferRenderable extends Renderable {
@@ -2765,7 +2783,16 @@ class TextBufferRenderable extends Renderable {
2765
2783
  super(ctx, options);
2766
2784
  this._defaultFg = parseColor(options.fg ?? this._defaultOptions.fg);
2767
2785
  this._defaultBg = parseColor(options.bg ?? this._defaultOptions.bg);
2768
- this._defaultAttributes = options.attributes ?? this._defaultOptions.attributes;
2786
+ this._defaultAttributes = options.attributes ?? createTextAttributes({
2787
+ bold: options.bold,
2788
+ italic: options.italic,
2789
+ underline: options.underline,
2790
+ dim: options.dim,
2791
+ blink: options.blink,
2792
+ inverse: options.inverse,
2793
+ hidden: options.hidden,
2794
+ strikethrough: options.strikethrough
2795
+ }) ?? this._defaultOptions.attributes;
2769
2796
  this._selectionBg = options.selectionBg ? parseColor(options.selectionBg) : this._defaultOptions.selectionBg;
2770
2797
  this._selectionFg = options.selectionFg ? parseColor(options.selectionFg) : this._defaultOptions.selectionFg;
2771
2798
  this.selectable = options.selectable ?? this._defaultOptions.selectable;
@@ -3044,6 +3071,77 @@ class TextBufferRenderable extends Renderable {
3044
3071
  const localY = y - this.y;
3045
3072
  return localX >= 0 && localX < this.width && localY >= 0 && localY < this.height;
3046
3073
  }
3074
+ selectWord(x, y) {
3075
+ if (!this.selectable)
3076
+ return false;
3077
+ const localX = x - this.x;
3078
+ const localY = y - this.y;
3079
+ const offset = this.resolveOffsetAtLocalPoint(localX, localY);
3080
+ if (offset === null)
3081
+ return false;
3082
+ const range = this.getWordRangeAtOffset(offset);
3083
+ if (!range)
3084
+ return false;
3085
+ const startPoint = this.resolveGlobalPointFromOffset(range.start);
3086
+ const endPoint = this.resolveGlobalPointFromOffset(range.end);
3087
+ if (!startPoint || !endPoint)
3088
+ return false;
3089
+ this._ctx.startSelection(this, startPoint.x, startPoint.y);
3090
+ this._ctx.updateSelection(this, endPoint.x, endPoint.y, { finishDragging: true });
3091
+ return true;
3092
+ }
3093
+ selectLine(x, y) {
3094
+ if (!this.selectable)
3095
+ return false;
3096
+ const localX = x - this.x;
3097
+ const localY = y - this.y;
3098
+ const offset = this.resolveOffsetAtLocalPoint(localX, localY);
3099
+ if (offset === null)
3100
+ return false;
3101
+ const range = this.getLineRangeAtOffset(offset);
3102
+ if (!range)
3103
+ return false;
3104
+ const startPoint = this.resolveGlobalPointFromOffset(range.start);
3105
+ const endPoint = this.resolveGlobalPointFromOffset(range.end);
3106
+ if (!startPoint || !endPoint)
3107
+ return false;
3108
+ this._ctx.startSelection(this, startPoint.x, startPoint.y);
3109
+ this._ctx.updateSelection(this, endPoint.x, endPoint.y, { finishDragging: true });
3110
+ return true;
3111
+ }
3112
+ updateSelectionWordSnap(x, y) {
3113
+ if (!this.selectable)
3114
+ return false;
3115
+ const selection = this._ctx.getSelection();
3116
+ if (!selection)
3117
+ return false;
3118
+ const localX = x - this.x;
3119
+ const localY = y - this.y;
3120
+ const focusOffset = this.resolveOffsetAtLocalPoint(localX, localY);
3121
+ if (focusOffset === null)
3122
+ return false;
3123
+ const range = this.getWordRangeAtOffset(focusOffset);
3124
+ if (!range)
3125
+ return false;
3126
+ const anchorOffset = this.resolveOffsetAtLocalPoint(selection.anchor.x - this.x, selection.anchor.y - this.y);
3127
+ let targetOffset = range.end;
3128
+ if (anchorOffset !== null) {
3129
+ if (anchorOffset <= range.start) {
3130
+ targetOffset = range.end;
3131
+ } else if (anchorOffset >= range.end) {
3132
+ targetOffset = range.start;
3133
+ } else {
3134
+ const toStart = Math.abs(range.start - anchorOffset);
3135
+ const toEnd = Math.abs(range.end - anchorOffset);
3136
+ targetOffset = toStart <= toEnd ? range.start : range.end;
3137
+ }
3138
+ }
3139
+ const targetPoint = this.resolveGlobalPointFromOffset(targetOffset);
3140
+ if (!targetPoint)
3141
+ return false;
3142
+ this._ctx.updateSelection(this, targetPoint.x, targetPoint.y);
3143
+ return true;
3144
+ }
3047
3145
  onSelectionChanged(selection) {
3048
3146
  const localSelection = convertGlobalToLocalSelection(selection, this.x, this.y);
3049
3147
  this.lastLocalSelection = localSelection;
@@ -3070,6 +3168,118 @@ class TextBufferRenderable extends Renderable {
3070
3168
  getSelection() {
3071
3169
  return this.textBufferView.getSelection();
3072
3170
  }
3171
+ resolveOffsetAtLocalPoint(localX, localY) {
3172
+ if (this.width <= 0 || this.height <= 0)
3173
+ return null;
3174
+ const lineInfo = this.textBufferView.lineInfo;
3175
+ if (lineInfo.lineStarts.length === 0)
3176
+ return null;
3177
+ let lineIndex = localY + this._scrollY;
3178
+ if (lineIndex < 0 || lineIndex >= lineInfo.lineStarts.length) {
3179
+ if (localY >= 0 && localY < lineInfo.lineStarts.length) {
3180
+ lineIndex = localY;
3181
+ } else {
3182
+ lineIndex = Math.max(0, Math.min(lineIndex, lineInfo.lineStarts.length - 1));
3183
+ }
3184
+ }
3185
+ const lineStart = lineInfo.lineStarts[lineIndex];
3186
+ const lineWidth = lineInfo.lineWidths[lineIndex] ?? 0;
3187
+ const column = this._wrapMode === "none" ? localX + this._scrollX : localX;
3188
+ const clampedColumn = Math.max(0, Math.min(Math.floor(column), lineWidth));
3189
+ return lineStart + clampedColumn;
3190
+ }
3191
+ getLineRangeAtOffset(offset) {
3192
+ const lineInfo = this.textBufferView.lineInfo;
3193
+ if (lineInfo.lineStarts.length === 0)
3194
+ return null;
3195
+ let lineIndex = 0;
3196
+ for (let i = lineInfo.lineStarts.length - 1;i >= 0; i -= 1) {
3197
+ if (offset >= lineInfo.lineStarts[i]) {
3198
+ lineIndex = i;
3199
+ break;
3200
+ }
3201
+ }
3202
+ const start = lineInfo.lineStarts[lineIndex];
3203
+ const width = lineInfo.lineWidths[lineIndex] ?? 0;
3204
+ const end = start + width;
3205
+ return { start, end };
3206
+ }
3207
+ getWordRangeAtOffset(offset) {
3208
+ const lineRange = this.getLineRangeAtOffset(offset);
3209
+ if (!lineRange)
3210
+ return null;
3211
+ if (lineRange.end <= lineRange.start) {
3212
+ return lineRange;
3213
+ }
3214
+ const lineText = this.readTextRange(lineRange.start, lineRange.end);
3215
+ if (lineText.length === 0) {
3216
+ return lineRange;
3217
+ }
3218
+ let localIndex = Math.max(0, Math.min(offset - lineRange.start, lineText.length - 1));
3219
+ if (localIndex >= lineText.length) {
3220
+ localIndex = lineText.length - 1;
3221
+ }
3222
+ const classify = (char) => {
3223
+ if (/\s/.test(char))
3224
+ return "space";
3225
+ if (/[A-Za-z0-9_]/.test(char))
3226
+ return "word";
3227
+ return "symbol";
3228
+ };
3229
+ const kind = classify(lineText[localIndex] ?? "");
3230
+ let start = localIndex;
3231
+ let end = localIndex + 1;
3232
+ while (start > 0 && classify(lineText[start - 1] ?? "") === kind) {
3233
+ start -= 1;
3234
+ }
3235
+ while (end < lineText.length && classify(lineText[end] ?? "") === kind) {
3236
+ end += 1;
3237
+ }
3238
+ return {
3239
+ start: lineRange.start + start,
3240
+ end: lineRange.start + end
3241
+ };
3242
+ }
3243
+ readTextRange(start, end) {
3244
+ const previousSelection = this.textBufferView.getSelection();
3245
+ this.textBufferView.setSelection(start, end, this._selectionBg, this._selectionFg);
3246
+ const text = this.textBufferView.getSelectedText();
3247
+ if (previousSelection) {
3248
+ this.textBufferView.setSelection(previousSelection.start, previousSelection.end, this._selectionBg, this._selectionFg);
3249
+ } else {
3250
+ this.textBufferView.resetSelection();
3251
+ }
3252
+ return text;
3253
+ }
3254
+ resolveGlobalPointFromOffset(offset) {
3255
+ const lineInfo = this.textBufferView.lineInfo;
3256
+ if (lineInfo.lineStarts.length === 0)
3257
+ return null;
3258
+ let lineIndex = lineInfo.lineStarts.length - 1;
3259
+ for (let i = 0;i < lineInfo.lineStarts.length; i += 1) {
3260
+ const start = lineInfo.lineStarts[i];
3261
+ const end = start + (lineInfo.lineWidths[i] ?? 0);
3262
+ if (offset >= start && offset <= end) {
3263
+ lineIndex = i;
3264
+ break;
3265
+ }
3266
+ }
3267
+ const lineStart = lineInfo.lineStarts[lineIndex];
3268
+ const lineWidth = lineInfo.lineWidths[lineIndex] ?? 0;
3269
+ const column = Math.max(0, Math.min(offset - lineStart, lineWidth));
3270
+ const viewportAdjustedY = lineIndex - this._scrollY;
3271
+ const localY = viewportAdjustedY >= 0 && viewportAdjustedY < this.height ? viewportAdjustedY : lineIndex;
3272
+ if (localY < 0 || localY >= this.height)
3273
+ return null;
3274
+ const viewportAdjustedX = this._wrapMode === "none" ? column - this._scrollX : column;
3275
+ const localX = viewportAdjustedX >= 0 && viewportAdjustedX <= this.width ? viewportAdjustedX : column;
3276
+ if (localX < 0 || localX > this.width)
3277
+ return null;
3278
+ return {
3279
+ x: this.x + localX,
3280
+ y: this.y + localY
3281
+ };
3282
+ }
3073
3283
  render(buffer, deltaTime) {
3074
3284
  if (!this.visible)
3075
3285
  return;
@@ -3361,7 +3571,16 @@ class TextNodeRenderable extends BaseRenderable {
3361
3571
  super(options);
3362
3572
  this._fg = options.fg ? parseColor(options.fg) : undefined;
3363
3573
  this._bg = options.bg ? parseColor(options.bg) : undefined;
3364
- this._attributes = options.attributes ?? 0;
3574
+ this._attributes = options.attributes ?? createTextAttributes({
3575
+ bold: options.bold,
3576
+ italic: options.italic,
3577
+ underline: options.underline,
3578
+ dim: options.dim,
3579
+ blink: options.blink,
3580
+ inverse: options.inverse,
3581
+ hidden: options.hidden,
3582
+ strikethrough: options.strikethrough
3583
+ }) ?? 0;
3365
3584
  this._link = options.link;
3366
3585
  }
3367
3586
  get children() {
@@ -12206,5 +12425,5 @@ export {
12206
12425
  ASCIIFont
12207
12426
  };
12208
12427
 
12209
- //# debugId=1568E6ED0E2204DD64756E2164756E21
12428
+ //# debugId=FC3249AEF487030064756E2164756E21
12210
12429
  //# sourceMappingURL=index.js.map