-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: ToolBox with controllers * feat: insert table and picture by ToolBar
- Loading branch information
Showing
17 changed files
with
274 additions
and
95 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
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,2 @@ | ||
export const TEXT_BOX_DEFAULT_WIDTH = 300 | ||
export const TEXT_BOX_DEFAULT_HEIGHT = 100 |
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,57 @@ | ||
import { EventManager } from '@Kernel/EventManager' | ||
import { controllers } from './controller/_index' | ||
|
||
export type ToolType = 'Default' | 'TextBox' | 'Shape' | 'Picture' | 'Table' | 'Pen' | ||
|
||
export class ToolBox { | ||
private _currentToolType: ToolType = 'Default' | ||
private _slideElement?: HTMLElement | ||
|
||
events = { | ||
toolChange: new EventManager<ToolType>(), | ||
} | ||
|
||
mount(slideElement: HTMLElement) { | ||
this._slideElement = slideElement | ||
this._bindEvents() | ||
} | ||
|
||
private get _currentController() { | ||
return controllers[this._currentToolType] | ||
} | ||
|
||
private _bindEvents() { | ||
const eventNames = ['Click', 'MouseDown', 'MouseMove', 'MouseUp'] as const | ||
const previousHandlerMap = Object.create(null) | ||
const _addListener = () => { | ||
eventNames.forEach((eventName) => { | ||
const handler = this._currentController[`handle${eventName}`].bind(this._currentController) | ||
if (eventName === 'MouseUp') { | ||
document.addEventListener('mouseup', handler) | ||
} else { | ||
this._slideElement?.addEventListener( | ||
eventName.toLowerCase() as Lowercase<(typeof eventNames)[number]>, | ||
handler | ||
) | ||
} | ||
previousHandlerMap[eventName] = handler | ||
}) | ||
} | ||
const _removeListener = () => { | ||
eventNames.forEach((eventName) => { | ||
const handler = previousHandlerMap[eventName] | ||
if (eventName === 'MouseUp') { | ||
document.removeEventListener('mouseup', handler) | ||
} else { | ||
this._slideElement?.removeEventListener(eventName.toLowerCase(), previousHandlerMap[eventName]) | ||
} | ||
}) | ||
} | ||
_addListener() | ||
this.events.toolChange.on((toolType) => { | ||
this._currentToolType = toolType | ||
_removeListener() | ||
_addListener() | ||
}) | ||
} | ||
} |
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,16 @@ | ||
import { ToolController } from './_ToolController' | ||
|
||
export class DefaultToolController extends ToolController { | ||
handleClick(): void { | ||
console.log('default click') | ||
} | ||
handleMouseDown(): void { | ||
console.log('default mousedown') | ||
} | ||
handleMouseMove(): void { | ||
console.log('default move') | ||
} | ||
handleMouseUp(): void { | ||
console.log('defualt up') | ||
} | ||
} |
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,16 @@ | ||
import { ToolController } from './_ToolController' | ||
|
||
export class PictureToolController extends ToolController { | ||
handleClick(): void { | ||
throw new Error('Method not implemented.') | ||
} | ||
handleMouseDown(): void { | ||
throw new Error('Method not implemented.') | ||
} | ||
handleMouseMove(): void { | ||
throw new Error('Method not implemented.') | ||
} | ||
handleMouseUp(): void { | ||
throw new Error('Method not implemented.') | ||
} | ||
} |
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,44 @@ | ||
import { TextBoxBlock } from '@BlockHub/TextBoxBlock/TextBoxBlock' | ||
import { ToolController } from './_ToolController' | ||
import { slideManager, toolBox } from '@Kernel/index' | ||
import { TEXT_BOX_DEFAULT_HEIGHT, TEXT_BOX_DEFAULT_WIDTH } from '@Const/block' | ||
|
||
export class TextBoxToolController extends ToolController { | ||
private _currentBlock?: TextBoxBlock | ||
private _dragging = false | ||
|
||
handleClick() {} | ||
|
||
handleMouseDown(event: MouseEvent) { | ||
const { offsetX, offsetY } = event | ||
this._currentBlock = new TextBoxBlock(offsetX, offsetY, 0, 0) | ||
slideManager.currentSlide.addBlock(this._currentBlock) | ||
} | ||
|
||
handleMouseMove(event: MouseEvent): void { | ||
const block = this._currentBlock | ||
const { x, y } = (event.currentTarget as HTMLElement).getBoundingClientRect() | ||
const { clientX, clientY } = event | ||
const slideX = clientX - x | ||
const slideY = clientY - y | ||
if (block && slideX > block.x && slideY > block.y) { | ||
this._dragging = true | ||
block.width = slideX - block.x | ||
block.height = slideY - block.y | ||
} | ||
} | ||
|
||
handleMouseUp(): void { | ||
const block = this._currentBlock | ||
if (!block) { | ||
return | ||
} | ||
if (this._dragging) { | ||
this._dragging = false | ||
} else { | ||
block.width = TEXT_BOX_DEFAULT_WIDTH | ||
block.height = TEXT_BOX_DEFAULT_HEIGHT | ||
} | ||
toolBox.events.toolChange.emit('Default') | ||
} | ||
} |
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,6 @@ | ||
export abstract class ToolController { | ||
abstract handleClick(event: MouseEvent): void | ||
abstract handleMouseDown(event: MouseEvent): void | ||
abstract handleMouseMove(event: MouseEvent): void | ||
abstract handleMouseUp(event: MouseEvent): void | ||
} |
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,10 @@ | ||
import { DefaultToolController } from './DefaultToolController' | ||
import { PictureToolController } from './PictureToolController' | ||
import { TextBoxToolController } from './TextBoxToolController' | ||
import { ToolController } from './_ToolController' | ||
|
||
export const controllers: { [key: string]: ToolController } = { | ||
Default: new DefaultToolController(), | ||
Picture: new PictureToolController(), | ||
TextBox: new TextBoxToolController(), | ||
} |
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.