Skip to content

Commit

Permalink
fix(brush): handle brush:highlight once when emit
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed May 19, 2023
1 parent af6e892 commit dd510a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 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
10 changes: 5 additions & 5 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);
if (emit) brushed(fx, fy, fx1, fy1);
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 @@ -648,7 +648,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 dd510a5

Please sign in to comment.