@cascadetui/core 0.1.4 → 0.1.6

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-rj3f00a6.js";
157
157
  // src/text-buffer-view.ts
158
158
  class TextBufferView {
159
159
  lib;
@@ -2765,7 +2765,16 @@ class TextBufferRenderable extends Renderable {
2765
2765
  super(ctx, options);
2766
2766
  this._defaultFg = parseColor(options.fg ?? this._defaultOptions.fg);
2767
2767
  this._defaultBg = parseColor(options.bg ?? this._defaultOptions.bg);
2768
- this._defaultAttributes = options.attributes ?? this._defaultOptions.attributes;
2768
+ this._defaultAttributes = options.attributes ?? createTextAttributes({
2769
+ bold: options.bold,
2770
+ italic: options.italic,
2771
+ underline: options.underline,
2772
+ dim: options.dim,
2773
+ blink: options.blink,
2774
+ inverse: options.inverse,
2775
+ hidden: options.hidden,
2776
+ strikethrough: options.strikethrough
2777
+ }) ?? this._defaultOptions.attributes;
2769
2778
  this._selectionBg = options.selectionBg ? parseColor(options.selectionBg) : this._defaultOptions.selectionBg;
2770
2779
  this._selectionFg = options.selectionFg ? parseColor(options.selectionFg) : this._defaultOptions.selectionFg;
2771
2780
  this.selectable = options.selectable ?? this._defaultOptions.selectable;
@@ -3044,6 +3053,77 @@ class TextBufferRenderable extends Renderable {
3044
3053
  const localY = y - this.y;
3045
3054
  return localX >= 0 && localX < this.width && localY >= 0 && localY < this.height;
3046
3055
  }
3056
+ selectWord(x, y) {
3057
+ if (!this.selectable)
3058
+ return false;
3059
+ const localX = x - this.x;
3060
+ const localY = y - this.y;
3061
+ const offset = this.resolveOffsetAtLocalPoint(localX, localY);
3062
+ if (offset === null)
3063
+ return false;
3064
+ const range = this.getWordRangeAtOffset(offset);
3065
+ if (!range)
3066
+ return false;
3067
+ const startPoint = this.resolveGlobalPointFromOffset(range.start);
3068
+ const endPoint = this.resolveGlobalPointFromOffset(range.end);
3069
+ if (!startPoint || !endPoint)
3070
+ return false;
3071
+ this._ctx.startSelection(this, startPoint.x, startPoint.y);
3072
+ this._ctx.updateSelection(this, endPoint.x, endPoint.y, { finishDragging: true });
3073
+ return true;
3074
+ }
3075
+ selectLine(x, y) {
3076
+ if (!this.selectable)
3077
+ return false;
3078
+ const localX = x - this.x;
3079
+ const localY = y - this.y;
3080
+ const offset = this.resolveOffsetAtLocalPoint(localX, localY);
3081
+ if (offset === null)
3082
+ return false;
3083
+ const range = this.getLineRangeAtOffset(offset);
3084
+ if (!range)
3085
+ return false;
3086
+ const startPoint = this.resolveGlobalPointFromOffset(range.start);
3087
+ const endPoint = this.resolveGlobalPointFromOffset(range.end);
3088
+ if (!startPoint || !endPoint)
3089
+ return false;
3090
+ this._ctx.startSelection(this, startPoint.x, startPoint.y);
3091
+ this._ctx.updateSelection(this, endPoint.x, endPoint.y, { finishDragging: true });
3092
+ return true;
3093
+ }
3094
+ updateSelectionWordSnap(x, y) {
3095
+ if (!this.selectable)
3096
+ return false;
3097
+ const selection = this._ctx.getSelection();
3098
+ if (!selection)
3099
+ return false;
3100
+ const localX = x - this.x;
3101
+ const localY = y - this.y;
3102
+ const focusOffset = this.resolveOffsetAtLocalPoint(localX, localY);
3103
+ if (focusOffset === null)
3104
+ return false;
3105
+ const range = this.getWordRangeAtOffset(focusOffset);
3106
+ if (!range)
3107
+ return false;
3108
+ const anchorOffset = this.resolveOffsetAtLocalPoint(selection.anchor.x - this.x, selection.anchor.y - this.y);
3109
+ let targetOffset = range.end;
3110
+ if (anchorOffset !== null) {
3111
+ if (anchorOffset <= range.start) {
3112
+ targetOffset = range.end;
3113
+ } else if (anchorOffset >= range.end) {
3114
+ targetOffset = range.start;
3115
+ } else {
3116
+ const toStart = Math.abs(range.start - anchorOffset);
3117
+ const toEnd = Math.abs(range.end - anchorOffset);
3118
+ targetOffset = toStart <= toEnd ? range.start : range.end;
3119
+ }
3120
+ }
3121
+ const targetPoint = this.resolveGlobalPointFromOffset(targetOffset);
3122
+ if (!targetPoint)
3123
+ return false;
3124
+ this._ctx.updateSelection(this, targetPoint.x, targetPoint.y);
3125
+ return true;
3126
+ }
3047
3127
  onSelectionChanged(selection) {
3048
3128
  const localSelection = convertGlobalToLocalSelection(selection, this.x, this.y);
3049
3129
  this.lastLocalSelection = localSelection;
@@ -3070,6 +3150,118 @@ class TextBufferRenderable extends Renderable {
3070
3150
  getSelection() {
3071
3151
  return this.textBufferView.getSelection();
3072
3152
  }
3153
+ resolveOffsetAtLocalPoint(localX, localY) {
3154
+ if (this.width <= 0 || this.height <= 0)
3155
+ return null;
3156
+ const lineInfo = this.textBufferView.lineInfo;
3157
+ if (lineInfo.lineStarts.length === 0)
3158
+ return null;
3159
+ let lineIndex = localY + this._scrollY;
3160
+ if (lineIndex < 0 || lineIndex >= lineInfo.lineStarts.length) {
3161
+ if (localY >= 0 && localY < lineInfo.lineStarts.length) {
3162
+ lineIndex = localY;
3163
+ } else {
3164
+ lineIndex = Math.max(0, Math.min(lineIndex, lineInfo.lineStarts.length - 1));
3165
+ }
3166
+ }
3167
+ const lineStart = lineInfo.lineStarts[lineIndex];
3168
+ const lineWidth = lineInfo.lineWidths[lineIndex] ?? 0;
3169
+ const column = this._wrapMode === "none" ? localX + this._scrollX : localX;
3170
+ const clampedColumn = Math.max(0, Math.min(Math.floor(column), lineWidth));
3171
+ return lineStart + clampedColumn;
3172
+ }
3173
+ getLineRangeAtOffset(offset) {
3174
+ const lineInfo = this.textBufferView.lineInfo;
3175
+ if (lineInfo.lineStarts.length === 0)
3176
+ return null;
3177
+ let lineIndex = 0;
3178
+ for (let i = lineInfo.lineStarts.length - 1;i >= 0; i -= 1) {
3179
+ if (offset >= lineInfo.lineStarts[i]) {
3180
+ lineIndex = i;
3181
+ break;
3182
+ }
3183
+ }
3184
+ const start = lineInfo.lineStarts[lineIndex];
3185
+ const width = lineInfo.lineWidths[lineIndex] ?? 0;
3186
+ const end = start + width;
3187
+ return { start, end };
3188
+ }
3189
+ getWordRangeAtOffset(offset) {
3190
+ const lineRange = this.getLineRangeAtOffset(offset);
3191
+ if (!lineRange)
3192
+ return null;
3193
+ if (lineRange.end <= lineRange.start) {
3194
+ return lineRange;
3195
+ }
3196
+ const lineText = this.readTextRange(lineRange.start, lineRange.end);
3197
+ if (lineText.length === 0) {
3198
+ return lineRange;
3199
+ }
3200
+ let localIndex = Math.max(0, Math.min(offset - lineRange.start, lineText.length - 1));
3201
+ if (localIndex >= lineText.length) {
3202
+ localIndex = lineText.length - 1;
3203
+ }
3204
+ const classify = (char) => {
3205
+ if (/\s/.test(char))
3206
+ return "space";
3207
+ if (/[A-Za-z0-9_]/.test(char))
3208
+ return "word";
3209
+ return "symbol";
3210
+ };
3211
+ const kind = classify(lineText[localIndex] ?? "");
3212
+ let start = localIndex;
3213
+ let end = localIndex + 1;
3214
+ while (start > 0 && classify(lineText[start - 1] ?? "") === kind) {
3215
+ start -= 1;
3216
+ }
3217
+ while (end < lineText.length && classify(lineText[end] ?? "") === kind) {
3218
+ end += 1;
3219
+ }
3220
+ return {
3221
+ start: lineRange.start + start,
3222
+ end: lineRange.start + end
3223
+ };
3224
+ }
3225
+ readTextRange(start, end) {
3226
+ const previousSelection = this.textBufferView.getSelection();
3227
+ this.textBufferView.setSelection(start, end, this._selectionBg, this._selectionFg);
3228
+ const text = this.textBufferView.getSelectedText();
3229
+ if (previousSelection) {
3230
+ this.textBufferView.setSelection(previousSelection.start, previousSelection.end, this._selectionBg, this._selectionFg);
3231
+ } else {
3232
+ this.textBufferView.resetSelection();
3233
+ }
3234
+ return text;
3235
+ }
3236
+ resolveGlobalPointFromOffset(offset) {
3237
+ const lineInfo = this.textBufferView.lineInfo;
3238
+ if (lineInfo.lineStarts.length === 0)
3239
+ return null;
3240
+ let lineIndex = lineInfo.lineStarts.length - 1;
3241
+ for (let i = 0;i < lineInfo.lineStarts.length; i += 1) {
3242
+ const start = lineInfo.lineStarts[i];
3243
+ const end = start + (lineInfo.lineWidths[i] ?? 0);
3244
+ if (offset >= start && offset <= end) {
3245
+ lineIndex = i;
3246
+ break;
3247
+ }
3248
+ }
3249
+ const lineStart = lineInfo.lineStarts[lineIndex];
3250
+ const lineWidth = lineInfo.lineWidths[lineIndex] ?? 0;
3251
+ const column = Math.max(0, Math.min(offset - lineStart, lineWidth));
3252
+ const viewportAdjustedY = lineIndex - this._scrollY;
3253
+ const localY = viewportAdjustedY >= 0 && viewportAdjustedY < this.height ? viewportAdjustedY : lineIndex;
3254
+ if (localY < 0 || localY >= this.height)
3255
+ return null;
3256
+ const viewportAdjustedX = this._wrapMode === "none" ? column - this._scrollX : column;
3257
+ const localX = viewportAdjustedX >= 0 && viewportAdjustedX <= this.width ? viewportAdjustedX : column;
3258
+ if (localX < 0 || localX > this.width)
3259
+ return null;
3260
+ return {
3261
+ x: this.x + localX,
3262
+ y: this.y + localY
3263
+ };
3264
+ }
3073
3265
  render(buffer, deltaTime) {
3074
3266
  if (!this.visible)
3075
3267
  return;
@@ -3361,7 +3553,16 @@ class TextNodeRenderable extends BaseRenderable {
3361
3553
  super(options);
3362
3554
  this._fg = options.fg ? parseColor(options.fg) : undefined;
3363
3555
  this._bg = options.bg ? parseColor(options.bg) : undefined;
3364
- this._attributes = options.attributes ?? 0;
3556
+ this._attributes = options.attributes ?? createTextAttributes({
3557
+ bold: options.bold,
3558
+ italic: options.italic,
3559
+ underline: options.underline,
3560
+ dim: options.dim,
3561
+ blink: options.blink,
3562
+ inverse: options.inverse,
3563
+ hidden: options.hidden,
3564
+ strikethrough: options.strikethrough
3565
+ }) ?? 0;
3365
3566
  this._link = options.link;
3366
3567
  }
3367
3568
  get children() {
@@ -12206,5 +12407,5 @@ export {
12206
12407
  ASCIIFont
12207
12408
  };
12208
12409
 
12209
- //# debugId=1568E6ED0E2204DD64756E2164756E21
12410
+ //# debugId=0605AEDD3A1567CB64756E2164756E21
12210
12411
  //# sourceMappingURL=index.js.map