@bigpixelrocket/uiok 0.2.3 → 0.4.0
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/CHANGELOG.md +48 -1
- package/dist/charts/Sparkline.d.ts +20 -0
- package/dist/charts/Sparkline.js +12 -0
- package/dist/charts/Sparkline.logic.d.ts +9 -0
- package/dist/charts/Sparkline.logic.js +27 -0
- package/dist/charts/charts.css +17 -0
- package/dist/charts/index.d.ts +2 -0
- package/dist/charts/index.js +2 -0
- package/dist/dialog/ConfirmDialog.d.ts +17 -1
- package/dist/dialog/ConfirmDialog.js +4 -3
- package/dist/dialog/ModalDialog.d.ts +8 -0
- package/dist/dialog/ModalDialog.js +4 -2
- package/dist/dialog/ProgressDialog.d.ts +40 -0
- package/dist/dialog/ProgressDialog.js +40 -0
- package/dist/dialog/TypedNameDialog.d.ts +44 -0
- package/dist/dialog/TypedNameDialog.js +17 -0
- package/dist/dialog/index.d.ts +5 -2
- package/dist/dialog/index.js +4 -1
- package/dist/field/Check.d.ts +9 -2
- package/dist/field/Check.js +5 -4
- package/dist/field/Controls.d.ts +198 -0
- package/dist/field/Controls.js +94 -0
- package/dist/field/FieldLayout.d.ts +126 -0
- package/dist/field/FieldLayout.js +89 -0
- package/dist/field/Switch.d.ts +26 -0
- package/dist/field/Switch.js +20 -0
- package/dist/field/index.d.ts +10 -6
- package/dist/field/index.js +10 -6
- package/dist/frame/AppRoot.d.ts +44 -0
- package/dist/frame/AppRoot.js +26 -0
- package/dist/frame/Dock.d.ts +88 -0
- package/dist/frame/Dock.js +129 -0
- package/dist/frame/FilterChips.d.ts +28 -0
- package/dist/frame/FilterChips.js +14 -0
- package/dist/frame/PageParts.d.ts +69 -0
- package/dist/frame/PageParts.js +60 -0
- package/dist/frame/PaneSplit.d.ts +74 -0
- package/dist/frame/PaneSplit.js +197 -0
- package/dist/frame/SidebarResizer.d.ts +35 -0
- package/dist/frame/SidebarResizer.js +43 -0
- package/dist/frame/StatusNotice.d.ts +20 -0
- package/dist/frame/StatusNotice.js +13 -0
- package/dist/frame/Tree.d.ts +147 -0
- package/dist/frame/Tree.js +164 -0
- package/dist/frame/TreeToggle.d.ts +18 -0
- package/dist/frame/TreeToggle.js +17 -0
- package/dist/frame/WindowFrame.d.ts +57 -0
- package/dist/frame/WindowFrame.js +19 -0
- package/dist/frame/frame.css +801 -0
- package/dist/frame/index.d.ts +22 -0
- package/dist/frame/index.js +22 -0
- package/dist/frame/sharedRef.d.ts +8 -0
- package/dist/frame/sharedRef.js +26 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/labels/UiokLabels.d.ts +6 -0
- package/dist/labels/UiokLabels.js +5 -1
- package/dist/labels/dialog.d.ts +12 -0
- package/dist/labels/dialog.js +7 -1
- package/dist/labels/index.d.ts +2 -0
- package/dist/labels/index.js +2 -0
- package/dist/labels/settings.d.ts +46 -0
- package/dist/labels/settings.js +29 -0
- package/dist/labels/viewer.d.ts +193 -0
- package/dist/labels/viewer.js +124 -0
- package/dist/listing/EmptyState.d.ts +40 -0
- package/dist/listing/EmptyState.js +14 -0
- package/dist/listing/PaneMessage.d.ts +28 -0
- package/dist/listing/PaneMessage.js +12 -0
- package/dist/listing/index.d.ts +5 -1
- package/dist/listing/index.js +5 -1
- package/dist/listing/listing.css +66 -2
- package/dist/log/LogWell.d.ts +34 -0
- package/dist/log/LogWell.js +39 -0
- package/dist/log/LogWell.logic.d.ts +16 -0
- package/dist/log/LogWell.logic.js +26 -0
- package/dist/log/index.d.ts +8 -0
- package/dist/log/index.js +8 -0
- package/dist/log/log.css +57 -0
- package/dist/pane/Pane.d.ts +49 -0
- package/dist/pane/Pane.js +17 -0
- package/dist/pane/PaneKeys.d.ts +10 -3
- package/dist/pane/PaneKeys.js +26 -7
- package/dist/pane/index.d.ts +4 -2
- package/dist/pane/index.js +4 -2
- package/dist/pane/usePaneKeys.js +9 -11
- package/dist/shell/AboutWindow.d.ts +21 -0
- package/dist/shell/AboutWindow.js +10 -0
- package/dist/shell/LanguageChoice.d.ts +27 -0
- package/dist/shell/LanguageChoice.js +17 -0
- package/dist/shell/LoadFailure.d.ts +22 -0
- package/dist/shell/LoadFailure.js +15 -0
- package/dist/shell/Lockup.d.ts +52 -0
- package/dist/shell/Lockup.js +30 -0
- package/dist/shell/SettingsDialog.d.ts +97 -0
- package/dist/shell/SettingsDialog.js +219 -0
- package/dist/shell/SettingsDialog.logic.d.ts +33 -0
- package/dist/shell/SettingsDialog.logic.js +42 -0
- package/dist/shell/SettingsFiles.d.ts +55 -0
- package/dist/shell/SettingsFiles.js +30 -0
- package/dist/shell/SettingsPageKeys.d.ts +42 -0
- package/dist/shell/SettingsPageKeys.js +70 -0
- package/dist/shell/SettingsSubsection.d.ts +34 -0
- package/dist/shell/SettingsSubsection.js +30 -0
- package/dist/shell/SignIn.d.ts +61 -0
- package/dist/shell/SignIn.js +36 -0
- package/dist/shell/ThemePicker.d.ts +74 -0
- package/dist/shell/ThemePicker.js +96 -0
- package/dist/shell/ThemePicker.logic.d.ts +13 -0
- package/dist/shell/ThemePicker.logic.js +26 -0
- package/dist/shell/TitleBar.d.ts +44 -0
- package/dist/shell/TitleBar.js +23 -0
- package/dist/shell/UpdatePill.d.ts +26 -0
- package/dist/shell/UpdatePill.js +17 -0
- package/dist/shell/Updates.d.ts +99 -0
- package/dist/shell/Updates.js +72 -0
- package/dist/shell/index.d.ts +27 -0
- package/dist/shell/index.js +27 -0
- package/dist/shell/shell.css +243 -4
- package/dist/styles.css +5 -0
- package/dist/surface/Card.d.ts +18 -0
- package/dist/surface/Card.js +10 -0
- package/dist/surface/Notice.d.ts +45 -0
- package/dist/surface/Notice.js +27 -0
- package/dist/surface/index.d.ts +5 -3
- package/dist/surface/index.js +5 -3
- package/dist/themes/catalog.js +4 -0
- package/dist/themes/themes.generated.css +245 -0
- package/dist/themes/tokens.d.ts +17 -5
- package/dist/themes/tokens.js +67 -1
- package/dist/tokens.css +11 -0
- package/dist/viewer/AudioView.d.ts +13 -0
- package/dist/viewer/AudioView.js +25 -0
- package/dist/viewer/HexJump.logic.d.ts +16 -0
- package/dist/viewer/HexJump.logic.js +27 -0
- package/dist/viewer/HexRows.logic.d.ts +55 -0
- package/dist/viewer/HexRows.logic.js +84 -0
- package/dist/viewer/HexSelection.logic.d.ts +57 -0
- package/dist/viewer/HexSelection.logic.js +68 -0
- package/dist/viewer/HexView.d.ts +10 -0
- package/dist/viewer/HexView.js +291 -0
- package/dist/viewer/HexViewport.logic.d.ts +61 -0
- package/dist/viewer/HexViewport.logic.js +79 -0
- package/dist/viewer/ImageView.d.ts +19 -0
- package/dist/viewer/ImageView.js +230 -0
- package/dist/viewer/ImageZoom.logic.d.ts +65 -0
- package/dist/viewer/ImageZoom.logic.js +119 -0
- package/dist/viewer/LatestRead.logic.d.ts +27 -0
- package/dist/viewer/LatestRead.logic.js +55 -0
- package/dist/viewer/MediaControls.d.ts +25 -0
- package/dist/viewer/MediaControls.js +183 -0
- package/dist/viewer/MediaPlayer.logic.d.ts +53 -0
- package/dist/viewer/MediaPlayer.logic.js +68 -0
- package/dist/viewer/PdfView.d.ts +23 -0
- package/dist/viewer/PdfView.js +25 -0
- package/dist/viewer/QuietFocus.d.ts +19 -0
- package/dist/viewer/QuietFocus.js +27 -0
- package/dist/viewer/ScaledScroll.logic.d.ts +30 -0
- package/dist/viewer/ScaledScroll.logic.js +28 -0
- package/dist/viewer/ScaledScrollbar.d.ts +19 -0
- package/dist/viewer/ScaledScrollbar.js +71 -0
- package/dist/viewer/SyntaxHighlighter.d.ts +29 -0
- package/dist/viewer/SyntaxHighlighter.js +144 -0
- package/dist/viewer/SyntaxLanguage.logic.d.ts +110 -0
- package/dist/viewer/SyntaxLanguage.logic.js +283 -0
- package/dist/viewer/SyntaxService.d.ts +75 -0
- package/dist/viewer/SyntaxService.js +49 -0
- package/dist/viewer/SyntaxSession.logic.d.ts +78 -0
- package/dist/viewer/SyntaxSession.logic.js +221 -0
- package/dist/viewer/SyntaxWorker.d.ts +6 -0
- package/dist/viewer/SyntaxWorker.js +8 -0
- package/dist/viewer/TextEncoding.logic.d.ts +35 -0
- package/dist/viewer/TextEncoding.logic.js +58 -0
- package/dist/viewer/TextRows.logic.d.ts +86 -0
- package/dist/viewer/TextRows.logic.js +241 -0
- package/dist/viewer/TextSelection.logic.d.ts +97 -0
- package/dist/viewer/TextSelection.logic.js +139 -0
- package/dist/viewer/TextView.d.ts +13 -0
- package/dist/viewer/TextView.js +448 -0
- package/dist/viewer/TextViewport.logic.d.ts +70 -0
- package/dist/viewer/TextViewport.logic.js +116 -0
- package/dist/viewer/VideoView.d.ts +18 -0
- package/dist/viewer/VideoView.js +53 -0
- package/dist/viewer/ViewerClipboard.d.ts +30 -0
- package/dist/viewer/ViewerClipboard.js +70 -0
- package/dist/viewer/ViewerFrame.logic.d.ts +61 -0
- package/dist/viewer/ViewerFrame.logic.js +130 -0
- package/dist/viewer/ViewerKeys.d.ts +16 -0
- package/dist/viewer/ViewerKeys.js +14 -0
- package/dist/viewer/ViewerScope.d.ts +12 -0
- package/dist/viewer/ViewerScope.js +22 -0
- package/dist/viewer/ViewerViews.d.ts +28 -0
- package/dist/viewer/ViewerViews.js +22 -0
- package/dist/viewer/ViewerWindow.d.ts +53 -0
- package/dist/viewer/ViewerWindow.js +219 -0
- package/dist/viewer/index.d.ts +12 -0
- package/dist/viewer/index.js +12 -0
- package/dist/viewer/types.d.ts +151 -0
- package/dist/viewer/types.js +26 -0
- package/dist/viewer/useElementHeight.d.ts +6 -0
- package/dist/viewer/useElementHeight.js +22 -0
- package/dist/viewer/useElementSize.d.ts +7 -0
- package/dist/viewer/useElementSize.js +24 -0
- package/dist/viewer/useEscapeToClear.d.ts +8 -0
- package/dist/viewer/useEscapeToClear.js +26 -0
- package/dist/viewer/useHexDocument.d.ts +41 -0
- package/dist/viewer/useHexDocument.js +144 -0
- package/dist/viewer/useMediaElement.d.ts +10 -0
- package/dist/viewer/useMediaElement.js +24 -0
- package/dist/viewer/useSyntaxHighlight.d.ts +24 -0
- package/dist/viewer/useSyntaxHighlight.js +133 -0
- package/dist/viewer/useTextDocument.d.ts +66 -0
- package/dist/viewer/useTextDocument.js +245 -0
- package/dist/viewer/viewer.css +645 -0
- package/package.json +7 -1
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shiki bound to the text view: the shipped grammars, each loaded only when a
|
|
3
|
+
* file needs it, the theme, and the line tokenizer the syntax session runs.
|
|
4
|
+
*
|
|
5
|
+
* Shiki runs on its JavaScript regex engine, so highlighting needs no
|
|
6
|
+
* WebAssembly and the viewer's CSP grants none. `SyntaxHighlighter.test.ts`
|
|
7
|
+
* compiles every pattern of every grammar here with that engine.
|
|
8
|
+
*
|
|
9
|
+
* The theme is Catppuccin Mocha, the app's own palette. Its colors become
|
|
10
|
+
* the palette's role names (`--syntax-mauve`, `--syntax-peach`), which every
|
|
11
|
+
* theme of the catalog sets (`@bigpixelrocket/uiok/themes`), so a token
|
|
12
|
+
* follows the window's theme without being tokenized again. A line's tokens cover
|
|
13
|
+
* only its styled text; the rest draws in the text's own color.
|
|
14
|
+
*/
|
|
15
|
+
import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript';
|
|
16
|
+
import { createShikiPrimitiveAsync } from '@shikijs/primitive';
|
|
17
|
+
import { EncodedTokenMetadata, INITIAL } from '@shikijs/primitive/textmate';
|
|
18
|
+
/** Each shipped grammar, as a chunk of its own that loads on first use. */
|
|
19
|
+
export const SYNTAX_GRAMMARS = {
|
|
20
|
+
apache: () => import('@shikijs/langs/apache'),
|
|
21
|
+
blade: () => import('@shikijs/langs/blade'),
|
|
22
|
+
c: () => import('@shikijs/langs/c'),
|
|
23
|
+
cpp: () => import('@shikijs/langs/cpp'),
|
|
24
|
+
css: () => import('@shikijs/langs/css'),
|
|
25
|
+
csv: () => import('@shikijs/langs/csv'),
|
|
26
|
+
diff: () => import('@shikijs/langs/diff'),
|
|
27
|
+
docker: () => import('@shikijs/langs/docker'),
|
|
28
|
+
dotenv: () => import('@shikijs/langs/dotenv'),
|
|
29
|
+
fish: () => import('@shikijs/langs/fish'),
|
|
30
|
+
go: () => import('@shikijs/langs/go'),
|
|
31
|
+
html: () => import('@shikijs/langs/html'),
|
|
32
|
+
ini: () => import('@shikijs/langs/ini'),
|
|
33
|
+
java: () => import('@shikijs/langs/java'),
|
|
34
|
+
javascript: () => import('@shikijs/langs/javascript'),
|
|
35
|
+
json: () => import('@shikijs/langs/json'),
|
|
36
|
+
jsonc: () => import('@shikijs/langs/jsonc'),
|
|
37
|
+
jsx: () => import('@shikijs/langs/jsx'),
|
|
38
|
+
kotlin: () => import('@shikijs/langs/kotlin'),
|
|
39
|
+
less: () => import('@shikijs/langs/less'),
|
|
40
|
+
lua: () => import('@shikijs/langs/lua'),
|
|
41
|
+
make: () => import('@shikijs/langs/make'),
|
|
42
|
+
markdown: () => import('@shikijs/langs/markdown'),
|
|
43
|
+
nginx: () => import('@shikijs/langs/nginx'),
|
|
44
|
+
'objective-c': () => import('@shikijs/langs/objective-c'),
|
|
45
|
+
perl: () => import('@shikijs/langs/perl'),
|
|
46
|
+
php: () => import('@shikijs/langs/php'),
|
|
47
|
+
python: () => import('@shikijs/langs/python'),
|
|
48
|
+
ruby: () => import('@shikijs/langs/ruby'),
|
|
49
|
+
rust: () => import('@shikijs/langs/rust'),
|
|
50
|
+
scss: () => import('@shikijs/langs/scss'),
|
|
51
|
+
shellscript: () => import('@shikijs/langs/shellscript'),
|
|
52
|
+
sql: () => import('@shikijs/langs/sql'),
|
|
53
|
+
'ssh-config': () => import('@shikijs/langs/ssh-config'),
|
|
54
|
+
swift: () => import('@shikijs/langs/swift'),
|
|
55
|
+
toml: () => import('@shikijs/langs/toml'),
|
|
56
|
+
tsx: () => import('@shikijs/langs/tsx'),
|
|
57
|
+
typescript: () => import('@shikijs/langs/typescript'),
|
|
58
|
+
vue: () => import('@shikijs/langs/vue'),
|
|
59
|
+
xml: () => import('@shikijs/langs/xml'),
|
|
60
|
+
yaml: () => import('@shikijs/langs/yaml')
|
|
61
|
+
};
|
|
62
|
+
/** The theme tokens are drawn from; its colors are mapped to palette roles below. */
|
|
63
|
+
const THEME = 'catppuccin-mocha';
|
|
64
|
+
/** Catppuccin Mocha's colors, as the theme spells them, by palette role. */
|
|
65
|
+
const MOCHA_ROLES = {
|
|
66
|
+
'#f5e0dc': 'rosewater',
|
|
67
|
+
'#f2cdcd': 'flamingo',
|
|
68
|
+
'#f5c2e7': 'pink',
|
|
69
|
+
'#cba6f7': 'mauve',
|
|
70
|
+
'#f38ba8': 'red',
|
|
71
|
+
'#eba0ac': 'maroon',
|
|
72
|
+
'#fab387': 'peach',
|
|
73
|
+
'#f9e2af': 'yellow',
|
|
74
|
+
'#a6e3a1': 'green',
|
|
75
|
+
'#94e2d5': 'teal',
|
|
76
|
+
'#89dceb': 'sky',
|
|
77
|
+
'#74c7ec': 'sapphire',
|
|
78
|
+
'#89b4fa': 'blue',
|
|
79
|
+
'#b4befe': 'lavender',
|
|
80
|
+
'#a6adc8': 'subtext0',
|
|
81
|
+
'#9399b2': 'overlay2'
|
|
82
|
+
};
|
|
83
|
+
/** Shiki's font style bits (TextMate's `FontStyle`). */
|
|
84
|
+
const ITALIC = 1;
|
|
85
|
+
const BOLD = 2;
|
|
86
|
+
const STRIKETHROUGH = 8;
|
|
87
|
+
/** The style for a theme color and font style; the text's own color stays unset. */
|
|
88
|
+
function styleFor(color, fontStyle) {
|
|
89
|
+
const role = color === undefined ? undefined : MOCHA_ROLES[color.toLowerCase()];
|
|
90
|
+
return {
|
|
91
|
+
color: role === undefined ? null : `var(--syntax-${role})`,
|
|
92
|
+
italic: (fontStyle & ITALIC) !== 0,
|
|
93
|
+
bold: (fontStyle & BOLD) !== 0,
|
|
94
|
+
strikethrough: (fontStyle & STRIKETHROUGH) !== 0
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** How long one line may tokenize before the rest of it shows plain, as Shiki's own limit. */
|
|
98
|
+
const LINE_TIME_LIMIT_MS = 500;
|
|
99
|
+
/**
|
|
100
|
+
* Loads `language`'s grammar (and the grammars it embeds) and the theme,
|
|
101
|
+
* and returns a tokenizer over them. Rejects when a chunk fails to load.
|
|
102
|
+
*/
|
|
103
|
+
export async function loadLineTokenizer(language) {
|
|
104
|
+
const shiki = await createShikiPrimitiveAsync({
|
|
105
|
+
engine: createJavaScriptRegexEngine(),
|
|
106
|
+
themes: [import('@shikijs/themes/catppuccin-mocha')],
|
|
107
|
+
langs: [SYNTAX_GRAMMARS[language]()]
|
|
108
|
+
});
|
|
109
|
+
const { colorMap } = shiki.setTheme(THEME);
|
|
110
|
+
const grammar = shiki.getLanguage(language);
|
|
111
|
+
// One style per theme color and font style, at the index a token's
|
|
112
|
+
// metadata encodes: foreground id times 16, plus the font style bits.
|
|
113
|
+
const styles = Array.from({ length: colorMap.length * 16 }, (_, index) => styleFor(colorMap[Math.floor(index / 16)], index % 16));
|
|
114
|
+
const styleOf = (metadata) => EncodedTokenMetadata.getForeground(metadata) * 16 + EncodedTokenMetadata.getFontStyle(metadata);
|
|
115
|
+
const plain = (style) => {
|
|
116
|
+
const { color, italic, bold, strikethrough } = styles[style] ?? styleFor(undefined, 0);
|
|
117
|
+
return color === null && !italic && !bold && !strikethrough;
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
initial: INITIAL,
|
|
121
|
+
styles,
|
|
122
|
+
tokenizeLine: (text, state) => {
|
|
123
|
+
const result = grammar.tokenizeLine2(text, state, LINE_TIME_LIMIT_MS);
|
|
124
|
+
const tokens = [];
|
|
125
|
+
const count = result.tokens.length / 2;
|
|
126
|
+
for (let index = 0; index < count; index += 1) {
|
|
127
|
+
const start = result.tokens[2 * index] ?? 0;
|
|
128
|
+
const end = index + 1 < count ? (result.tokens[2 * index + 2] ?? text.length) : text.length;
|
|
129
|
+
const style = styleOf(result.tokens[2 * index + 1] ?? 0);
|
|
130
|
+
const previous = tokens.at(-1);
|
|
131
|
+
// Text in the text's own style needs no token.
|
|
132
|
+
if (end <= start || plain(style))
|
|
133
|
+
continue;
|
|
134
|
+
if (previous !== undefined && previous.style === style && previous.end === start) {
|
|
135
|
+
tokens[tokens.length - 1] = { start: previous.start, end, style };
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
tokens.push({ start, end, style });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return { tokens, state: result.ruleStack };
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which grammar highlights a file, and whether it is highlighted at all. The
|
|
3
|
+
* language comes from the file's name (a known name such as `Dockerfile`,
|
|
4
|
+
* then its extension) and, failing that, from a `#!` line naming the
|
|
5
|
+
* interpreter. Highlighting steps down to plain text past a file size, and a
|
|
6
|
+
* line steps down past a length; both numbers were measured on real files
|
|
7
|
+
* (see the constants), and the view says why whenever it steps down.
|
|
8
|
+
*/
|
|
9
|
+
import type { ViewerContentKind } from './types.js';
|
|
10
|
+
import type { ViewerLabels } from '../labels/viewer.js';
|
|
11
|
+
/**
|
|
12
|
+
* The grammars the viewer ships, by Shiki's id, with the name the status bar
|
|
13
|
+
* shows, a proper name left untranslated. `SyntaxHighlighter.ts` loads each
|
|
14
|
+
* lazily.
|
|
15
|
+
*/
|
|
16
|
+
export declare const SYNTAX_LANGUAGES: {
|
|
17
|
+
readonly apache: "Apache";
|
|
18
|
+
readonly blade: "Blade";
|
|
19
|
+
readonly c: "C";
|
|
20
|
+
readonly cpp: "C++";
|
|
21
|
+
readonly css: "CSS";
|
|
22
|
+
readonly csv: "CSV";
|
|
23
|
+
readonly diff: "Diff";
|
|
24
|
+
readonly docker: "Dockerfile";
|
|
25
|
+
readonly dotenv: "dotenv";
|
|
26
|
+
readonly fish: "Fish";
|
|
27
|
+
readonly go: "Go";
|
|
28
|
+
readonly html: "HTML";
|
|
29
|
+
readonly ini: "INI";
|
|
30
|
+
readonly java: "Java";
|
|
31
|
+
readonly javascript: "JavaScript";
|
|
32
|
+
readonly json: "JSON";
|
|
33
|
+
readonly jsonc: "JSON with Comments";
|
|
34
|
+
readonly jsx: "JSX";
|
|
35
|
+
readonly kotlin: "Kotlin";
|
|
36
|
+
readonly less: "Less";
|
|
37
|
+
readonly lua: "Lua";
|
|
38
|
+
readonly make: "Makefile";
|
|
39
|
+
readonly markdown: "Markdown";
|
|
40
|
+
readonly nginx: "nginx";
|
|
41
|
+
readonly 'objective-c': "Objective-C";
|
|
42
|
+
readonly perl: "Perl";
|
|
43
|
+
readonly php: "PHP";
|
|
44
|
+
readonly python: "Python";
|
|
45
|
+
readonly ruby: "Ruby";
|
|
46
|
+
readonly rust: "Rust";
|
|
47
|
+
readonly scss: "SCSS";
|
|
48
|
+
readonly shellscript: "Shell";
|
|
49
|
+
readonly sql: "SQL";
|
|
50
|
+
readonly 'ssh-config': "SSH Config";
|
|
51
|
+
readonly swift: "Swift";
|
|
52
|
+
readonly toml: "TOML";
|
|
53
|
+
readonly tsx: "TSX";
|
|
54
|
+
readonly typescript: "TypeScript";
|
|
55
|
+
readonly vue: "Vue";
|
|
56
|
+
readonly xml: "XML";
|
|
57
|
+
readonly yaml: "YAML";
|
|
58
|
+
};
|
|
59
|
+
/** A shipped grammar's id. */
|
|
60
|
+
export type SyntaxLanguageId = keyof typeof SYNTAX_LANGUAGES;
|
|
61
|
+
/**
|
|
62
|
+
* Files past this size open as plain text. Highlighting costs the same per
|
|
63
|
+
* screen at any size, since only the rows in view are tokenized, so this is
|
|
64
|
+
* no speed limit: it keeps generated data plain. Measured on this Mac's
|
|
65
|
+
* sources: the largest hand-written ones (sqlite3.c at 9.0 MB, typescript.js
|
|
66
|
+
* at 9.1 MB) stay highlighted, while generated tables (PHP's fileinfo
|
|
67
|
+
* `data_file.c`, 16.9 to 48 MB of byte arrays) and logs step down.
|
|
68
|
+
*/
|
|
69
|
+
export declare const HIGHLIGHT_MAX_FILE_BYTES: number;
|
|
70
|
+
/**
|
|
71
|
+
* Lines longer than this show as plain text. Tokenizing a line costs more
|
|
72
|
+
* than its length: on five minified bundles (tldts, ajv, Testing Library,
|
|
73
|
+
* Go's webcomponents, yt-dlp's solver), a 4 KB prefix of the longest line
|
|
74
|
+
* took 8 to 43 ms with the JavaScript regex engine, 8 KB took up to 103 ms,
|
|
75
|
+
* and 20 KB up to 375 ms. 4 KB keeps every line under 50 ms.
|
|
76
|
+
*/
|
|
77
|
+
export declare const HIGHLIGHT_MAX_LINE_BYTES: number;
|
|
78
|
+
/**
|
|
79
|
+
* The language a `#!` line names: the interpreter's base name, or the
|
|
80
|
+
* command `env` runs (past its options and variable assignments), with a
|
|
81
|
+
* version suffix dropped (`python3.12` is Python). Null for any other line.
|
|
82
|
+
*/
|
|
83
|
+
export declare function languageFromShebang(firstLine: string): SyntaxLanguageId | null;
|
|
84
|
+
/**
|
|
85
|
+
* The language of a file named `name` whose first line is `firstLine`: a
|
|
86
|
+
* known file name first (`Dockerfile`, `.zshrc`, `Dockerfile.dev`,
|
|
87
|
+
* `.env.local`), then the extension, then a `#!` line. Null when none names
|
|
88
|
+
* a shipped grammar.
|
|
89
|
+
*/
|
|
90
|
+
export declare function languageFor(name: string, firstLine: string): SyntaxLanguageId | null;
|
|
91
|
+
/** Whether a file is highlighted, and if not, why. */
|
|
92
|
+
export type HighlightPlan = {
|
|
93
|
+
readonly kind: 'none';
|
|
94
|
+
} | {
|
|
95
|
+
readonly kind: 'highlight';
|
|
96
|
+
readonly language: SyntaxLanguageId;
|
|
97
|
+
} | {
|
|
98
|
+
readonly kind: 'too-large';
|
|
99
|
+
readonly language: SyntaxLanguageId;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* The plan for a file: no known language, highlighted, or stepped down for
|
|
103
|
+
* its size. A file detected as SVG (`kind`) is XML whatever its name says,
|
|
104
|
+
* so its source highlights even as `logo` or `logo.js`.
|
|
105
|
+
*/
|
|
106
|
+
export declare function highlightPlan(name: string, sizeBytes: number, firstLine: string, kind: ViewerContentKind): HighlightPlan;
|
|
107
|
+
/** Why a highlighted file shows some of its text plain. */
|
|
108
|
+
export type HighlightNote = 'too-large' | 'long-lines' | 'unavailable' | null;
|
|
109
|
+
/** The status bar's word on highlighting: the language, and why it stepped down if it did. */
|
|
110
|
+
export declare function highlightStatus(language: SyntaxLanguageId, note: HighlightNote, labels: ViewerLabels): string;
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which grammar highlights a file, and whether it is highlighted at all. The
|
|
3
|
+
* language comes from the file's name (a known name such as `Dockerfile`,
|
|
4
|
+
* then its extension) and, failing that, from a `#!` line naming the
|
|
5
|
+
* interpreter. Highlighting steps down to plain text past a file size, and a
|
|
6
|
+
* line steps down past a length; both numbers were measured on real files
|
|
7
|
+
* (see the constants), and the view says why whenever it steps down.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* The grammars the viewer ships, by Shiki's id, with the name the status bar
|
|
11
|
+
* shows, a proper name left untranslated. `SyntaxHighlighter.ts` loads each
|
|
12
|
+
* lazily.
|
|
13
|
+
*/
|
|
14
|
+
export const SYNTAX_LANGUAGES = {
|
|
15
|
+
apache: 'Apache',
|
|
16
|
+
blade: 'Blade',
|
|
17
|
+
c: 'C',
|
|
18
|
+
cpp: 'C++',
|
|
19
|
+
css: 'CSS',
|
|
20
|
+
csv: 'CSV',
|
|
21
|
+
diff: 'Diff',
|
|
22
|
+
docker: 'Dockerfile',
|
|
23
|
+
dotenv: 'dotenv',
|
|
24
|
+
fish: 'Fish',
|
|
25
|
+
go: 'Go',
|
|
26
|
+
html: 'HTML',
|
|
27
|
+
ini: 'INI',
|
|
28
|
+
java: 'Java',
|
|
29
|
+
javascript: 'JavaScript',
|
|
30
|
+
json: 'JSON',
|
|
31
|
+
jsonc: 'JSON with Comments',
|
|
32
|
+
jsx: 'JSX',
|
|
33
|
+
kotlin: 'Kotlin',
|
|
34
|
+
less: 'Less',
|
|
35
|
+
lua: 'Lua',
|
|
36
|
+
make: 'Makefile',
|
|
37
|
+
markdown: 'Markdown',
|
|
38
|
+
nginx: 'nginx',
|
|
39
|
+
'objective-c': 'Objective-C',
|
|
40
|
+
perl: 'Perl',
|
|
41
|
+
php: 'PHP',
|
|
42
|
+
python: 'Python',
|
|
43
|
+
ruby: 'Ruby',
|
|
44
|
+
rust: 'Rust',
|
|
45
|
+
scss: 'SCSS',
|
|
46
|
+
shellscript: 'Shell',
|
|
47
|
+
sql: 'SQL',
|
|
48
|
+
'ssh-config': 'SSH Config',
|
|
49
|
+
swift: 'Swift',
|
|
50
|
+
toml: 'TOML',
|
|
51
|
+
tsx: 'TSX',
|
|
52
|
+
typescript: 'TypeScript',
|
|
53
|
+
vue: 'Vue',
|
|
54
|
+
xml: 'XML',
|
|
55
|
+
yaml: 'YAML'
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Files past this size open as plain text. Highlighting costs the same per
|
|
59
|
+
* screen at any size, since only the rows in view are tokenized, so this is
|
|
60
|
+
* no speed limit: it keeps generated data plain. Measured on this Mac's
|
|
61
|
+
* sources: the largest hand-written ones (sqlite3.c at 9.0 MB, typescript.js
|
|
62
|
+
* at 9.1 MB) stay highlighted, while generated tables (PHP's fileinfo
|
|
63
|
+
* `data_file.c`, 16.9 to 48 MB of byte arrays) and logs step down.
|
|
64
|
+
*/
|
|
65
|
+
export const HIGHLIGHT_MAX_FILE_BYTES = 16 * 1024 * 1024;
|
|
66
|
+
/**
|
|
67
|
+
* Lines longer than this show as plain text. Tokenizing a line costs more
|
|
68
|
+
* than its length: on five minified bundles (tldts, ajv, Testing Library,
|
|
69
|
+
* Go's webcomponents, yt-dlp's solver), a 4 KB prefix of the longest line
|
|
70
|
+
* took 8 to 43 ms with the JavaScript regex engine, 8 KB took up to 103 ms,
|
|
71
|
+
* and 20 KB up to 375 ms. 4 KB keeps every line under 50 ms.
|
|
72
|
+
*/
|
|
73
|
+
export const HIGHLIGHT_MAX_LINE_BYTES = 4 * 1024;
|
|
74
|
+
/** Names that mean a language whatever their extension, lowercased. */
|
|
75
|
+
const FILE_NAMES = {
|
|
76
|
+
dockerfile: 'docker',
|
|
77
|
+
containerfile: 'docker',
|
|
78
|
+
makefile: 'make',
|
|
79
|
+
gnumakefile: 'make',
|
|
80
|
+
'.zshrc': 'shellscript',
|
|
81
|
+
'.zshenv': 'shellscript',
|
|
82
|
+
'.zprofile': 'shellscript',
|
|
83
|
+
'.zlogin': 'shellscript',
|
|
84
|
+
'.zlogout': 'shellscript',
|
|
85
|
+
'.bashrc': 'shellscript',
|
|
86
|
+
'.bash_profile': 'shellscript',
|
|
87
|
+
'.bash_login': 'shellscript',
|
|
88
|
+
'.bash_logout': 'shellscript',
|
|
89
|
+
'.bash_aliases': 'shellscript',
|
|
90
|
+
'.profile': 'shellscript',
|
|
91
|
+
'.kshrc': 'shellscript',
|
|
92
|
+
'.env': 'dotenv',
|
|
93
|
+
'.gitconfig': 'ini',
|
|
94
|
+
'.editorconfig': 'ini',
|
|
95
|
+
'.npmrc': 'ini',
|
|
96
|
+
'.htaccess': 'apache',
|
|
97
|
+
'httpd.conf': 'apache',
|
|
98
|
+
'apache2.conf': 'apache',
|
|
99
|
+
'nginx.conf': 'nginx',
|
|
100
|
+
ssh_config: 'ssh-config',
|
|
101
|
+
sshd_config: 'ssh-config',
|
|
102
|
+
'tsconfig.json': 'jsonc',
|
|
103
|
+
'jsconfig.json': 'jsonc',
|
|
104
|
+
gemfile: 'ruby',
|
|
105
|
+
rakefile: 'ruby',
|
|
106
|
+
podfile: 'ruby',
|
|
107
|
+
brewfile: 'ruby',
|
|
108
|
+
vagrantfile: 'ruby'
|
|
109
|
+
};
|
|
110
|
+
/** Extensions, lowercased and without the dot. */
|
|
111
|
+
const EXTENSIONS = {
|
|
112
|
+
html: 'html',
|
|
113
|
+
htm: 'html',
|
|
114
|
+
xhtml: 'html',
|
|
115
|
+
css: 'css',
|
|
116
|
+
scss: 'scss',
|
|
117
|
+
less: 'less',
|
|
118
|
+
js: 'javascript',
|
|
119
|
+
mjs: 'javascript',
|
|
120
|
+
cjs: 'javascript',
|
|
121
|
+
jsx: 'jsx',
|
|
122
|
+
ts: 'typescript',
|
|
123
|
+
mts: 'typescript',
|
|
124
|
+
cts: 'typescript',
|
|
125
|
+
tsx: 'tsx',
|
|
126
|
+
vue: 'vue',
|
|
127
|
+
php: 'php',
|
|
128
|
+
phtml: 'php',
|
|
129
|
+
json: 'json',
|
|
130
|
+
jsonl: 'json',
|
|
131
|
+
ndjson: 'json',
|
|
132
|
+
webmanifest: 'json',
|
|
133
|
+
jsonc: 'jsonc',
|
|
134
|
+
yml: 'yaml',
|
|
135
|
+
yaml: 'yaml',
|
|
136
|
+
toml: 'toml',
|
|
137
|
+
ini: 'ini',
|
|
138
|
+
cfg: 'ini',
|
|
139
|
+
properties: 'ini',
|
|
140
|
+
xml: 'xml',
|
|
141
|
+
plist: 'xml',
|
|
142
|
+
svg: 'xml',
|
|
143
|
+
xsd: 'xml',
|
|
144
|
+
xsl: 'xml',
|
|
145
|
+
xslt: 'xml',
|
|
146
|
+
env: 'dotenv',
|
|
147
|
+
nginx: 'nginx',
|
|
148
|
+
csv: 'csv',
|
|
149
|
+
diff: 'diff',
|
|
150
|
+
patch: 'diff',
|
|
151
|
+
md: 'markdown',
|
|
152
|
+
markdown: 'markdown',
|
|
153
|
+
mdown: 'markdown',
|
|
154
|
+
mkd: 'markdown',
|
|
155
|
+
sql: 'sql',
|
|
156
|
+
dockerfile: 'docker',
|
|
157
|
+
mk: 'make',
|
|
158
|
+
mak: 'make',
|
|
159
|
+
sh: 'shellscript',
|
|
160
|
+
bash: 'shellscript',
|
|
161
|
+
zsh: 'shellscript',
|
|
162
|
+
ksh: 'shellscript',
|
|
163
|
+
command: 'shellscript',
|
|
164
|
+
fish: 'fish',
|
|
165
|
+
c: 'c',
|
|
166
|
+
h: 'c',
|
|
167
|
+
cc: 'cpp',
|
|
168
|
+
cpp: 'cpp',
|
|
169
|
+
cxx: 'cpp',
|
|
170
|
+
'c++': 'cpp',
|
|
171
|
+
hh: 'cpp',
|
|
172
|
+
hpp: 'cpp',
|
|
173
|
+
hxx: 'cpp',
|
|
174
|
+
'h++': 'cpp',
|
|
175
|
+
ipp: 'cpp',
|
|
176
|
+
m: 'objective-c',
|
|
177
|
+
rs: 'rust',
|
|
178
|
+
go: 'go',
|
|
179
|
+
swift: 'swift',
|
|
180
|
+
java: 'java',
|
|
181
|
+
kt: 'kotlin',
|
|
182
|
+
kts: 'kotlin',
|
|
183
|
+
py: 'python',
|
|
184
|
+
pyi: 'python',
|
|
185
|
+
pyw: 'python',
|
|
186
|
+
rb: 'ruby',
|
|
187
|
+
rake: 'ruby',
|
|
188
|
+
gemspec: 'ruby',
|
|
189
|
+
ru: 'ruby',
|
|
190
|
+
pl: 'perl',
|
|
191
|
+
pm: 'perl',
|
|
192
|
+
lua: 'lua'
|
|
193
|
+
};
|
|
194
|
+
/** Interpreters a `#!` line names, without a version suffix. */
|
|
195
|
+
const INTERPRETERS = {
|
|
196
|
+
sh: 'shellscript',
|
|
197
|
+
bash: 'shellscript',
|
|
198
|
+
zsh: 'shellscript',
|
|
199
|
+
ksh: 'shellscript',
|
|
200
|
+
mksh: 'shellscript',
|
|
201
|
+
dash: 'shellscript',
|
|
202
|
+
ash: 'shellscript',
|
|
203
|
+
fish: 'fish',
|
|
204
|
+
node: 'javascript',
|
|
205
|
+
nodejs: 'javascript',
|
|
206
|
+
bun: 'javascript',
|
|
207
|
+
deno: 'typescript',
|
|
208
|
+
'ts-node': 'typescript',
|
|
209
|
+
tsx: 'typescript',
|
|
210
|
+
python: 'python',
|
|
211
|
+
pypy: 'python',
|
|
212
|
+
ruby: 'ruby',
|
|
213
|
+
jruby: 'ruby',
|
|
214
|
+
perl: 'perl',
|
|
215
|
+
php: 'php',
|
|
216
|
+
lua: 'lua',
|
|
217
|
+
luajit: 'lua',
|
|
218
|
+
make: 'make',
|
|
219
|
+
gmake: 'make'
|
|
220
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* The language a `#!` line names: the interpreter's base name, or the
|
|
223
|
+
* command `env` runs (past its options and variable assignments), with a
|
|
224
|
+
* version suffix dropped (`python3.12` is Python). Null for any other line.
|
|
225
|
+
*/
|
|
226
|
+
export function languageFromShebang(firstLine) {
|
|
227
|
+
if (!firstLine.startsWith('#!'))
|
|
228
|
+
return null;
|
|
229
|
+
const words = firstLine.slice(2).trim().split(/\s+/u);
|
|
230
|
+
let command = words[0]?.split('/').at(-1) ?? '';
|
|
231
|
+
if (command === 'env') {
|
|
232
|
+
command = words.slice(1).find((word) => !word.startsWith('-') && !word.includes('=')) ?? '';
|
|
233
|
+
}
|
|
234
|
+
return INTERPRETERS[command.replace(/[\d.]+$/u, '')] ?? null;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* The language of a file named `name` whose first line is `firstLine`: a
|
|
238
|
+
* known file name first (`Dockerfile`, `.zshrc`, `Dockerfile.dev`,
|
|
239
|
+
* `.env.local`), then the extension, then a `#!` line. Null when none names
|
|
240
|
+
* a shipped grammar.
|
|
241
|
+
*/
|
|
242
|
+
export function languageFor(name, firstLine) {
|
|
243
|
+
const lower = name.toLowerCase();
|
|
244
|
+
const known = FILE_NAMES[lower];
|
|
245
|
+
if (known)
|
|
246
|
+
return known;
|
|
247
|
+
if (lower.endsWith('.blade.php'))
|
|
248
|
+
return 'blade';
|
|
249
|
+
if (lower.startsWith('dockerfile.') || lower.startsWith('containerfile.'))
|
|
250
|
+
return 'docker';
|
|
251
|
+
if (lower.startsWith('.env.'))
|
|
252
|
+
return 'dotenv';
|
|
253
|
+
const dot = lower.lastIndexOf('.');
|
|
254
|
+
const byExtension = dot > 0 ? EXTENSIONS[lower.slice(dot + 1)] : undefined;
|
|
255
|
+
return byExtension ?? languageFromShebang(firstLine);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* The plan for a file: no known language, highlighted, or stepped down for
|
|
259
|
+
* its size. A file detected as SVG (`kind`) is XML whatever its name says,
|
|
260
|
+
* so its source highlights even as `logo` or `logo.js`.
|
|
261
|
+
*/
|
|
262
|
+
export function highlightPlan(name, sizeBytes, firstLine, kind) {
|
|
263
|
+
const language = kind === 'svg' ? 'xml' : languageFor(name, firstLine);
|
|
264
|
+
if (language === null)
|
|
265
|
+
return { kind: 'none' };
|
|
266
|
+
if (sizeBytes > HIGHLIGHT_MAX_FILE_BYTES)
|
|
267
|
+
return { kind: 'too-large', language };
|
|
268
|
+
return { kind: 'highlight', language };
|
|
269
|
+
}
|
|
270
|
+
/** The status bar's word on highlighting: the language, and why it stepped down if it did. */
|
|
271
|
+
export function highlightStatus(language, note, labels) {
|
|
272
|
+
const name = SYNTAX_LANGUAGES[language];
|
|
273
|
+
switch (note) {
|
|
274
|
+
case 'too-large':
|
|
275
|
+
return labels.text.tooLarge(name, labels.size(HIGHLIGHT_MAX_FILE_BYTES));
|
|
276
|
+
case 'long-lines':
|
|
277
|
+
return labels.text.longLines(name, labels.size(HIGHLIGHT_MAX_LINE_BYTES));
|
|
278
|
+
case 'unavailable':
|
|
279
|
+
return labels.text.unavailable(name);
|
|
280
|
+
case null:
|
|
281
|
+
return name;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The text view's highlighting worker and the messages it takes. The worker
|
|
3
|
+
* cannot reach the viewer's reader, so the page keeps reading the file and
|
|
4
|
+
* hands the worker the chunk of bytes it holds, then asks for a screen of
|
|
5
|
+
* rows by their starting bytes; the worker answers with each row's tokens.
|
|
6
|
+
* Grammar states never leave the worker.
|
|
7
|
+
*/
|
|
8
|
+
import type { SyntaxLanguageId } from './SyntaxLanguage.logic.js';
|
|
9
|
+
import { type LineTokenizer, type RowHighlight, type SyntaxStyle } from './SyntaxSession.logic.js';
|
|
10
|
+
import type { TextLayout } from './TextEncoding.logic.js';
|
|
11
|
+
import type { RowStart } from './TextRows.logic.js';
|
|
12
|
+
/** What the page asks of the worker. */
|
|
13
|
+
type SyntaxRequest =
|
|
14
|
+
/** Load the grammar and theme; answered with `ready` or `failed`. */
|
|
15
|
+
{
|
|
16
|
+
readonly type: 'open';
|
|
17
|
+
readonly language: SyntaxLanguageId;
|
|
18
|
+
readonly maxLineBytes: number;
|
|
19
|
+
}
|
|
20
|
+
/** The bytes the page holds now, replacing the last chunk. */
|
|
21
|
+
| {
|
|
22
|
+
readonly type: 'chunk';
|
|
23
|
+
readonly offset: number;
|
|
24
|
+
readonly bytes: Uint8Array;
|
|
25
|
+
readonly eof: boolean;
|
|
26
|
+
readonly layout: TextLayout;
|
|
27
|
+
}
|
|
28
|
+
/** Highlight these rows of the last chunk; answered with `rows`. */
|
|
29
|
+
| {
|
|
30
|
+
readonly type: 'highlight';
|
|
31
|
+
readonly rows: readonly RowStart[];
|
|
32
|
+
};
|
|
33
|
+
/** What the worker answers. */
|
|
34
|
+
type SyntaxResponse = {
|
|
35
|
+
readonly type: 'ready';
|
|
36
|
+
readonly styles: readonly SyntaxStyle[];
|
|
37
|
+
} | {
|
|
38
|
+
readonly type: 'failed';
|
|
39
|
+
} | {
|
|
40
|
+
readonly type: 'rows';
|
|
41
|
+
readonly rows: readonly RowHighlight[];
|
|
42
|
+
};
|
|
43
|
+
/** The page's end of the worker. */
|
|
44
|
+
export interface SyntaxPort {
|
|
45
|
+
readonly postMessage: (message: SyntaxRequest, transfer?: Transferable[]) => void;
|
|
46
|
+
onmessage: ((event: MessageEvent<SyntaxResponse>) => void) | null;
|
|
47
|
+
onerror: ((event: Event) => void) | null;
|
|
48
|
+
readonly terminate: () => void;
|
|
49
|
+
}
|
|
50
|
+
/** The worker's end: where requests arrive and answers go. */
|
|
51
|
+
export interface SyntaxScope {
|
|
52
|
+
readonly postMessage: (message: SyntaxResponse) => void;
|
|
53
|
+
onmessage: ((event: MessageEvent<SyntaxRequest>) => void) | null;
|
|
54
|
+
}
|
|
55
|
+
/** Starts a highlighting worker; one per text view and file generation. */
|
|
56
|
+
export type SyntaxWorkerFactory = () => SyntaxPort;
|
|
57
|
+
/**
|
|
58
|
+
* Starts the viewer's own highlighting worker: Shiki on its JavaScript
|
|
59
|
+
* regex engine, loaded from the module beside this one, so a bundler emits
|
|
60
|
+
* the worker and each grammar as chunks of their own that load only when a
|
|
61
|
+
* text file in a known language opens.
|
|
62
|
+
*/
|
|
63
|
+
export declare const openSyntaxWorker: SyntaxWorkerFactory;
|
|
64
|
+
/**
|
|
65
|
+
* The worker factory the text view starts highlighting with; null turns
|
|
66
|
+
* highlighting off. `ViewerWindow` provides it from its `syntaxWorker` prop.
|
|
67
|
+
*/
|
|
68
|
+
export declare const SyntaxWorkerContext: import("react").Context<SyntaxWorkerFactory | null>;
|
|
69
|
+
/**
|
|
70
|
+
* Answers the page's requests in `scope`, tokenizing with what `load`
|
|
71
|
+
* returns for the language `open` names. Requests other than `open` wait
|
|
72
|
+
* for the page to have seen `ready`, so none arrive before a session exists.
|
|
73
|
+
*/
|
|
74
|
+
export declare function serveSyntax<State>(scope: SyntaxScope, load: (language: SyntaxLanguageId) => Promise<LineTokenizer<State>>): void;
|
|
75
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The text view's highlighting worker and the messages it takes. The worker
|
|
3
|
+
* cannot reach the viewer's reader, so the page keeps reading the file and
|
|
4
|
+
* hands the worker the chunk of bytes it holds, then asks for a screen of
|
|
5
|
+
* rows by their starting bytes; the worker answers with each row's tokens.
|
|
6
|
+
* Grammar states never leave the worker.
|
|
7
|
+
*/
|
|
8
|
+
import { createContext } from 'react';
|
|
9
|
+
import { createSyntaxSession } from './SyntaxSession.logic.js';
|
|
10
|
+
/**
|
|
11
|
+
* Starts the viewer's own highlighting worker: Shiki on its JavaScript
|
|
12
|
+
* regex engine, loaded from the module beside this one, so a bundler emits
|
|
13
|
+
* the worker and each grammar as chunks of their own that load only when a
|
|
14
|
+
* text file in a known language opens.
|
|
15
|
+
*/
|
|
16
|
+
export const openSyntaxWorker = () => new Worker(new URL('./SyntaxWorker.js', import.meta.url), {
|
|
17
|
+
type: 'module',
|
|
18
|
+
name: 'Syntax highlighting'
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* The worker factory the text view starts highlighting with; null turns
|
|
22
|
+
* highlighting off. `ViewerWindow` provides it from its `syntaxWorker` prop.
|
|
23
|
+
*/
|
|
24
|
+
export const SyntaxWorkerContext = createContext(openSyntaxWorker);
|
|
25
|
+
/**
|
|
26
|
+
* Answers the page's requests in `scope`, tokenizing with what `load`
|
|
27
|
+
* returns for the language `open` names. Requests other than `open` wait
|
|
28
|
+
* for the page to have seen `ready`, so none arrive before a session exists.
|
|
29
|
+
*/
|
|
30
|
+
export function serveSyntax(scope, load) {
|
|
31
|
+
let session = null;
|
|
32
|
+
scope.onmessage = ({ data }) => {
|
|
33
|
+
switch (data.type) {
|
|
34
|
+
case 'open':
|
|
35
|
+
load(data.language).then((tokenizer) => {
|
|
36
|
+
session = createSyntaxSession(tokenizer, data.maxLineBytes);
|
|
37
|
+
scope.postMessage({ type: 'ready', styles: tokenizer.styles });
|
|
38
|
+
}, () => scope.postMessage({ type: 'failed' }));
|
|
39
|
+
return;
|
|
40
|
+
case 'chunk':
|
|
41
|
+
session?.setChunk({ offset: data.offset, bytes: data.bytes, eof: data.eof }, data.layout);
|
|
42
|
+
return;
|
|
43
|
+
case 'highlight':
|
|
44
|
+
if (session !== null)
|
|
45
|
+
scope.postMessage({ type: 'rows', rows: session.highlight(data.rows) });
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|