diff --git a/packages/board/src/index.ts b/packages/board/src/index.ts index f3850364b..817f34ad2 100644 --- a/packages/board/src/index.ts +++ b/packages/board/src/index.ts @@ -102,7 +102,6 @@ export class Board { 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)); diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 742d0e46a..ec2d2c23b 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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; diff --git a/packages/core/src/middleware/ruler/index.ts b/packages/core/src/middleware/ruler/index.ts index 489070fce..8deb7daf6 100644 --- a/packages/core/src/middleware/ruler/index.ts +++ b/packages/core/src/middleware/ruler/index.ts @@ -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, 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 + }); + } } }; }; diff --git a/packages/core/src/middleware/scaler/index.ts b/packages/core/src/middleware/scaler/index.ts index 1d88a7ca3..59bad7673 100644 --- a/packages/core/src/middleware/scaler/index.ts +++ b/packages/core/src/middleware/scaler/index.ts @@ -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, CoreEvent> = (opts) => { const key = 'SCALE'; const { viewer, sharer, eventHub } = opts; @@ -21,7 +23,7 @@ export const MiddlewareScaler: BoardMiddleware, CoreEvent> = viewer.scroll({ moveX, moveY }); viewer.drawFrame(); const scaleNum = formatNumber(scale); - eventHub.trigger('scale', { scale: scaleNum }); + eventHub.trigger(middlewareEventScale, { scale: scaleNum }); } }; }; diff --git a/packages/core/src/middleware/selector/index.ts b/packages/core/src/middleware/selector/index.ts index 4af1c56cd..7f727aead 100644 --- a/packages/core/src/middleware/selector/index.ts +++ b/packages/core/src/middleware/selector/index.ts @@ -52,13 +52,15 @@ import { // keyDebugStartVertical } from './config'; +export const middlewareEventSelect: string = '@middleware/select'; + export const MiddlewareSelector: BoardMiddleware = (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 || []); @@ -131,7 +133,7 @@ export const MiddlewareSelector: BoardMiddleware elem.uuid) }); + eventHub.trigger(middlewareEventSelect, { uuids: list.map((elem) => elem.uuid) }); } }; diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts index f298a16de..607709928 100644 --- a/packages/renderer/src/index.ts +++ b/packages/renderer/src/index.ts @@ -78,20 +78,4 @@ export class Renderer extends EventEmitter 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); - // } } diff --git a/packages/types/src/lib/core.ts b/packages/types/src/lib/core.ts index d4f7a212c..ff3aaae43 100644 --- a/packages/types/src/lib/core.ts +++ b/packages/types/src/lib/core.ts @@ -35,7 +35,6 @@ export interface CoreEventScale { export type CoreEvent = { cursor: CoreEventCursor; - select: CoreEventSelect; change: CoreEventChange; - scale: CoreEventScale; + [key: string]: any; }; diff --git a/packages/types/src/lib/idraw.ts b/packages/types/src/lib/idraw.ts index 87fc4d57d..7885ce914 100644 --- a/packages/types/src/lib/idraw.ts +++ b/packages/types/src/lib/idraw.ts @@ -1,5 +1,3 @@ -import type { CoreOptions, CoreEvent } from './core'; +import type { CoreOptions } from './core'; export type IDrawOptions = CoreOptions; - -export type IDrawEvent = CoreEvent;