Skip to content

Commit

Permalink
Merge pull request #235 from idrawjs/dev-v0.4
Browse files Browse the repository at this point in the history
refactor: refactor middleware event logic
  • Loading branch information
chenshenhai authored Nov 19, 2023
2 parents a97ba6b + d6debd0 commit 2f704c0
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 42 deletions.
1 change: 0 additions & 1 deletion packages/board/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export class Board<T extends BoardExtendEvent = BoardExtendEvent> {
this._handleWheelScale(e);
}, frameTime)
);
// this._watcher.on('scale', this._handleScale.bind(this));
this._watcher.on('scrollX', this._handleScrollX.bind(this));
this._watcher.on('scrollY', this._handleScrollY.bind(this));
this._watcher.on('resize', this._handleResize.bind(this));
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { createBoardContexts, validateElements } from '@idraw/util';
import { Cursor } from './lib/cursor';

// export { MiddlewareSelector } from './middleware/selector';
export { MiddlewareSelector } from './middleware/selector';
export { MiddlewareSelector, middlewareEventSelect } from './middleware/selector';
export { MiddlewareScroller } from './middleware/scroller';
export { MiddlewareScaler } from './middleware/scaler';
export { MiddlewareRuler } from './middleware/ruler';
export { MiddlewareScaler, middlewareEventScale } from './middleware/scaler';
export { MiddlewareRuler, middlewareEventRuler } from './middleware/ruler';

export class Core {
private _board: Board<CoreEvent>;
Expand Down
39 changes: 25 additions & 14 deletions packages/core/src/middleware/ruler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,42 @@ import type { BoardMiddleware, CoreEvent } from '@idraw/types';
import { getViewScaleInfoFromSnapshot, getViewSizeInfoFromSnapshot } from '@idraw/util';
import { drawRulerBackground, drawXRuler, drawYRuler, calcXRulerScaleList, calcYRulerScaleList, drawUnderGrid } from './util';

export const middlewareEventRuler = '@middleware/show-ruler';

export const MiddlewareRuler: BoardMiddleware<Record<string, any>, CoreEvent> = (opts) => {
const key = 'RULE';
const { viewContent } = opts;
const { viewContent, viewer, eventHub } = opts;
const { helperContext, underContext } = viewContent;
let showRuler: boolean = true;

eventHub.on(middlewareEventRuler, (e: { show: boolean }) => {
if (typeof e?.show === 'boolean') {
showRuler = e.show;
viewer.drawFrame();
}
});
return {
mode: key,
isDefault: true,
beforeDrawFrame: ({ snapshot }) => {
const viewScaleInfo = getViewScaleInfoFromSnapshot(snapshot);
const viewSizeInfo = getViewSizeInfoFromSnapshot(snapshot);
drawRulerBackground(helperContext, { viewScaleInfo, viewSizeInfo });
if (showRuler === true) {
const viewScaleInfo = getViewScaleInfoFromSnapshot(snapshot);
const viewSizeInfo = getViewSizeInfoFromSnapshot(snapshot);
drawRulerBackground(helperContext, { viewScaleInfo, viewSizeInfo });

const xList = calcXRulerScaleList({ viewScaleInfo, viewSizeInfo });
drawXRuler(helperContext, { scaleList: xList });
const xList = calcXRulerScaleList({ viewScaleInfo, viewSizeInfo });
drawXRuler(helperContext, { scaleList: xList });

const yList = calcYRulerScaleList({ viewScaleInfo, viewSizeInfo });
drawYRuler(helperContext, { scaleList: yList });
const yList = calcYRulerScaleList({ viewScaleInfo, viewSizeInfo });
drawYRuler(helperContext, { scaleList: yList });

drawUnderGrid(underContext, {
xList,
yList,
viewScaleInfo,
viewSizeInfo
});
drawUnderGrid(underContext, {
xList,
yList,
viewScaleInfo,
viewSizeInfo
});
}
}
};
};
4 changes: 3 additions & 1 deletion packages/core/src/middleware/scaler/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { BoardMiddleware, CoreEvent } from '@idraw/types';
import { formatNumber } from '@idraw/util';

export const middlewareEventScale = '@middleware/scale';

export const MiddlewareScaler: BoardMiddleware<Record<string, any>, CoreEvent> = (opts) => {
const key = 'SCALE';
const { viewer, sharer, eventHub } = opts;
Expand All @@ -21,7 +23,7 @@ export const MiddlewareScaler: BoardMiddleware<Record<string, any>, CoreEvent> =
viewer.scroll({ moveX, moveY });
viewer.drawFrame();
const scaleNum = formatNumber(scale);
eventHub.trigger('scale', { scale: scaleNum });
eventHub.trigger(middlewareEventScale, { scale: scaleNum });
}
};
};
6 changes: 4 additions & 2 deletions packages/core/src/middleware/selector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ import {
// keyDebugStartVertical
} from './config';

export const middlewareEventSelect: string = '@middleware/select';

export const MiddlewareSelector: BoardMiddleware<DeepSelectorSharedStorage, CoreEvent> = (opts) => {
const { viewer, sharer, viewContent, calculator, eventHub } = opts;
const { helperContext } = viewContent;
let prevPoint: Point | null = null;
let inBusyMode: 'resize' | 'drag' | 'drag-list' | 'area' | null = null;

eventHub.on('select', ({ uuids }) => {
eventHub.on(middlewareEventSelect, ({ uuids }) => {
const actionType = sharer.getSharedStorage(keyActionType);
const data = sharer.getActiveStorage('data');
const elements = findElementsFromList(uuids, data?.elements || []);
Expand Down Expand Up @@ -131,7 +133,7 @@ export const MiddlewareSelector: BoardMiddleware<DeepSelectorSharedStorage, Core
}

if (opts?.triggerEvent === true) {
eventHub.trigger('select', { uuids: list.map((elem) => elem.uuid) });
eventHub.trigger(middlewareEventSelect, { uuids: list.map((elem) => elem.uuid) });
}
};

Expand Down
16 changes: 0 additions & 16 deletions packages/renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,4 @@ export class Renderer extends EventEmitter<RendererEventMap> implements BoardRen
});
}
}

// scroll(opts: { offsetTop?: number; offsetLeft?: number }) {
// const { sharer } = this._opts;
// const { data, scale, offsetTop, offsetBottom, offsetLeft, offsetRight } = sharer.getActiveStoreSnapshot();
// // TODO calc offset data
// if (data) {
// this.drawData(data, {
// scale,
// offsetTop,
// offsetBottom,
// offsetLeft,
// offsetRight
// });
// }
// // sharer.setActiveStorage('scale', num);
// }
}
3 changes: 1 addition & 2 deletions packages/types/src/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export interface CoreEventScale {

export type CoreEvent = {
cursor: CoreEventCursor;
select: CoreEventSelect;
change: CoreEventChange;
scale: CoreEventScale;
[key: string]: any;
};
4 changes: 1 addition & 3 deletions packages/types/src/lib/idraw.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { CoreOptions, CoreEvent } from './core';
import type { CoreOptions } from './core';

export type IDrawOptions = CoreOptions;

export type IDrawEvent = CoreEvent;

0 comments on commit 2f704c0

Please sign in to comment.