@@ -6,11 +6,9 @@ import {
66 parseColor ,
77 type TokenStyleSpec ,
88} from "@acode.app/canvas-view" ;
9- import { HighlightStyle } from "@codemirror/language" ;
10- import { oneDark } from "@codemirror/theme-one-dark" ;
119import { tags as t } from "@lezer/highlight" ;
1210import { getRainbowBracketColors } from "cm/rainbowBrackets" ;
13- import { getThemeById , getThemeConfig , getThemeExtensions } from "cm/themes" ;
11+ import { getThemeById , getThemeConfig } from "cm/themes" ;
1412
1513/**
1614 * Maps Acode's editor settings and themes to @acode.app/canvas-view options.
@@ -70,175 +68,6 @@ function clampSetting(
7068 return Math . min ( Math . max ( Math . round ( number ) , min ) , max ) ;
7169}
7270
73- /**
74- * Find the HighlightStyle a theme passes to `syntaxHighlighting()`, by
75- * walking its extension tree (arrays, precedence wrappers, compartments,
76- * `facet.of(value)` providers). Returns null for themes that compute their
77- * highlighter (`themeType`) or don't have one.
78- */
79- export function findHighlightStyle (
80- extension : unknown ,
81- seen = new Set < unknown > ( ) ,
82- ) : HighlightStyle | null {
83- if ( ! extension || typeof extension !== "object" || seen . has ( extension ) ) {
84- return null ;
85- }
86- seen . add ( extension ) ;
87- if ( extension instanceof HighlightStyle ) return extension ;
88- if ( Array . isArray ( extension ) ) {
89- for ( const item of extension ) {
90- const found = findHighlightStyle ( item , seen ) ;
91- if ( found ) return found ;
92- }
93- return null ;
94- }
95- const node = extension as {
96- value ?: unknown ;
97- inner ?: unknown ;
98- extension ?: unknown ;
99- } ;
100- return (
101- findHighlightStyle ( node . value , seen ) ||
102- findHighlightStyle ( node . inner , seen ) ||
103- findHighlightStyle ( node . extension , seen )
104- ) ;
105- }
106-
107- /**
108- * Editor surface colors a theme's own `EditorView.theme` rules set, for
109- * the canvas renderer, which doesn't draw from CSS. Plugin themes are often
110- * registered with only background, foreground and syntax colors in their
111- * config, and style the active line, gutter and cursor in CSS.
112- * Parsed from the style modules' rule strings when the canvas theme is
113- * built (theme changes): no DOM is read, nothing runs per frame.
114- */
115- interface RuleColors {
116- activeLine ?: string ;
117- activeLineBorder ?: string ;
118- activeLineBorderWidth ?: number ;
119- activeLineGutter ?: string ;
120- gutterBackground ?: string ;
121- gutterForeground ?: string ;
122- gutterActiveForeground ?: string ;
123- selection ?: string ;
124- cursor ?: string ;
125- }
126-
127- function collectStyleRules (
128- extension : unknown ,
129- rules : string [ ] ,
130- seen = new Set < unknown > ( ) ,
131- ) : string [ ] {
132- if ( ! extension || typeof extension !== "object" || seen . has ( extension ) ) {
133- return rules ;
134- }
135- seen . add ( extension ) ;
136- if ( Array . isArray ( extension ) ) {
137- for ( const item of extension ) collectStyleRules ( item , rules , seen ) ;
138- return rules ;
139- }
140- const node = extension as {
141- getRules ?: ( ) => unknown ;
142- value ?: unknown ;
143- inner ?: unknown ;
144- extension ?: unknown ;
145- } ;
146- // A StyleModule (style-mod): one "selectors {declarations}" rule per line.
147- if ( typeof node . getRules === "function" ) {
148- const text = node . getRules ( ) ;
149- if ( typeof text === "string" ) rules . push ( ...text . split ( "\n" ) ) ;
150- return rules ;
151- }
152- collectStyleRules ( node . value , rules , seen ) ;
153- collectStyleRules ( node . inner , rules , seen ) ;
154- collectStyleRules ( node . extension , rules , seen ) ;
155- return rules ;
156- }
157-
158- const COLOR_TOKEN =
159- / (?: r g b a ? | h s l a ? | h w b | l a b | l c h | o k l a b | o k l c h | c o l o r ) \( [ ^ ) ] * \) | # [ 0 - 9 a - f ] { 3 , 8 } \b | \b (? ! i n s e t \b ) [ a - z ] + \b / i;
160-
161- /** A color in a CSS value ("2px solid #333" -> "#333"); null for var(). */
162- function colorIn ( value : string ) : string | null {
163- if ( / v a r \( / . test ( value ) ) return null ;
164- const m = COLOR_TOKEN . exec (
165- value . replace ( / \b (?: s o l i d | d a s h e d | d o t t e d | d o u b l e | n o n e ) \b / g, "" ) ,
166- ) ;
167- if (
168- ! m ||
169- / ^ (?: t r a n s p a r e n t | c u r r e n t c o l o r | i n h e r i t | i n i t i a l | u n s e t ) $ / i. test ( m [ 0 ] )
170- ) {
171- return m && / ^ t r a n s p a r e n t $ / i. test ( m [ 0 ] ) ? "transparent" : null ;
172- }
173- return m [ 0 ] ;
174- }
175-
176- /** An inset box-shadow without offset or blur as a frame (`0 0 0 2px #333 inset`). */
177- function insetFrame ( value : string ) : { color : string ; width : number } | null {
178- const first = value . split ( / , (? ! [ ^ ( ] * \) ) / ) [ 0 ] ;
179- if ( ! / \b i n s e t \b / . test ( first ) ) return null ;
180- const color = colorIn ( first . replace ( / \b i n s e t \b / , "" ) ) ;
181- const lengths = (
182- first . replace ( COLOR_TOKEN , " " ) . match ( / - ? \d * \. ? \d + (?: p x ) ? \b / g) ?? [ ]
183- ) . map ( Number . parseFloat ) ;
184- const [ x = 0 , y = 0 , blur = 0 , spread = 0 ] = lengths ;
185- if ( ! color || color === "transparent" || x || y || blur || ! ( spread > 0 ) ) {
186- return null ;
187- }
188- return { color, width : spread } ;
189- }
190-
191- export function themeRuleColors ( extension : unknown ) : RuleColors {
192- const found : RuleColors = { } ;
193- let focusedSelection : string | undefined ;
194- for ( const rule of collectStyleRules ( extension , [ ] ) ) {
195- const m = / ^ ( [ ^ { ] + ) \{ ( [ ^ } ] * ) \} \s * $ / . exec ( rule ) ;
196- if ( ! m ) continue ;
197- const decls = new Map < string , string > ( ) ;
198- for ( const decl of m [ 2 ] . split ( / ; (? ! [ ^ ( ] * \) ) / ) ) {
199- const i = decl . indexOf ( ":" ) ;
200- if ( i > 0 ) decls . set ( decl . slice ( 0 , i ) . trim ( ) , decl . slice ( i + 1 ) . trim ( ) ) ;
201- }
202- const bg = decls . get ( "background-color" ) ?? decls . get ( "background" ) ;
203- const bgColor = bg ? colorIn ( bg ) : null ;
204- for ( const sel of m [ 1 ] . split ( "," ) . map ( ( s ) => s . trim ( ) ) ) {
205- // Only the element itself, not states or descendants of it.
206- if ( / : (?: h o v e r | a c t i v e | f o c u s ) | : : / . test ( sel ) ) continue ;
207- if ( / \. c m - a c t i v e L i n e $ / . test ( sel ) ) {
208- if ( bgColor ) found . activeLine = bgColor ;
209- const frame = decls . has ( "box-shadow" )
210- ? insetFrame ( decls . get ( "box-shadow" ) as string )
211- : null ;
212- if ( frame ) {
213- found . activeLineBorder = frame . color ;
214- found . activeLineBorderWidth = frame . width ;
215- }
216- } else if ( / \. c m - g u t t e r s $ / . test ( sel ) ) {
217- if ( bgColor ) found . gutterBackground = bgColor ;
218- const color = decls . get ( "color" ) ;
219- if ( color && colorIn ( color ) )
220- found . gutterForeground = colorIn ( color ) as string ;
221- } else if ( / \. c m - a c t i v e L i n e G u t t e r $ / . test ( sel ) ) {
222- if ( bgColor ) found . activeLineGutter = bgColor ;
223- const color = decls . get ( "color" ) ;
224- if ( color && colorIn ( color ) )
225- found . gutterActiveForeground = colorIn ( color ) as string ;
226- } else if ( / \. c m - c u r s o r (?: - p r i m a r y ) ? $ / . test ( sel ) ) {
227- const border =
228- decls . get ( "border-left-color" ) ?? decls . get ( "border-left" ) ;
229- const color = border ? colorIn ( border ) : null ;
230- if ( color && color !== "transparent" ) found . cursor = color ;
231- } else if ( / \. c m - s e l e c t i o n B a c k g r o u n d $ / . test ( sel ) && bgColor ) {
232- // The focused rule is what a focused editor shows.
233- if ( / \. c m - f o c u s e d \b / . test ( sel ) ) focusedSelection = bgColor ;
234- else found . selection ??= bgColor ;
235- }
236- }
237- }
238- if ( focusedSelection ) found . selection = focusedSelection ;
239- return found ;
240- }
241-
24271/** Token colors from a theme config, for themes without a HighlightStyle. */
24372function highlightFromConfig ( config : ThemeConfig ) : TokenStyleSpec [ ] {
24473 const color = ( key : string ) =>
@@ -265,88 +94,57 @@ function highlightFromConfig(config: ThemeConfig): TokenStyleSpec[] {
26594 return specs . filter ( ( spec ) => spec . color ) ;
26695}
26796
268- /**
269- * Decoration colors the canvas draws natively, as the themes' CSS has them:
270- * selection matches use the theme's `selectionMatch`, else its selection.
271- */
272- function themeClasses ( config : ThemeConfig ) : CanvasThemeSpec [ "classes" ] {
273- const classes : NonNullable < CanvasThemeSpec [ "classes" ] > = { } ;
274- if ( config . matchingBracket ) {
275- classes [ "cm-matchingBracket" ] = { background : config . matchingBracket } ;
276- }
277- const selectionMatch = config . selectionMatch || config . selection ;
278- if ( selectionMatch ) {
279- classes [ "cm-selectionMatch" ] = { background : selectionMatch } ;
280- }
281- return Object . keys ( classes ) . length ? classes : undefined ;
282- }
97+ /** What `CanvasView`'s `theme` option and `setTheme` take. */
98+ export type CanvasThemeSource = NonNullable < CanvasViewConfig [ "theme" ] > ;
28399
284100/**
285- * The canvas theme for an Acode editor theme: colors from its config, syntax
286- * colors from the HighlightStyle in its extensions. The theme's
287- * EditorView.theme stays in the state, where it styles the DOM UI (tooltips,
288- * panels, autocomplete).
289- */
290- /**
291- * The canvas theme spec with the active line frame, which canvas-view
292- * releases before it ignore (the line is then drawn without one).
101+ * The canvas theme for an Acode editor theme. canvas-view resolves the
102+ * colors itself from the theme's own CSS (the `EditorView.theme` and
103+ * highlight style in the state: background, text, cursor, selection, active
104+ * line fill or frame, gutter, line numbers, matching brackets, selection
105+ * matches), like the DOM editor shows them, and again when the theme
106+ * compartment changes. The CSS is the only source for those: a theme's
107+ * config doesn't override it, so both renderers draw a theme the same.
108+ * This adds what the CSS doesn't say: Acode's own scrollbars, a
109+ * see-through canvas over a picture, the gutter border and config-only
110+ * syntax colors.
293111 */
294- type FramedThemeSpec = CanvasThemeSpec & {
295- activeLineBorder ?: string ;
296- activeLineBorderWidth ?: number ;
297- } ;
298-
299112export function getCanvasTheme (
300113 themeId : string ,
301114 seeThrough = false ,
302- ) : CanvasThemeSpec {
115+ ) : CanvasThemeSource {
303116 // Resolve ids like getThemeExtensions does ("one-dark" -> "one_dark").
304117 const theme =
305118 getThemeById ( themeId ) || getThemeById ( String ( themeId ) . replace ( / - / g, "_" ) ) ;
306119 const config = ( theme ?. config ||
307120 getThemeConfig ( themeId ) ||
308121 { } ) as ThemeConfig ;
309- const extensions = getThemeExtensions ( themeId , [ oneDark ] ) ;
310- const highlight =
311- findHighlightStyle ( extensions ) || highlightFromConfig ( config ) ;
312- // What the config leaves out, from the theme's own CSS rules.
313- const rules = themeRuleColors ( extensions ) ;
314- const background = config . background || "#282c34" ;
315- const foreground = config . foreground || "#abb2bf" ;
316- // The config's active line replaces the rules' (fill and frame).
317- const framed = ! config . activeLine && rules . activeLineBorder ;
318- const activeLine = config . activeLine ?? rules . activeLine ;
319- const spec : FramedThemeSpec = {
320- dark : config . dark !== false ,
321- // With a picture behind the editor, the canvas is see-through. The
322- // editor element keeps the theme's CSS background under the picture,
323- // and sticky lines take it from there.
324- background : seeThrough ? "transparent" : background ,
325- foreground,
326- selection : config . selection ?? rules . selection ,
327- cursor : config . cursor ?? rules . cursor ,
328- activeLine :
329- seeThrough && ! framed
330- ? translucentActiveLine ( activeLine , foreground )
331- : ( activeLine ?? ( framed ? "transparent" : undefined ) ) ,
332- activeLineBorder : framed ? rules . activeLineBorder : undefined ,
333- activeLineBorderWidth : framed ? rules . activeLineBorderWidth : undefined ,
334- // Like the DOM themes, which give the active gutter line no background.
335- activeLineGutter : seeThrough
336- ? "transparent"
337- : ( rules . activeLineGutter ?? background ) ,
338- gutterBackground : seeThrough ? undefined : rules . gutterBackground ,
339- gutterForeground : config . lineNumber ?? rules . gutterForeground ,
340- gutterActiveForeground :
341- config . lineNumberActive ?? rules . gutterActiveForeground ,
342- // A line between the gutter and the code, only when the theme asks.
343- gutterBorder : config . gutterBorder ,
344- // Acode draws its own (draggable) scrollbars over the editor.
345- scrollbar : "transparent" ,
346- highlight,
347- classes : themeClasses ( config ) ,
122+ // Only used for a theme without a highlight style in its extensions.
123+ const highlight = highlightFromConfig ( config ) ;
124+ return ( css : CanvasThemeSpec ) : CanvasThemeSpec => {
125+ const framed = ! ! css . activeLineBorder ;
126+ return {
127+ ...css ,
128+ ...( highlight . length ? { highlight } : null ) ,
129+ // With a picture behind the editor, the canvas is see-through. The
130+ // editor element keeps the theme's CSS background under the
131+ // picture, and sticky lines take it from there.
132+ background : seeThrough ? "transparent" : css . background ,
133+ activeLine :
134+ seeThrough && ! framed
135+ ? translucentActiveLine ( css . activeLine , css . foreground )
136+ : css . activeLine ,
137+ // Like the DOM themes, which give the active gutter line no background.
138+ activeLineGutter : seeThrough
139+ ? "transparent"
140+ : ( css . activeLineGutter ?? css . background ) ,
141+ gutterBackground : seeThrough ? undefined : css . gutterBackground ,
142+ // A line between the gutter and the code, when the theme asks.
143+ gutterBorder : config . gutterBorder ?? css . gutterBorder ,
144+ // Acode draws its own (draggable) scrollbars over the editor.
145+ scrollbar : "transparent" ,
146+ } ;
348147 } ;
349- return spec ;
350148}
351149
352150/**
0 commit comments