added config as global

This commit is contained in:
Austin Fulbright
2024-08-14 10:51:45 -04:00
parent 3539a35578
commit 299e559aa5

View File

@@ -12,6 +12,9 @@ import type {
} from './gitGraphTypes.js'; } from './gitGraphTypes.js';
import type { GitGraphDiagramConfig } from '../../config.type.js'; import type { GitGraphDiagramConfig } from '../../config.type.js';
const DEFAULT_CONFIG = getConfig();
const DEFAULT_GITGRAPH_CONFIG: Required<GitGraphDiagramConfig> = DEFAULT_CONFIG.gitGraph;
let allCommitsDict = new Map(); let allCommitsDict = new Map();
const LAYOUT_OFFSET = 10; const LAYOUT_OFFSET = 10;
@@ -299,13 +302,12 @@ const drawCommitLabel = (
gLabels: d3.Selection<SVGGElement, unknown, HTMLElement, any>, gLabels: d3.Selection<SVGGElement, unknown, HTMLElement, any>,
commit: Commit, commit: Commit,
commitPosition: CommitPositionOffset, commitPosition: CommitPositionOffset,
pos: number, pos: number
gitGraphConfig: GitGraphDiagramConfig
) => { ) => {
if ( if (
commit.type !== commitType.CHERRY_PICK && commit.type !== commitType.CHERRY_PICK &&
((commit.customId && commit.type === commitType.MERGE) || commit.type !== commitType.MERGE) && ((commit.customId && commit.type === commitType.MERGE) || commit.type !== commitType.MERGE) &&
gitGraphConfig.showCommitLabel DEFAULT_GITGRAPH_CONFIG.showCommitLabel
) { ) {
const wrapper = gLabels.append('g'); const wrapper = gLabels.append('g');
const labelBkg = wrapper.insert('rect').attr('class', 'commit-label-bkg'); const labelBkg = wrapper.insert('rect').attr('class', 'commit-label-bkg');
@@ -335,7 +337,7 @@ const drawCommitLabel = (
text.attr('x', commitPosition.posWithOffset - bbox.width / 2); text.attr('x', commitPosition.posWithOffset - bbox.width / 2);
} }
if (gitGraphConfig.rotateCommitLabel) { if (DEFAULT_GITGRAPH_CONFIG.rotateCommitLabel) {
if (dir === 'TB' || dir === 'BT') { if (dir === 'TB' || dir === 'BT') {
text.attr( text.attr(
'transform', 'transform',
@@ -529,15 +531,14 @@ const drawCommits = (
commits: Map<string, Commit>, commits: Map<string, Commit>,
modifyGraph: boolean modifyGraph: boolean
) => { ) => {
const gitGraphConfig = getConfig().gitGraph; if (!DEFAULT_GITGRAPH_CONFIG) {
if (!gitGraphConfig) {
throw new Error('GitGraph config not found'); throw new Error('GitGraph config not found');
} }
const gBullets = svg.append('g').attr('class', 'commit-bullets'); const gBullets = svg.append('g').attr('class', 'commit-bullets');
const gLabels = svg.append('g').attr('class', 'commit-labels'); const gLabels = svg.append('g').attr('class', 'commit-labels');
let pos = dir === 'TB' || dir === 'BT' ? defaultPos : 0; let pos = dir === 'TB' || dir === 'BT' ? defaultPos : 0;
const keys = [...commits.keys()]; const keys = [...commits.keys()];
const isParallelCommits = gitGraphConfig?.parallelCommits ?? false; const isParallelCommits = DEFAULT_GITGRAPH_CONFIG?.parallelCommits ?? false;
const sortKeys = (a: string, b: string) => { const sortKeys = (a: string, b: string) => {
const seqA = commits.get(a)?.seq; const seqA = commits.get(a)?.seq;
@@ -571,7 +572,7 @@ const drawCommits = (
const commitSymbolType = commit.customType ?? commit.type; const commitSymbolType = commit.customType ?? commit.type;
const branchIndex = branchPos.get(commit.branch)?.index ?? 0; const branchIndex = branchPos.get(commit.branch)?.index ?? 0;
drawCommitBullet(gBullets, commit, commitPosition, typeClass, branchIndex, commitSymbolType); drawCommitBullet(gBullets, commit, commitPosition, typeClass, branchIndex, commitSymbolType);
drawCommitLabel(gLabels, commit, commitPosition, pos, gitGraphConfig); drawCommitLabel(gLabels, commit, commitPosition, pos);
drawCommitTags(gLabels, commit, commitPosition, pos); drawCommitTags(gLabels, commit, commitPosition, pos);
} }
if (dir === 'TB' || dir === 'BT') { if (dir === 'TB' || dir === 'BT') {
@@ -830,7 +831,6 @@ const drawBranches = (
svg: d3.Selection<d3.BaseType, unknown, HTMLElement, any>, svg: d3.Selection<d3.BaseType, unknown, HTMLElement, any>,
branches: { name: string }[] branches: { name: string }[]
) => { ) => {
const gitGraphConfig = getConfig().gitGraph;
const g = svg.append('g'); const g = svg.append('g');
branches.forEach((branch, index) => { branches.forEach((branch, index) => {
const adjustIndexForTheme = index % THEME_COLOR_LIMIT; const adjustIndexForTheme = index % THEME_COLOR_LIMIT;
@@ -876,14 +876,14 @@ const drawBranches = (
.attr('class', 'branchLabelBkg label' + adjustIndexForTheme) .attr('class', 'branchLabelBkg label' + adjustIndexForTheme)
.attr('rx', 4) .attr('rx', 4)
.attr('ry', 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('y', -bbox.height / 2 + 8)
.attr('width', bbox.width + 18) .attr('width', bbox.width + 18)
.attr('height', bbox.height + 4); .attr('height', bbox.height + 4);
label.attr( label.attr(
'transform', 'transform',
'translate(' + 'translate(' +
(-bbox.width - 14 - (gitGraphConfig?.rotateCommitLabel === true ? 30 : 0)) + (-bbox.width - 14 - (DEFAULT_GITGRAPH_CONFIG?.rotateCommitLabel === true ? 30 : 0)) +
', ' + ', ' +
(pos - bbox.height / 2 - 1) + (pos - bbox.height / 2 - 1) +
')' ')'
@@ -914,14 +914,12 @@ const setBranchPosition = function (
export const draw: DrawDefinition = function (txt, id, ver, diagObj) { export const draw: DrawDefinition = function (txt, id, ver, diagObj) {
clear(); clear();
const conf = getConfig();
const gitGraphConfig = conf.gitGraph;
log.debug('in gitgraph renderer', txt + '\n', 'id:', id, ver); log.debug('in gitgraph renderer', txt + '\n', 'id:', id, ver);
if (!gitGraphConfig) { if (!DEFAULT_GITGRAPH_CONFIG) {
throw new Error('GitGraph config not found'); 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; const db = diagObj.db as GitGraphDBRenderProvider;
allCommitsDict = db.getCommits(); allCommitsDict = db.getCommits();
const branches = db.getBranchesAsObjArray(); const branches = db.getBranchesAsObjArray();
@@ -944,7 +942,7 @@ export const draw: DrawDefinition = function (txt, id, ver, diagObj) {
}); });
drawCommits(diagram, allCommitsDict, false); drawCommits(diagram, allCommitsDict, false);
if (gitGraphConfig.showBranches) { if (DEFAULT_GITGRAPH_CONFIG.showBranches) {
drawBranches(diagram, branches); drawBranches(diagram, branches);
} }
drawArrows(diagram, allCommitsDict); drawArrows(diagram, allCommitsDict);
@@ -953,12 +951,17 @@ export const draw: DrawDefinition = function (txt, id, ver, diagObj) {
utils.insertTitle( utils.insertTitle(
diagram, diagram,
'gitTitleText', 'gitTitleText',
gitGraphConfig.titleTopMargin ?? 0, DEFAULT_GITGRAPH_CONFIG.titleTopMargin ?? 0,
db.getDiagramTitle() db.getDiagramTitle()
); );
// Setup the view box and size of the svg element // 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 { export default {
@@ -1321,8 +1324,7 @@ if (import.meta.vitest) {
branchPos.set('main', { pos: 0, index: 0 }); branchPos.set('main', { pos: 0, index: 0 });
branchPos.set('develop', { pos: 107.49609375, index: 1 }); branchPos.set('develop', { pos: 107.49609375, index: 1 });
branchPos.set('feature', { pos: 225.70703125, index: 2 }); 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) => { commits.forEach((commit, key) => {
if (commit.parents.length > 0) { if (commit.parents.length > 0) {
curPos = calculateCommitPosition(commit); curPos = calculateCommitPosition(commit);
@@ -1350,7 +1352,7 @@ if (import.meta.vitest) {
}); });
}); });
}); });
DEFAULT_GITGRAPH_CONFIG.parallelCommits = false;
it('add', () => { it('add', () => {
commitPos.set('parent1', { x: 1, y: 1 }); commitPos.set('parent1', { x: 1, y: 1 });
commitPos.set('parent2', { x: 2, y: 2 }); commitPos.set('parent2', { x: 2, y: 2 });