fix(dep-graph): fix label width calculation (#10044)

This commit is contained in:
Philip Fulcher 2022-04-28 13:08:54 -06:00 committed by GitHub
parent b31be80c02
commit 01fa62dcb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 24 deletions

View File

@ -0,0 +1,28 @@
import { NodeSingular } from 'cytoscape';
export class LabelWidthCalculator {
private cache: Map<string, number> = new Map<string, number>();
private ctx: CanvasRenderingContext2D;
calculateWidth(node: NodeSingular) {
if (!this.ctx) {
this.ctx = document.createElement('canvas').getContext('2d');
const fStyle = node.style('font-style');
const size = node.style('font-size');
const family = node.style('font-family');
const weight = node.style('font-weight');
this.ctx.font = fStyle + ' ' + weight + ' ' + size + ' ' + family;
}
const label = node.data('id');
if (this.cache.has(label)) {
return this.cache.get(label);
} else {
const width = this.ctx.measureText(node.data('id')).width;
this.cache.set(label, width);
return width;
}
}
}

View File

@ -2,6 +2,9 @@ import { Stylesheet } from 'cytoscape';
import { selectValueByThemeDynamic } from '../theme-resolver';
import { FONTS } from './fonts';
import { NrwlPalette } from './palette';
import { LabelWidthCalculator } from './label-width';
const labelWidthCalculator = new LabelWidthCalculator();
const allNodes: Stylesheet = {
selector: 'node',
@ -19,7 +22,8 @@ const allNodes: Stylesheet = {
'padding-left': '16px',
color: selectValueByThemeDynamic(NrwlPalette.white, NrwlPalette.black),
label: 'data(id)',
width: (node) => node.data('id').length * 16,
// width: (node) => node.data('id').length * 16,
width: (node) => labelWidthCalculator.calculateWidth(node),
backgroundColor: selectValueByThemeDynamic(
NrwlPalette.black,
NrwlPalette.white
@ -28,26 +32,6 @@ const allNodes: Stylesheet = {
'background-color, border-color, line-color, target-arrow-color',
'transition-duration': 250,
'transition-timing-function': 'ease-out',
},
};
const appNodes: Stylesheet = {
selector: 'node[type="app"]',
style: {
shape: 'round-rectangle',
},
};
const libNodes: Stylesheet = {
selector: 'node[type="lib"]',
style: {
shape: 'round-rectangle',
},
};
const e2eNodes: Stylesheet = {
selector: 'node[type="e2e"]',
style: {
shape: 'round-rectangle',
},
};
@ -109,9 +93,6 @@ const transparentParentNodes: Stylesheet = {
export const nodeStyles = [
allNodes,
appNodes,
libNodes,
e2eNodes,
focusedNodes,
affectedNodes,
parentNodes,