mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-18 19:51:38 +03:00
Merge pull request #112 from githbq/feature/page-tree-code-style
Feature/page tree code style
This commit is contained in:
commit
34d30d9e43
@ -1,5 +1,5 @@
|
|||||||
import type { BlockClientInstance } from '@toeverything/datasource/jwt';
|
import type { BlockClientInstance } from '@toeverything/datasource/jwt';
|
||||||
import { PAGE_TREE } from '../../utils';
|
import { PAGE_TREE as pageTreeName } from '../../utils';
|
||||||
import type { ReturnUnobserve } from '../database/observer';
|
import type { ReturnUnobserve } from '../database/observer';
|
||||||
import { ServiceBaseClass } from '../base';
|
import { ServiceBaseClass } from '../base';
|
||||||
import { TreeItem } from './types';
|
import { TreeItem } from './types';
|
||||||
@ -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 pageTreeConfig =
|
||||||
workspace_db_block.getDecoration<TreeItem[]>(PAGE_TREE);
|
workspaceDbBlock.getDecoration<TreeItem[]>(pageTreeName);
|
||||||
return page_tree_config;
|
return pageTreeConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 pageTree = await this._fetchPageTree(workspace);
|
||||||
if (page_tree && page_tree.length) {
|
if (pageTree && pageTree.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 [],
|
pageTree 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(pageTreeName, treeData);
|
||||||
}
|
}
|
||||||
|
|
||||||
async addPage<TreeItem>(workspace: string, treeData: TreeItem[] | string) {
|
async addPage<TreeItem>(workspace: string, treeData: TreeItem[] | string) {
|
||||||
@ -51,110 +51,107 @@ export class PageTree extends ServiceBaseClass {
|
|||||||
await dbBlock?.remove();
|
await dbBlock?.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
async addPageToWorkspacee(
|
async addPageToWorkspacee(targetWorkspaceId: string, newPageId: string) {
|
||||||
target_workspace_id: string,
|
const items = await this.getPageTree<TreeItem>(targetWorkspaceId);
|
||||||
new_page_id: string
|
await this.setPageTree(targetWorkspaceId, [
|
||||||
) {
|
{ id: newPageId, children: [] },
|
||||||
const items = await this.getPageTree<TreeItem>(target_workspace_id);
|
|
||||||
await this.setPageTree(target_workspace_id, [
|
|
||||||
{ id: new_page_id, 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 +161,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 +184,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 +210,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
|
||||||
|
Loading…
Reference in New Issue
Block a user