mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-28 07:03:17 +08:00
#3358 Adding support for style statements
This commit is contained in:
parent
275e01acba
commit
818cb2fd76
@ -73,17 +73,37 @@ block-beta
|
|||||||
<pre id="diagram" class="mermaid">
|
<pre id="diagram" class="mermaid">
|
||||||
block-beta
|
block-beta
|
||||||
columns 3
|
columns 3
|
||||||
classDef green fill:#9f6,stroke:#333,stroke-width:2px;
|
space Browser space
|
||||||
|
space:3
|
||||||
|
|
||||||
A
|
A
|
||||||
B
|
B
|
||||||
class A green
|
C
|
||||||
</pre>
|
|
||||||
<pre id="diagram" class="mermaid">
|
space:3
|
||||||
stateDiagram-v2
|
space
|
||||||
|
db{{"This is the text in the box"}}
|
||||||
|
|
||||||
classDef green fill:#9f6,stroke:#333,stroke-width:2px;
|
classDef green fill:#9f6,stroke:#333,stroke-width:2px;
|
||||||
A
|
style B fill:#f9F,stroke:#333,stroke-width:4px
|
||||||
class A green
|
class A green
|
||||||
|
|
||||||
|
Browser --> A
|
||||||
|
Browser --> B
|
||||||
|
Browser --> C
|
||||||
|
A --> db
|
||||||
|
B --> db
|
||||||
|
C--> db
|
||||||
|
|
||||||
|
block
|
||||||
|
D
|
||||||
|
E
|
||||||
|
end
|
||||||
|
</pre>
|
||||||
|
<pre id="diagram" class="mermaid2">
|
||||||
|
flowchart
|
||||||
|
B
|
||||||
|
style B fill:#f9F,stroke:#333,stroke-width:4px
|
||||||
</pre>
|
</pre>
|
||||||
<pre id="diagram" class="mermaid2">
|
<pre id="diagram" class="mermaid2">
|
||||||
block-beta
|
block-beta
|
||||||
|
@ -56,6 +56,20 @@ export const addStyleClass = function (id: string, styleAttributes = '') {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the parser comes across a (style) class definition
|
||||||
|
* @example classDef my-style fill:#f96;
|
||||||
|
*
|
||||||
|
* @param {string} id - the id of this (style) class
|
||||||
|
* @param {string | null} styles - the string with 1 or more style attributes (each separated by a comma)
|
||||||
|
*/
|
||||||
|
export const addStyle2Node = function (id: string, styles = '') {
|
||||||
|
let foundBlock = blockDatabase[id];
|
||||||
|
if (styles !== undefined && styles !== null) {
|
||||||
|
foundBlock.styles = styles.split(STYLECLASS_SEP);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a (style) class or css class to a state with the given id.
|
* Add a (style) class or css class to a state with the given id.
|
||||||
* If the state isn't already in the list of known states, add it.
|
* If the state isn't already in the list of known states, add it.
|
||||||
@ -116,14 +130,21 @@ const populateBlockDatabase = (_blockList: Block[], parent: Block): void => {
|
|||||||
const children = [];
|
const children = [];
|
||||||
for (const block of blockList) {
|
for (const block of blockList) {
|
||||||
if (block.type === 'classDef') {
|
if (block.type === 'classDef') {
|
||||||
console.log('abc88 classDef', block);
|
// console.log('abc88 classDef', block);
|
||||||
addStyleClass(block.id, block.css);
|
addStyleClass(block.id, block.css);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (block.type === 'applyClass') {
|
if (block.type === 'applyClass') {
|
||||||
console.log('abc88 applyClass', block);
|
// console.log('abc88 applyClass', block);
|
||||||
// addStyleClass(block.id, block.css);
|
// addStyleClass(block.id, block.css);
|
||||||
setCssClass(block.id, block.styleClass);
|
setCssClass(block.id, block?.styleClass || '');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (block.type === 'applyStyles') {
|
||||||
|
console.log('abc88 applyStyles', block);
|
||||||
|
addStyle2Node(block.id, block.styles);
|
||||||
|
// addStyleClass(block.id, block.css);
|
||||||
|
// setCssClass(block.id, block.styles);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (block.type === 'column-setting') {
|
if (block.type === 'column-setting') {
|
||||||
|
@ -30,6 +30,7 @@ export type BlockType =
|
|||||||
| 'doublecircle'
|
| 'doublecircle'
|
||||||
| 'classDef'
|
| 'classDef'
|
||||||
| 'applyClass'
|
| 'applyClass'
|
||||||
|
| 'applyStyles'
|
||||||
| 'composite';
|
| 'composite';
|
||||||
|
|
||||||
export interface Block {
|
export interface Block {
|
||||||
@ -57,6 +58,7 @@ export interface Block {
|
|||||||
directions?: string[];
|
directions?: string[];
|
||||||
css?: string;
|
css?: string;
|
||||||
styleClass?: string;
|
styleClass?: string;
|
||||||
|
styles?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Link {
|
export interface Link {
|
||||||
|
@ -23,6 +23,8 @@
|
|||||||
%x CLASS_STYLE
|
%x CLASS_STYLE
|
||||||
%x CLASSDEF
|
%x CLASSDEF
|
||||||
%x CLASSDEFID
|
%x CLASSDEFID
|
||||||
|
%x STYLE_STMNT
|
||||||
|
%x STYLE_DEFINITION
|
||||||
|
|
||||||
|
|
||||||
// as per section 6.1 of RFC 2234 [2]
|
// as per section 6.1 of RFC 2234 [2]
|
||||||
@ -53,7 +55,6 @@ CRLF \u000D\u000A
|
|||||||
<string>[^"]* { yy.getLogger().debug('LEX: STR end:', yytext); return "STR";}
|
<string>[^"]* { yy.getLogger().debug('LEX: STR end:', yytext); return "STR";}
|
||||||
space[:]\d+ { yytext = yytext.replace(/space\:/,'');yy.getLogger().info('SPACE NUM (LEX)', yytext); return 'SPACE_BLOCK'; }
|
space[:]\d+ { yytext = yytext.replace(/space\:/,'');yy.getLogger().info('SPACE NUM (LEX)', yytext); return 'SPACE_BLOCK'; }
|
||||||
space { yytext = '1'; yy.getLogger().info('COLUMNS (LEX)', yytext); return 'SPACE_BLOCK'; }
|
space { yytext = '1'; yy.getLogger().info('COLUMNS (LEX)', yytext); return 'SPACE_BLOCK'; }
|
||||||
"style" return 'STYLE';
|
|
||||||
"default" return 'DEFAULT';
|
"default" return 'DEFAULT';
|
||||||
"linkStyle" return 'LINKSTYLE';
|
"linkStyle" return 'LINKSTYLE';
|
||||||
"interpolate" return 'INTERPOLATE';
|
"interpolate" return 'INTERPOLATE';
|
||||||
@ -67,6 +68,10 @@ space { yytext = '1'; yy.getLogger().info('COLUMNS (LEX)', yyte
|
|||||||
<CLASS>(\w+)+((","\s*\w+)*) { this.popState(); this.pushState('CLASS_STYLE'); return 'CLASSENTITY_IDS' }
|
<CLASS>(\w+)+((","\s*\w+)*) { this.popState(); this.pushState('CLASS_STYLE'); return 'CLASSENTITY_IDS' }
|
||||||
<CLASS_STYLE>[^\n]* { this.popState(); return 'STYLECLASS' }
|
<CLASS_STYLE>[^\n]* { this.popState(); return 'STYLECLASS' }
|
||||||
|
|
||||||
|
"style"\s+ { this.pushState('STYLE_STMNT'); return 'style'; }
|
||||||
|
<STYLE_STMNT>(\w+)+((","\s*\w+)*) { this.popState(); this.pushState('STYLE_DEFINITION'); return 'STYLE_ENTITY_IDS' }
|
||||||
|
<STYLE_DEFINITION>[^\n]* { this.popState(); return 'STYLE_DEFINITION_DATA' }
|
||||||
|
|
||||||
accTitle\s*":"\s* { this.pushState("acc_title");return 'acc_title'; }
|
accTitle\s*":"\s* { this.pushState("acc_title");return 'acc_title'; }
|
||||||
<acc_title>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_title_value"; }
|
<acc_title>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_title_value"; }
|
||||||
accDescr\s*":"\s* { this.pushState("acc_descr");return 'acc_descr'; }
|
accDescr\s*":"\s* { this.pushState("acc_descr");return 'acc_descr'; }
|
||||||
@ -208,6 +213,7 @@ statement
|
|||||||
| blockStatement
|
| blockStatement
|
||||||
| classDefStatement
|
| classDefStatement
|
||||||
| cssClassStatement
|
| cssClassStatement
|
||||||
|
| styleStatement
|
||||||
;
|
;
|
||||||
|
|
||||||
nodeStatement
|
nodeStatement
|
||||||
@ -271,4 +277,11 @@ cssClassStatement
|
|||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
styleStatement
|
||||||
|
: style STYLE_ENTITY_IDS STYLE_DEFINITION_DATA {
|
||||||
|
console.log('abc88 apply class: id(s): ',$2, ' style class: ', $3);
|
||||||
|
$$={ type: 'applyStyles', id: $2.trim(), styles: $3.trim() };
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
@ -16,10 +16,9 @@ function getNodeFromBlock(block: Block, db: BlockDB, positioned = false) {
|
|||||||
|
|
||||||
let classStr = 'default';
|
let classStr = 'default';
|
||||||
if ((vertex?.classes?.length || 0) > 0) {
|
if ((vertex?.classes?.length || 0) > 0) {
|
||||||
console.log('abc88 vertex.classes', block.id, vertex?.classes);
|
|
||||||
classStr = (vertex?.classes || []).join(' ');
|
classStr = (vertex?.classes || []).join(' ');
|
||||||
}
|
}
|
||||||
console.log('abc88 vertex.classes done');
|
console.log('abc88 vertex.classes styles', block.id, vertex?.styles);
|
||||||
classStr = classStr + ' flowchart-label';
|
classStr = classStr + ' flowchart-label';
|
||||||
|
|
||||||
// We create a SVG label, either by delegating to addHtmlLabel or manually
|
// We create a SVG label, either by delegating to addHtmlLabel or manually
|
||||||
@ -100,8 +99,8 @@ function getNodeFromBlock(block: Block, db: BlockDB, positioned = false) {
|
|||||||
_shape = 'rect';
|
_shape = 'rect';
|
||||||
}
|
}
|
||||||
|
|
||||||
// const styles = getStylesFromArray(vertex.styles);
|
const styles = getStylesFromArray(vertex?.styles || '');
|
||||||
const styles = getStylesFromArray([]);
|
// const styles = getStylesFromArray([]);
|
||||||
|
|
||||||
// Use vertex id as text in the box if no text is provided by the graph definition
|
// Use vertex id as text in the box if no text is provided by the graph definition
|
||||||
const vertexText = vertex.label;
|
const vertexText = vertex.label;
|
||||||
@ -116,7 +115,7 @@ function getNodeFromBlock(block: Block, db: BlockDB, positioned = false) {
|
|||||||
rx: radious,
|
rx: radious,
|
||||||
ry: radious,
|
ry: radious,
|
||||||
class: classStr,
|
class: classStr,
|
||||||
style: styles.style,
|
style: styles.style, // + 'fill:#9f9;stroke:#333;stroke-width:4px;',
|
||||||
id: vertex.id,
|
id: vertex.id,
|
||||||
directions: vertex.directions,
|
directions: vertex.directions,
|
||||||
// link: vertex.link,
|
// link: vertex.link,
|
||||||
|
@ -169,7 +169,7 @@ export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
|
|||||||
padding: getConfig().flowchart.padding,
|
padding: getConfig().flowchart.padding,
|
||||||
});
|
});
|
||||||
|
|
||||||
log.info('setNode', {
|
log.info('abc88 setNode', {
|
||||||
labelStyle: styles.labelStyle,
|
labelStyle: styles.labelStyle,
|
||||||
labelType: vertex.labelType,
|
labelType: vertex.labelType,
|
||||||
shape: _shape,
|
shape: _shape,
|
||||||
|
@ -513,7 +513,6 @@ const render = async function (
|
|||||||
const a11yTitle: string | undefined = diag.db.getAccTitle?.();
|
const a11yTitle: string | undefined = diag.db.getAccTitle?.();
|
||||||
const a11yDescr: string | undefined = diag.db.getAccDescription?.();
|
const a11yDescr: string | undefined = diag.db.getAccDescription?.();
|
||||||
addA11yInfo(diagramType, svgNode, a11yTitle, a11yDescr);
|
addA11yInfo(diagramType, svgNode, a11yTitle, a11yDescr);
|
||||||
|
|
||||||
// -------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------
|
||||||
// Clean up SVG code
|
// Clean up SVG code
|
||||||
root.select(`[id="${id}"]`).selectAll('foreignobject > *').attr('xmlns', XMLNS_XHTML_STD);
|
root.select(`[id="${id}"]`).selectAll('foreignobject > *').attr('xmlns', XMLNS_XHTML_STD);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user