mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 10:36:22 +08:00
feat: 1. add toc;
This commit is contained in:
@@ -333,6 +333,12 @@ export class Editor implements Virgo {
|
||||
return await this.getBlock({ workspace: this.workspace, id: blockId });
|
||||
}
|
||||
|
||||
async getBlockByIds(ids: string[]): Promise<Awaited<AsyncBlock | null>[]> {
|
||||
return await Promise.all(
|
||||
ids.map(id => this.getBlock({ workspace: this.workspace, id }))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: to be optimized
|
||||
* get block`s dom by block`s id
|
||||
@@ -477,6 +483,13 @@ export class Editor implements Virgo {
|
||||
return await services.api.editorBlock.query(this.workspace, query);
|
||||
}
|
||||
|
||||
async queryByPageId(pageId: string) {
|
||||
return await services.api.editorBlock.get({
|
||||
workspace: this.workspace,
|
||||
ids: [pageId],
|
||||
});
|
||||
}
|
||||
|
||||
/** Hooks */
|
||||
|
||||
public getHooks(): HooksRunner & PluginHooks {
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user