22// SPDX-License-Identifier: MIT
33// Copyright (c) vis.gl contributors
44
5- import { StyleEngine , type DeckGLAccessorMap , type DeckGLUpdateTriggers } from './style-engine' ;
5+ import { ZodError } from 'zod' ;
6+
7+ /** Supported scale families for attribute references. *
8+ export type GraphStyleScaleType
69
7- /** Supported scale families for attribute references. */
8- export type GraphStyleScaleType =
9- | 'linear'
1010 | 'log'
1111 | 'pow'
1212 | 'sqrt'
1313 | 'quantize'
1414 | 'quantile'
1515 | 'ordinal';
16+ /** Configuration for attribute scale mapping. *
17+ export type GraphStyleScale =
1618
17- /** Configuration for attribute scale mapping. */
18- export type GraphStyleScale = {
19- type ?: GraphStyleScaleType ;
2019 domain?: (number | string)[];
2120 range?: any[];
2221 clamp?: boolean;
2322 nice?: boolean | number;
2423 base?: number;
2524 exponent?: number;
26- unknown ?: unknown ;
2725};
2826
29- /** Declares that a style property should derive its value from a graph attribute. */
30- export type GraphStyleAttributeReference < TValue = unknown > =
31- | `@${ string } `
27+ /** Declares that a style property should derive its value from a graph attribute. *
28+ export type GraphStyleAttributeReference<TValue = unknown>
29+
3230 | {
3331 attribute: string;
3432 fallback?: TValue;
3533 scale?: GraphStyleScale | ((value: unknown) => unknown);
3634 };
3735
38- /** Acceptable value for a single style state or accessor. */
39- export type GraphStyleLeafValue < TValue = unknown > =
40- | TValue
36+ export type GraphStyleLeafValue<TValue = unknown>
37+
4138 | GraphStyleAttributeReference<TValue>
4239 | ((datum: unknown) => TValue);
4340
44- /** Acceptable value for a style property, including optional interaction states. */
45- export type GraphStyleValue < TValue = unknown > =
46- | GraphStyleLeafValue < TValue >
47- | { [ state : string ] : GraphStyleLeafValue < TValue > } ;
41+ /** Acceptable value for a style property, including optional interaction states. *
42+ export type GraphStyleValue<TValue = unknown>
43+
4844
4945const COMMON_DECKGL_PROPS = {
5046 getOffset: 'offset',
5147 opacity: 'opacity'
5248} as const;
53-
5449const GRAPH_DECKGL_ACCESSOR_MAP = {
5550 circle: {
5651 ...COMMON_DECKGL_PROPS,
@@ -164,6 +159,15 @@ export type GraphStylesheet<
164159> = {type: TType} &
165160 GraphStylePropertyMap<TType, TValue> &
166161 Partial<Record<GraphStyleSelector, GraphStylePropertyMap<TType, TValue>>>;
162+ */
163+
164+ import { StyleEngine , type DeckGLUpdateTriggers } from './style-engine' ;
165+ import {
166+ GraphStylesheetSchema ,
167+ GRAPH_DECKGL_ACCESSOR_MAP ,
168+ type GraphStylesheet ,
169+ type GraphStylesheetParsed
170+ } from './graph-stylesheet.schema' ;
167171
168172const GRAPH_DECKGL_UPDATE_TRIGGERS : DeckGLUpdateTriggers = {
169173 circle : [ 'getFillColor' , 'getRadius' , 'getLineColor' , 'getLineWidth' ] ,
@@ -179,12 +183,55 @@ const GRAPH_DECKGL_UPDATE_TRIGGERS: DeckGLUpdateTriggers = {
179183 arrow : [ 'getColor' , 'getSize' , 'getOffset' ]
180184} ;
181185
186+ function formatStylesheetError ( error : ZodError ) {
187+ const details = error . issues
188+ . map ( ( issue ) => {
189+ const path = issue . path . length ? issue . path . join ( '.' ) : 'root' ;
190+ return ` • ${ path } : ${ issue . message } ` ;
191+ } )
192+ . join ( '\n' ) ;
193+ return `Invalid graph stylesheet:\n${ details } ` ;
194+ }
195+
182196export class GraphStyleEngine extends StyleEngine {
183197 constructor ( style : GraphStylesheet , { stateUpdateTrigger} : { stateUpdateTrigger ?: unknown } = { } ) {
184- super ( style , {
198+ let parsedStyle : GraphStylesheetParsed ;
199+ try {
200+ parsedStyle = GraphStylesheetSchema . parse ( style ) ;
201+ } catch ( error ) {
202+ if ( error instanceof ZodError ) {
203+ throw new Error ( formatStylesheetError ( error ) ) ;
204+ }
205+ throw error ;
206+ }
207+
208+ super ( parsedStyle as GraphStylesheet , {
185209 deckglAccessorMap : GRAPH_DECKGL_ACCESSOR_MAP ,
186210 deckglUpdateTriggers : GRAPH_DECKGL_UPDATE_TRIGGERS ,
187211 stateUpdateTrigger
188212 } ) ;
189213 }
190214}
215+
216+ export {
217+ GraphStyleScaleTypeEnum ,
218+ GraphStyleScaleSchema ,
219+ GraphStyleAttributeReferenceSchema ,
220+ GraphStyleLeafValueSchema ,
221+ GraphStyleStateMapSchema ,
222+ GraphStyleValueSchema ,
223+ GraphStylesheetSchema
224+ } from './graph-stylesheet.schema' ;
225+
226+ export type {
227+ GraphStyleAttributeReference ,
228+ GraphStyleLeafValue ,
229+ GraphStyleScale ,
230+ GraphStyleScaleType ,
231+ GraphStyleSelector ,
232+ GraphStyleType ,
233+ GraphStyleValue ,
234+ GraphStylesheet ,
235+ GraphStylesheetInput ,
236+ GraphStylesheetParsed
237+ } from './graph-stylesheet.schema' ;
0 commit comments