Skip to content

Commit

Permalink
fix(brush): handle brush:highlight once when emit (#5063)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed May 19, 2023
1 parent af6e892 commit 321fb1b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
18 changes: 12 additions & 6 deletions __tests__/integration/api-chart-render-brush-end.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ describe('chart.render', () => {
canvas,
container: document.createElement('div'),
});
await finished;
await sleep(20);

chart.off();
const fn = jest.fn();

const end = jest.fn();
const start = jest.fn();
chart.on('brush:highlight', () => {
start();
});
chart.on('brush:end', () => {
fn();
end();
});
await finished;
await sleep(20);

button.dispatchEvent(new CustomEvent('click'));
await rerendered;
await sleep(20);
expect(fn).toBeCalledTimes(0);
expect(start).toBeCalledTimes(1);
expect(end).toBeCalledTimes(0);
});

afterAll(() => {
Expand Down
22 changes: 22 additions & 0 deletions __tests__/unit/api/options.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Chart } from '../../../src';

describe('API and Options', () => {
it('', () => {
const chart = new Chart({});

chart.options({
type: 'interval',
data: [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
],
encode: {
x: 'genre',
y: 'sold',
},
});
});
});
22 changes: 12 additions & 10 deletions src/interaction/brushHighlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export function brush(
};

// Update mask and invoke brushended callback.
const updateMask = (start, end) => {
const updateMask = (start, end, emit = true) => {
const [x, y, x1, y1] = normalizeBounds(
start[0],
start[1],
Expand All @@ -326,7 +326,7 @@ export function brush(
const [fx, fy, fx1, fy1] = brushRegion(x, y, x1, y1, extent);
if (reverse) updateReverseMask(fx, fy, fx1, fy1);
else updateNormalMask(fx, fy, fx1, fy1);
brushed(fx, fy, fx1, fy1);
brushed(fx, fy, fx1, fy1, emit);
return [fx, fy, fx1, fy1];
};

Expand Down Expand Up @@ -470,11 +470,11 @@ export function brush(

return {
mask,
move(x, y, x1, y1) {
move(x, y, x1, y1, emit = true) {
if (!mask) initMask(x, y);
start = [x, y];
end = [x1, y1];
updateMask([x, y], [x1, y1]);
updateMask([x, y], [x1, y1], emit);
},
remove() {
if (mask) removeMask();
Expand Down Expand Up @@ -632,12 +632,14 @@ export function brushHighlight(
emitter.emit('brush:end', { nativeEvent: true });
handler();
},
brushed: (x, y, x1, y1) => {
brushed: (x, y, x1, y1, emit) => {
const selection = selectionOf(x, y, x1, y1, scale, coordinate);
emitter.emit('brush:highlight', {
nativeEvent: true,
data: { selection },
});
if (emit) {
emitter.emit('brush:highlight', {
nativeEvent: true,
data: { selection },
});
}
const handler = series ? seriesBrushed : brushed;
handler(x, y, x1, y1);
},
Expand All @@ -648,7 +650,7 @@ export function brushHighlight(
if (nativeEvent) return;
const { selection } = data;
const [x, y, x1, y1] = pixelsOf(selection, scale, coordinate);
brushHandler.move(x, y, x1, y1);
brushHandler.move(x, y, x1, y1, false);
};
emitter.on('brush:highlight', onHighlight);

Expand Down

0 comments on commit 321fb1b

Please sign in to comment.