Updated code review changes

This commit is contained in:
Subhash Halder 2023-09-29 18:59:05 +05:30
parent 8b04c2ce88
commit 0d348b7994
9 changed files with 47 additions and 35 deletions

View File

@ -1,4 +1,4 @@
import type { SVGGType } from '../../../xychartDb.js'; import type { Group } from '../../../../../diagram-api/types.js';
import type { import type {
AxisDataType, AxisDataType,
ChartComponent, ChartComponent,
@ -25,9 +25,9 @@ export function getAxis(
data: AxisDataType, data: AxisDataType,
axisConfig: XYChartAxisConfig, axisConfig: XYChartAxisConfig,
axisThemeConfig: XYChartAxisThemeConfig, axisThemeConfig: XYChartAxisThemeConfig,
tmpSVGGElem: SVGGType tmpSVGGroup: Group
): Axis { ): Axis {
const textDimansionCalculator = new TextDimensionCalculatorWithFont(tmpSVGGElem); const textDimansionCalculator = new TextDimensionCalculatorWithFont(tmpSVGGroup);
if (isBandAxisData(data)) { if (isBandAxisData(data)) {
return new BandAxis( return new BandAxis(
axisConfig, axisConfig,

View File

@ -1,4 +1,4 @@
import type { SVGGType } from '../../xychartDb.js'; import type { Group } from '../../../../diagram-api/types.js';
import type { import type {
BoundingRect, BoundingRect,
ChartComponent, ChartComponent,
@ -84,8 +84,8 @@ export function getChartTitleComponent(
chartConfig: XYChartConfig, chartConfig: XYChartConfig,
chartData: XYChartData, chartData: XYChartData,
chartThemeConfig: XYChartThemeConfig, chartThemeConfig: XYChartThemeConfig,
tmpSVGGElem: SVGGType tmpSVGGroup: Group
): ChartComponent { ): ChartComponent {
const textDimensionCalculator = new TextDimensionCalculatorWithFont(tmpSVGGElem); const textDimensionCalculator = new TextDimensionCalculatorWithFont(tmpSVGGroup);
return new ChartTitle(textDimensionCalculator, chartConfig, chartData, chartThemeConfig); return new ChartTitle(textDimensionCalculator, chartConfig, chartData, chartThemeConfig);
} }

View File

@ -16,7 +16,7 @@ export interface Plot extends ChartComponent {
setAxes(xAxis: Axis, yAxis: Axis): void; setAxes(xAxis: Axis, yAxis: Axis): void;
} }
export class Plot implements Plot { export class BasePlot implements Plot {
private boundingRect: BoundingRect; private boundingRect: BoundingRect;
private xAxis?: Axis; private xAxis?: Axis;
private yAxis?: Axis; private yAxis?: Axis;
@ -93,5 +93,5 @@ export function getPlotComponent(
chartData: XYChartData, chartData: XYChartData,
chartThemeConfig: XYChartThemeConfig chartThemeConfig: XYChartThemeConfig
): Plot { ): Plot {
return new Plot(chartConfig, chartData, chartThemeConfig); return new BasePlot(chartConfig, chartData, chartThemeConfig);
} }

View File

@ -1,4 +1,4 @@
import type { SVGGType } from '../xychartDb.js'; import type { Group } from '../../../diagram-api/types.js';
import type { DrawableElem, XYChartConfig, XYChartData, XYChartThemeConfig } from './interfaces.js'; import type { DrawableElem, XYChartConfig, XYChartData, XYChartThemeConfig } from './interfaces.js';
import { Orchestrator } from './orchestrator.js'; import { Orchestrator } from './orchestrator.js';
@ -7,9 +7,9 @@ export class XYChartBuilder {
config: XYChartConfig, config: XYChartConfig,
chartData: XYChartData, chartData: XYChartData,
chartThemeConfig: XYChartThemeConfig, chartThemeConfig: XYChartThemeConfig,
tmpSVGGElem: SVGGType tmpSVGGroup: Group
): DrawableElem[] { ): DrawableElem[] {
const orchestrator = new Orchestrator(config, chartData, chartThemeConfig, tmpSVGGElem); const orchestrator = new Orchestrator(config, chartData, chartThemeConfig, tmpSVGGroup);
return orchestrator.getDrawableElement(); return orchestrator.getDrawableElement();
} }
} }

View File

@ -1,4 +1,3 @@
import type { SVGGType } from '../xychartDb.js';
import type { import type {
ChartComponent, ChartComponent,
DrawableElem, DrawableElem,
@ -12,6 +11,7 @@ import { getAxis } from './components/axis/index.js';
import { getChartTitleComponent } from './components/chartTitle.js'; import { getChartTitleComponent } from './components/chartTitle.js';
import type { Plot } from './components/plot/index.js'; import type { Plot } from './components/plot/index.js';
import { getPlotComponent } from './components/plot/index.js'; import { getPlotComponent } from './components/plot/index.js';
import type { Group } from '../../../diagram-api/types.js';
export class Orchestrator { export class Orchestrator {
private componentStore: { private componentStore: {
@ -24,10 +24,10 @@ export class Orchestrator {
private chartConfig: XYChartConfig, private chartConfig: XYChartConfig,
private chartData: XYChartData, private chartData: XYChartData,
chartThemeConfig: XYChartThemeConfig, chartThemeConfig: XYChartThemeConfig,
tmpSVGGElem: SVGGType tmpSVGGroup: Group
) { ) {
this.componentStore = { this.componentStore = {
title: getChartTitleComponent(chartConfig, chartData, chartThemeConfig, tmpSVGGElem), title: getChartTitleComponent(chartConfig, chartData, chartThemeConfig, tmpSVGGroup),
plot: getPlotComponent(chartConfig, chartData, chartThemeConfig), plot: getPlotComponent(chartConfig, chartData, chartThemeConfig),
xAxis: getAxis( xAxis: getAxis(
chartData.xAxis, chartData.xAxis,
@ -38,7 +38,7 @@ export class Orchestrator {
tickColor: chartThemeConfig.xAxisTickColor, tickColor: chartThemeConfig.xAxisTickColor,
axisLineColor: chartThemeConfig.xAxisLineColor, axisLineColor: chartThemeConfig.xAxisLineColor,
}, },
tmpSVGGElem tmpSVGGroup
), ),
yAxis: getAxis( yAxis: getAxis(
chartData.yAxis, chartData.yAxis,
@ -49,7 +49,7 @@ export class Orchestrator {
tickColor: chartThemeConfig.yAxisTickColor, tickColor: chartThemeConfig.yAxisTickColor,
axisLineColor: chartThemeConfig.yAxisLineColor, axisLineColor: chartThemeConfig.yAxisLineColor,
}, },
tmpSVGGElem tmpSVGGroup
), ),
}; };
} }

View File

@ -1,13 +1,13 @@
import type { Dimension } from './interfaces.js'; import type { Dimension } from './interfaces.js';
import { computeDimensionOfText } from '../../../rendering-util/createText.js'; import { computeDimensionOfText } from '../../../rendering-util/createText.js';
import type { SVGGType } from '../xychartDb.js'; import type { Group } from '../../../diagram-api/types.js';
export interface TextDimensionCalculator { export interface TextDimensionCalculator {
getMaxDimension(texts: string[], fontSize: number): Dimension; getMaxDimension(texts: string[], fontSize: number): Dimension;
} }
export class TextDimensionCalculatorWithFont implements TextDimensionCalculator { export class TextDimensionCalculatorWithFont implements TextDimensionCalculator {
constructor(private parentGroup: SVGGType) {} constructor(private parentGroup: Group) {}
getMaxDimension(texts: string[], fontSize: number): Dimension { getMaxDimension(texts: string[], fontSize: number): Dimension {
if (!this.parentGroup) { if (!this.parentGroup) {
return { return {
@ -28,8 +28,10 @@ export class TextDimensionCalculatorWithFont implements TextDimensionCalculator
for (const t of texts) { for (const t of texts) {
const bbox = computeDimensionOfText(elem, 1, t); const bbox = computeDimensionOfText(elem, 1, t);
dimension.width = Math.max(dimension.width, bbox.width); const width = bbox ? bbox.width : t.length * fontSize;
dimension.height = Math.max(dimension.height, bbox.height); const height = bbox ? bbox.height : fontSize;
dimension.width = Math.max(dimension.width, width);
dimension.height = Math.max(dimension.height, height);
} }
elem.remove(); elem.remove();
return dimension; return dimension;

View File

@ -1,4 +1,3 @@
import type { Selection } from 'd3-selection';
import { import {
clear as commonClear, clear as commonClear,
getAccDescription, getAccDescription,
@ -22,12 +21,11 @@ import type {
XYChartThemeConfig, XYChartThemeConfig,
} from './chartBuilder/interfaces.js'; } from './chartBuilder/interfaces.js';
import { isBandAxisData, isLinearAxisData } from './chartBuilder/interfaces.js'; import { isBandAxisData, isLinearAxisData } from './chartBuilder/interfaces.js';
import type { Group } from '../../diagram-api/types.js';
export type SVGGType = Selection<SVGGElement, unknown, Element | null, unknown>;
let plotIndex = 0; let plotIndex = 0;
let tmpSVGGElem: SVGGType; let tmpSVGGroup: Group;
let xyChartConfig: XYChartConfig = getChartDefaultConfig(); let xyChartConfig: XYChartConfig = getChartDefaultConfig();
let xyChartThemeConfig: XYChartThemeConfig = getChartDefaultThemeConfig(); let xyChartThemeConfig: XYChartThemeConfig = getChartDefaultThemeConfig();
@ -77,8 +75,8 @@ function textSanitizer(text: string) {
return sanitizeText(text.trim(), config); return sanitizeText(text.trim(), config);
} }
function setTmpSVGG(SVGG: SVGGType) { function setTmpSVGG(SVGG: Group) {
tmpSVGGElem = SVGG; tmpSVGGroup = SVGG;
} }
function setOrientation(oriantation: string) { function setOrientation(oriantation: string) {
if (oriantation === 'horizontal') { if (oriantation === 'horizontal') {
@ -186,7 +184,7 @@ function getDrawableElem(): DrawableElem[] {
throw Error('No Plot to render, please provide a plot with some data'); throw Error('No Plot to render, please provide a plot with some data');
} }
xyChartData.title = getDiagramTitle(); xyChartData.title = getDiagramTitle();
return XYChartBuilder.build(xyChartConfig, xyChartData, xyChartThemeConfig, tmpSVGGElem); return XYChartBuilder.build(xyChartConfig, xyChartData, xyChartThemeConfig, tmpSVGGroup);
} }
function getChartThemeConfig() { function getChartThemeConfig() {

View File

@ -1,12 +1,16 @@
import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-api/types.js'; import type {
DiagramDetector,
DiagramLoader,
ExternalDiagramDefinition,
} from '../../diagram-api/types.js';
const id = 'xychart'; const id = 'xychart';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*xychart/i) !== null; return /^\s*xychart/i.test(txt);
}; };
const loader = async () => { const loader: DiagramLoader = async () => {
const { diagram } = await import('./xychartDiagram.js'); const { diagram } = await import('./xychartDiagram.js');
return { id, diagram }; return { id, diagram };
}; };

View File

@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
// @ts-nocheck TODO: Fix types // @ts-nocheck TODO: Fix types
import type { Group } from '../diagram-api/types.js';
import type { D3TSpanElement, D3TextElement } from '../diagrams/common/commonTypes.js';
import { log } from '../logger.js'; import { log } from '../logger.js';
import { decodeEntities } from '../mermaidAPI.js'; import { decodeEntities } from '../mermaidAPI.js';
import { markdownToHTML, markdownToLines } from '../rendering-util/handle-markdown-text.js'; import { markdownToHTML, markdownToLines } from '../rendering-util/handle-markdown-text.js';
@ -76,12 +78,18 @@ function computeWidthOfText(parentNode: any, lineHeight: number, line: MarkdownL
return textLength; return textLength;
} }
export function computeDimensionOfText(parentNode: any, lineHeight: number, text: string) { export function computeDimensionOfText(
const testElement = parentNode.append('text'); parentNode: Group,
const testSpan = createTspan(testElement, 1, lineHeight); lineHeight: number,
text: string
): DOMRect | undefined {
const testElement: D3TextElement = parentNode.append('text');
const testSpan: D3TSpanElement = createTspan(testElement, 1, lineHeight);
updateTextContentAndStyles(testSpan, [{ content: text, type: 'normal' }]); updateTextContentAndStyles(testSpan, [{ content: text, type: 'normal' }]);
const textDimension = testSpan.node().getBoundingClientRect(); const textDimension: DOMRect | undefined = testSpan.node()?.getBoundingClientRect();
testElement.remove(); if (textDimension) {
testElement.remove();
}
return textDimension; return textDimension;
} }