forked from ZenUml/core
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from mermaid-js/feat/comment-styling
feat: introduce separately styling in comments
- Loading branch information
Showing
13 changed files
with
283 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,76 @@ | ||
import Comment from '../Comment/Comment'; | ||
import Comment from "./Comment"; | ||
|
||
describe('Comment', function () { | ||
describe("Comment", function () { | ||
test.each([ | ||
['[red] comment1 \n', ' comment1', {color: 'red'}], | ||
['[red] comment \n multiple-line\n', ' comment \n multiple-line', {color: 'red'}], | ||
['comment \n', 'comment', {}], | ||
['[red] \n', '', {color: 'red'}], | ||
['[bold] \n', '', {fontWeight: 'bold'}], | ||
['[italic] \n', '', {fontStyle: 'italic'}], | ||
['[underline] \n', '', {textDecoration: 'underline'}], | ||
['[red, bold] \n', '', {color: 'red', fontWeight: 'bold'}], | ||
])('parse %s as text %s and color %s', function (raw, text, textStyle) { | ||
const comment = new Comment(raw); | ||
expect(comment.textStyle).toEqual(textStyle); | ||
expect(comment.text).toBe(text); | ||
}); | ||
[ | ||
"[red] comment1 \n", | ||
"comment1", | ||
{ color: "red" }, | ||
{ color: "red" }, | ||
[], | ||
[], | ||
], | ||
[ | ||
"[red] comment \n multiple-line\n", | ||
"[red] comment \n multiple-line", | ||
{}, | ||
{}, | ||
[], | ||
[], | ||
], | ||
["comment \n", "comment", {}, {}, [], []], | ||
["[red] \n", "", { color: "red" }, { color: "red" }, [], []], | ||
["[bold] \n", "", { fontWeight: "bold" }, { fontWeight: "bold" }, [], []], | ||
[ | ||
"[italic] \n", | ||
"", | ||
{ fontStyle: "italic" }, | ||
{ fontStyle: "italic" }, | ||
[], | ||
[], | ||
], | ||
[ | ||
"[underline] \n", | ||
"", | ||
{ textDecoration: "underline" }, | ||
{ textDecoration: "underline" }, | ||
[], | ||
[], | ||
], | ||
[ | ||
"[red, bold] \n", | ||
"", | ||
{ color: "red", fontWeight: "bold" }, | ||
{ color: "red", fontWeight: "bold" }, | ||
[], | ||
[], | ||
], | ||
["<red> (bold) \ncomment \n", "<red> (bold) \ncomment", {}, {}, [], []], | ||
[ | ||
"<red> (bold) comment \n", | ||
"comment", | ||
{ color: "red" }, | ||
{ fontWeight: "bold" }, | ||
[], | ||
[], | ||
], | ||
["[color-red] comment \n", "comment", {}, {}, ["color-red"], ["color-red"]], | ||
])( | ||
"parse %s as text %s and color %s", | ||
function ( | ||
raw, | ||
text, | ||
commentStyle, | ||
messageStyle, | ||
commentClassNames, | ||
messageClassNames, | ||
) { | ||
const comment = new Comment(raw); | ||
expect(comment.commentStyle).toEqual(commentStyle); | ||
expect(comment.messageStyle).toEqual(messageStyle); | ||
expect(comment.commentClassNames).toEqual(commentClassNames); | ||
expect(comment.messageClassNames).toEqual(messageClassNames); | ||
expect(comment.text).toBe(text); | ||
}, | ||
); | ||
}); |
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 |
---|---|---|
@@ -1,42 +1,84 @@ | ||
import { CSSProperties } from "vue"; | ||
import { getStyle } from "@/utils/messageStyling"; | ||
|
||
function splitStringWithBrackets(input: string) { | ||
const startIndex = input.indexOf("["); | ||
const endIndex = input.indexOf("]", startIndex); | ||
if (startIndex !== -1 && endIndex !== -1 && input.trim().indexOf("[") === 0) { | ||
return [input.slice(startIndex + 1, endIndex), input.slice(endIndex + 1)]; | ||
function parseLine(input: string): [string[], string[], string[], string] { | ||
// <red> controls comment only; | ||
// (red) controls message only; | ||
// [red] controls both comment and message. | ||
const result = { | ||
"<>": new Set<string>(), | ||
"()": new Set<string>(), | ||
"[]": new Set<string>(), | ||
}; | ||
const pattern = /<([^>]*)>|\(([^)]*)\)|\[([^\]]*)\]|([^<>()[\]\s]+)/g; | ||
let match; | ||
let lastMatchIndex: number | undefined; | ||
while ((match = pattern.exec(input))) { | ||
if (match[4]) { | ||
if (lastMatchIndex !== undefined) lastMatchIndex = match.index; | ||
// non-empty character outside brackets is encountered | ||
break; | ||
} | ||
lastMatchIndex = match.index + match[0].length; | ||
if (match[1]) { | ||
match[1].split(",").forEach((s) => result["<>"].add(s.trim())); | ||
} | ||
if (match[2]) { | ||
match[2].split(",").forEach((s) => result["()"].add(s.trim())); | ||
} | ||
if (match[3]) { | ||
match[3].split(",").forEach((s) => result["[]"].add(s.trim())); | ||
} | ||
} | ||
return ["", input]; | ||
} | ||
|
||
return [ | ||
Array.from(result["<>"]), | ||
Array.from(result["()"]), | ||
Array.from(result["[]"]), | ||
input.slice(lastMatchIndex), | ||
]; | ||
} | ||
export default class Comment { | ||
// define properties color and text | ||
public text: string; | ||
public textStyle: CSSProperties = {}; | ||
public text: string = ""; | ||
/** @deprecated use commentStyle or messageStyle instead */ | ||
public classNames: string[] = []; | ||
/** @deprecated use commentClassNames or messageClassNames instead */ | ||
public textStyle: CSSProperties = {}; | ||
|
||
public commentStyle: CSSProperties = {}; | ||
public messageStyle: CSSProperties = {}; | ||
public commentClassNames: string[] = []; | ||
public messageClassNames: string[] = []; | ||
|
||
// Raw comment contains all spaces and newlines | ||
constructor(raw: string) { | ||
const lines = raw.split("\n"); | ||
const lines = raw.slice(0, -1).split("\n"); | ||
const lastLine = lines[lines.length - 1]; | ||
const [commentOnlyStyles, messageOnlyStyles, commonStyles, text] = | ||
parseLine(lastLine); | ||
|
||
const styles = lines.reduce((acc, line) => { | ||
const [style] = splitStringWithBrackets(line); | ||
if (style) { | ||
acc = [...acc, ...style.split(",").map((s) => s.trim())]; | ||
} | ||
return acc; | ||
}, [] as string[]); | ||
const { textStyle, classNames } = getStyle(styles); | ||
this.textStyle = textStyle; | ||
this.classNames = classNames; | ||
const { textStyle: commentStyle, classNames: commentClassNames } = | ||
getStyle(commentOnlyStyles); | ||
const { textStyle: messageStyle, classNames: messageClassNames } = | ||
getStyle(messageOnlyStyles); | ||
const { textStyle: commonStyle, classNames: commonClassNames } = | ||
getStyle(commonStyles); | ||
|
||
this.text = lines.reduce((acc, line) => { | ||
const [, text] = splitStringWithBrackets(line); | ||
if (acc && text) { | ||
return `${acc}\n${text}` | ||
} | ||
return acc || text; | ||
}, '').trimEnd() | ||
this.text = ( | ||
lines.slice(0, lines.length - 1).join("\n") + | ||
"\n" + | ||
text | ||
).trim(); | ||
this.textStyle = { ...commonStyle, ...commentStyle, ...messageStyle }; | ||
this.classNames = [ | ||
...commonClassNames, | ||
...commentClassNames, | ||
...messageClassNames, | ||
]; | ||
this.commentStyle = { ...commonStyle, ...commentStyle }; | ||
this.messageStyle = { ...commonStyle, ...messageStyle }; | ||
this.commentClassNames = [...commonClassNames, ...commentClassNames]; | ||
this.messageClassNames = [...commonClassNames, ...messageClassNames]; | ||
} | ||
} |
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
Oops, something went wrong.