From 299e559aa518da4907dd624c76f866bed2428fda Mon Sep 17 00:00:00 2001 From: Austin Fulbright Date: Wed, 14 Aug 2024 10:51:45 -0400 Subject: [PATCH] added config as global --- .../src/diagrams/git/gitGraphRenderer.ts | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphRenderer.ts b/packages/mermaid/src/diagrams/git/gitGraphRenderer.ts index b187bee91..1aba40cec 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphRenderer.ts +++ b/packages/mermaid/src/diagrams/git/gitGraphRenderer.ts @@ -12,6 +12,9 @@ import type { } from './gitGraphTypes.js'; import type { GitGraphDiagramConfig } from '../../config.type.js'; +const DEFAULT_CONFIG = getConfig(); +const DEFAULT_GITGRAPH_CONFIG: Required = DEFAULT_CONFIG.gitGraph; + let allCommitsDict = new Map(); const LAYOUT_OFFSET = 10; @@ -299,13 +302,12 @@ const drawCommitLabel = ( gLabels: d3.Selection, commit: Commit, commitPosition: CommitPositionOffset, - pos: number, - gitGraphConfig: GitGraphDiagramConfig + pos: number ) => { if ( commit.type !== commitType.CHERRY_PICK && ((commit.customId && commit.type === commitType.MERGE) || commit.type !== commitType.MERGE) && - gitGraphConfig.showCommitLabel + DEFAULT_GITGRAPH_CONFIG.showCommitLabel ) { const wrapper = gLabels.append('g'); const labelBkg = wrapper.insert('rect').attr('class', 'commit-label-bkg'); @@ -335,7 +337,7 @@ const drawCommitLabel = ( text.attr('x', commitPosition.posWithOffset - bbox.width / 2); } - if (gitGraphConfig.rotateCommitLabel) { + if (DEFAULT_GITGRAPH_CONFIG.rotateCommitLabel) { if (dir === 'TB' || dir === 'BT') { text.attr( 'transform', @@ -529,15 +531,14 @@ const drawCommits = ( commits: Map, modifyGraph: boolean ) => { - const gitGraphConfig = getConfig().gitGraph; - if (!gitGraphConfig) { + if (!DEFAULT_GITGRAPH_CONFIG) { throw new Error('GitGraph config not found'); } const gBullets = svg.append('g').attr('class', 'commit-bullets'); const gLabels = svg.append('g').attr('class', 'commit-labels'); let pos = dir === 'TB' || dir === 'BT' ? defaultPos : 0; const keys = [...commits.keys()]; - const isParallelCommits = gitGraphConfig?.parallelCommits ?? false; + const isParallelCommits = DEFAULT_GITGRAPH_CONFIG?.parallelCommits ?? false; const sortKeys = (a: string, b: string) => { const seqA = commits.get(a)?.seq; @@ -571,7 +572,7 @@ const drawCommits = ( const commitSymbolType = commit.customType ?? commit.type; const branchIndex = branchPos.get(commit.branch)?.index ?? 0; drawCommitBullet(gBullets, commit, commitPosition, typeClass, branchIndex, commitSymbolType); - drawCommitLabel(gLabels, commit, commitPosition, pos, gitGraphConfig); + drawCommitLabel(gLabels, commit, commitPosition, pos); drawCommitTags(gLabels, commit, commitPosition, pos); } if (dir === 'TB' || dir === 'BT') { @@ -830,7 +831,6 @@ const drawBranches = ( svg: d3.Selection, branches: { name: string }[] ) => { - const gitGraphConfig = getConfig().gitGraph; const g = svg.append('g'); branches.forEach((branch, index) => { const adjustIndexForTheme = index % THEME_COLOR_LIMIT; @@ -876,14 +876,14 @@ const drawBranches = ( .attr('class', 'branchLabelBkg label' + adjustIndexForTheme) .attr('rx', 4) .attr('ry', 4) - .attr('x', -bbox.width - 4 - (gitGraphConfig?.rotateCommitLabel === true ? 30 : 0)) + .attr('x', -bbox.width - 4 - (DEFAULT_GITGRAPH_CONFIG?.rotateCommitLabel === true ? 30 : 0)) .attr('y', -bbox.height / 2 + 8) .attr('width', bbox.width + 18) .attr('height', bbox.height + 4); label.attr( 'transform', 'translate(' + - (-bbox.width - 14 - (gitGraphConfig?.rotateCommitLabel === true ? 30 : 0)) + + (-bbox.width - 14 - (DEFAULT_GITGRAPH_CONFIG?.rotateCommitLabel === true ? 30 : 0)) + ', ' + (pos - bbox.height / 2 - 1) + ')' @@ -914,14 +914,12 @@ const setBranchPosition = function ( export const draw: DrawDefinition = function (txt, id, ver, diagObj) { clear(); - const conf = getConfig(); - const gitGraphConfig = conf.gitGraph; log.debug('in gitgraph renderer', txt + '\n', 'id:', id, ver); - if (!gitGraphConfig) { + if (!DEFAULT_GITGRAPH_CONFIG) { throw new Error('GitGraph config not found'); } - const rotateCommitLabel = gitGraphConfig.rotateCommitLabel ?? false; + const rotateCommitLabel = DEFAULT_GITGRAPH_CONFIG.rotateCommitLabel ?? false; const db = diagObj.db as GitGraphDBRenderProvider; allCommitsDict = db.getCommits(); const branches = db.getBranchesAsObjArray(); @@ -944,7 +942,7 @@ export const draw: DrawDefinition = function (txt, id, ver, diagObj) { }); drawCommits(diagram, allCommitsDict, false); - if (gitGraphConfig.showBranches) { + if (DEFAULT_GITGRAPH_CONFIG.showBranches) { drawBranches(diagram, branches); } drawArrows(diagram, allCommitsDict); @@ -953,12 +951,17 @@ export const draw: DrawDefinition = function (txt, id, ver, diagObj) { utils.insertTitle( diagram, 'gitTitleText', - gitGraphConfig.titleTopMargin ?? 0, + DEFAULT_GITGRAPH_CONFIG.titleTopMargin ?? 0, db.getDiagramTitle() ); // Setup the view box and size of the svg element - setupGraphViewbox(undefined, diagram, gitGraphConfig.diagramPadding, gitGraphConfig.useMaxWidth); + setupGraphViewbox( + undefined, + diagram, + DEFAULT_GITGRAPH_CONFIG.diagramPadding, + DEFAULT_GITGRAPH_CONFIG.useMaxWidth + ); }; export default { @@ -1321,8 +1324,7 @@ if (import.meta.vitest) { branchPos.set('main', { pos: 0, index: 0 }); branchPos.set('develop', { pos: 107.49609375, index: 1 }); branchPos.set('feature', { pos: 225.70703125, index: 2 }); - //TODO: need to make sure you set the parallel commits to true - + DEFAULT_GITGRAPH_CONFIG.parallelCommits = true; commits.forEach((commit, key) => { if (commit.parents.length > 0) { curPos = calculateCommitPosition(commit); @@ -1350,7 +1352,7 @@ if (import.meta.vitest) { }); }); }); - + DEFAULT_GITGRAPH_CONFIG.parallelCommits = false; it('add', () => { commitPos.set('parent1', { x: 1, y: 1 }); commitPos.set('parent2', { x: 2, y: 2 });