mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-18 11:31:36 +03:00
feat: block remove event
This commit is contained in:
parent
4f0232cbbf
commit
eb2ad57c5a
@ -19,8 +19,8 @@ declare const JWT_DEV: boolean;
|
||||
const logger = getLogger('BlockDB:block');
|
||||
const logger_debug = getLogger('debug:BlockDB:block');
|
||||
|
||||
const GET_BLOCK = Symbol('GET_BLOCK');
|
||||
const SET_PARENT = Symbol('SET_PARENT');
|
||||
const _GET_BLOCK = Symbol('GET_BLOCK');
|
||||
const _SET_PARENT = Symbol('SET_PARENT');
|
||||
|
||||
export class AbstractBlock<
|
||||
B extends BlockInstance<C>,
|
||||
@ -47,6 +47,13 @@ export class AbstractBlock<
|
||||
this._parentListener = new Map();
|
||||
this._parent = parent;
|
||||
JWT_DEV && logger_debug(`init: exists ${this._id}`);
|
||||
if (parent) {
|
||||
parent.addChildrenListener(this._id, states => {
|
||||
if (states.get(this._id) === 'delete') {
|
||||
this._emitParent(parent._id, 'delete');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public get root() {
|
||||
@ -176,18 +183,27 @@ export class AbstractBlock<
|
||||
return this.#block.creator;
|
||||
}
|
||||
|
||||
[GET_BLOCK]() {
|
||||
[_GET_BLOCK]() {
|
||||
return this.#block;
|
||||
}
|
||||
|
||||
[SET_PARENT](parent: AbstractBlock<B, C>) {
|
||||
this._parent = parent;
|
||||
const states: Map<string, 'update'> = new Map([[parent.id, 'update']]);
|
||||
private _emitParent(
|
||||
parentId: string,
|
||||
type: 'update' | 'delete' = 'update'
|
||||
) {
|
||||
const states: Map<string, 'update' | 'delete'> = new Map([
|
||||
[parentId, type],
|
||||
]);
|
||||
for (const listener of this._parentListener.values()) {
|
||||
listener(states);
|
||||
}
|
||||
}
|
||||
|
||||
[_SET_PARENT](parent: AbstractBlock<B, C>) {
|
||||
this._parent = parent;
|
||||
this._emitParent(parent.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get document index tags
|
||||
*/
|
||||
@ -258,8 +274,8 @@ export class AbstractBlock<
|
||||
throw new Error('insertChildren: binary not allow insert children');
|
||||
}
|
||||
|
||||
this.#block.insertChildren(block[GET_BLOCK](), position);
|
||||
block[SET_PARENT](this);
|
||||
this.#block.insertChildren(block[_GET_BLOCK](), position);
|
||||
block[_SET_PARENT](this);
|
||||
}
|
||||
|
||||
public hasChildren(id: string): boolean {
|
||||
|
Loading…
Reference in New Issue
Block a user