Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove deprecated dom-util #307

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/unit/util/style-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createDom } from '@antv/dom-util';
import { createDOM } from '@antv/util';
import { applyStyleSheet } from '../../../src/util';

describe('getStyle', () => {
test('applyStyleSheet', async () => {
const dom = createDom("<div class='a'><div class='b'><div class='c'></div><div class='d'></div></div></div>");
const dom = createDOM("<div class='a'><div class='b'><div class='c'></div><div class='d'></div></div></div>");
const styleSheet = {
'.a': {
width: '100px',
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@
"component"
],
"dependencies": {
"@antv/dom-util": "^2.0.3",
"@antv/g": "^5.15.15",
"@antv/scale": "^0.4.3",
"@antv/util": "^3.3.1",
"@antv/util": "^3.3.5",
"svg-path-parser": "^1.1.0"
},
"peerDependencies": {
Expand Down
21 changes: 9 additions & 12 deletions src/ui/axis/guides/labels.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IAnimation } from '@antv/g';
import { get, isFunction, memoize } from '@antv/util';
import { get, isFunction } from '@antv/util';
import type { StandardAnimationOption } from '../../../animation';
import { fadeOut, onAnimateFinished, onAnimatesFinished, transition, transitionShape } from '../../../animation';
import type { DisplayObject, TextStyleProps } from '../../../shapes';
Expand Down Expand Up @@ -30,21 +30,18 @@ import { getFactor } from '../utils';
import { getValuePos } from './line';
import { filterExec, getCallbackStyle, getLabelVector, getLineTangentVector } from './utils';

const angleNormalizer = (angle: number) => {
function angleNormalizer(angle: number) {
let normalizedAngle = angle;
while (normalizedAngle < 0) normalizedAngle += 360;
return Math.round(normalizedAngle % 360);
};
}

const getAngle = memoize(
(v1: Vector2, v2: Vector2) => {
const [x1, y1] = v1;
const [x2, y2] = v2;
const [dot, det] = [x1 * x2 + y1 * y2, x1 * y2 - y1 * x2];
return Math.atan2(det, dot);
},
(v1, v2) => [...v1, ...v2].join()
);
function getAngle(v1: Vector2, v2: Vector2) {
const [x1, y1] = v1;
const [x2, y2] = v2;
const [dot, det] = [x1 * x2 + y1 * y2, x1 * y2 - y1 * x2];
return Math.atan2(det, dot);
}

/** to correct label rotation to avoid inverted character */
function correctLabelRotation(_rotate: number) {
Expand Down
43 changes: 17 additions & 26 deletions src/ui/axis/guides/line.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, memoize } from '@antv/util';
import { get } from '@antv/util';
import type { AnimationResult, StandardAnimationOption } from '../../../animation';
import { transition } from '../../../animation';
import type { DisplayObject, Line } from '../../../shapes';
Expand All @@ -21,32 +21,23 @@ type LineDatum = {
className: string;
};

export const getLinearValuePos = memoize(
(value: number, attr: RequiredLinearAxisStyleProps): Vector2 => {
const {
startPos: [sx, sy],
endPos: [ex, ey],
} = attr;
const [dx, dy] = [ex - sx, ey - sy];
return [sx + dx * value, sy + dy * value];
},
(value, attr) => [value, ...attr.startPos, ...attr.endPos].join()
);
export function getLinearValuePos(value: number, attr: RequiredLinearAxisStyleProps): Vector2 {
const {
startPos: [sx, sy],
endPos: [ex, ey],
} = attr;
const [dx, dy] = [ex - sx, ey - sy];
return [sx + dx * value, sy + dy * value];
}

export const getArcValuePos = memoize(
(value: number, attr: RequiredArcAxisStyleProps): Vector2 => {
const {
radius,
center: [cx, cy],
} = attr;
const angle = degToRad(getLineAngle(value, attr));
return [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)];
},
(value, attr: RequiredArcAxisStyleProps) => {
const { startAngle, endAngle, radius, center } = attr;
return [value, startAngle, endAngle, radius, ...center].join();
}
);
export function getArcValuePos(value: number, attr: RequiredArcAxisStyleProps): Vector2 {
const {
radius,
center: [cx, cy],
} = attr;
const angle = degToRad(getLineAngle(value, attr));
return [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)];
}

export function getValuePos(value: number, attr: RequiredAxisStyleProps) {
if (attr.type === 'linear') return getLinearValuePos(value, attr);
Expand Down
19 changes: 8 additions & 11 deletions src/ui/axis/guides/ticks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isFunction, memoize } from '@antv/util';
import { isFunction } from '@antv/util';
import type { GenericAnimation, StandardAnimationOption } from '../../../animation';
import { fadeOut, onAnimateFinished, transition } from '../../../animation';
import type { Group } from '../../../shapes';
Expand All @@ -15,16 +15,13 @@ export function getTickVector(value: number, attr: RequiredAxisStyleProps): Vect
return getDirectionVector(value, attr.tickDirection, attr);
}

export const getTickPoints = memoize(
(unitVector: Vector2, tickLength: number) => {
const [dx, dy] = unitVector;
return [
[0, 0],
[dx * tickLength, dy * tickLength],
];
},
(unitVector, tickLength) => [...unitVector, tickLength].join()
);
export function getTickPoints(unitVector: Vector2, tickLength: number) {
const [dx, dy] = unitVector;
return [
[0, 0],
[dx * tickLength, dy * tickLength],
];
}

function getTickLineLayout(
datum: AxisDatum,
Expand Down
43 changes: 16 additions & 27 deletions src/ui/axis/guides/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isFunction, memoize } from '@antv/util';
import { isFunction } from '@antv/util';
import type { CallableObject, Vector2 } from '../../../types';
import { degToRad, getCallbackValue, normalize, vertical } from '../../../util';
import type {
Expand Down Expand Up @@ -35,34 +35,23 @@ export function filterExec<T>(data: T[], filter?: (...args: any) => boolean): T[

/** ---- to avoid cycle dependency */

export const getLineAngle = memoize(
(value: number, attr: RequiredArcAxisStyleProps) => {
const { startAngle, endAngle } = attr;
return (endAngle - startAngle) * value + startAngle;
},
(value, attr) => [value, attr.startAngle, attr.endAngle].join()
);

export const getLineTangentVector = memoize(
(value: number, attr: RequiredAxisStyleProps) => {
if (attr.type === 'linear') {
const {
startPos: [startX, startY],
endPos: [endX, endY],
} = attr;
const [dx, dy] = [endX - startX, endY - startY];
return normalize([dx, dy]);
}
export function getLineAngle(value: number, attr: RequiredArcAxisStyleProps) {
const { startAngle, endAngle } = attr;
return (endAngle - startAngle) * value + startAngle;
}

const angle = degToRad(getLineAngle(value, attr));
return [-Math.sin(angle), Math.cos(angle)] as Vector2;
},
(value, attr: RequiredAxisStyleProps) => {
const dependencies = baseDependencies(attr);
attr.type === 'arc' && dependencies.push(value);
return dependencies.join();
export function getLineTangentVector(value: number, attr: RequiredAxisStyleProps) {
if (attr.type === 'linear') {
const {
startPos: [startX, startY],
endPos: [endX, endY],
} = attr;
const [dx, dy] = [endX - startX, endY - startY];
return normalize([dx, dy]);
}
);
const angle = degToRad(getLineAngle(value, attr));
return [-Math.sin(angle), Math.cos(angle)] as Vector2;
}

export function getDirectionVector(value: number, direction: Direction, attr: RequiredAxisStyleProps): Vector2 {
const tangentVector = getLineTangentVector(value, attr);
Expand Down
17 changes: 7 additions & 10 deletions src/ui/legend/continuous.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CustomEvent } from '@antv/g';
import { Linear } from '@antv/scale';
import { clamp, isUndefined, memoize } from '@antv/util';
import { clamp, isUndefined } from '@antv/util';
import { Component } from '../../core';
import type { DisplayObject, TextStyleProps } from '../../shapes';
import { Group } from '../../shapes';
Expand Down Expand Up @@ -36,15 +36,12 @@ import { getSafetySelections, getStepValueByValue, ifHorizontal } from './utils'

export type { ContinuousOptions, ContinuousStyleProps };

const getMinMax = memoize(
(data: ContinuousDatum[]) => {
return {
min: Math.min(...data.map((d) => d.value)),
max: Math.max(...data.map((d) => d.value)),
};
},
(data) => data.map((d: any) => d.id)
);
function getMinMax(data: ContinuousDatum[]) {
return {
min: Math.min(...data.map((d) => d.value)),
max: Math.max(...data.map((d) => d.value)),
};
}

export class Continuous extends Component<ContinuousStyleProps> {
constructor(options: ContinuousOptions) {
Expand Down
7 changes: 3 additions & 4 deletions src/ui/tooltip/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createDom } from '@antv/dom-util';
import { substitute } from '@antv/util';
import { substitute, createDOM } from '@antv/util';
import { Component } from '../../core';
import { Group } from '../../shapes';
import { applyStyleSheet, throttle } from '../../util';
Expand Down Expand Up @@ -35,7 +34,7 @@ export class Tooltip extends Component<TooltipStyleProps> {
const { data, template } = this.attributes;
return data.map(({ name = '', color = 'black', index, ...rest }, idx) => {
const datum = { name, color, index: index ?? idx, ...rest };
return createDom(substitute(template.item!, datum)) as HTMLElement;
return createDOM(substitute(template.item!, datum)) as HTMLElement;
});
}

Expand Down Expand Up @@ -111,7 +110,7 @@ export class Tooltip extends Component<TooltipStyleProps> {
*/
private initShape() {
const { template } = this.attributes;
this.element = createDom(template.container!) as HTMLElement;
this.element = createDOM(template.container!) as HTMLElement;
if (this.id) this.element.setAttribute('id', this.id);
}

Expand Down
3 changes: 2 additions & 1 deletion src/util/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const measureTextWidth = memoize(
return ctx!.measureText(content).width;
},
(text: any, font?: any) =>
[isString(text) ? text : text.style.text.toString(), Object.values(font || getFont(text as Text)).join()].join('')
[isString(text) ? text : text.style.text.toString(), Object.values(font || getFont(text as Text)).join()].join(''),
4096
);

export const getFont = (textShape: Text) => {
Expand Down
Loading