@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,46 @@
1
+ export type { CodeEditorConfig, CodeEditorHooks, CompletionRequest, PopupItem, PopupState, RunDetail, RunSelectionDetail, SelectionDetail } from './controller'
2
+ export type { CompletionItem, Keymap, Position } from './core'
3
+ export type { CaretAnchor, DiagnosticSeverity, EditorDiagnostic, EditorViewOptions, EditorWidget } from './view'
4
+ export { default as CodeEditor } from './CodeEditor.stx'
5
+
6
+ export interface CodeEditorProps {
7
+ /** The text. Two-way with `v-model:value`. */
8
+ value?: string
9
+ /** A language id the highlighter knows. Default `typescript`. */
10
+ language?: string
11
+ readonly?: boolean
12
+ disabled?: boolean
13
+ /** Show the gutter. Default true. */
14
+ lineNumbers?: boolean
15
+ /** Colour the code. Default true. */
16
+ highlight?: boolean
17
+ /** Columns per indent level. Default 2. */
18
+ tabSize?: number
19
+ /** Indent with spaces rather than tab characters. Default true. */
20
+ insertSpaces?: boolean
21
+ /** Offer completions while typing, not only on Ctrl-Space. Default true. */
22
+ autoComplete?: boolean
23
+ /** Names always on offer in the completion popup. */
24
+ completions?: import('./core').CompletionItem[]
25
+ /** Results, notes and errors shown beside lines. Lines are zero-based. */
26
+ widgets?: import('./view').EditorWidget[]
27
+ /** Ranges drawn with a squiggle. */
28
+ diagnostics?: import('./view').EditorDiagnostic[]
29
+ /** Show each diagnostic's message after its line. Default true. */
30
+ inlineDiagnostics?: boolean
31
+ /** A suggestion shown at the caret. Tab takes it, Escape lets it go. */
32
+ ghostText?: string
33
+ /** Bindings laid over the defaults, such as `{ 'Mod-s': 'run' }`. */
34
+ keymap?: import('./core').Keymap
35
+ /** Pin the code colours to `light` or `dark`. Follows the page otherwise. */
36
+ theme?: 'light' | 'dark'
37
+ /** A CSS length. Default `20rem`. */
38
+ height?: string
39
+ placeholder?: string
40
+ /** The field's accessible name. Default `Code editor`. */
41
+ label?: string
42
+ autofocus?: boolean
43
+ name?: string
44
+ id?: string
45
+ className?: string
46
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * The highlighter, as the line tokenizer the editor core asks for.
3
+ *
4
+ * The core takes a tokenizer rather than importing one, so this is the only
5
+ * file in the editor that knows `ts-syntax-highlighter` exists. It reaches it
6
+ * through the package's browser entry: the root entry loads a config file from
7
+ * disk, which a page cannot do.
8
+ */
9
+ import type { TokenizerState } from 'ts-syntax-highlighter/browser'
10
+ import type { HighlightToken, LineTokenizer } from './core/highlight'
11
+ import { resolveGrammar, tokenClass, Tokenizer } from 'ts-syntax-highlighter/browser'
12
+
13
+ /**
14
+ * A line longer than this is shown plain.
15
+ *
16
+ * A minified bundle is one line of a hundred thousand characters. Nobody reads
17
+ * it, some grammars are quadratic in it, and it would be re-tokenized whole on
18
+ * every key typed into it.
19
+ */
20
+ export const MAX_TOKENIZED_LINE_LENGTH = 10_000
21
+
22
+ /** A string equal exactly when two tokenizer states are. */
23
+ export function tokenizerStateKey(state: TokenizerState): string {
24
+ const { frames } = state
25
+ // Nearly every line of nearly every file ends where it began: at the root.
26
+ if (frames.length === 1 && frames[0]!.pattern === null && !frames[0]!.raw)
27
+ return ''
28
+
29
+ let key = ''
30
+ for (const frame of frames)
31
+ key += `${frame.pattern ?? ''}\u0001${frame.raw ? frame.end ?? '' : ''}\u0001${frame.scopes.join(' ')}\u0002`
32
+
33
+ return key
34
+ }
35
+
36
+ /**
37
+ * A line tokenizer for a language, or null when the highlighter has no grammar
38
+ * for it. The editor shows plain text then, which is the right floor: the code
39
+ * is the content and the colour is decoration.
40
+ */
41
+ export async function createLanguageTokenizer(language: string): Promise<LineTokenizer<TokenizerState> | null> {
42
+ const grammar = await resolveGrammar(language)
43
+ if (!grammar)
44
+ return null
45
+
46
+ const tokenizer = new Tokenizer(grammar)
47
+ const lines: string[] = ['']
48
+
49
+ return {
50
+ initialState: () => tokenizer.initialState(),
51
+ stateKey: tokenizerStateKey,
52
+ tokenizeLine(text, state) {
53
+ if (text.length > MAX_TOKENIZED_LINE_LENGTH)
54
+ return { tokens: [{ type: '', content: text }], endState: state }
55
+
56
+ lines[0] = text
57
+ const result = tokenizer.tokenizeLinesFrom(lines, state, 1)
58
+ const tokens: HighlightToken[] = []
59
+ let last: HighlightToken | undefined
60
+
61
+ for (const token of result.lines[0]!.tokens) {
62
+ const type = tokenClass(token.type)
63
+ // Neighbours of one colour are one run: half the elements to create,
64
+ // and nothing a reader could tell apart.
65
+ if (last && last.type === type)
66
+ last.content += token.content
67
+ else
68
+ tokens.push(last = { type, content: token.content })
69
+ }
70
+
71
+ return { tokens, endState: result.endState }
72
+ },
73
+ }
74
+ }