@stacksjs/components 0.2.854 → 0.2.855

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.
Files changed (44) hide show
  1. package/dist/CodeEditor-jg4w7e15.stx +497 -0
  2. package/dist/code-editor/editor.js +2166 -0
  3. package/dist/components/CodeBlock.d.ts +12 -5
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.js +29 -12
  6. package/dist/ui/code-editor/CodeEditor.d.ts +46 -0
  7. package/dist/ui/code-editor/controller.d.ts +103 -0
  8. package/dist/ui/code-editor/core/commands.d.ts +55 -0
  9. package/dist/ui/code-editor/core/completion.d.ts +40 -0
  10. package/dist/ui/code-editor/core/editor.d.ts +69 -0
  11. package/dist/ui/code-editor/core/highlight.d.ts +61 -0
  12. package/dist/ui/code-editor/core/history.d.ts +27 -0
  13. package/dist/ui/code-editor/core/index.d.ts +13 -0
  14. package/dist/ui/code-editor/core/keymap.d.ts +38 -0
  15. package/dist/ui/code-editor/core/selection.d.ts +30 -0
  16. package/dist/ui/code-editor/core/text.d.ts +64 -0
  17. package/dist/ui/code-editor/editor.d.ts +15 -0
  18. package/dist/ui/code-editor/index.d.ts +29 -0
  19. package/dist/ui/code-editor/tokenizer.d.ts +18 -0
  20. package/dist/ui/code-editor/view.d.ts +59 -0
  21. package/dist/utils/highlighter.d.ts +19 -1
  22. package/dist/utils/highlighter.js +24 -7
  23. package/dist/utils/syntax-theme.d.ts +44 -0
  24. package/package.json +9 -4
  25. package/src/components/CodeBlock.stx +195 -17
  26. package/src/index.ts +2 -0
  27. package/src/ui/code-editor/CodeEditor.stx +497 -0
  28. package/src/ui/code-editor/README.md +134 -0
  29. package/src/ui/code-editor/controller.ts +517 -0
  30. package/src/ui/code-editor/core/commands.ts +381 -0
  31. package/src/ui/code-editor/core/completion.ts +212 -0
  32. package/src/ui/code-editor/core/editor.ts +319 -0
  33. package/src/ui/code-editor/core/highlight.ts +203 -0
  34. package/src/ui/code-editor/core/history.ts +190 -0
  35. package/src/ui/code-editor/core/index.ts +14 -0
  36. package/src/ui/code-editor/core/keymap.ts +125 -0
  37. package/src/ui/code-editor/core/selection.ts +104 -0
  38. package/src/ui/code-editor/core/text.ts +252 -0
  39. package/src/ui/code-editor/editor.ts +16 -0
  40. package/src/ui/code-editor/index.ts +46 -0
  41. package/src/ui/code-editor/tokenizer.ts +74 -0
  42. package/src/ui/code-editor/view.ts +1066 -0
  43. package/src/utils/highlighter.ts +121 -218
  44. package/src/utils/syntax-theme.ts +144 -0
