Skip to content

Commit

Permalink
feat: add updateStates in vgrammar element
Browse files Browse the repository at this point in the history
  • Loading branch information
xile611 committed May 28, 2024
1 parent ab2dbe3 commit d8c730c
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 26 deletions.
62 changes: 61 additions & 1 deletion packages/vgrammar-core/src/graph/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
isArray,
get,
isEmpty,
isEqual as isObjEqual
isEqual as isObjEqual,
isObject
} from '@visactor/vutils';
import { isEqual } from '@visactor/vgrammar-util';
import type { IBaseCoordinate } from '@visactor/vgrammar-coordinate';
Expand Down Expand Up @@ -333,6 +334,65 @@ export class Element implements IElement {
return this.states && state && this.states.includes(state);
}

updateStates(states: Record<string, boolean | BaseSingleEncodeSpec>) {
if (!this.graphicItem) {
return false;
}
let nextStates = this.states.slice();
const encode = (this.mark.getSpec() as MarkSpec).encode;
let forceClearState = false;
let hasUpdate = false;

Object.keys(states).forEach(stateKey => {
if (!stateKey) {
return;
}

const stateValue = states[stateKey];
const isRuntimeStateUpdate =
isObject(stateValue) && !isObjEqual(stateValue, this.runtimeStatesEncoder?.[stateKey]);

if (isRuntimeStateUpdate) {
if (nextStates.includes(stateKey)) {
forceClearState = true;
} else {
nextStates.push(stateKey);
}
this._updateRuntimeStates(stateKey, stateValue);
hasUpdate = true;
} else if (stateValue) {
if (!nextStates.includes(stateKey) && encode?.[stateKey]) {
nextStates.push(stateKey);
hasUpdate = true;
}
} else {
if (nextStates.length) {
const newNextStates = nextStates.filter(state => state !== stateKey);

if (newNextStates.length !== nextStates.length) {
hasUpdate = true;
nextStates = newNextStates;
}

if (this.runtimeStatesEncoder && this.runtimeStatesEncoder[stateKey]) {
this.runtimeStatesEncoder[stateKey] = null;
}
}
}
});

if (forceClearState) {
this.graphicItem.clearStates();
}

if (hasUpdate) {
this.useStates(nextStates);
return true;
}

return false;
}

addState(state: string | string[], attrs?: BaseSingleEncodeSpec) {
if (!this.graphicItem) {
return false;
Expand Down
12 changes: 8 additions & 4 deletions packages/vgrammar-core/src/interactions/brush-highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ export class BrushHighlight extends BrushBase<BrushHighlightOptions> {

if (isHighlight) {
elements.push(el);
el.removeState(this.options.blurState);
el.addState(this.options.highlightState);
el.updateStates({
[this.options.blurState]: false,
[this.options.highlightState]: true
});
} else {
el.removeState(this.options.highlightState);
el.addState(this.options.blurState);
el.updateStates({
[this.options.blurState]: true,
[this.options.highlightState]: false
});
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ export class ElementHighlightByGroup extends BaseInteraction<ElementHighlightOpt
const isHighlight = el.groupKey === highlightKey;

if (isHighlight) {
el.removeState(this.options.blurState);
el.addState(this.options.highlightState);
el.updateStates({
[this.options.blurState]: false,
[this.options.highlightState]: true
});
} else {
el.removeState(this.options.highlightState);
el.addState(this.options.blurState);
el.updateStates({
[this.options.blurState]: true,
[this.options.highlightState]: false
});
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ export class ElementHighlightByKey extends BaseInteraction<ElementHighlightOptio
const isHighlight = el.key === highlightKey;

if (isHighlight) {
el.removeState(this.options.blurState);
el.addState(this.options.highlightState);
el.updateStates({
[this.options.blurState]: false,
[this.options.highlightState]: true
});
} else {
el.removeState(this.options.highlightState);
el.addState(this.options.blurState);
el.updateStates({
[this.options.blurState]: true,
[this.options.highlightState]: false
});
}
});
});
Expand All @@ -82,11 +86,15 @@ export class ElementHighlightByKey extends BaseInteraction<ElementHighlightOptio
const isHighlight = el.key === highlightKey;

if (isHighlight) {
el.removeState(this.options.blurState);
el.addState(this.options.highlightState);
el.updateStates({
[this.options.blurState]: false,
[this.options.highlightState]: true
});
} else {
el.removeState(this.options.highlightState);
el.addState(this.options.blurState);
el.updateStates({
[this.options.blurState]: true,
[this.options.highlightState]: false
});
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ export class ElementHighlightByLegend extends BaseInteraction<ElementHighlightBy
const isHighlight = filterValue(el) === itemKey;

if (isHighlight) {
el.removeState(this.options.blurState);
el.addState(this.options.highlightState);
el.updateStates({
[this.options.blurState]: false,
[this.options.highlightState]: true
});
} else {
el.removeState(this.options.highlightState);
el.addState(this.options.blurState);
el.updateStates({
[this.options.blurState]: true,
[this.options.highlightState]: false
});
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ export class ElementHighlightByName extends BaseInteraction<ElementHighlightByNa
mark.elements.forEach(el => {
const isHighlight = filterValue(el) === itemKey;
if (isHighlight) {
el.removeState(this.options.blurState);
el.addState(this.options.highlightState);
el.updateStates({
[this.options.blurState]: false,
[this.options.highlightState]: true
});
} else {
el.removeState(this.options.highlightState);
el.addState(this.options.blurState);
el.updateStates({
[this.options.blurState]: true,
[this.options.highlightState]: false
});
}
});
});
Expand Down
10 changes: 9 additions & 1 deletion packages/vgrammar-core/src/types/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ import type {
} from '@visactor/vrender-core';
import type { DiffState } from '../graph/enums';
import type { IMark, IGlyphMark } from './grammar';
import type { BaseEncodeSpec, IMarkConfig, MarkFunctionType, MarkKeySpec, MarkType } from './mark';
import type {
BaseEncodeSpec,
BaseSingleEncodeSpec,
IMarkConfig,
MarkFunctionType,
MarkKeySpec,
MarkType
} from './mark';

export interface ElementGraphicMap {
circle: ICircle;
Expand Down Expand Up @@ -154,6 +161,7 @@ export interface IElement {
addState: (state: string | string[], attrs?: any) => boolean;
removeState: (state: string | string[]) => boolean;
useStates: (states: string[], noAnimation?: boolean) => boolean;
updateStates: (states: Record<string, boolean | BaseSingleEncodeSpec>) => any;
}

export interface IGlyphElement<P = any> extends IElement {
Expand Down

0 comments on commit d8c730c

Please sign in to comment.