feat: 新增 onDetectingChange 事件监听, 可监听 hover 元素变化

This commit is contained in:
lihao.ylh 2021-09-16 16:11:40 +08:00
parent 2c0761fb6b
commit 0672f3b8cc

View File

@ -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);
};
}
}