@@ -0,0 +1,103 @@
1
+ import { EditorCore } from './core/index';
2
+ import { EditorView } from './view';
3
+ import type { CompletionItem, Keymap, Transaction } from './core/index';
4
+ import type { EditorDiagnostic, EditorWidget } from './view';
5
+ export declare interface CodeEditorConfig {
6
+ value?: string
7
+ language?: string
8
+ tabSize?: number
9
+ insertSpaces?: boolean
10
+ readOnly?: boolean
11
+ lineNumbers?: boolean
12
+ highlight?: boolean
13
+ autoComplete?: boolean
14
+ completions?: readonly CompletionItem[]
15
+ keymap?: Keymap
16
+ inlineDiagnostics?: boolean
17
+ }
18
+ export declare interface SelectionDetail {
19
+ anchor: number
20
+ head: number
21
+ from: number
22
+ to: number
23
+ line: number
24
+ column: number
25
+ text: string
26
+ empty: boolean
27
+ }
28
+ export declare interface RunDetail {
29
+ value: string
30
+ selection: SelectionDetail
31
+ }
32
+ export declare interface RunSelectionDetail {
33
+ text: string
34
+ from: number
35
+ to: number
36
+ line: number
37
+ selected: boolean
38
+ }
39
+ export declare interface CompletionRequest {
40
+ query: string
41
+ from: number
42
+ to: number
43
+ line: number
44
+ column: number
45
+ textBefore: string
46
+ explicit: boolean
47
+ trigger: string
48
+ }
49
+ export declare interface PopupItem {
50
+ id: string
51
+ index: number
52
+ label: string
53
+ detail: string
54
+ kind: string
55
+ selected: boolean
56
+ }
57
+ export declare interface PopupState {
58
+ open: boolean
59
+ items: PopupItem[]
60
+ activeId: string
61
+ left: number
62
+ top: number
63
+ placement: 'below' | 'above'
64
+ }
65
+ export declare interface CodeEditorHooks {
66
+ onChange?: (value: string, transaction: Transaction) => void
67
+ onSelection?: (detail: SelectionDetail) => void
68
+ onRun?: (detail: RunDetail) => void
69
+ onRunSelection?: (detail: RunSelectionDetail) => void
70
+ onCompleteRequest?: (request: CompletionRequest) => void
71
+ onCompletionAccept?: (item: CompletionItem) => void
72
+ onPopup?: (state: PopupState) => void
73
+ onGhost?: (outcome: 'accept' | 'dismiss', text: string) => void
74
+ onFocus?: () => void
75
+ onBlur?: () => void
76
+ }
77
+ export declare class CodeEditorController {
78
+ readonly core: EditorCore;
79
+ readonly view: EditorView;
80
+ constructor(root: HTMLElement, config?: CodeEditorConfig, hooks?: CodeEditorHooks);
81
+ get value(): string;
82
+ setValue(value: string): void;
83
+ get selection(): SelectionDetail;
84
+ setSelection(anchor: number, head?: number): void;
85
+ focus(): void;
86
+ setLanguage(language: string): void;
87
+ setHighlight(highlight: boolean): void;
88
+ setReadOnly(readOnly: boolean): void;
89
+ setLineNumbers(show: boolean): void;
90
+ setTabSize(tabSize: number): void;
91
+ setInsertSpaces(insertSpaces: boolean): void;
92
+ setAutoComplete(autoComplete: boolean): void;
93
+ setKeymap(keymap: Keymap): void;
94
+ setCompletions(completions: readonly CompletionItem[]): void;
95
+ setWidgets(widgets: readonly EditorWidget[]): void;
96
+ setDiagnostics(diagnostics: readonly EditorDiagnostic[]): void;
97
+ setGhostText(text: string | null): void;
98
+ showCompletions(items: readonly CompletionItem[], options?: { from?: number }): void;
99
+ closeCompletions(): void;
100
+ acceptCompletion(index?: number): boolean;
101
+ selectCompletion(index: number): void;
102
+ destroy(): void;
103
+ }
@@ -0,0 +1,55 @@
1
+ import type { Command, EditorCore } from './editor';
2
+ /** First and last line the main range touches. */
3
+ declare function selectedLines(editor: EditorCore): { first: number, last: number };
4
+ /**
5
+ * Where the selection goes when whole lines are changed under it.
6
+ *
7
+ * The end that sits at the very start of a line stays there. Left to the
8
+ * default, indenting would push it in with the text, and a selection that
9
+ * covered three whole lines would cover two and most of a third.
10
+ */
11
+ declare function linewiseSelection(editor: EditorCore, changes: readonly ChangeSpec[]): SelectionRange;
12
+ /**
13
+ * A typed character, where the editor has an opinion about it.
14
+ *
15
+ * Opening a bracket closes it. Typing a closer that is already there steps
16
+ * over it instead of doubling it. A closing brace on a line of nothing but
17
+ * indentation drops back a level. With text selected, an opener wraps it.
18
+ *
19
+ * Returns false for everything else, and the text field inserts the character
20
+ * itself - which is what keeps input methods and dead keys working, since the
21
+ * editor never sees a composition as individual keys.
22
+ */
23
+ export declare function handleTextInput(editor: EditorCore, text: string): boolean;
24
+ export declare function registerDefaultCommands(editor: EditorCore): void;
25
+ /**
26
+ * Enter: a new line that starts where the old one did.
27
+ *
28
+ * One level deeper after an opening bracket. Between a pair, the closer goes
29
+ * to a line of its own and the caret is left on the indented line between.
30
+ */
31
+ export declare const insertNewlineAndIndent: Command;
32
+ /** Tab: indent the selected lines, or move the caret to the next tab stop. */
33
+ export declare const indentMore: Command;
34
+ /** Shift-Tab: take one level of indentation off every selected line. */
35
+ export declare const indentLess: Command;
36
+ /**
37
+ * Comment the selected lines, or uncomment them if every one already is.
38
+ *
39
+ * The markers line up at the shallowest indentation among the lines rather
40
+ * than at column zero or at each line's own, so a commented block still reads
41
+ * as a block and uncommenting gives back exactly what was there.
42
+ */
43
+ export declare const toggleLineComment: Command;
44
+ /**
45
+ * Backspace, where it means more than one character.
46
+ *
47
+ * Between an empty pair it takes both halves, since the closer was never
48
+ * typed. In leading spaces it goes back a whole tab stop. Anything else is
49
+ * left to the text field.
50
+ */
51
+ export declare const deleteBackward: Command;
52
+ export declare const undo: Command;
53
+ export declare const redo: Command;
54
+ /** The commands every editor starts with, by the name a keymap refers to. */
55
+ export declare const defaultCommands: Readonly<Record<string, Command>>;
@@ -0,0 +1,40 @@
1
+ import type { ChangeSpec } from './text';
2
+ /** Where the word ending at `offset` begins. */
3
+ export declare function wordStart(text: string, offset: number): number;
4
+ /**
5
+ * How well `query` matches `text`, or null when it does not.
6
+ *
7
+ * Every character of the query has to appear in order. A match that starts at
8
+ * the beginning beats one that does not, a run of adjacent characters beats
9
+ * the same characters scattered, a match at the start of a word (`gU` for
10
+ * `getUser`) beats one in the middle of it, and an exact case match breaks ties.
11
+ */
12
+ export declare function matchScore(query: string, text: string): { score: number, matches: number[] } | null;
13
+ /** The items a query leaves, best first. Order among equals is the host's. */
14
+ export declare function rankCompletions(items: readonly CompletionItem[], query: string): RankedCompletion[];
15
+ export declare interface CompletionItem {
16
+ label: string
17
+ insert?: string
18
+ detail?: string
19
+ kind?: string
20
+ filterText?: string
21
+ boost?: number
22
+ }
23
+ export declare interface RankedCompletion {
24
+ item: CompletionItem
25
+ score: number
26
+ matches: number[]
27
+ }
28
+ export declare class CompletionList {
29
+ get isOpen(): boolean;
30
+ get visible(): readonly RankedCompletion[];
31
+ get selectedIndex(): number;
32
+ get selected(): CompletionItem | undefined;
33
+ get from(): number;
34
+ open(items: readonly CompletionItem[], from: number, query: string): void;
35
+ filter(query: string): void;
36
+ move(delta: number): void;
37
+ select(index: number): void;
38
+ close(): void;
39
+ accept(head: number): { change: ChangeSpec, item: CompletionItem } | null;
40
+ }
@@ -0,0 +1,69 @@
1
+ import { History } from './history';
2
+ import { TextDocument } from './text';
3
+ import type { AppliedChange, ChangeSpec, Position } from './text';
4
+ import type { EditorSelection, SelectionRange } from './selection';
5
+ export declare function languageConfig(language: string | undefined): LanguageConfig;
6
+ export declare const DEFAULT_LANGUAGE: LanguageConfig;
7
+ /** Per-language overrides, by the id the highlighter knows the language as. */
8
+ export declare const LANGUAGE_CONFIGS: Readonly<Record<string, Partial<LanguageConfig>>>;
9
+ export declare interface LanguageConfig {
10
+ lineComment: string
11
+ brackets: Record<string, string>
12
+ quotes: string[]
13
+ }
14
+ export declare interface EditorOptions {
15
+ value?: string
16
+ tabSize?: number
17
+ insertSpaces?: boolean
18
+ readOnly?: boolean
19
+ language?: Partial<LanguageConfig>
20
+ historyDepth?: number
21
+ now?: () => number
22
+ }
23
+ export declare interface TransactionSpec {
24
+ changes?: ChangeSpec[]
25
+ selection?: EditorSelection | SelectionRange
26
+ kind?: string
27
+ origin?: TransactionOrigin
28
+ addToHistory?: boolean
29
+ scrollIntoView?: boolean
30
+ }
31
+ export declare interface Transaction {
32
+ changes: AppliedChange[]
33
+ before: EditorSelection
34
+ selection: EditorSelection
35
+ docChanged: boolean
36
+ selectionChanged: boolean
37
+ kind: string
38
+ origin: TransactionOrigin
39
+ scrollIntoView: boolean
40
+ }
41
+ /** Where a transaction came from, which decides who still has to be told. */
42
+ export type TransactionOrigin = 'input' | 'command' | 'api' | 'history';
43
+ export type TransactionListener = (transaction: Transaction, editor: EditorCore) => void;
44
+ /** A command answers whether it did anything, so a key can fall through. */
45
+ export type Command = (editor: EditorCore) => boolean;
46
+ export declare class EditorCore {
47
+ readonly doc: TextDocument;
48
+ readonly history: History;
49
+ readonly commands: Map<string, Command>;
50
+ tabSize: number;
51
+ insertSpaces: boolean;
52
+ readOnly: boolean;
53
+ language: LanguageConfig;
54
+ constructor(options?: EditorOptions);
55
+ get selection(): EditorSelection;
56
+ get main(): SelectionRange;
57
+ get value(): string;
58
+ get indentUnit(): string;
59
+ get cursor(): Position;
60
+ get selectedText(): string;
61
+ subscribe(listener: TransactionListener): () => void;
62
+ dispatch(spec: TransactionSpec): Transaction;
63
+ setSelection(anchor: number, head?: number, origin?: TransactionOrigin): Transaction;
64
+ setValue(text: string, options?: { keepHistory?: boolean }): Transaction;
65
+ syncInput(value: string, selectionStart: number, selectionEnd: number, backward?: boolean): Transaction;
66
+ undo(): boolean;
67
+ redo(): boolean;
68
+ exec(name: string): boolean;
69
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Highlighting that only redoes what a change could have affected.
3
+ *
4
+ * A tokenizer reads a line given the state the previous line left it in:
5
+ * inside a block comment, inside a template string, or nowhere in particular.
6
+ * So each line is remembered with the state it started in, and after an edit the
7
+ * tokenizer starts at the first changed line and stops at the first unchanged
8
+ * line it would enter in the same state as last time. That line, and everything
9
+ * below it, would tokenize exactly as it already has.
10
+ *
11
+ * For an ordinary keystroke that is one line. For one that opens a block
12
+ * comment it is every line down to wherever the comment ends, which is the
13
+ * honest answer, and it is why the work is given a budget: a pass stops when
14
+ * the budget is spent and says where it got to, and the caller continues when
15
+ * it is idle. The lines on screen are done first either way.
16
+ *
17
+ * The tokenizer is passed in. This file does not import one, which keeps the
18
+ * editor core free of the highlighter and lets a test use a tokenizer small
19
+ * enough to reason about.
20
+ */
21
+ export declare interface HighlightToken {
22
+ type: string
23
+ content: string
24
+ }
25
+ /** A tokenizer, one line at a time, with its state made explicit. */
26
+ export declare interface LineTokenizer<State = unknown> {
27
+ initialState: () => State
28
+ tokenizeLine: (text: string, state: State) => { tokens: HighlightToken[], endState: State }
29
+ stateKey: (state: State) => string
30
+ }
31
+ export declare interface HighlightedLine<State = unknown> {
32
+ text: string
33
+ tokens: HighlightToken[]
34
+ startKey: string
35
+ endState: State
36
+ endKey: string
37
+ }
38
+ export declare interface LineChange {
39
+ fromLine: number
40
+ removedLines: number
41
+ insertedLines: number
42
+ }
43
+ export declare interface HighlightPass {
44
+ from: number
45
+ to: number
46
+ pending: boolean
47
+ }
48
+ /** A source of line text: the document, or anything shaped like it. */
49
+ export declare interface LineSource {
50
+ readonly lineCount: number
51
+ line: (index: number) => string
52
+ }
53
+ export declare class LineHighlighter<State = unknown> {
54
+ constructor(tokenizer: LineTokenizer<State>, source: LineSource);
55
+ reset(tokenizer?: LineTokenizer<State>, source?: LineSource): void;
56
+ get(index: number): HighlightedLine<State> | null;
57
+ get pending(): boolean;
58
+ get firstDirtyLine(): number;
59
+ applyChange(change: LineChange): void;
60
+ update(options?: { through?: number, maxLines?: number }): HighlightPass;
61
+ }
@@ -0,0 +1,27 @@
1
+ import type { AppliedChange, ChangeSpec } from './text';
2
+ import type { EditorSelection } from './selection';
3
+ /** The changes that put the document back, in after-the-change coordinates. */
4
+ export declare function invertChanges(applied: readonly AppliedChange[]): ChangeSpec[];
5
+ export declare interface HistoryEntry {
6
+ changes: ChangeSpec[]
7
+ inverse: ChangeSpec[]
8
+ before: EditorSelection
9
+ after: EditorSelection
10
+ kind: string
11
+ time: number
12
+ }
13
+ export declare interface HistoryOptions {
14
+ depth?: number
15
+ groupDelay?: number
16
+ }
17
+ export declare class History {
18
+ constructor(options?: HistoryOptions);
19
+ get canUndo(): boolean;
20
+ get canRedo(): boolean;
21
+ get undoDepth(): number;
22
+ clear(): void;
23
+ record(applied: readonly AppliedChange[], before: EditorSelection, after: EditorSelection, kind: string, time: number): void;
24
+ breakGroup(): void;
25
+ popUndo(): HistoryEntry | undefined;
26
+ popRedo(): HistoryEntry | undefined;
27
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The editor core: a document, a selection, a history, commands and a keymap.
3
+ *
4
+ * Framework free and DOM free. `../view.ts` is the half that draws it.
5
+ */
6
+ export * from './commands';
7
+ export * from './completion';
8
+ export * from './editor';
9
+ export * from './highlight';
10
+ export * from './history';
11
+ export * from './keymap';
12
+ export * from './selection';
13
+ export * from './text';
@@ -0,0 +1,38 @@
1
+ /** Whether this is a platform where the command key is Command. */
2
+ export declare function isApplePlatform(platform?: string): boolean;
3
+ /** `Mod-Shift-z` and `shift-mod-Z` as the one canonical spelling. */
4
+ export declare function normalizeBinding(binding: string, apple: boolean): string;
5
+ /** The canonical name of a key press. */
6
+ export declare function keyName(event: KeyLike): string;
7
+ /**
8
+ * Build a keymap for one platform.
9
+ *
10
+ * Later maps win, and binding a key to an empty string unbinds it - which is
11
+ * how a host takes Tab back for focus, or gives `Mod-Enter` to something else.
12
+ */
13
+ export declare function createKeymap(maps?: readonly Keymap[], apple?: boolean): ResolvedKeymap;
14
+ export declare const defaultKeymap: Keymap;
15
+ /**
16
+ * Keys to command names.
17
+ *
18
+ * A binding is written the way it is said: `Mod-Shift-Enter`. `Mod` is Command
19
+ * on a Mac and Control everywhere else, resolved when the keymap is built, so
20
+ * one table serves both and neither shows up in the other's shortcuts.
21
+ *
22
+ * The map answers with a name rather than a function. The editor core owns
23
+ * some commands (indent, undo) and the view owns others (run, complete), and
24
+ * neither should have to import the other to be bound to a key.
25
+ */
26
+ /** The part of a keyboard event a binding is decided on. */
27
+ export declare interface KeyLike {
28
+ key: string
29
+ ctrlKey?: boolean
30
+ metaKey?: boolean
31
+ altKey?: boolean
32
+ shiftKey?: boolean
33
+ }
34
+ export declare interface ResolvedKeymap {
35
+ lookup: (event: KeyLike) => string | undefined
36
+ bindings: ReadonlyMap<string, string>
37
+ }
38
+ export type Keymap = Readonly<Record<string, string>>;
@@ -0,0 +1,30 @@
1
+ import type { ChangeSpec } from './text';
2
+ export declare function range(anchor: number, head?: number): SelectionRange;
3
+ export declare function singleSelection(anchor: number, head?: number): EditorSelection;
4
+ export declare function rangeFrom(value: SelectionRange): number;
5
+ export declare function rangeTo(value: SelectionRange): number;
6
+ export declare function rangeEmpty(value: SelectionRange): boolean;
7
+ export declare function mainRange(selection: EditorSelection): SelectionRange;
8
+ export declare function selectionsEqual(a: EditorSelection, b: EditorSelection): boolean;
9
+ export declare function clampSelection(selection: EditorSelection, length: number): EditorSelection;
10
+ /**
11
+ * Where an offset ends up after a set of changes.
12
+ *
13
+ * `changes` are in the coordinates of the document before any of them, sorted
14
+ * and not overlapping, which is the form a transaction holds them in.
15
+ *
16
+ * `assoc` settles the one ambiguous case, an offset exactly where text was
17
+ * inserted: `1` puts it after the insertion, which is what a caret wants (you
18
+ * typed, the caret moved), and `-1` leaves it before.
19
+ */
20
+ export declare function mapOffset(offset: number, changes: readonly ChangeSpec[], assoc?: -1 | 1): number;
21
+ export declare function mapSelection(selection: EditorSelection, changes: readonly ChangeSpec[]): EditorSelection;
22
+ /** `anchor` is where the selection started, `head` where the caret is. */
23
+ export declare interface SelectionRange {
24
+ anchor: number
25
+ head: number
26
+ }
27
+ export declare interface EditorSelection {
28
+ ranges: SelectionRange[]
29
+ main: number
30
+ }
@@ -0,0 +1,64 @@
1
+ /** One line ending throughout: a textarea reports `\n` whatever was pasted. */
2
+ export declare function normalizeNewlines(text: string): string;
3
+ /** Leading spaces and tabs of a line. */
4
+ export declare function leadingWhitespace(text: string): string;
5
+ /** Width of a run of indentation in columns, tabs advancing to the next stop. */
6
+ export declare function indentWidth(whitespace: string, tabSize: number): number;
7
+ /** Indentation of a given width, written the way the editor is set to write it. */
8
+ export declare function indentString(width: number, tabSize: number, insertSpaces: boolean): string;
9
+ /**
10
+ * The document: text held as lines.
11
+ *
12
+ * Lines, not one string, because everything an editor asks is a line question.
13
+ * Which lines are on screen, which line changed, what the indentation of this
14
+ * one is. A single string answers each of those with a scan from the top, and a
15
+ * keystroke in a two-thousand-line file would pay for all two thousand.
16
+ *
17
+ * Offsets are still the coordinate everything else speaks - a textarea reports
18
+ * its selection as two of them, and a change is "from here to there" - so the
19
+ * document converts both ways. The table of line starts behind that is rebuilt
20
+ * lazily and only from the first line that changed.
21
+ *
22
+ * Mutable on purpose. An edit touches one or two entries of the array, and the
23
+ * history keeps what it needs to reverse it (the removed text), which is all an
24
+ * immutable document would have bought.
25
+ */
26
+ /** A place in the document. Both zero-based; `column` counts UTF-16 units. */
27
+ export declare interface Position {
28
+ line: number
29
+ column: number
30
+ }
31
+ /** Replace `from..to` with `insert`. Offsets into the document before the change. */
32
+ export declare interface ChangeSpec {
33
+ from: number
34
+ to: number
35
+ insert: string
36
+ }
37
+ /** What a change did, in the terms each consumer needs. */
38
+ export declare interface AppliedChange extends ChangeSpec {
39
+ removed: string
40
+ fromLine: number
41
+ removedLines: number
42
+ insertedLines: number
43
+ }
44
+ export declare interface LineInfo {
45
+ index: number
46
+ from: number
47
+ to: number
48
+ text: string
49
+ }
50
+ export declare class TextDocument {
51
+ constructor(text?: string);
52
+ get lineCount(): number;
53
+ get length(): number;
54
+ line(index: number): string;
55
+ toString(): string;
56
+ adoptText(text: string): void;
57
+ lineIndexAt(offset: number): number;
58
+ lineAt(offset: number): LineInfo;
59
+ lineInfo(index: number): LineInfo;
60
+ positionAt(offset: number): Position;
61
+ offsetAt(position: Position): number;
62
+ slice(from: number, to: number): string;
63
+ replace(from: number, to: number, insert: string): AppliedChange;
64
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The code editor without its stx template.
3
+ *
4
+ * `@stacksjs/components/code-editor`. Everything `<CodeEditor>` is made of, for
5
+ * a host that mounts one itself or that wants the model on its own:
6
+ *
7
+ * - `EditorCore` and the rest of `./core`: the document, selection, history,
8
+ * commands and keymap. No DOM, so it runs anywhere.
9
+ * - `EditorView`: the text field over its painted copy.
10
+ * - `CodeEditorController`: the two joined, with highlighting and completion.
11
+ */
12
+ export * from './controller';
13
+ export * from './core/index';
14
+ export { createLanguageTokenizer, MAX_TOKENIZED_LINE_LENGTH, tokenizerStateKey } from './tokenizer';
15
+ export * from './view';
@@ -0,0 +1,29 @@
1
+ export type { CodeEditorConfig, CodeEditorHooks, CompletionRequest, PopupItem, PopupState, RunDetail, RunSelectionDetail, SelectionDetail } from './controller';
2
+ export type { CompletionItem, Keymap, Position } from './core/index';
3
+ export type { CaretAnchor, DiagnosticSeverity, EditorDiagnostic, EditorViewOptions, EditorWidget } from './view';
4
+ export declare interface CodeEditorProps {
5
+ value?: string
6
+ language?: string
7
+ readonly?: boolean
8
+ disabled?: boolean
9
+ lineNumbers?: boolean
10
+ highlight?: boolean
11
+ tabSize?: number
12
+ insertSpaces?: boolean
13
+ autoComplete?: boolean
14
+ completions?: import('./core/index').CompletionItem[]
15
+ widgets?: import('./view').EditorWidget[]
16
+ diagnostics?: import('./view').EditorDiagnostic[]
17
+ inlineDiagnostics?: boolean
18
+ ghostText?: string
19
+ keymap?: import('./core/index').Keymap
20
+ theme?: 'light' | 'dark'
21
+ height?: string
22
+ placeholder?: string
23
+ label?: string
24
+ autofocus?: boolean
25
+ name?: string
26
+ id?: string
27
+ className?: string
28
+ }
29
+ export { default as CodeEditor } from './CodeEditor';
@@ -0,0 +1,18 @@
1
+ import type { LineTokenizer } from './core/highlight';
2
+ import type { TokenizerState } from 'ts-syntax-highlighter/browser';
3
+ /** A string equal exactly when two tokenizer states are. */
4
+ export declare function tokenizerStateKey(state: TokenizerState): string;
5
+ /**
6
+ * A line tokenizer for a language, or null when the highlighter has no grammar
7
+ * for it. The editor shows plain text then, which is the right floor: the code
8
+ * is the content and the colour is decoration.
9
+ */
10
+ export declare function createLanguageTokenizer(language: string): Promise<LineTokenizer<TokenizerState> | null>;
11
+ /**
12
+ * A line longer than this is shown plain.
13
+ *
14
+ * A minified bundle is one line of a hundred thousand characters. Nobody reads
15
+ * it, some grammars are quadratic in it, and it would be re-tokenized whole on
16
+ * every key typed into it.
17
+ */
18
+ export declare const MAX_TOKENIZED_LINE_LENGTH: unknown;
@@ -0,0 +1,59 @@
1
+ import type { EditorCore, HighlightedLine, LineTokenizer, Position, ResolvedKeymap, Transaction } from './core/index';
2
+ /** One line as HTML: a span per coloured run, plain text left unwrapped. */
3
+ export declare function renderLineHtml(line: HighlightedLine | null, text: string): string;
4
+ /** Something shown beside or after a line: a result, a note, an error. */
5
+ export declare interface EditorWidget {
6
+ line: number
7
+ column?: number | 'end'
8
+ text: string
9
+ kind?: string
10
+ className?: string
11
+ }
12
+ /** A range with something to say about it, drawn as a squiggle. */
13
+ export declare interface EditorDiagnostic {
14
+ from: Position
15
+ to: Position
16
+ message: string
17
+ severity?: DiagnosticSeverity
18
+ source?: string
19
+ }
20
+ /** Where the caret is, for anything that has to be placed beside it. */
21
+ export declare interface CaretAnchor {
22
+ left: number
23
+ top: number
24
+ bottom: number
25
+ spaceAbove: number
26
+ spaceBelow: number
27
+ }
28
+ export declare interface EditorViewOptions {
29
+ core: EditorCore
30
+ keymap: ResolvedKeymap
31
+ onTransaction?: (transaction: Transaction) => void
32
+ onCommand?: (name: string) => boolean
33
+ onKeydown?: (event: KeyboardEvent) => boolean
34
+ onFocus?: () => void
35
+ onBlur?: () => void
36
+ onGhost?: (outcome: 'accept' | 'dismiss', text: string) => void
37
+ inlineDiagnostics?: boolean
38
+ }
39
+ export type DiagnosticSeverity = 'error' | 'warning' | 'info' | 'hint';
40
+ export declare class EditorView {
41
+ readonly core: EditorCore;
42
+ constructor(root: HTMLElement, options: EditorViewOptions);
43
+ setKeymap(keymap: ResolvedKeymap): void;
44
+ setLineNumbers(show: boolean): void;
45
+ setReadOnly(readOnly: boolean): void;
46
+ setTokenizer<State>(tokenizer: LineTokenizer<State> | null): void;
47
+ layout(): void;
48
+ render(): void;
49
+ caretAnchor(offset?: number): CaretAnchor;
50
+ revealCaret(): void;
51
+ focus(): void;
52
+ get hasFocus(): boolean;
53
+ setWidgets(widgets: readonly EditorWidget[]): void;
54
+ setDiagnostics(diagnostics: readonly EditorDiagnostic[]): void;
55
+ setGhostText(text: string | null, outcome?: 'accept' | 'dismiss' | null): void;
56
+ get ghostText(): string;
57
+ acceptGhost(): boolean;
58
+ destroy(): void;
59
+ }
@@ -1,8 +1,25 @@
1
1
  import type { Highlighter as TSHighlighter } from 'ts-syntax-highlighter';
2
2
  /**
3
- * Initialize or get the syntax highlighter instance
3
+ * The full highlighter from the package root, for a caller that wants its API.
4
+ *
5
+ * Server only: the root entry reads a config file. `highlight()` below does
6
+ * not use it.
4
7
  */
5
8
  export declare function getHighlighter(): Promise<TSHighlighter>;
9
+ /**
10
+ * Tokenized code as HTML, or null when it cannot be tokenized.
11
+ *
12
+ * A class on each token and no colour: `tok-keyword`, not
13
+ * `style="color: #cf222e"`. The highlighter's own renderer writes the colour
14
+ * inline, where no stylesheet can reach it, which is why this block could not
15
+ * follow a dark-mode switch without rendering everything twice and reading the
16
+ * colours back out (stacksjs/stx#2015). With classes, light and dark are one
17
+ * piece of markup under two sets of custom properties.
18
+ *
19
+ * Works in a browser as well as on a server, so a block whose code changes
20
+ * after the page has loaded can be highlighted again where it is.
21
+ */
22
+ export declare function highlightToHtml(code: string, options?: { language?: string, theme?: 'light' | 'dark' | 'auto', lineNumbers?: boolean }): Promise<string | null>;
6
23
  /**
7
24
  * Highlight code with syntax highlighting
8
25
  */
@@ -27,3 +44,4 @@ export declare interface HighlightResult {
27
44
  language: string
28
45
  theme: string
29
46
  }
47
+ declare type BrowserEntry = typeof import('ts-syntax-highlighter/browser');