feat: 1. add toc;

This commit is contained in:
mitsuha
2022-08-23 15:37:44 +08:00
parent 7b4999225a
commit ab070daa7e
9 changed files with 350 additions and 38 deletions
@@ -5,6 +5,7 @@ import { Database } from './database';
import { EditorBlock } from './editor-block';
import { FileService } from './file';
import { PageTree } from './workspace/page-tree';
import { TOC } from './workspace/toc';
import { UserConfig } from './workspace/user-config';
export {
@@ -48,6 +49,7 @@ export interface DbServicesMap {
userConfig: UserConfig;
file: FileService;
commentService: CommentService;
tocService: TOC;
}
interface RegisterDependencyConfigWithName extends RegisterDependencyConfig {
@@ -76,6 +78,13 @@ const dbServiceConfig: RegisterDependencyConfigWithName[] = [
value: PageTree,
dependencies: [{ token: Database }],
},
{
type: 'class',
callName: 'tocService',
token: TOC,
value: TOC,
dependencies: [{ token: Database }],
},
{
type: 'class',
callName: 'userConfig',
@@ -0,0 +1,28 @@
import { ServiceBaseClass } from '../base';
import { ReturnUnobserve } from '../database/observer';
import { ObserveCallback } from './page-tree';
export class TOC extends ServiceBaseClass {
private onActivePageChange?: () => void;
async observe(
{ workspace, pageId }: { workspace: string; pageId: string },
callback: ObserveCallback
): Promise<ReturnUnobserve> {
// not only use observe, but also addChildrenListener is OK 🎉🎉🎉
// const pageBlock = await this.getBlock(workspace, pageId);
//
// this.onActivePageChange?.();
// pageBlock?.addChildrenListener('onPageChildrenChange', () => {
// callback();
// });
//
// this.onActivePageChange = () => pageBlock?.removeChildrenListener('onPageChildrenChange');
const unobserve = await this._observe(workspace, pageId, callback);
return () => {
unobserve();
};
}
}