@blocksuite/block-std
Advanced tools
Comparing version 0.0.0-canary-20241105001358 to 0.0.0-canary-20241105063536
# @blocksuite/block-std | ||
## 0.0.0-canary-20241105001358 | ||
## 0.0.0-canary-20241105063536 | ||
@@ -9,5 +9,5 @@ ### Patch Changes | ||
- Updated dependencies | ||
- @blocksuite/global@0.0.0-canary-20241105001358 | ||
- @blocksuite/inline@0.0.0-canary-20241105001358 | ||
- @blocksuite/store@0.0.0-canary-20241105001358 | ||
- @blocksuite/global@0.0.0-canary-20241105063536 | ||
- @blocksuite/inline@0.0.0-canary-20241105063536 | ||
- @blocksuite/store@0.0.0-canary-20241105063536 | ||
@@ -14,0 +14,0 @@ ## 0.17.25 |
@@ -16,3 +16,3 @@ export { compare as compareLayer, renderableInEdgeless, SortOrder, } from '../utils/layer.js'; | ||
export { BaseTool, type GfxToolsFullOption, type GfxToolsFullOptionValue, type GfxToolsMap, type GfxToolsOption, } from './tool/tool.js'; | ||
export { ToolController } from './tool/tool-controller.js'; | ||
export { MouseButton, ToolController } from './tool/tool-controller.js'; | ||
export * from './viewport.js'; | ||
@@ -19,0 +19,0 @@ export { GfxViewportElement } from './viewport-element.js'; |
@@ -16,3 +16,3 @@ export { compare as compareLayer, renderableInEdgeless, SortOrder, } from '../utils/layer.js'; | ||
export { BaseTool, } from './tool/tool.js'; | ||
export { ToolController } from './tool/tool-controller.js'; | ||
export { MouseButton, ToolController } from './tool/tool-controller.js'; | ||
export * from './viewport.js'; | ||
@@ -19,0 +19,0 @@ export { GfxViewportElement } from './viewport-element.js'; |
@@ -21,4 +21,11 @@ import type { ServiceIdentifier } from '@blocksuite/global/di'; | ||
export type SupportedHooks = keyof BuiltInEventMap; | ||
declare const supportedEvents: readonly ["dragStart", "dragEnd", "dragMove", "pointerMove", "pointerDown", "pointerUp", "click", "doubleClick", "tripleClick", "pointerOut", "contextMenu"]; | ||
declare const supportedEvents: readonly ["dragStart", "dragEnd", "dragMove", "pointerMove", "contextMenu", "pointerDown", "pointerUp", "click", "doubleClick", "tripleClick", "pointerOut"]; | ||
export type SupportedEvents = (typeof supportedEvents)[number]; | ||
export declare enum MouseButton { | ||
FIFTH = 4, | ||
FOURTH = 3, | ||
MAIN = 0, | ||
MIDDLE = 1, | ||
SECONDARY = 2 | ||
} | ||
export interface ToolEventTarget { | ||
@@ -25,0 +32,0 @@ addHook<K extends SupportedHooks | SupportedEvents>(evtName: K, handler: (evtState: K extends SupportedHooks ? BuiltInEventMap[K] : PointerEventState) => void | boolean): void; |
@@ -11,2 +11,3 @@ import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions'; | ||
'pointerMove', | ||
'contextMenu', | ||
'pointerDown', | ||
@@ -18,4 +19,11 @@ 'pointerUp', | ||
'pointerOut', | ||
'contextMenu', | ||
]; | ||
export var MouseButton; | ||
(function (MouseButton) { | ||
MouseButton[MouseButton["FIFTH"] = 4] = "FIFTH"; | ||
MouseButton[MouseButton["FOURTH"] = 3] = "FOURTH"; | ||
MouseButton[MouseButton["MAIN"] = 0] = "MAIN"; | ||
MouseButton[MouseButton["MIDDLE"] = 1] = "MIDDLE"; | ||
MouseButton[MouseButton["SECONDARY"] = 2] = "SECONDARY"; | ||
})(MouseButton || (MouseButton = {})); | ||
export const eventTarget = Symbol('eventTarget'); | ||
@@ -140,3 +148,3 @@ export class ToolController extends GfxExtension { | ||
const hooks = {}; | ||
const invokeToolHandler = (evtName, evt) => { | ||
const invokeToolHandler = (evtName, evt, tool) => { | ||
const evtHooks = hooks[evtName]; | ||
@@ -149,3 +157,11 @@ const stopHandler = evtHooks?.reduce((pre, hook) => { | ||
} | ||
this.currentTool$.peek()?.[evtName](evt); | ||
tool = tool ?? this.currentTool$.peek(); | ||
try { | ||
tool?.[evtName](evt); | ||
} | ||
catch (e) { | ||
throw new BlockSuiteError(ErrorCode.ExecutionError, `Error occurred while executing ${evtName} handler of tool "${tool?.toolName}"`, { | ||
cause: e, | ||
}); | ||
} | ||
}; | ||
@@ -169,5 +185,13 @@ /** | ||
}; | ||
let dragContext = null; | ||
this._disposableGroup.add(this.std.event.add('dragStart', ctx => { | ||
const evt = ctx.get('pointerState'); | ||
if (evt.button === MouseButton.SECONDARY && | ||
!this.currentTool$.peek()?.allowDragWithRightButton) { | ||
return; | ||
} | ||
if (evt.button === MouseButton.MIDDLE) { | ||
evt.raw.preventDefault(); | ||
} | ||
this.dragging$.value = true; | ||
const evt = ctx.get('pointerState'); | ||
this.draggingViewArea$.value = { | ||
@@ -184,5 +208,12 @@ startX: evt.x, | ||
invokeToolHandler('dragStart', evt); | ||
dragContext = this.currentTool$.peek() | ||
? { | ||
tool: this.currentTool$.peek(), | ||
} | ||
: null; | ||
})); | ||
this._disposableGroup.add(this.std.event.add('dragMove', ctx => { | ||
this.dragging$.value = true; | ||
if (!this.dragging$.peek()) { | ||
return; | ||
} | ||
const evt = ctx.get('pointerState'); | ||
@@ -204,15 +235,12 @@ const draggingStart = { | ||
}; | ||
invokeToolHandler('dragMove', evt); | ||
invokeToolHandler('dragMove', evt, dragContext?.tool); | ||
})); | ||
this._disposableGroup.add(this.std.event.add('dragEnd', ctx => { | ||
if (!this.dragging$.peek()) { | ||
return; | ||
} | ||
this.dragging$.value = false; | ||
const evt = ctx.get('pointerState'); | ||
try { | ||
invokeToolHandler('dragEnd', evt); | ||
} | ||
catch (e) { | ||
throw new Error(`dragEnd handler of ${this.currentToolName$.peek()} throws an error`, { | ||
cause: e, | ||
}); | ||
} | ||
invokeToolHandler('dragEnd', evt, dragContext?.tool); | ||
dragContext = null; | ||
this.draggingViewArea$.value = { | ||
@@ -230,3 +258,2 @@ x: 0, | ||
this._disposableGroup.add(this.std.event.add('pointerMove', ctx => { | ||
this.dragging$.value = false; | ||
const evt = ctx.get('pointerState'); | ||
@@ -239,3 +266,10 @@ this.lastMousePos$.value = { | ||
})); | ||
supportedEvents.slice(4).forEach(evtName => { | ||
this._disposableGroup.add(this.std.event.add('contextMenu', ctx => { | ||
const evt = ctx.get('defaultState'); | ||
// when in editing mode, allow context menu to pop up | ||
if (this.gfx.selection.editing) | ||
return; | ||
evt.event.preventDefault(); | ||
})); | ||
supportedEvents.slice(5).forEach(evtName => { | ||
this._disposableGroup.add(this.std.event.add(evtName, ctx => { | ||
@@ -283,2 +317,6 @@ const evt = ctx.get('pointerState'); | ||
this._builtInHookSlot.emit(beforeUpdateCtx.slotCtx); | ||
if (beforeUpdateCtx.prevented) { | ||
return; | ||
} | ||
this.gfx.selection.clear(); | ||
this.currentTool$.peek()?.deactivate(); | ||
@@ -285,0 +323,0 @@ this.currentToolName$.value = toolNameStr; |
@@ -18,2 +18,3 @@ import { type Container } from '@blocksuite/global/di'; | ||
get active(): boolean; | ||
get allowDragWithRightButton(): boolean; | ||
get controller(): import("./tool-controller.js").ToolController; | ||
@@ -20,0 +21,0 @@ get doc(): import("@blocksuite/store").Doc; |
@@ -11,2 +11,5 @@ import { createIdentifier } from '@blocksuite/global/di'; | ||
} | ||
get allowDragWithRightButton() { | ||
return false; | ||
} | ||
get controller() { | ||
@@ -13,0 +16,0 @@ return this.gfx.tool; |
@@ -9,3 +9,2 @@ import { Container } from '@blocksuite/global/di'; | ||
import { SurfaceMiddlewareExtension } from '../gfx/surface-middleware.js'; | ||
import { ToolController } from '../gfx/tool/tool-controller.js'; | ||
import { BlockServiceIdentifier, BlockViewIdentifier, ConfigIdentifier, LifeCycleWatcherIdentifier, StdIdentifier, } from '../identifier.js'; | ||
@@ -30,3 +29,2 @@ import { RangeManager } from '../range/index.js'; | ||
CursorSelectionExtension, | ||
ToolController, | ||
GfxSelectionManager, | ||
@@ -33,0 +31,0 @@ SurfaceMiddlewareExtension, |
{ | ||
"name": "@blocksuite/block-std", | ||
"version": "0.0.0-canary-20241105001358", | ||
"version": "0.0.0-canary-20241105063536", | ||
"description": "Std for blocksuite blocks", | ||
@@ -23,5 +23,5 @@ "type": "module", | ||
"dependencies": { | ||
"@blocksuite/global": "0.0.0-canary-20241105001358", | ||
"@blocksuite/inline": "0.0.0-canary-20241105001358", | ||
"@blocksuite/store": "0.0.0-canary-20241105001358", | ||
"@blocksuite/global": "0.0.0-canary-20241105063536", | ||
"@blocksuite/inline": "0.0.0-canary-20241105063536", | ||
"@blocksuite/store": "0.0.0-canary-20241105063536", | ||
"@lit/context": "^1.1.2", | ||
@@ -28,0 +28,0 @@ "@preact/signals-core": "^1.8.0", |
@@ -65,5 +65,5 @@ export { | ||
} from './tool/tool.js'; | ||
export { ToolController } from './tool/tool-controller.js'; | ||
export { MouseButton, ToolController } from './tool/tool-controller.js'; | ||
export * from './viewport.js'; | ||
export { GfxViewportElement } from './viewport-element.js'; | ||
export { generateKeyBetween, generateNKeysBetween } from 'fractional-indexing'; |
@@ -47,2 +47,3 @@ import type { ServiceIdentifier } from '@blocksuite/global/di'; | ||
'pointerMove', | ||
'contextMenu', | ||
'pointerDown', | ||
@@ -54,3 +55,2 @@ 'pointerUp', | ||
'pointerOut', | ||
'contextMenu', | ||
] as const; | ||
@@ -60,2 +60,10 @@ | ||
export enum MouseButton { | ||
FIFTH = 4, | ||
FOURTH = 3, | ||
MAIN = 0, | ||
MIDDLE = 1, | ||
SECONDARY = 2, | ||
} | ||
export interface ToolEventTarget { | ||
@@ -238,3 +246,4 @@ addHook<K extends SupportedHooks | SupportedEvents>( | ||
evtName: SupportedEvents, | ||
evt: PointerEventState | ||
evt: PointerEventState, | ||
tool?: BaseTool | ||
) => { | ||
@@ -250,3 +259,14 @@ const evtHooks = hooks[evtName]; | ||
this.currentTool$.peek()?.[evtName](evt); | ||
tool = tool ?? this.currentTool$.peek(); | ||
try { | ||
tool?.[evtName](evt); | ||
} catch (e) { | ||
throw new BlockSuiteError( | ||
ErrorCode.ExecutionError, | ||
`Error occurred while executing ${evtName} handler of tool "${tool?.toolName}"`, | ||
{ | ||
cause: e as Error, | ||
} | ||
); | ||
} | ||
}; | ||
@@ -281,7 +301,22 @@ | ||
let dragContext: { | ||
tool: BaseTool; | ||
} | null = null; | ||
this._disposableGroup.add( | ||
this.std.event.add('dragStart', ctx => { | ||
this.dragging$.value = true; | ||
const evt = ctx.get('pointerState'); | ||
if ( | ||
evt.button === MouseButton.SECONDARY && | ||
!this.currentTool$.peek()?.allowDragWithRightButton | ||
) { | ||
return; | ||
} | ||
if (evt.button === MouseButton.MIDDLE) { | ||
evt.raw.preventDefault(); | ||
} | ||
this.dragging$.value = true; | ||
this.draggingViewArea$.value = { | ||
@@ -299,2 +334,8 @@ startX: evt.x, | ||
invokeToolHandler('dragStart', evt); | ||
dragContext = this.currentTool$.peek() | ||
? { | ||
tool: this.currentTool$.peek()!, | ||
} | ||
: null; | ||
}) | ||
@@ -305,3 +346,6 @@ ); | ||
this.std.event.add('dragMove', ctx => { | ||
this.dragging$.value = true; | ||
if (!this.dragging$.peek()) { | ||
return; | ||
} | ||
const evt = ctx.get('pointerState'); | ||
@@ -325,3 +369,3 @@ const draggingStart = { | ||
invokeToolHandler('dragMove', evt); | ||
invokeToolHandler('dragMove', evt, dragContext?.tool); | ||
}) | ||
@@ -332,16 +376,12 @@ ); | ||
this.std.event.add('dragEnd', ctx => { | ||
if (!this.dragging$.peek()) { | ||
return; | ||
} | ||
this.dragging$.value = false; | ||
const evt = ctx.get('pointerState'); | ||
try { | ||
invokeToolHandler('dragEnd', evt); | ||
} catch (e) { | ||
throw new Error( | ||
`dragEnd handler of ${this.currentToolName$.peek()} throws an error`, | ||
{ | ||
cause: e, | ||
} | ||
); | ||
} | ||
invokeToolHandler('dragEnd', evt, dragContext?.tool); | ||
dragContext = null; | ||
this.draggingViewArea$.value = { | ||
@@ -362,3 +402,2 @@ x: 0, | ||
this.std.event.add('pointerMove', ctx => { | ||
this.dragging$.value = false; | ||
const evt = ctx.get('pointerState'); | ||
@@ -375,3 +414,14 @@ | ||
supportedEvents.slice(4).forEach(evtName => { | ||
this._disposableGroup.add( | ||
this.std.event.add('contextMenu', ctx => { | ||
const evt = ctx.get('defaultState'); | ||
// when in editing mode, allow context menu to pop up | ||
if (this.gfx.selection.editing) return; | ||
evt.event.preventDefault(); | ||
}) | ||
); | ||
supportedEvents.slice(5).forEach(evtName => { | ||
this._disposableGroup.add( | ||
@@ -446,2 +496,8 @@ this.std.event.add(evtName, ctx => { | ||
if (beforeUpdateCtx.prevented) { | ||
return; | ||
} | ||
this.gfx.selection.clear(); | ||
this.currentTool$.peek()?.deactivate(); | ||
@@ -448,0 +504,0 @@ this.currentToolName$.value = toolNameStr; |
@@ -34,2 +34,6 @@ import { type Container, createIdentifier } from '@blocksuite/global/di'; | ||
get allowDragWithRightButton() { | ||
return false; | ||
} | ||
get controller() { | ||
@@ -36,0 +40,0 @@ return this.gfx.tool; |
@@ -15,3 +15,2 @@ import type { ServiceProvider } from '@blocksuite/global/di'; | ||
import { SurfaceMiddlewareExtension } from '../gfx/surface-middleware.js'; | ||
import { ToolController } from '../gfx/tool/tool-controller.js'; | ||
import { | ||
@@ -54,3 +53,2 @@ BlockServiceIdentifier, | ||
CursorSelectionExtension, | ||
ToolController, | ||
GfxSelectionManager, | ||
@@ -57,0 +55,0 @@ SurfaceMiddlewareExtension, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1385814
20099
+ Added@blocksuite/global@0.0.0-canary-20241105063536(transitive)
+ Added@blocksuite/inline@0.0.0-canary-20241105063536(transitive)
+ Added@blocksuite/store@0.0.0-canary-20241105063536(transitive)
+ Added@blocksuite/sync@0.0.0-canary-20241105063536(transitive)
- Removed@blocksuite/global@0.0.0-canary-20241105001358(transitive)
- Removed@blocksuite/inline@0.0.0-canary-20241105001358(transitive)
- Removed@blocksuite/store@0.0.0-canary-20241105001358(transitive)
- Removed@blocksuite/sync@0.0.0-canary-20241105001358(transitive)