-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #337 from idrawjs/dev-v0.4
refactor: refactor events
- Loading branch information
Showing
23 changed files
with
350 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,49 @@ | ||
export const eventChange = 'change'; | ||
export const EVENT_KEY_CHANGE = 'change'; | ||
export const EVENT_KEY_CURSOR = 'cursor'; | ||
export const EVENT_KEY_RULER = 'ruler'; | ||
export const EVENT_KEY_SCALE = 'scale'; | ||
export const EVENT_KEY_SELECT = 'select'; | ||
export const EVENT_KEY_CLEAR_SELECT = 'clearSelect'; | ||
export const EVENT_KEY_TEXT_EDIT = 'textEdit'; | ||
export const EVENT_KEY_TEXT_CHANGE = 'textChange'; | ||
export const EVENT_KEY_CONTEXT_MENU = 'contextMenu'; | ||
export const EVENT_KEY_SELECT_IN_GROUP = 'selectInGroup'; | ||
export const EVENT_KEY_SNAP_TO_GRID = 'snapToGrid'; | ||
|
||
export type CoreEventKeys = { | ||
CURSOR: typeof EVENT_KEY_CURSOR; | ||
CHANGE: typeof EVENT_KEY_CHANGE; | ||
RULER: typeof EVENT_KEY_RULER; | ||
SCALE: typeof EVENT_KEY_SCALE; | ||
SELECT: typeof EVENT_KEY_SELECT; | ||
CLEAR_SELECT: typeof EVENT_KEY_CLEAR_SELECT; | ||
TEXT_EDIT: typeof EVENT_KEY_TEXT_EDIT; | ||
TEXT_CHANGE: typeof EVENT_KEY_TEXT_CHANGE; | ||
CONTEXT_MENU: typeof EVENT_KEY_CONTEXT_MENU; | ||
SELECT_IN_GROUP: typeof EVENT_KEY_SELECT_IN_GROUP; | ||
SNAP_TO_GRID: typeof EVENT_KEY_SELECT_IN_GROUP; | ||
}; | ||
|
||
const innerEventKeys: CoreEventKeys = { | ||
CURSOR: EVENT_KEY_CURSOR, | ||
CHANGE: EVENT_KEY_CHANGE, | ||
RULER: EVENT_KEY_RULER, | ||
SCALE: EVENT_KEY_SCALE, | ||
SELECT: EVENT_KEY_SELECT, | ||
CLEAR_SELECT: EVENT_KEY_CLEAR_SELECT, | ||
TEXT_EDIT: EVENT_KEY_TEXT_EDIT, | ||
TEXT_CHANGE: EVENT_KEY_TEXT_CHANGE, | ||
CONTEXT_MENU: EVENT_KEY_CONTEXT_MENU, | ||
SELECT_IN_GROUP: EVENT_KEY_SELECT_IN_GROUP, | ||
SNAP_TO_GRID: EVENT_KEY_SELECT_IN_GROUP | ||
}; | ||
|
||
const coreEventKeys = {} as CoreEventKeys; | ||
Object.keys(innerEventKeys).forEach((keyName: string) => { | ||
Object.defineProperty(coreEventKeys, keyName, { | ||
value: innerEventKeys[keyName as keyof CoreEventKeys], | ||
writable: false | ||
}); | ||
}); | ||
|
||
export { coreEventKeys }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { BoardMiddleware, CoreEventMap } from '@idraw/types'; | ||
import type { DeepPointerSharedStorage } from './types'; | ||
import { keySelectedElementList } from '../selector'; | ||
import { coreEventKeys } from '../../config'; | ||
|
||
export const MiddlewarePointer: BoardMiddleware<DeepPointerSharedStorage, CoreEventMap> = (opts) => { | ||
const { boardContent, eventHub, sharer } = opts; | ||
const canvas = boardContent.boardContext.canvas; | ||
const container = opts.container || document.body; | ||
const id = `idraw-middleware-pointer-${Math.random().toString(26).substring(2)}`; | ||
|
||
const getCanvasRect = () => { | ||
const clientRect = canvas.getBoundingClientRect() as DOMRect; | ||
const { left, top, width, height } = clientRect; | ||
return { left, top, width, height }; | ||
}; | ||
|
||
const contextMenuPointer = document.createElement('div'); | ||
contextMenuPointer.setAttribute('id', id); | ||
contextMenuPointer.style.position = 'fixed'; | ||
contextMenuPointer.style.top = '0'; | ||
contextMenuPointer.style.bottom = 'unset'; | ||
contextMenuPointer.style.left = '0'; | ||
contextMenuPointer.style.right = 'unset'; | ||
|
||
// // TODO | ||
// contextMenuPointer.style.width = '10px'; | ||
// contextMenuPointer.style.height = '10px'; | ||
// contextMenuPointer.style.background = 'red'; | ||
|
||
container.appendChild(contextMenuPointer); | ||
|
||
return { | ||
name: '@middleware/pointer', | ||
use() { | ||
// TODO | ||
}, | ||
disuse() { | ||
// TODO | ||
}, | ||
pointStart(e) { | ||
// TODO | ||
}, | ||
pointEnd() { | ||
// TODO | ||
}, | ||
contextMenu(e) { | ||
const { point } = e; | ||
const { left, top } = getCanvasRect(); | ||
contextMenuPointer.style.left = `${left + point.x}px`; | ||
contextMenuPointer.style.top = `${top + point.y}px`; | ||
|
||
const selectedElements = sharer.getSharedStorage(keySelectedElementList); | ||
eventHub.trigger(coreEventKeys.CONTEXT_MENU, { | ||
pointerContainer: contextMenuPointer, | ||
selectedElements: selectedElements || [] | ||
}); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { keySelectedElementList } from '../selector'; | ||
import type { DeepSelectorSharedStorage } from '../selector'; | ||
|
||
export type DeepPointerSharedStorage = Pick<DeepSelectorSharedStorage, typeof keySelectedElementList>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.