Skip to content

Commit

Permalink
Merge pull request #251 from idrawjs/dev-v0.4
Browse files Browse the repository at this point in the history
feat: add dragger and optimize renderer
  • Loading branch information
chenshenhai authored Dec 23, 2023
2 parents dd99249 + 4d95fdb commit 3b41f92
Show file tree
Hide file tree
Showing 38 changed files with 571 additions and 284 deletions.
279 changes: 150 additions & 129 deletions packages/board/src/index.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/board/src/lib/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Calculator implements ViewCalculator {
}

isPointInElement(p: Point, elem: Element<ElementType>, viewScaleInfo: ViewScaleInfo, viewSizeInfo: ViewSizeInfo): boolean {
const context2d = this._opts.viewContent.boardContext;
const context2d = this._opts.viewContext;
return isViewPointInElement(p, {
context2d,
element: elem,
Expand All @@ -30,7 +30,7 @@ export class Calculator implements ViewCalculator {
p: Point,
opts: { data: Data; viewScaleInfo: ViewScaleInfo; viewSizeInfo: ViewSizeInfo }
): { index: number; element: null | Element<ElementType>; groupQueueIndex: number } {
const context2d = this._opts.viewContent.boardContext;
const context2d = this._opts.viewContext;
return getViewPointAtElement(p, { ...opts, ...{ context2d } });
}
}
6 changes: 3 additions & 3 deletions packages/board/src/lib/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Viewer extends EventEmitter<BoardViewerEventMap> implements BoardVi
}
const snapshot = this._drawFrameSnapshotQueue.shift();

const { renderer, viewContent, beforeDrawFrame, afterDrawFrame } = this._opts;
const { renderer, boardContent, beforeDrawFrame, afterDrawFrame } = this._opts;

if (snapshot) {
const { scale, offsetTop, offsetBottom, offsetLeft, offsetRight, width, height, contextHeight, contextWidth, devicePixelRatio } = snapshot.activeStore;
Expand All @@ -65,7 +65,7 @@ export class Viewer extends EventEmitter<BoardViewerEventMap> implements BoardVi
}
beforeDrawFrame({ snapshot });

viewContent.drawView();
boardContent.drawView();

afterDrawFrame({ snapshot });
}
Expand Down Expand Up @@ -161,7 +161,7 @@ export class Viewer extends EventEmitter<BoardViewerEventMap> implements BoardVi
const newViewSize = { ...originViewSize, ...viewSize };

const { width, height, devicePixelRatio } = newViewSize;
const { underContext, boardContext, helperContext, viewContext } = this._opts.viewContent;
const { underContext, boardContext, helperContext, viewContext } = this._opts.boardContent;
boardContext.canvas.width = width * devicePixelRatio;
boardContext.canvas.height = height * devicePixelRatio;
boardContext.canvas.style.width = `${width}px`;
Expand Down
4 changes: 2 additions & 2 deletions packages/board/src/lib/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ export class BoardWatcher extends EventEmitter<BoardWatcherEventMap> {
}

private _isInTarget(e: MouseEvent | WheelEvent) {
return e.target === this._opts.viewContent.boardContext.canvas;
return e.target === this._opts.boardContent.boardContext.canvas;
}

