mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-25 18:26:05 +08:00
refactor(user-config): refactor snake_case to lowerUpperCase
This commit is contained in:
@@ -6,40 +6,40 @@ import { PageConfigItem } from './types';
|
|||||||
|
|
||||||
/** Operate the user configuration at the workspace level */
|
/** Operate the user configuration at the workspace level */
|
||||||
export class UserConfig extends ServiceBaseClass {
|
export class UserConfig extends ServiceBaseClass {
|
||||||
private async fetch_recent_pages(
|
private async _fetchRecentPages(
|
||||||
workspace: string
|
workspace: string
|
||||||
): Promise<Record<string, Array<PageConfigItem>>> {
|
): Promise<Record<string, Array<PageConfigItem>>> {
|
||||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||||
const recent_work_pages =
|
const recentWorkPages =
|
||||||
workspace_db_block.getDecoration<
|
workspaceDbBlock.getDecoration<
|
||||||
Record<string, Array<PageConfigItem>>
|
Record<string, Array<PageConfigItem>>
|
||||||
>(RECENT_PAGES) || {};
|
>(RECENT_PAGES) || {};
|
||||||
return recent_work_pages;
|
return recentWorkPages;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async save_recent_pages(
|
private async _saveRecentPages(
|
||||||
workspace: string,
|
workspace: string,
|
||||||
recentPages: Record<string, Array<PageConfigItem>>
|
recentPages: Record<string, Array<PageConfigItem>>
|
||||||
) {
|
) {
|
||||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||||
workspace_db_block.setDecoration(RECENT_PAGES, recentPages);
|
workspaceDbBlock.setDecoration(RECENT_PAGES, recentPages);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUserInitialPage(
|
async getUserInitialPage(
|
||||||
workspace: string,
|
workspace: string,
|
||||||
userId: string
|
userId: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const recent_pages = await this.getRecentPages(workspace, userId);
|
const recentPages = await this.getRecentPages(workspace, userId);
|
||||||
if (recent_pages.length > 0) {
|
if (recentPages.length > 0) {
|
||||||
return recent_pages[0].id;
|
return recentPages[0].id;
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = await this.database.getDatabase(workspace);
|
const db = await this.database.getDatabase(workspace);
|
||||||
const new_page = await db.get('page');
|
const newPage = await db.get('page');
|
||||||
|
|
||||||
await this.get_dependency(PageTree).addPage(workspace, new_page.id);
|
await this.get_dependency(PageTree).addPage(workspace, newPage.id);
|
||||||
await this.addRecentPage(workspace, userId, new_page.id);
|
await this.addRecentPage(workspace, userId, newPage.id);
|
||||||
return new_page.id;
|
return newPage.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRecentPages(
|
async getRecentPages(
|
||||||
@@ -47,13 +47,10 @@ export class UserConfig extends ServiceBaseClass {
|
|||||||
userId: string,
|
userId: string,
|
||||||
topNumber = 5
|
topNumber = 5
|
||||||
): Promise<PageConfigItem[]> {
|
): Promise<PageConfigItem[]> {
|
||||||
const recent_work_pages = await this.fetch_recent_pages(workspace);
|
const recentWorkPages = await this._fetchRecentPages(workspace);
|
||||||
const recent_pages = (recent_work_pages[userId] || []).slice(
|
const recentPages = (recentWorkPages[userId] || []).slice(0, topNumber);
|
||||||
0,
|
|
||||||
topNumber
|
|
||||||
);
|
|
||||||
const db = await this.database.getDatabase(workspace);
|
const db = await this.database.getDatabase(workspace);
|
||||||
for (const item of recent_pages) {
|
for (const item of recentPages) {
|
||||||
const page = await db.get(item.id as 'page');
|
const page = await db.get(item.id as 'page');
|
||||||
item.title =
|
item.title =
|
||||||
page
|
page
|
||||||
@@ -61,39 +58,39 @@ export class UserConfig extends ServiceBaseClass {
|
|||||||
?.value?.map(v => v.text)
|
?.value?.map(v => v.text)
|
||||||
.join('') || 'Untitled';
|
.join('') || 'Untitled';
|
||||||
}
|
}
|
||||||
return recent_pages;
|
return recentPages;
|
||||||
}
|
}
|
||||||
|
|
||||||
async addRecentPage(workspace: string, userId: string, pageId: string) {
|
async addRecentPage(workspace: string, userId: string, pageId: string) {
|
||||||
const recent_work_pages = await this.fetch_recent_pages(workspace);
|
const recentWorkPages = await this._fetchRecentPages(workspace);
|
||||||
let recent_pages = recent_work_pages[userId] || [];
|
let recentPages = recentWorkPages[userId] || [];
|
||||||
recent_pages = recent_pages.filter(item => item.id !== pageId);
|
recentPages = recentPages.filter(item => item.id !== pageId);
|
||||||
recent_pages.unshift({
|
recentPages.unshift({
|
||||||
id: pageId,
|
id: pageId,
|
||||||
lastOpenTime: Date.now(),
|
lastOpenTime: Date.now(),
|
||||||
});
|
});
|
||||||
recent_work_pages[userId] = recent_pages;
|
recentWorkPages[userId] = recentPages;
|
||||||
await this.save_recent_pages(workspace, recent_work_pages);
|
await this._saveRecentPages(workspace, recentWorkPages);
|
||||||
}
|
}
|
||||||
|
|
||||||
async removePage(workspace: string, pageId: string) {
|
async removePage(workspace: string, pageId: string) {
|
||||||
const recent_work_pages = await this.fetch_recent_pages(workspace);
|
const recentWorkPages = await this._fetchRecentPages(workspace);
|
||||||
for (const key in recent_work_pages) {
|
for (const key in recentWorkPages) {
|
||||||
recent_work_pages[key] = recent_work_pages[key].filter(
|
recentWorkPages[key] = recentWorkPages[key].filter(
|
||||||
item => item.id !== pageId
|
item => item.id !== pageId
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
await this.save_recent_pages(workspace, recent_work_pages);
|
await this._saveRecentPages(workspace, recentWorkPages);
|
||||||
}
|
}
|
||||||
|
|
||||||
async observe(
|
async observe(
|
||||||
{ workspace }: { workspace: string },
|
{ workspace }: { workspace: 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(states, block);
|
callback(states, block);
|
||||||
}
|
}
|
||||||
@@ -105,24 +102,24 @@ export class UserConfig 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 getWorkspaceName(workspace: string): Promise<string> {
|
async getWorkspaceName(workspace: string): Promise<string> {
|
||||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||||
const workspaceName =
|
const workspaceName =
|
||||||
workspace_db_block.getDecoration<string>(WORKSPACE_CONFIG) || '';
|
workspaceDbBlock.getDecoration<string>(WORKSPACE_CONFIG) || '';
|
||||||
return workspaceName;
|
return workspaceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getWorkspaceId(workspace: string): Promise<string> {
|
async getWorkspaceId(workspace: string): Promise<string> {
|
||||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||||
return workspace_db_block.id;
|
return workspaceDbBlock.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
async setWorkspaceName(workspace: string, workspaceName: string) {
|
async setWorkspaceName(workspace: string, workspaceName: string) {
|
||||||
const workspace_db_block = await this.getWorkspaceDbBlock(workspace);
|
const workspaceDbBlock = await this.getWorkspaceDbBlock(workspace);
|
||||||
workspace_db_block.setDecoration(WORKSPACE_CONFIG, workspaceName);
|
workspaceDbBlock.setDecoration(WORKSPACE_CONFIG, workspaceName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user