-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom style engine and
@style
rule in sequenceDiagram
- Loading branch information
Showing
12 changed files
with
164 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* This module evaluate custom style params to real style configs | ||
*/ | ||
import { parseColor } from './util/color' | ||
|
||
export type StyleValueType = 'color' | 'fontSize' | ||
|
||
export type StyleMeta = { | ||
valueType: StyleValueType | StyleValueType[] | ||
} | ||
|
||
export type StyleParam<K = string> = { | ||
key: K | ||
value: string | ||
} | ||
|
||
type StyleEvaluateResult<T> = { valid: true; value: T } | { valid: false } | ||
|
||
interface StyleValueTypeMap { | ||
color: string | ||
fontSize: number | ||
} | ||
|
||
const styleValueEvaluators: { [K in StyleValueType]: (p: StyleParam) => StyleEvaluateResult<StyleValueTypeMap[K]> } = { | ||
color({ value }) { | ||
const parsed = parseColor(value) | ||
return { value: parsed.color, valid: true } | ||
}, | ||
fontSize({ value }) { | ||
const parsed = parseInt(value) | ||
if (isNaN(parsed)) return { valid: false } | ||
return { value: parsed, valid: true } | ||
}, | ||
} | ||
|
||
type StyleRuleMap = Record<string, StyleMeta> | ||
|
||
/** | ||
* Generate style config from style params | ||
*/ | ||
export function interpreteStyles<T extends StyleRuleMap>( | ||
ruleMap: T, | ||
params: StyleParam[], | ||
): { [K in keyof T]: T[K]['valueType'] } { | ||
const out: any = {} | ||
params.forEach(param => { | ||
const meta = ruleMap[param.key] | ||
if (!meta) return | ||
const valueTypes = Array.isArray(meta.valueType) ? meta.valueType : [meta.valueType] | ||
for (const valueType of valueTypes) { | ||
const evaluator = styleValueEvaluators[valueType] | ||
if (!evaluator) continue | ||
const result = evaluator(param) | ||
if (result.valid) { | ||
out[param.key] = result.value | ||
return | ||
} | ||
} | ||
}) | ||
// console.log('interpreteStyles', out) | ||
return out | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/pintora-diagrams/src/util/parser-grammars/style.ne
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@{% | ||
export const COLOR = /#[a-zA-Z0-9]+/ | ||
export const STYLE = /@style/ | ||
%} | ||
|
||
color -> %COLOR {% (d) => tv(d[0]) %} | ||
|
||
styleClause -> | ||
%STYLE __ [a-zA-Z0-9]:+ __ [^ \n]:+ {% | ||
function(d) { | ||
// console.log('[styleClause]', d[2], JSON.stringify(d[4])) | ||
const key = d[2].map(v => tv(v)).join('') | ||
let value = d[4] | ||
if (typeof value !== 'string') value = value.map(v => tv(v)).join('') | ||
// console.log('[styleClause] result', key, value) | ||
return { type: 'addStyle', key, value } | ||
} | ||
%} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { StyleParam, interpreteStyles } from '@pintora/core' | ||
|
||
export { | ||
StyleParam, | ||
interpreteStyles, | ||
} |