private _getPoint(e: MouseEvent): Point {
const boardCanvas = this._opts.viewContent.boardContext.canvas;
const boardCanvas = this._opts.boardContent.boardContext.canvas;
const rect = boardCanvas.getBoundingClientRect();
const p: Point = {
x: e.clientX - rect.left,
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Data, PointSize, CoreOptions, BoardMiddleware, ViewSizeInfo, CoreEvent, ViewScaleInfo } from '@idraw/types';
import { Board } from '@idraw/board';
import { createViewContent, validateElements } from '@idraw/util';
import { createBoardContent, validateElements } from '@idraw/util';
import { Cursor } from './lib/cursor';

// export { MiddlewareSelector } from './middleware/selector';
Expand All @@ -9,6 +9,7 @@ export { MiddlewareScroller } from './middleware/scroller';
export { MiddlewareScaler, middlewareEventScale } from './middleware/scaler';
export { MiddlewareRuler, middlewareEventRuler } from './middleware/ruler';
export { MiddlewareTextEditor, middlewareEventTextEdit } from './middleware/text-editor';
export { MiddlewareDragger } from './middleware/dragger';

export class Core<E extends CoreEvent = CoreEvent> {
#board: Board<E>;
Expand All @@ -25,8 +26,8 @@ export class Core<E extends CoreEvent = CoreEvent> {
this.#initContainer();
container.appendChild(canvas);

const viewContent = createViewContent(canvas, { width, height, devicePixelRatio, offscreen: true });
const board = new Board<E>({ viewContent, container });
const boardContent = createBoardContent(canvas, { width, height, devicePixelRatio, offscreen: true });
const board = new Board<E>({ boardContent, container });
const sharer = board.getSharer();
sharer.setActiveViewSizeInfo({
width,
Expand All @@ -52,6 +53,10 @@ export class Core<E extends CoreEvent = CoreEvent> {
this.#board.use(middleware);
}

disuse(middleware: BoardMiddleware<any, any>) {
this.#board.disuse(middleware);
}

setData(data: Data) {
validateElements(data?.elements || []);
this.#board.setData(data);
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/lib/cursor-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export const CURSOR =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAACXBIWXMAAAsTAAALEwEAmpwYAAAF92lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNi4wLWMwMDYgNzkuMTY0NzUzLCAyMDIxLzAyLzE1LTExOjUyOjEzICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWVudHMvMS4xLyIgeG1sbnM6cGhvdG9zaG9wPSJodHRwOi8vbnMuYWRvYmUuY29tL3Bob3Rvc2hvcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cD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kLH20dEOhumft97pQI4s3+iiRSPwtxVSbd39J8eEGvzFXc1NAs8KSFAZeBt4AHgNeBFDWObkEAne7HAK2gT2gCFQatdca1GbtBj4E3veFprVYVLXXcg4GqM6588BbAMlkcm1qamqzr6/v6ikBet5RgiizJoDPAXdgYGDZXHkzMzPrrk9CdRWngUeAsxxdCzVwpgZ/BigWiz1mo4mJif7jqMajeJrU5hywgGvAej6fvzA0NLThN1Eul9uT4g5VTbYFNKUIfAbsZ7PZvuHh4Wt+As7Ozu5IcY2j219TQB0NV4A7qODgO4CFhYWLg4ODOb8AHccpG4A68m6pRQ1YAQ6A28A+8BXwBcDq6upTqVTquh+AuVyuW4q/opRRaQdZD1gCbgE3ge+BT4HdfD7/ZDwe/z2bzRY6hRsfH1/e3Nx8FDX/sgbgoZfvg6jo4ixqW7oIPA+8CrwHrAJuJBLJd+JaHMf5k6qmPgGeQe1SCenXk0/U21xcIC8AzwGvAJPAEuAmEon1xcXFba9w8/Pz5oqdB14CnpY+oij35km0qwmjwqAU8ISM9hIwBeQA17KsG9PT07+1gxsZGdmgdqW+BjwLDAAPoo4ALU+W9arVwWoIpXp9kouKZpPAO8AwQCwW+2d0dLQ0OTkZSafT0UKhUJ6bm9t2HKeysrIS3tra0g7+KvABUJC0g5rrJdRcbLpImtleRzYashc1P2OSXwbebDVykX3gW+Ab1AHqhuQ3pe6AJlFMO0CoPROHDcio5I8DL1A9C8dQbmod+APYAK4DvwjQnsDdErgyVTfTEaCG1GFYGHXG7TVSo2OkdvhlAflXoHSuNdfStFqaRhEi2kfdprrj6M5LAt0I8EDaaMdflPal48CB95hMr3Bt8h4jD0kyL5E0pN6dysZzW7N2AqjbmhdIZjJvufTOpE19x3g+9s1XJ/ck5tVbfdhu+rxDfLiSO+lFToCjZwrXyH2/0Lwv95z8B1jAqXmDnj4YAAAAAElFTkSuQmCC`;
9 changes: 7 additions & 2 deletions packages/core/src/lib/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UtilEventEmitter, CoreEvent } from '@idraw/types';
import { limitAngle, loadImage, parseAngleToRadian } from '@idraw/util';
import { CURSOR, CURSOR_RESIZE } from './cursor-image';
import { CURSOR, CURSOR_RESIZE, CURSOR_DRAG_DEFAULT, CURSOR_DRAG_ACTIVE } from './cursor-image';

export class Cursor {
private _eventHub: UtilEventEmitter<CoreEvent>;
Expand All @@ -9,6 +9,8 @@ export class Cursor {
private _resizeCursorBaseImage: HTMLImageElement | null = null;
private _cursorImageMap: Record<string, string> = {
auto: CURSOR,
'drag-default': CURSOR_DRAG_DEFAULT,
'drag-active': CURSOR_DRAG_ACTIVE,
'rotate-0': CURSOR_RESIZE
};
constructor(
Expand All @@ -31,8 +33,11 @@ export class Cursor {
this._resetCursor('auto');
} else if (typeof e.type === 'string' && e.type?.startsWith('resize-')) {
this._setCursorResize(e);
} else if (e.type === 'drag-default') {
this._resetCursor('drag-default');
} else if (e.type === 'drag-active') {
this._resetCursor('drag-active');
} else {
// TODO
this._resetCursor('auto');
}
});
Expand Down
53 changes: 53 additions & 0 deletions packages/core/src/middleware/dragger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { BoardMiddleware, CoreEvent, Point } from '@idraw/types';

const key = 'DRAG';
const keyPrevPoint = Symbol(`${key}_prevPoint`);

type DraggerSharedStorage = {
[keyPrevPoint]: Point | null;
};

export const MiddlewareDragger: BoardMiddleware<DraggerSharedStorage, CoreEvent> = (opts) => {
const { eventHub, sharer, viewer } = opts;
let isDragging = false;

return {
hover() {
if (isDragging === true) {
return;
}
eventHub.trigger('cursor', {
type: 'drag-default'
});
},

pointStart(e) {
const { point } = e;
sharer.setSharedStorage(keyPrevPoint, point);
isDragging = true;
eventHub.trigger('cursor', {
type: 'drag-active'
});
},

pointMove(e) {
const { point } = e;
const prevPoint = sharer.getSharedStorage(keyPrevPoint);
if (point && prevPoint) {
const moveX = point.x - prevPoint.x;
const moveY = point.y - prevPoint.y;
viewer.scroll({ moveX, moveY });
viewer.drawFrame();
}
sharer.setSharedStorage(keyPrevPoint, point);
},

pointEnd() {
isDragging = false;
sharer.setSharedStorage(keyPrevPoint, null);
eventHub.trigger('cursor', {
type: 'drag-default'
});
}
};
};
18 changes: 11 additions & 7 deletions packages/core/src/middleware/ruler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import { drawRulerBackground, drawXRuler, drawYRuler, calcXRulerScaleList, calcY
export const middlewareEventRuler = '@middleware/show-ruler';

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

eventHub.on(middlewareEventRuler, (e: { show: boolean; showGrid: boolean }) => {
const rulerCallback = (e: { show: boolean; showGrid: boolean }) => {
if (typeof e?.show === 'boolean') {
show = e.show;
}
Expand All @@ -22,10 +21,15 @@ export const MiddlewareRuler: BoardMiddleware<Record<string, any>, CoreEvent> =
if (typeof e?.show === 'boolean' || typeof e?.showGrid === 'boolean') {
viewer.drawFrame();
}
});
};

return {
mode: key,
isDefault: true,
use() {
eventHub.on(middlewareEventRuler, rulerCallback);
},
disuse() {
eventHub.off(middlewareEventRuler, rulerCallback);
},
beforeDrawFrame: ({ snapshot }) => {
if (show === true) {
const viewScaleInfo = getViewScaleInfoFromSnapshot(snapshot);
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/middleware/scaler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ 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;
const maxScale = 50;
const minScale = 0.05;

return {
mode: key,
isDefault: true,
wheelScale(e) {
const { deltaY, point } = e;
const { scale } = sharer.getActiveViewScaleInfo();
Expand Down
Loading

0 comments on commit 3b41f92

Please sign in to comment.