feat: page-tree code style

This commit is contained in:
biqing.hu
2022-08-05 23:24:42 +08:00
parent 62b057d5c1
commit 78ef250ee1

View File

@@ -7,22 +7,22 @@ import { TreeItem } from './types';
export type ObserveCallback = () => void; export type ObserveCallback = () => void;
export class PageTree extends ServiceBaseClass { export class PageTree extends ServiceBaseClass {
private async fetch_page_tree<TreeItem>(workspace: string) { private async fetchPageTree<TreeItem>(workspace: string) {
const workspace_db_block = await this.getWorkspaceDbBlock(workspace); const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
const page_tree_config = const PAGE_TREEConfig =
workspace_db_block.getDecoration<TreeItem[]>(PAGE_TREE); workspaceDbBlock.getDecoration<TreeItem[]>(PAGE_TREE);
return page_tree_config; return PAGE_TREEConfig;
} }
async getPageTree<TreeItem>(workspace: string): Promise<TreeItem[]> { async getPageTree<TreeItem>(workspace: string): Promise<TreeItem[]> {
try { try {
const page_tree = await this.fetch_page_tree(workspace); const PAGE_TREE = await this.fetchPageTree(workspace);
if (page_tree && page_tree.length) { if (PAGE_TREE && PAGE_TREE.length) {
const db = await this.database.getDatabase(workspace); const db = await this.database.getDatabase(workspace);
const pages = await update_tree_items_title( const pages = await updateTreeItemsTitle(
db, db,
page_tree as [], PAGE_TREE as [],
{} {}
); );
return pages; return pages;
@@ -36,8 +36,8 @@ export class PageTree extends ServiceBaseClass {
/** @deprecated should implement more fine-grained crud methods instead of replacing each time with a new array */ /** @deprecated should implement more fine-grained crud methods instead of replacing each time with a new array */
async setPageTree<TreeItem>(workspace: string, treeData: TreeItem[]) { async setPageTree<TreeItem>(workspace: string, treeData: TreeItem[]) {
const workspace_db_block = await this.getWorkspaceDbBlock(workspace); const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
workspace_db_block.setDecoration(PAGE_TREE, treeData); workspaceDbBlock.setDecoration(PAGE_TREE, treeData);
} }
async addPage<TreeItem>(workspace: string, treeData: TreeItem[] | string) { async addPage<TreeItem>(workspace: string, treeData: TreeItem[] | string) {
@@ -52,109 +52,109 @@ export class PageTree extends ServiceBaseClass {
} }
async addPageToWorkspacee( async addPageToWorkspacee(
target_workspace_id: string, targetWorkspaceId: string,
new_page_id: string newPageId: string
) { ) {
const items = await this.getPageTree<TreeItem>(target_workspace_id); const items = await this.getPageTree<TreeItem>(targetWorkspaceId);
await this.setPageTree(target_workspace_id, [ await this.setPageTree(targetWorkspaceId, [
{ id: new_page_id, children: [] }, { id: newPageId, children: [] },
...items, ...items,
]); ]);
} }
async addChildPageToWorkspace( async addChildPageToWorkspace(
target_workspace_id: string, targetWorkspaceId: string,
parent_page_id: string, parentPageId: string,
new_page_id: string newPageId: string
) { ) {
const pages = await this.getPageTree<TreeItem>(target_workspace_id); const pages = await this.getPageTree<TreeItem>(targetWorkspaceId);
this.build_items_for_child_page(parent_page_id, new_page_id, pages); this.buildItemsForChildPage(parentPageId, newPageId, pages);
await this.setPageTree<TreeItem>(target_workspace_id, [...pages]); await this.setPageTree<TreeItem>(targetWorkspaceId, [...pages]);
} }
async addPrevPageToWorkspace( async addPrevPageToWorkspace(
target_workspace_id: string, targetWorkspaceId: string,
parent_page_id: string, parentPageId: string,
new_page_id: string newPageId: string
) { ) {
const pages = await this.getPageTree<TreeItem>(target_workspace_id); const pages = await this.getPageTree<TreeItem>(targetWorkspaceId);
this.build_items_for_prev_page(parent_page_id, new_page_id, pages); this.buildItemsForPrevPage(parentPageId, newPageId, pages);
await this.setPageTree<TreeItem>(target_workspace_id, [...pages]); await this.setPageTree<TreeItem>(targetWorkspaceId, [...pages]);
} }
async addNextPageToWorkspace( async addNextPageToWorkspace(
target_workspace_id: string, targetWorkspaceId: string,
parent_page_id: string, parentPageId: string,
new_page_id: string newPageId: string
) { ) {
const pages = await this.getPageTree<TreeItem>(target_workspace_id); const pages = await this.getPageTree<TreeItem>(targetWorkspaceId);
this.build_items_for_next_page(parent_page_id, new_page_id, pages); this.buildItemsForNextPage(parentPageId, newPageId, pages);
await this.setPageTree<TreeItem>(target_workspace_id, [...pages]); await this.setPageTree<TreeItem>(targetWorkspaceId, [...pages]);
} }
private build_items_for_next_page( private buildItemsForNextPage(
parent_page_id: string, parentPageId: string,
new_page_id: string, newPageId: string,
children: TreeItem[] children: TreeItem[]
) { ) {
for (let i = 0; i < children.length; i++) { for (let i = 0; i < children.length; i++) {
const child_page = children[i]; const childPage = children[i];
if (child_page.id === parent_page_id) { if (childPage.id === parentPageId) {
const new_page = { const newPage = {
id: new_page_id, id: newPageId,
title: 'Untitled', title: 'Untitled',
children: [] as TreeItem[], children: [] as TreeItem[],
}; };
children = children.splice(i + 1, 0, new_page); children = children.splice(i + 1, 0, newPage);
} else if (child_page.children && child_page.children.length) { } else if (childPage.children && childPage.children.length) {
this.build_items_for_next_page( this.buildItemsForNextPage(
parent_page_id, parentPageId,
new_page_id, newPageId,
child_page.children childPage.children
); );
} }
} }
} }
private build_items_for_prev_page( private buildItemsForPrevPage(
parent_page_id: string, parentPageId: string,
new_page_id: string, newPageId: string,
children: TreeItem[] children: TreeItem[]
) { ) {
for (let i = 0; i < children.length; i++) { for (let i = 0; i < children.length; i++) {
const child_page = children[i]; const childPage = children[i];
if (child_page.id === parent_page_id) { if (childPage.id === parentPageId) {
const new_page = { const newPage = {
id: new_page_id, id: newPageId,
title: 'Untitled', title: 'Untitled',
children: [] as TreeItem[], children: [] as TreeItem[],
}; };
children = children.splice(i - 1, 0, new_page); children = children.splice(i - 1, 0, newPage);
} else if (child_page.children && child_page.children.length) { } else if (childPage.children && childPage.children.length) {
this.build_items_for_prev_page( this.buildItemsForPrevPage(
parent_page_id, parentPageId,
new_page_id, newPageId,
child_page.children childPage.children
); );
} }
} }
} }
private build_items_for_child_page( private buildItemsForChildPage(
parent_page_id: string, parentPageId: string,
new_page_id: string, newPageId: string,
children: TreeItem[] children: TreeItem[]
) { ) {
for (let i = 0; i < children.length; i++) { for (let i = 0; i < children.length; i++) {
const child_page = children[i]; const childPage = children[i];
if (child_page.id === parent_page_id) { if (childPage.id === parentPageId) {
child_page.children = child_page.children || []; childPage.children = childPage.children || [];
child_page.children.push({ childPage.children.push({
id: new_page_id, id: newPageId,
title: 'Untitled', title: 'Untitled',
children: [], children: [],
}); });
} else if (child_page.children && child_page.children.length) { } else if (childPage.children && childPage.children.length) {
this.build_items_for_child_page( this.buildItemsForChildPage(
parent_page_id, parentPageId,
new_page_id, newPageId,
child_page.children childPage.children
); );
} }
} }
@@ -164,10 +164,10 @@ export class PageTree extends ServiceBaseClass {
{ workspace, page }: { workspace: string; page: string }, { workspace, page }: { workspace: string; page: string },
callback: ObserveCallback callback: ObserveCallback
): Promise<ReturnUnobserve> { ): Promise<ReturnUnobserve> {
const workspace_db_block = await this.getWorkspaceDbBlock(workspace); const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
const unobserveWorkspace = await this._observe( const unobserveWorkspace = await this._observe(
workspace, workspace,
workspace_db_block.id, workspaceDbBlock.id,
(states, block) => { (states, block) => {
callback(); callback();
} }
@@ -187,12 +187,12 @@ export class PageTree extends ServiceBaseClass {
} }
async unobserve({ workspace }: { workspace: string }) { async unobserve({ workspace }: { workspace: string }) {
const workspace_db_block = await this.getWorkspaceDbBlock(workspace); const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
await this._unobserve(workspace, workspace_db_block.id); await this._unobserve(workspace, workspaceDbBlock.id);
} }
} }
async function update_tree_items_title< async function updateTreeItemsTitle<
TreeItem extends { id: string; title: string; children: TreeItem[] } TreeItem extends { id: string; title: string; children: TreeItem[] }
>( >(
db: BlockClientInstance, db: BlockClientInstance,
@@ -213,7 +213,7 @@ async function update_tree_items_title<
} }
if (item.children.length) { if (item.children.length) {
item.children = await update_tree_items_title( item.children = await updateTreeItemsTitle(
db, db,
item.children, item.children,
cache cache