@blocksuite/block-std
Advanced tools
Comparing version 0.0.0-canary-20241105114520 to 0.0.0-canary-20241106001401
# @blocksuite/block-std | ||
## 0.0.0-canary-20241105114520 | ||
## 0.0.0-canary-20241106001401 | ||
@@ -9,5 +9,5 @@ ### Patch Changes | ||
- Updated dependencies | ||
- @blocksuite/global@0.0.0-canary-20241105114520 | ||
- @blocksuite/inline@0.0.0-canary-20241105114520 | ||
- @blocksuite/store@0.0.0-canary-20241105114520 | ||
- @blocksuite/global@0.0.0-canary-20241106001401 | ||
- @blocksuite/inline@0.0.0-canary-20241106001401 | ||
- @blocksuite/store@0.0.0-canary-20241106001401 | ||
@@ -14,0 +14,0 @@ ## 0.17.26 |
@@ -31,2 +31,8 @@ import type { ServiceIdentifier } from '@blocksuite/global/di'; | ||
export interface ToolEventTarget { | ||
/** | ||
* Add a hook before the event is handled by the tool. | ||
* Return false to prevent the tool from handling the event. | ||
* @param evtName | ||
* @param handler | ||
*/ | ||
addHook<K extends SupportedHooks | SupportedEvents>(evtName: K, handler: (evtState: K extends SupportedHooks ? BuiltInEventMap[K] : PointerEventState) => void | boolean): void; | ||
@@ -33,0 +39,0 @@ } |
@@ -146,2 +146,6 @@ import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions'; | ||
const hooks = {}; | ||
/** | ||
* Invoke the hook and the tool handler. | ||
* @returns false if the handler is prevented by the hook | ||
*/ | ||
const invokeToolHandler = (evtName, evt, tool) => { | ||
@@ -152,8 +156,9 @@ const evtHooks = hooks[evtName]; | ||
}, false); | ||
if (stopHandler || !this.currentTool$.peek()) { | ||
return; | ||
tool = tool ?? this.currentTool$.peek(); | ||
if (stopHandler) { | ||
return false; | ||
} | ||
tool = tool ?? this.currentTool$.peek(); | ||
try { | ||
tool?.[evtName](evt); | ||
return true; | ||
} | ||
@@ -204,8 +209,15 @@ catch (e) { | ||
}; | ||
invokeToolHandler('dragStart', evt); | ||
dragContext = this.currentTool$.peek() | ||
? { | ||
tool: this.currentTool$.peek(), | ||
} | ||
: null; | ||
// this means the dragEnd event is not even fired | ||
// so we need to manually call the dragEnd method | ||
if (dragContext?.tool) { | ||
dragContext.tool.dragEnd(evt); | ||
dragContext = null; | ||
} | ||
if (invokeToolHandler('dragStart', evt)) { | ||
dragContext = this.currentTool$.peek() | ||
? { | ||
tool: this.currentTool$.peek(), | ||
} | ||
: null; | ||
} | ||
})); | ||
@@ -240,3 +252,9 @@ this._disposableGroup.add(this.std.event.add('dragMove', ctx => { | ||
const evt = ctx.get('pointerState'); | ||
invokeToolHandler('dragEnd', evt, dragContext?.tool); | ||
if (!invokeToolHandler('dragEnd', evt, dragContext?.tool)) { | ||
// if the tool dragEnd is prevented by the hook, call the dragEnd method manually | ||
// this guarantee the dragStart and dragEnd events are always called together | ||
if (dragContext?.tool) { | ||
dragContext.tool.dragEnd(evt); | ||
} | ||
} | ||
dragContext = null; | ||
@@ -243,0 +261,0 @@ this.draggingViewArea$.value = { |
{ | ||
"name": "@blocksuite/block-std", | ||
"version": "0.0.0-canary-20241105114520", | ||
"version": "0.0.0-canary-20241106001401", | ||
"description": "Std for blocksuite blocks", | ||
@@ -23,5 +23,5 @@ "type": "module", | ||
"dependencies": { | ||
"@blocksuite/global": "0.0.0-canary-20241105114520", | ||
"@blocksuite/inline": "0.0.0-canary-20241105114520", | ||
"@blocksuite/store": "0.0.0-canary-20241105114520", | ||
"@blocksuite/global": "0.0.0-canary-20241106001401", | ||
"@blocksuite/inline": "0.0.0-canary-20241106001401", | ||
"@blocksuite/store": "0.0.0-canary-20241106001401", | ||
"@lit/context": "^1.1.2", | ||
@@ -28,0 +28,0 @@ "@preact/signals-core": "^1.8.0", |
@@ -67,2 +67,8 @@ import type { ServiceIdentifier } from '@blocksuite/global/di'; | ||
export interface ToolEventTarget { | ||
/** | ||
* Add a hook before the event is handled by the tool. | ||
* Return false to prevent the tool from handling the event. | ||
* @param evtName | ||
* @param handler | ||
*/ | ||
addHook<K extends SupportedHooks | SupportedEvents>( | ||
@@ -242,2 +248,6 @@ evtName: K, | ||
> = {}; | ||
/** | ||
* Invoke the hook and the tool handler. | ||
* @returns false if the handler is prevented by the hook | ||
*/ | ||
const invokeToolHandler = ( | ||
@@ -253,9 +263,11 @@ evtName: SupportedEvents, | ||
if (stopHandler || !this.currentTool$.peek()) { | ||
return; | ||
tool = tool ?? this.currentTool$.peek(); | ||
if (stopHandler) { | ||
return false; | ||
} | ||
tool = tool ?? this.currentTool$.peek(); | ||
try { | ||
tool?.[evtName](evt); | ||
return true; | ||
} catch (e) { | ||
@@ -330,9 +342,16 @@ throw new BlockSuiteError( | ||
invokeToolHandler('dragStart', evt); | ||
// this means the dragEnd event is not even fired | ||
// so we need to manually call the dragEnd method | ||
if (dragContext?.tool) { | ||
dragContext.tool.dragEnd(evt); | ||
dragContext = null; | ||
} | ||
dragContext = this.currentTool$.peek() | ||
? { | ||
tool: this.currentTool$.peek()!, | ||
} | ||
: null; | ||
if (invokeToolHandler('dragStart', evt)) { | ||
dragContext = this.currentTool$.peek() | ||
? { | ||
tool: this.currentTool$.peek()!, | ||
} | ||
: null; | ||
} | ||
}) | ||
@@ -378,3 +397,9 @@ ); | ||
invokeToolHandler('dragEnd', evt, dragContext?.tool); | ||
if (!invokeToolHandler('dragEnd', evt, dragContext?.tool)) { | ||
// if the tool dragEnd is prevented by the hook, call the dragEnd method manually | ||
// this guarantee the dragStart and dragEnd events are always called together | ||
if (dragContext?.tool) { | ||
dragContext.tool.dragEnd(evt); | ||
} | ||
} | ||
@@ -381,0 +406,0 @@ dragContext = null; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1389999
20147
+ Added@blocksuite/global@0.0.0-canary-20241106001401(transitive)
+ Added@blocksuite/inline@0.0.0-canary-20241106001401(transitive)
+ Added@blocksuite/store@0.0.0-canary-20241106001401(transitive)
+ Added@blocksuite/sync@0.0.0-canary-20241106001401(transitive)
- Removed@blocksuite/global@0.0.0-canary-20241105114520(transitive)
- Removed@blocksuite/inline@0.0.0-canary-20241105114520(transitive)
- Removed@blocksuite/store@0.0.0-canary-20241105114520(transitive)
- Removed@blocksuite/sync@0.0.0-canary-20241105114520(transitive)