From 0672f3b8cc22b34e6a1662526e0341b91b7e1565 Mon Sep 17 00:00:00 2001 From: "lihao.ylh" Date: Thu, 16 Sep 2021 16:11:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20onDetectingChange?= =?UTF-8?q?=20=E4=BA=8B=E4=BB=B6=E7=9B=91=E5=90=AC,=20=E5=8F=AF=E7=9B=91?= =?UTF-8?q?=E5=90=AC=20hover=20=E5=85=83=E7=B4=A0=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/designer/src/designer/detecting.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/designer/src/designer/detecting.ts b/packages/designer/src/designer/detecting.ts index 92f0ceb17..e818d1379 100644 --- a/packages/designer/src/designer/detecting.ts +++ b/packages/designer/src/designer/detecting.ts @@ -1,6 +1,9 @@ import { makeObservable, obx } from '@ali/lowcode-editor-core'; +import { EventEmitter } from 'events'; import { Node, DocumentModel } from '../document'; +const DETECTING_CHANGE_EVENT = 'detectingChange'; + export class Detecting { @obx.ref private _enable = true; @@ -19,6 +22,8 @@ export class Detecting { @obx.ref private _current: Node | null = null; + private emitter: EventEmitter = new EventEmitter(); + constructor() { makeObservable(this); } @@ -28,12 +33,16 @@ export class Detecting { } capture(node: Node | null) { - this._current = node; + if (this._current !== node) { + this._current = node; + this.emitter.emit(DETECTING_CHANGE_EVENT, this.current); + } } release(node: Node) { if (this._current === node) { this._current = null; + this.emitter.emit(DETECTING_CHANGE_EVENT, this.current); } } @@ -42,4 +51,11 @@ export class Detecting { this._current = null; } } + + onDetectingChange(fn: () => void) { + this.emitter.on(DETECTING_CHANGE_EVENT, fn); + return () => { + this.emitter.off(DETECTING_CHANGE_EVENT, fn); + }; + } }