Skip to content

Commit

Permalink
feat(ssr): improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovilia committed May 31, 2023
1 parent 26abab1 commit d7005b8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 55 deletions.
2 changes: 0 additions & 2 deletions src/svg/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export interface BrushScope {
*/
cssStyleCache: Record<string, string>

cssClassIdx: number
cssAnimIdx: number

shadowIdx: number
Expand Down Expand Up @@ -175,7 +174,6 @@ export function createBrushScope(zrId: string): BrushScope {
cssAnims: {},
cssStyleCache: {},

cssClassIdx: 0,
cssAnimIdx: 0,

shadowIdx: 0,
Expand Down
3 changes: 2 additions & 1 deletion src/svg/cssAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Animator from '../animation/Animator';
import CompoundPath from '../graphic/CompoundPath';
import { AnimationEasing } from '../animation/easing';
import { createCubicEasingFunc } from '../animation/cubicEasing';
import { getClassId } from './cssClassId';

export const EASING_MAP: Record<string, string> = {
// From https://easings.net/
Expand Down Expand Up @@ -355,7 +356,7 @@ export function createCSSAnimation(
}

if (cssAnimations.length) {
const className = scope.zrId + '-cls-' + scope.cssClassIdx++;
const className = scope.zrId + '-cls-' + getClassId();
scope.cssNodes['.' + className] = {
animation: cssAnimations.join(',')
};
Expand Down
5 changes: 5 additions & 0 deletions src/svg/cssClassId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let cssClassIdx = 0;

export function getClassId() {
return cssClassIdx++;
}
55 changes: 16 additions & 39 deletions src/svg/cssEmphasis.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,56 @@
import LRU from '../core/LRU';
import { extend, isGradientObject, isString, map } from '../core/util';
import * as colorTool from '../tool/color';
import Displayable from '../graphic/Displayable';
import { GradientObject } from '../graphic/Gradient';
import { liftColor } from '../tool/color';
import { BrushScope, SVGVNodeAttrs } from './core';

// TODO: Consider deleting the same logic in ECharts and call this method?
const liftedColorCache = new LRU<string>(100);
function liftColor(color: GradientObject): GradientObject;
function liftColor(color: string): string;
function liftColor(color: string | GradientObject): string | GradientObject {
if (isString(color)) {
let liftedColor = liftedColorCache.get(color);
if (!liftedColor) {
liftedColor = colorTool.lift(color, -0.1);
liftedColorCache.put(color, liftedColor);
}
return liftedColor;
}
else if (isGradientObject(color)) {
const ret = extend({}, color) as GradientObject;
ret.colorStops = map(color.colorStops, stop => ({
offset: stop.offset,
color: colorTool.lift(stop.color, -0.1)
}));
return ret;
}
// Change nothing.
return color;
}
import { getClassId } from './cssClassId';

export function createCSSEmphasis(
el: Displayable,
attrs: SVGVNodeAttrs,
scope: BrushScope
) {
if (!el.ignore && el.__metaData) {
const empahsisStyle = el.states.emphasis
const emphasisStyle = el.states.emphasis && el.states.emphasis.style
? el.states.emphasis.style
: {};
let fill = empahsisStyle.fill;
let fill = emphasisStyle.fill;
if (!fill) {
// No empahsis fill, lift color
const normalFill = el.style.fill;
const selectFill = el.states.select && el.states.select.style.fill;
const normalFill = el.style && el.style.fill;
const selectFill = el.states.select
&& el.states.select.style
&& el.states.select.style.fill;
const fromFill = el.currentStates.indexOf('select') >= 0
? (selectFill || normalFill)
: normalFill;
if (fromFill) {
fill = liftColor(fromFill);
}
}
let lineWidth = empahsisStyle.lineWidth;
let lineWidth = emphasisStyle.lineWidth;
if (lineWidth) {
// Symbols use transform to set size, so lineWidth
// should be divided by scaleX
const scaleX = el.transform ? el.transform[0] : 1;
const scaleX = (!emphasisStyle.strokeNoScale && el.transform)
? el.transform[0]
: 1;
lineWidth = lineWidth / scaleX;
}
const style = {
cursor: 'pointer', // TODO: Should be included in el
cursor: 'pointer', // TODO: Should this be customized?
} as any;
if (fill) {
style.fill = fill;
}
if (empahsisStyle.stroke) {
style.stroke = empahsisStyle.stroke;
if (emphasisStyle.stroke) {
style.stroke = emphasisStyle.stroke;
}
if (lineWidth) {
style['stroke-width'] = lineWidth;
}
const styleKey = JSON.stringify(style);
let className = scope.cssStyleCache[styleKey];
if (!className) {
className = scope.zrId + '-cls-' + scope.cssClassIdx++;
className = scope.zrId + '-cls-' + getClassId();
scope.cssStyleCache[styleKey] = className;
scope.cssNodes['.' + className + ':hover'] = style;
}
Expand Down
9 changes: 5 additions & 4 deletions src/svg/graphic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import mapStyleToAttrs from './mapStyleToAttrs';
import { SVGVNodeAttrs, createVNode, SVGVNode, vNodeToString, BrushScope, META_DATA_PREFIX } from './core';
import { MatrixArray } from '../core/matrix';
import Displayable from '../graphic/Displayable';
import { assert, clone, isFunction, isString, logError, map, retrieve2 } from '../core/util';
import { assert, clone, each, isFunction, isString, logError, map, retrieve2 } from '../core/util';
import Polyline from '../graphic/shape/Polyline';
import Polygon from '../graphic/shape/Polygon';
import { GradientObject } from '../graphic/Gradient';
Expand Down Expand Up @@ -72,9 +72,10 @@ function setStyleAttrs(attrs: SVGVNodeAttrs, style: AllStyleOption, el: Path | T

function setMetaData(attrs: SVGVNodeAttrs, el: Path | TSpan | ZRImage) {
if (el.__metaData) {
for (const key in el.__metaData) {
attrs[META_DATA_PREFIX + key] = el.__metaData[key] + '';
}
each(el.__metaData, function (val, key) {
attrs[(META_DATA_PREFIX + key).toLowerCase()]
= val + '';
});
}
}

Expand Down
28 changes: 27 additions & 1 deletion src/tool/color.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import LRU from '../core/LRU';
import { extend, isGradientObject, isString, map } from '../core/util';
import { GradientObject } from '../graphic/Gradient';

const kCSSColorTable = {
'transparent': [0, 0, 0, 0], 'aliceblue': [240, 248, 255, 1],
Expand Down Expand Up @@ -566,4 +568,28 @@ export function random(): string {
Math.round(Math.random() * 255),
Math.round(Math.random() * 255)
], 'rgb');
}
}

const liftedColorCache = new LRU<string>(100);
export function liftColor(color: GradientObject): GradientObject;
export function liftColor(color: string): string;
export function liftColor(color: string | GradientObject): string | GradientObject {
if (isString(color)) {
let liftedColor = liftedColorCache.get(color);
if (!liftedColor) {
liftedColor = lift(color, -0.1);
liftedColorCache.put(color, liftedColor);
}
return liftedColor;
}
else if (isGradientObject(color)) {
const ret = extend({}, color) as GradientObject;
ret.colorStops = map(color.colorStops, stop => ({
offset: stop.offset,
color: lift(stop.color, -0.1)
}));
return ret;
}
// Change nothing.
return color;
}
15 changes: 7 additions & 8 deletions test/svg-meta-data.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
if (type === 'seriesItem') {
child.addEventListener('mouseover', event => {
const attrs = event.target.attributes;
const seriesIndex = attrs.ecmeta_seriesIndex.value;
const dataIndex = attrs.ecmeta_dataIndex.value;
const styleType = attrs.ecmeta_styleType.value;
const seriesIndex = attrs.ecmeta_seriesindex.value;
const dataIndex = attrs.ecmeta_dataindex.value;
const styleType = attrs.ecmeta_styletype.value;
const emphasis = metaData.series[seriesIndex].emphasis;
if (emphasis && emphasis[styleType]) {
const style = emphasis[styleType];
Expand All @@ -71,9 +71,9 @@
});
child.addEventListener('mouseout', event => {
const attrs = event.target.attributes;
const seriesIndex = attrs.ecmeta_seriesIndex.value;
const dataIndex = attrs.ecmeta_dataIndex.value;
const styleType = attrs.ecmeta_styleType.value;
const seriesIndex = attrs.ecmeta_seriesindex.value;
const dataIndex = attrs.ecmeta_dataindex.value;
const styleType = attrs.ecmeta_styletype.value;
const normal = metaData.series[seriesIndex].normal;
if (normal && normal[styleType]) {
const style = normal[styleType];
Expand All @@ -84,8 +84,7 @@
});
}
}
console.log(root)
});
}, 100);
</script>

</body>
Expand Down

0 comments on commit d7005b8

Please sign in to comment.