From 5631a218d14cc00a35ea3fa005995a03492a261d Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 13:58:22 +0000 Subject: [PATCH 01/20] fix: make gantt chart interval weeks start on monday instead of sunday --- packages/mermaid/src/diagrams/gantt/ganttRenderer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js index 215a4df29..6e34276ed 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js +++ b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js @@ -13,7 +13,7 @@ import { timeMinute, timeHour, timeDay, - timeWeek, + timeMonday, timeMonth, } from 'd3'; import common from '../common/common.js'; @@ -572,7 +572,7 @@ export const draw = function (text, id, version, diagObj) { bottomXAxis.ticks(timeDay.every(every)); break; case 'week': - bottomXAxis.ticks(timeWeek.every(every)); + bottomXAxis.ticks(timeMonday.every(every)); break; case 'month': bottomXAxis.ticks(timeMonth.every(every)); @@ -611,7 +611,7 @@ export const draw = function (text, id, version, diagObj) { topXAxis.ticks(timeDay.every(every)); break; case 'week': - topXAxis.ticks(timeWeek.every(every)); + topXAxis.ticks(timeMonday.every(every)); break; case 'month': topXAxis.ticks(timeMonth.every(every)); From 03ce2810b53920d7de2b6fa6eae3e5bc1e7ac580 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 17:23:21 +0200 Subject: [PATCH 02/20] feat: allow specifying on which weekday a tickInterval should start --- docs/syntax/gantt.md | 6 ++++ packages/mermaid/src/defaultConfig.ts | 1 + .../mermaid/src/diagrams/gantt/ganttDb.js | 12 +++++++ .../src/diagrams/gantt/ganttRenderer.js | 33 ++++++++++++++++++- .../src/diagrams/gantt/parser/gantt.jison | 2 ++ .../src/diagrams/gantt/parser/gantt.spec.js | 5 +++ 6 files changed, 58 insertions(+), 1 deletion(-) diff --git a/docs/syntax/gantt.md b/docs/syntax/gantt.md index 8e64a268a..ef40aef0f 100644 --- a/docs/syntax/gantt.md +++ b/docs/syntax/gantt.md @@ -257,6 +257,12 @@ The pattern is: More info in: +Week-based `tickInterval`s start the week on sunday by default. If you wish to specify another weekday on which the `tickInterval` should start, use the `weekday` option: + +```markdown +weekday monday +``` + ## Output in compact mode The compact mode allows you to display multiple tasks in the same row. Compact mode can be enabled for a gantt chart by setting the display mode of the graph via preceeding YAML settings. diff --git a/packages/mermaid/src/defaultConfig.ts b/packages/mermaid/src/defaultConfig.ts index 62b361cff..28454e353 100644 --- a/packages/mermaid/src/defaultConfig.ts +++ b/packages/mermaid/src/defaultConfig.ts @@ -50,6 +50,7 @@ const config: Partial = { ...defaultConfigJson.gantt, tickInterval: undefined, useWidth: undefined, // can probably be removed since `configKeys` already includes this + weekday: 'sunday', // the sane default is a monday, but it's set to sunday for legacy reasons }, c4: { ...defaultConfigJson.c4, diff --git a/packages/mermaid/src/diagrams/gantt/ganttDb.js b/packages/mermaid/src/diagrams/gantt/ganttDb.js index 396402702..339cb65ec 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttDb.js +++ b/packages/mermaid/src/diagrams/gantt/ganttDb.js @@ -37,6 +37,7 @@ const tags = ['active', 'done', 'crit', 'milestone']; let funs = []; let inclusiveEndDates = false; let topAxis = false; +let weekday = undefined; // The serial order of the task in the script let lastOrder = 0; @@ -66,6 +67,7 @@ export const clear = function () { lastOrder = 0; links = {}; commonClear(); + weekday = undefined; }; export const setAxisFormat = function (txt) { @@ -179,6 +181,14 @@ export const isInvalidDate = function (date, dateFormat, excludes, includes) { return excludes.includes(date.format(dateFormat.trim())); }; +export const setWeekday = function (txt) { + weekday = txt; +}; + +export const getWeekday = function () { + return weekday; +}; + /** * TODO: fully document what this function does and what types it accepts * @@ -759,6 +769,8 @@ export default { bindFunctions, parseDuration, isInvalidDate, + setWeekday, + getWeekday, }; /** diff --git a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js index 6e34276ed..05ff7f8a9 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js +++ b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js @@ -14,6 +14,12 @@ import { timeHour, timeDay, timeMonday, + timeTuesday, + timeWednesday, + timeThursday, + timeFriday, + timeSaturday, + timeSunday, timeMonth, } from 'd3'; import common from '../common/common.js'; @@ -561,6 +567,8 @@ export const draw = function (text, id, version, diagObj) { if (resultTickInterval !== null) { const every = resultTickInterval[1]; const interval = resultTickInterval[2]; + const weekday = diagObj.db.getWeekday() || conf.weekday; + switch (interval) { case 'minute': bottomXAxis.ticks(timeMinute.every(every)); @@ -572,7 +580,30 @@ export const draw = function (text, id, version, diagObj) { bottomXAxis.ticks(timeDay.every(every)); break; case 'week': - bottomXAxis.ticks(timeMonday.every(every)); + switch (weekday) { + case 'monday': + bottomXAxis.ticks(timeMonday.every(every)); + break; + case 'tuesday': + bottomXAxis.ticks(timeTuesday.every(every)); + break; + case 'wednesday': + bottomXAxis.ticks(timeWednesday.every(every)); + break; + case 'thursday': + bottomXAxis.ticks(timeThursday.every(every)); + break; + case 'friday': + bottomXAxis.ticks(timeFriday.every(every)); + break; + case 'saturday': + bottomXAxis.ticks(timeSaturday.every(every)); + break; + case 'sunday': + default: + bottomXAxis.ticks(timeSunday.every(every)); + break; + } break; case 'month': bottomXAxis.ticks(timeMonth.every(every)); diff --git a/packages/mermaid/src/diagrams/gantt/parser/gantt.jison b/packages/mermaid/src/diagrams/gantt/parser/gantt.jison index 0eb45ec41..d83baeea9 100644 --- a/packages/mermaid/src/diagrams/gantt/parser/gantt.jison +++ b/packages/mermaid/src/diagrams/gantt/parser/gantt.jison @@ -86,6 +86,7 @@ that id. "includes"\s[^#\n;]+ return 'includes'; "excludes"\s[^#\n;]+ return 'excludes'; "todayMarker"\s[^\n;]+ return 'todayMarker'; +"weekday"\s[^#\n;]+ return 'weekday'; \d\d\d\d"-"\d\d"-"\d\d return 'date'; "title"\s[^#\n;]+ return 'title'; "accDescription"\s[^#\n;]+ return 'accDescription' @@ -130,6 +131,7 @@ statement | excludes {yy.setExcludes($1.substr(9));$$=$1.substr(9);} | includes {yy.setIncludes($1.substr(9));$$=$1.substr(9);} | todayMarker {yy.setTodayMarker($1.substr(12));$$=$1.substr(12);} + | weekday { yy.setWeekday($1.substr(8));$$=$1.substr(8);} | title {yy.setDiagramTitle($1.substr(6));$$=$1.substr(6);} | acc_title acc_title_value { $$=$2.trim();yy.setAccTitle($$); } | acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); } diff --git a/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js b/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js index 020bab0ed..575833399 100644 --- a/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js +++ b/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js @@ -180,4 +180,9 @@ row2`; expect(ganttDb.getAccTitle()).toBe(expectedTitle); expect(ganttDb.getAccDescription()).toBe(expectedAccDescription); }); + + it('should allow for customising the weekday for tick intervals', function () { + parser.parse('gantt\nweekday wednesday'); + expect(ganttDb.getWeekday()).toBe('wednesday'); + }); }); From e360e90f884ff71e371c892c19c41ab499380e32 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 17:30:44 +0200 Subject: [PATCH 03/20] chore: move documentation to source file --- docs/config/setup/modules/defaultConfig.md | 2 +- packages/mermaid/src/docs/syntax/gantt.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/config/setup/modules/defaultConfig.md b/docs/config/setup/modules/defaultConfig.md index a55ec1808..d03ed36da 100644 --- a/docs/config/setup/modules/defaultConfig.md +++ b/docs/config/setup/modules/defaultConfig.md @@ -14,7 +14,7 @@ #### Defined in -[defaultConfig.ts:266](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L266) +[defaultConfig.ts:267](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L267) --- diff --git a/packages/mermaid/src/docs/syntax/gantt.md b/packages/mermaid/src/docs/syntax/gantt.md index 710b39e52..5859c7403 100644 --- a/packages/mermaid/src/docs/syntax/gantt.md +++ b/packages/mermaid/src/docs/syntax/gantt.md @@ -189,6 +189,12 @@ The pattern is: More info in: [https://github.com/d3/d3-time#interval_every](https://github.com/d3/d3-time#interval_every) +Week-based `tickInterval`s start the week on sunday by default. If you wish to specify another weekday on which the `tickInterval` should start, use the `weekday` option: + +```markdown +weekday monday +``` + ## Output in compact mode The compact mode allows you to display multiple tasks in the same row. Compact mode can be enabled for a gantt chart by setting the display mode of the graph via preceeding YAML settings. From 0d7427ed20529019bb623c5207c171d70779175b Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 22:31:34 +0200 Subject: [PATCH 04/20] chore: add cypress test --- cypress/integration/rendering/gantt.spec.js | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cypress/integration/rendering/gantt.spec.js b/cypress/integration/rendering/gantt.spec.js index cb65f73b0..0005e3e76 100644 --- a/cypress/integration/rendering/gantt.spec.js +++ b/cypress/integration/rendering/gantt.spec.js @@ -414,6 +414,28 @@ describe('Gantt diagram', () => { ); }); + it('should render a gantt diagram with tick is 1 week, with the day starting on monday', () => { + imgSnapshotTest( + ` + gantt + title A Gantt Diagram + dateFormat YYYY-MM-DD + axisFormat %m-%d + tickInterval 1week + weekday monday + excludes weekends + + section Section + A task : a1, 2022-10-01, 30d + Another task : after a1, 20d + section Another + Task in sec : 2022-10-20, 12d + another task : 24d + `, + {} + ); + }); + it('should render a gantt diagram with tick is 1 month', () => { imgSnapshotTest( ` From df10ab501a84c99b6d34f944f62d864689168dd1 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 22:31:51 +0200 Subject: [PATCH 05/20] chore: add tests for all days --- .../src/diagrams/gantt/parser/gantt.spec.js | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js b/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js index 575833399..6c1f2e2ca 100644 --- a/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js +++ b/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js @@ -181,8 +181,32 @@ row2`; expect(ganttDb.getAccDescription()).toBe(expectedAccDescription); }); - it('should allow for customising the weekday for tick intervals', function () { + it('should allow for setting the starting weekday to monday for tick intervals', function () { + parser.parse('gantt\nweekday monday'); + expect(ganttDb.getWeekday()).toBe('monday'); + }); + it('should allow for setting the starting weekday to tuesday for tick intervals', function () { + parser.parse('gantt\nweekday tuesday'); + expect(ganttDb.getWeekday()).toBe('tuesday'); + }); + it('should allow for setting the starting weekday to wednesday for tick intervals', function () { parser.parse('gantt\nweekday wednesday'); expect(ganttDb.getWeekday()).toBe('wednesday'); }); + it('should allow for setting the starting weekday to thursday for tick intervals', function () { + parser.parse('gantt\nweekday thursday'); + expect(ganttDb.getWeekday()).toBe('thursday'); + }); + it('should allow for setting the starting weekday to friday for tick intervals', function () { + parser.parse('gantt\nweekday friday'); + expect(ganttDb.getWeekday()).toBe('friday'); + }); + it('should allow for setting the starting weekday to saturday for tick intervals', function () { + parser.parse('gantt\nweekday saturday'); + expect(ganttDb.getWeekday()).toBe('saturday'); + }); + it('should allow for setting the starting weekday to sunday for tick intervals', function () { + parser.parse('gantt\nweekday sunday'); + expect(ganttDb.getWeekday()).toBe('sunday'); + }); }); From cd92c46f3180a1db1533d2d4aeab35ff0b3e7adb Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 22:34:22 +0200 Subject: [PATCH 06/20] chore: format example more correctly --- packages/mermaid/src/docs/syntax/gantt.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/mermaid/src/docs/syntax/gantt.md b/packages/mermaid/src/docs/syntax/gantt.md index 5859c7403..05ccf7bff 100644 --- a/packages/mermaid/src/docs/syntax/gantt.md +++ b/packages/mermaid/src/docs/syntax/gantt.md @@ -191,8 +191,10 @@ More info in: [https://github.com/d3/d3-time#interval_every](https://github.com/ Week-based `tickInterval`s start the week on sunday by default. If you wish to specify another weekday on which the `tickInterval` should start, use the `weekday` option: -```markdown -weekday monday +```mermaid-example +gantt + tickInterval 1week + weekday monday ``` ## Output in compact mode From 5f7212c7696b1058e1aa0e60b804b85220431ac4 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 22:38:00 +0200 Subject: [PATCH 07/20] chore: move default value to config.schema.yaml --- packages/mermaid/src/defaultConfig.ts | 1 - packages/mermaid/src/schemas/config.schema.yaml | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/mermaid/src/defaultConfig.ts b/packages/mermaid/src/defaultConfig.ts index 28454e353..62b361cff 100644 --- a/packages/mermaid/src/defaultConfig.ts +++ b/packages/mermaid/src/defaultConfig.ts @@ -50,7 +50,6 @@ const config: Partial = { ...defaultConfigJson.gantt, tickInterval: undefined, useWidth: undefined, // can probably be removed since `configKeys` already includes this - weekday: 'sunday', // the sane default is a monday, but it's set to sunday for legacy reasons }, c4: { ...defaultConfigJson.c4, diff --git a/packages/mermaid/src/schemas/config.schema.yaml b/packages/mermaid/src/schemas/config.schema.yaml index 306aab2cc..8bebd68cf 100644 --- a/packages/mermaid/src/schemas/config.schema.yaml +++ b/packages/mermaid/src/schemas/config.schema.yaml @@ -1455,6 +1455,7 @@ $defs: # JSON Schema definition (maybe we should move these to a seperate file) - axisFormat - useMaxWidth - topAxis + - weekday properties: titleTopMargin: $ref: '#/$defs/GitGraphDiagramConfig/properties/titleTopMargin' @@ -1544,6 +1545,20 @@ $defs: # JSON Schema definition (maybe we should move these to a seperate file) default: '' # Allow any string for typescript backwards compatibility (fix in Mermaid v10) tsType: 'string | "compact"' + weekday: + description: | + On which day a week-based interval should start + type: string + enum: + - monday + - tuesday + - wednesday + - thursday + - friday + - saturday + - sunday + # the sane default is a monday, but it's set to sunday for legacy reasons + default: sunday SequenceDiagramConfig: title: Sequence Diagram Config From 37adc23ae2bc674cdac6c40ccb83e4df0afd6a3b Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 22:38:48 +0200 Subject: [PATCH 08/20] chore: also default to sundat in ganttDb --- packages/mermaid/src/diagrams/gantt/ganttDb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mermaid/src/diagrams/gantt/ganttDb.js b/packages/mermaid/src/diagrams/gantt/ganttDb.js index 339cb65ec..d79a57fc2 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttDb.js +++ b/packages/mermaid/src/diagrams/gantt/ganttDb.js @@ -37,7 +37,7 @@ const tags = ['active', 'done', 'crit', 'milestone']; let funs = []; let inclusiveEndDates = false; let topAxis = false; -let weekday = undefined; +let weekday = 'sunday'; // The serial order of the task in the script let lastOrder = 0; From ebf639377bd6296429ae39a267d3490e08b7ffdf Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 22:54:28 +0200 Subject: [PATCH 09/20] chore: generate typescript config type --- packages/mermaid/src/config.type.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/mermaid/src/config.type.ts b/packages/mermaid/src/config.type.ts index 352181c86..df87e9c40 100644 --- a/packages/mermaid/src/config.type.ts +++ b/packages/mermaid/src/config.type.ts @@ -1063,6 +1063,11 @@ export interface GanttDiagramConfig extends BaseDiagramConfig { * */ displayMode?: string | "compact"; + /** + * On which day a week-based interval should start + * + */ + weekday?: "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"; } /** * The object containing configurations specific for sequence diagrams From cfe31fe89ff0da74c70d932d457c8ee0f7784f25 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Wed, 12 Jul 2023 22:59:06 +0200 Subject: [PATCH 10/20] chore: generate new docs --- docs/config/setup/modules/defaultConfig.md | 2 +- docs/syntax/gantt.md | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/config/setup/modules/defaultConfig.md b/docs/config/setup/modules/defaultConfig.md index d03ed36da..a55ec1808 100644 --- a/docs/config/setup/modules/defaultConfig.md +++ b/docs/config/setup/modules/defaultConfig.md @@ -14,7 +14,7 @@ #### Defined in -[defaultConfig.ts:267](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L267) +[defaultConfig.ts:266](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L266) --- diff --git a/docs/syntax/gantt.md b/docs/syntax/gantt.md index ef40aef0f..5cf584050 100644 --- a/docs/syntax/gantt.md +++ b/docs/syntax/gantt.md @@ -259,8 +259,16 @@ More info in: Week-based `tickInterval`s start the week on sunday by default. If you wish to specify another weekday on which the `tickInterval` should start, use the `weekday` option: -```markdown -weekday monday +```mermaid-example +gantt + tickInterval 1week + weekday monday +``` + +```mermaid +gantt + tickInterval 1week + weekday monday ``` ## Output in compact mode From 4e8eeda30e351b1976c88383466b28d26de82015 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Thu, 13 Jul 2023 17:18:13 +0200 Subject: [PATCH 11/20] chore: also apply weekday to topXAxis --- .../src/diagrams/gantt/ganttRenderer.js | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js index 05ff7f8a9..def988ed2 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js +++ b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js @@ -631,6 +631,8 @@ export const draw = function (text, id, version, diagObj) { if (resultTickInterval !== null) { const every = resultTickInterval[1]; const interval = resultTickInterval[2]; + const weekday = diagObj.db.getWeekday() || conf.weekday; + switch (interval) { case 'minute': topXAxis.ticks(timeMinute.every(every)); @@ -642,7 +644,30 @@ export const draw = function (text, id, version, diagObj) { topXAxis.ticks(timeDay.every(every)); break; case 'week': - topXAxis.ticks(timeMonday.every(every)); + switch (weekday) { + case 'monday': + topXAxis.ticks(timeMonday.every(every)); + break; + case 'tuesday': + topXAxis.ticks(timeTuesday.every(every)); + break; + case 'wednesday': + topXAxis.ticks(timeWednesday.every(every)); + break; + case 'thursday': + topXAxis.ticks(timeThursday.every(every)); + break; + case 'friday': + topXAxis.ticks(timeFriday.every(every)); + break; + case 'saturday': + topXAxis.ticks(timeSaturday.every(every)); + break; + case 'sunday': + default: + topXAxis.ticks(timeSunday.every(every)); + break; + } break; case 'month': topXAxis.ticks(timeMonth.every(every)); From d9c15b1e7ae42564edfeea824963ed68f4a3fb54 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Thu, 13 Jul 2023 17:26:49 +0200 Subject: [PATCH 12/20] chore: move spec test to it.each --- .../src/diagrams/gantt/parser/gantt.spec.js | 35 ++++--------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js b/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js index 6c1f2e2ca..e7ce1ffa4 100644 --- a/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js +++ b/packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js @@ -181,32 +181,11 @@ row2`; expect(ganttDb.getAccDescription()).toBe(expectedAccDescription); }); - it('should allow for setting the starting weekday to monday for tick intervals', function () { - parser.parse('gantt\nweekday monday'); - expect(ganttDb.getWeekday()).toBe('monday'); - }); - it('should allow for setting the starting weekday to tuesday for tick intervals', function () { - parser.parse('gantt\nweekday tuesday'); - expect(ganttDb.getWeekday()).toBe('tuesday'); - }); - it('should allow for setting the starting weekday to wednesday for tick intervals', function () { - parser.parse('gantt\nweekday wednesday'); - expect(ganttDb.getWeekday()).toBe('wednesday'); - }); - it('should allow for setting the starting weekday to thursday for tick intervals', function () { - parser.parse('gantt\nweekday thursday'); - expect(ganttDb.getWeekday()).toBe('thursday'); - }); - it('should allow for setting the starting weekday to friday for tick intervals', function () { - parser.parse('gantt\nweekday friday'); - expect(ganttDb.getWeekday()).toBe('friday'); - }); - it('should allow for setting the starting weekday to saturday for tick intervals', function () { - parser.parse('gantt\nweekday saturday'); - expect(ganttDb.getWeekday()).toBe('saturday'); - }); - it('should allow for setting the starting weekday to sunday for tick intervals', function () { - parser.parse('gantt\nweekday sunday'); - expect(ganttDb.getWeekday()).toBe('sunday'); - }); + it.each(['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'])( + 'should allow for setting the starting weekday to %s for tick interval', + (day) => { + parser.parse(`gantt\nweekday ${day}`); + expect(ganttDb.getWeekday()).toBe(day); + } + ); }); From 11f2e31ff12cf29fc023e585e41b98684dcce60a Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Fri, 14 Jul 2023 10:04:22 +0200 Subject: [PATCH 13/20] fix: also set weekday value to sunday in clear --- packages/mermaid/src/diagrams/gantt/ganttDb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mermaid/src/diagrams/gantt/ganttDb.js b/packages/mermaid/src/diagrams/gantt/ganttDb.js index d79a57fc2..da838f839 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttDb.js +++ b/packages/mermaid/src/diagrams/gantt/ganttDb.js @@ -67,7 +67,7 @@ export const clear = function () { lastOrder = 0; links = {}; commonClear(); - weekday = undefined; + weekday = 'sunday'; }; export const setAxisFormat = function (txt) { From d0afc3bffeb2ac94191a7146a1e3bb1eb5d09509 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Fri, 14 Jul 2023 14:09:29 +0200 Subject: [PATCH 14/20] feat: validate individual values of weekdays --- .../src/diagrams/gantt/parser/gantt.jison | 56 ++++++++++++------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/packages/mermaid/src/diagrams/gantt/parser/gantt.jison b/packages/mermaid/src/diagrams/gantt/parser/gantt.jison index d83baeea9..068a6fb92 100644 --- a/packages/mermaid/src/diagrams/gantt/parser/gantt.jison +++ b/packages/mermaid/src/diagrams/gantt/parser/gantt.jison @@ -77,25 +77,31 @@ that id. [\s\n] this.popState(); [^\s\n]* return 'click'; -"gantt" return 'gantt'; -"dateFormat"\s[^#\n;]+ return 'dateFormat'; -"inclusiveEndDates" return 'inclusiveEndDates'; -"topAxis" return 'topAxis'; -"axisFormat"\s[^#\n;]+ return 'axisFormat'; -"tickInterval"\s[^#\n;]+ return 'tickInterval'; -"includes"\s[^#\n;]+ return 'includes'; -"excludes"\s[^#\n;]+ return 'excludes'; -"todayMarker"\s[^\n;]+ return 'todayMarker'; -"weekday"\s[^#\n;]+ return 'weekday'; -\d\d\d\d"-"\d\d"-"\d\d return 'date'; -"title"\s[^#\n;]+ return 'title'; -"accDescription"\s[^#\n;]+ return 'accDescription' -"section"\s[^#:\n;]+ return 'section'; -[^#:\n;]+ return 'taskTxt'; -":"[^#\n;]+ return 'taskData'; -":" return ':'; -<> return 'EOF'; -. return 'INVALID'; +"gantt" return 'gantt'; +"dateFormat"\s[^#\n;]+ return 'dateFormat'; +"inclusiveEndDates" return 'inclusiveEndDates'; +"topAxis" return 'topAxis'; +"axisFormat"\s[^#\n;]+ return 'axisFormat'; +"tickInterval"\s[^#\n;]+ return 'tickInterval'; +"includes"\s[^#\n;]+ return 'includes'; +"excludes"\s[^#\n;]+ return 'excludes'; +"todayMarker"\s[^\n;]+ return 'todayMarker'; +.*weekday\s+monday[^\n]* return 'weekday_monday' +.*weekday\s+tuesday[^\n]* return 'weekday_tuesday' +.*weekday\s+wednesday[^\n]* return 'weekday_wednesday' +.*weekday\s+thursday[^\n]* return 'weekday_thursday' +.*weekday\s+friday[^\n]* return 'weekday_friday' +.*weekday\s+saturday[^\n]* return 'weekday_saturday' +.*weekday\s+sunday[^\n]* return 'weekday_sunday' +\d\d\d\d"-"\d\d"-"\d\d return 'date'; +"title"\s[^#\n;]+ return 'title'; +"accDescription"\s[^#\n;]+ return 'accDescription' +"section"\s[^#:\n;]+ return 'section'; +[^#:\n;]+ return 'taskTxt'; +":"[^#\n;]+ return 'taskData'; +":" return ':'; +<> return 'EOF'; +. return 'INVALID'; /lex @@ -122,6 +128,16 @@ line | EOF { $$=[];} ; +weekday + : weekday_monday { yy.setWeekday("monday");} + | weekday_tuesday { yy.setWeekday("tuesday");} + | weekday_wednesday { yy.setWeekday("wednesday");} + | weekday_thursday { yy.setWeekday("thursday");} + | weekday_friday { yy.setWeekday("friday");} + | weekday_saturday { yy.setWeekday("saturday");} + | weekday_sunday { yy.setWeekday("sunday");} + ; + statement : dateFormat {yy.setDateFormat($1.substr(11));$$=$1.substr(11);} | inclusiveEndDates {yy.enableInclusiveEndDates();$$=$1.substr(18);} @@ -131,7 +147,7 @@ statement | excludes {yy.setExcludes($1.substr(9));$$=$1.substr(9);} | includes {yy.setIncludes($1.substr(9));$$=$1.substr(9);} | todayMarker {yy.setTodayMarker($1.substr(12));$$=$1.substr(12);} - | weekday { yy.setWeekday($1.substr(8));$$=$1.substr(8);} + | weekday | title {yy.setDiagramTitle($1.substr(6));$$=$1.substr(6);} | acc_title acc_title_value { $$=$2.trim();yy.setAccTitle($$); } | acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); } From 103321bf72ef48b0c81cc06cb25e36a159c01280 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Sat, 15 Jul 2023 00:17:09 +0200 Subject: [PATCH 15/20] chore: return after scanning weekday in jison --- .../mermaid/src/diagrams/gantt/parser/gantt.jison | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/mermaid/src/diagrams/gantt/parser/gantt.jison b/packages/mermaid/src/diagrams/gantt/parser/gantt.jison index 068a6fb92..1d213834e 100644 --- a/packages/mermaid/src/diagrams/gantt/parser/gantt.jison +++ b/packages/mermaid/src/diagrams/gantt/parser/gantt.jison @@ -86,13 +86,13 @@ that id. "includes"\s[^#\n;]+ return 'includes'; "excludes"\s[^#\n;]+ return 'excludes'; "todayMarker"\s[^\n;]+ return 'todayMarker'; -.*weekday\s+monday[^\n]* return 'weekday_monday' -.*weekday\s+tuesday[^\n]* return 'weekday_tuesday' -.*weekday\s+wednesday[^\n]* return 'weekday_wednesday' -.*weekday\s+thursday[^\n]* return 'weekday_thursday' -.*weekday\s+friday[^\n]* return 'weekday_friday' -.*weekday\s+saturday[^\n]* return 'weekday_saturday' -.*weekday\s+sunday[^\n]* return 'weekday_sunday' +.*weekday\s+monday[^\n]+ return 'weekday_monday' +.*weekday\s+tuesday[^\n]+ return 'weekday_tuesday' +.*weekday\s+wednesday[^\n]+ return 'weekday_wednesday' +.*weekday\s+thursday[^\n]+ return 'weekday_thursday' +.*weekday\s+friday[^\n]+ return 'weekday_friday' +.*weekday\s+saturday[^\n]+ return 'weekday_saturday' +.*weekday\s+sunday[^\n]+ return 'weekday_sunday' \d\d\d\d"-"\d\d"-"\d\d return 'date'; "title"\s[^#\n;]+ return 'title'; "accDescription"\s[^#\n;]+ return 'accDescription' From 90c68f50695d2ccf278ef076debadbb5df8e961d Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Sat, 15 Jul 2023 00:17:09 +0200 Subject: [PATCH 16/20] chore: add tsType for weekday config --- packages/mermaid/src/schemas/config.schema.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mermaid/src/schemas/config.schema.yaml b/packages/mermaid/src/schemas/config.schema.yaml index 8bebd68cf..71e7de477 100644 --- a/packages/mermaid/src/schemas/config.schema.yaml +++ b/packages/mermaid/src/schemas/config.schema.yaml @@ -1549,6 +1549,7 @@ $defs: # JSON Schema definition (maybe we should move these to a seperate file) description: | On which day a week-based interval should start type: string + tsType: 'string | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"' enum: - monday - tuesday @@ -1557,7 +1558,6 @@ $defs: # JSON Schema definition (maybe we should move these to a seperate file) - friday - saturday - sunday - # the sane default is a monday, but it's set to sunday for legacy reasons default: sunday SequenceDiagramConfig: From 803cd826ed7606f45ff084b7f13921623ad67a4e Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Sat, 15 Jul 2023 00:17:10 +0200 Subject: [PATCH 17/20] chore: replace switch-case with map --- .../src/diagrams/gantt/ganttRenderer.js | 64 +++++-------------- 1 file changed, 16 insertions(+), 48 deletions(-) diff --git a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js index def988ed2..522f59e2c 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js +++ b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js @@ -30,6 +30,20 @@ export const setConf = function () { log.debug('Something is calling, setConf, remove the call'); }; +/** + * This will map any day of the week that can be set in the `weekday` option to + * the corresponding d3-time function that is used to calculate the ticks. + */ +const mapWeekdayToTimeFunction = { + monday: timeMonday, + tuesday: timeTuesday, + wednesday: timeWednesday, + thursday: timeThursday, + friday: timeFriday, + saturday: timeSaturday, + sunday: timeSunday, +}; + /** * For this issue: * https://github.com/mermaid-js/mermaid/issues/1618 @@ -580,30 +594,7 @@ export const draw = function (text, id, version, diagObj) { bottomXAxis.ticks(timeDay.every(every)); break; case 'week': - switch (weekday) { - case 'monday': - bottomXAxis.ticks(timeMonday.every(every)); - break; - case 'tuesday': - bottomXAxis.ticks(timeTuesday.every(every)); - break; - case 'wednesday': - bottomXAxis.ticks(timeWednesday.every(every)); - break; - case 'thursday': - bottomXAxis.ticks(timeThursday.every(every)); - break; - case 'friday': - bottomXAxis.ticks(timeFriday.every(every)); - break; - case 'saturday': - bottomXAxis.ticks(timeSaturday.every(every)); - break; - case 'sunday': - default: - bottomXAxis.ticks(timeSunday.every(every)); - break; - } + bottomXAxis.ticks(mapWeekdayToTimeFunction[weekday].every(every)); break; case 'month': bottomXAxis.ticks(timeMonth.every(every)); @@ -644,30 +635,7 @@ export const draw = function (text, id, version, diagObj) { topXAxis.ticks(timeDay.every(every)); break; case 'week': - switch (weekday) { - case 'monday': - topXAxis.ticks(timeMonday.every(every)); - break; - case 'tuesday': - topXAxis.ticks(timeTuesday.every(every)); - break; - case 'wednesday': - topXAxis.ticks(timeWednesday.every(every)); - break; - case 'thursday': - topXAxis.ticks(timeThursday.every(every)); - break; - case 'friday': - topXAxis.ticks(timeFriday.every(every)); - break; - case 'saturday': - topXAxis.ticks(timeSaturday.every(every)); - break; - case 'sunday': - default: - topXAxis.ticks(timeSunday.every(every)); - break; - } + topXAxis.ticks(mapWeekdayToTimeFunction[weekday].every(every)); break; case 'month': topXAxis.ticks(timeMonth.every(every)); From eff9f7e5db28578ed534a1f7322a70d14f2589be Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Sat, 15 Jul 2023 00:24:02 +0200 Subject: [PATCH 18/20] chore: re-generate config --- packages/mermaid/src/config.type.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mermaid/src/config.type.ts b/packages/mermaid/src/config.type.ts index df87e9c40..1163016aa 100644 --- a/packages/mermaid/src/config.type.ts +++ b/packages/mermaid/src/config.type.ts @@ -1067,7 +1067,7 @@ export interface GanttDiagramConfig extends BaseDiagramConfig { * On which day a week-based interval should start * */ - weekday?: "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"; + weekday?: string | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"; } /** * The object containing configurations specific for sequence diagrams From 85515bcf8d47717ba32884c8ec7981ae4ecc710b Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Sat, 15 Jul 2023 15:28:48 +0200 Subject: [PATCH 19/20] chore: leave the newLine out of the jison spec --- .../mermaid/src/diagrams/gantt/parser/gantt.jison | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/mermaid/src/diagrams/gantt/parser/gantt.jison b/packages/mermaid/src/diagrams/gantt/parser/gantt.jison index 1d213834e..f7fd40c1b 100644 --- a/packages/mermaid/src/diagrams/gantt/parser/gantt.jison +++ b/packages/mermaid/src/diagrams/gantt/parser/gantt.jison @@ -86,13 +86,13 @@ that id. "includes"\s[^#\n;]+ return 'includes'; "excludes"\s[^#\n;]+ return 'excludes'; "todayMarker"\s[^\n;]+ return 'todayMarker'; -.*weekday\s+monday[^\n]+ return 'weekday_monday' -.*weekday\s+tuesday[^\n]+ return 'weekday_tuesday' -.*weekday\s+wednesday[^\n]+ return 'weekday_wednesday' -.*weekday\s+thursday[^\n]+ return 'weekday_thursday' -.*weekday\s+friday[^\n]+ return 'weekday_friday' -.*weekday\s+saturday[^\n]+ return 'weekday_saturday' -.*weekday\s+sunday[^\n]+ return 'weekday_sunday' +weekday\s+monday return 'weekday_monday' +weekday\s+tuesday return 'weekday_tuesday' +weekday\s+wednesday return 'weekday_wednesday' +weekday\s+thursday return 'weekday_thursday' +weekday\s+friday return 'weekday_friday' +weekday\s+saturday return 'weekday_saturday' +weekday\s+sunday return 'weekday_sunday' \d\d\d\d"-"\d\d"-"\d\d return 'date'; "title"\s[^#\n;]+ return 'title'; "accDescription"\s[^#\n;]+ return 'accDescription' From 7e39c13836dce0d2401ca5f07364d0e7074e99e2 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Sat, 15 Jul 2023 15:38:21 +0200 Subject: [PATCH 20/20] chore: narrow down tstype --- packages/mermaid/src/config.type.ts | 2 +- packages/mermaid/src/schemas/config.schema.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mermaid/src/config.type.ts b/packages/mermaid/src/config.type.ts index 1163016aa..df87e9c40 100644 --- a/packages/mermaid/src/config.type.ts +++ b/packages/mermaid/src/config.type.ts @@ -1067,7 +1067,7 @@ export interface GanttDiagramConfig extends BaseDiagramConfig { * On which day a week-based interval should start * */ - weekday?: string | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"; + weekday?: "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"; } /** * The object containing configurations specific for sequence diagrams diff --git a/packages/mermaid/src/schemas/config.schema.yaml b/packages/mermaid/src/schemas/config.schema.yaml index 71e7de477..6e5f48d95 100644 --- a/packages/mermaid/src/schemas/config.schema.yaml +++ b/packages/mermaid/src/schemas/config.schema.yaml @@ -1549,7 +1549,7 @@ $defs: # JSON Schema definition (maybe we should move these to a seperate file) description: | On which day a week-based interval should start type: string - tsType: 'string | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"' + tsType: '"monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"' enum: - monday - tuesday