mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-25 22:21:03 +10:00
Rework sidebar pages
* Move sidebar pages from workspace to space level * Replace array sorting with lexicographical fractional indexing * Fixes and updates
This commit is contained in:
@ -1,332 +0,0 @@
|
||||
import {
|
||||
forwardRef,
|
||||
Inject,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { MovePageDto } from '../dto/move-page.dto';
|
||||
import {
|
||||
OrderingEntity,
|
||||
orderPageList,
|
||||
removeFromArrayAndSave,
|
||||
TreeNode,
|
||||
} from '../page.util';
|
||||
import { PageService } from './page.service';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||
import { executeTx } from '@docmost/db/utils';
|
||||
import { Page, PageOrdering } from '@docmost/db/types/entity.types';
|
||||
import { PageWithOrderingDto } from '../dto/page-with-ordering.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PageOrderingService {
|
||||
constructor(
|
||||
@Inject(forwardRef(() => PageService))
|
||||
private pageService: PageService,
|
||||
@InjectKysely() private readonly db: KyselyDB,
|
||||
) {}
|
||||
|
||||
// TODO: scope to workspace and space
|
||||
async movePage(dto: MovePageDto, trx?: KyselyTransaction): Promise<void> {
|
||||
await executeTx(
|
||||
this.db,
|
||||
async (trx) => {
|
||||
const movedPageId = dto.pageId;
|
||||
|
||||
const movedPage = await trx
|
||||
.selectFrom('pages as page')
|
||||
.select(['page.id', 'page.spaceId', 'page.parentPageId'])
|
||||
.where('page.id', '=', movedPageId)
|
||||
.executeTakeFirst();
|
||||
|
||||
if (!movedPage) throw new NotFoundException('Moved page not found');
|
||||
|
||||
// if no parentId, it means the page is a root page or now a root page
|
||||
if (!dto.parentId) {
|
||||
// if it had a parent before being moved, we detach it from the previous parent
|
||||
if (movedPage.parentPageId) {
|
||||
await this.removeFromParent(
|
||||
movedPage.parentPageId,
|
||||
dto.pageId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
const spaceOrdering = await this.getEntityOrdering(
|
||||
movedPage.spaceId,
|
||||
OrderingEntity.SPACE,
|
||||
trx,
|
||||
);
|
||||
|
||||
orderPageList(spaceOrdering.childrenIds, dto);
|
||||
await trx
|
||||
.updateTable('pageOrdering')
|
||||
.set(spaceOrdering)
|
||||
.where('id', '=', spaceOrdering.id)
|
||||
.execute();
|
||||
} else {
|
||||
const parentPageId = dto.parentId;
|
||||
|
||||
let parentPageOrdering = await this.getEntityOrdering(
|
||||
parentPageId,
|
||||
OrderingEntity.PAGE,
|
||||
trx,
|
||||
);
|
||||
|
||||
if (!parentPageOrdering) {
|
||||
parentPageOrdering = await this.createPageOrdering(
|
||||
parentPageId,
|
||||
OrderingEntity.PAGE,
|
||||
movedPage.spaceId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
// Check if the parent was changed
|
||||
if (
|
||||
movedPage.parentPageId &&
|
||||
movedPage.parentPageId !== parentPageId
|
||||
) {
|
||||
//if yes, remove moved page from old parent's children
|
||||
await this.removeFromParent(
|
||||
movedPage.parentPageId,
|
||||
dto.pageId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
// If movedPage didn't have a parent initially (was at root level), update the root level
|
||||
if (!movedPage.parentPageId) {
|
||||
await this.removeFromSpacePageOrder(
|
||||
movedPage.spaceId,
|
||||
dto.pageId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
// Modify the children list of the new parentPage and save
|
||||
orderPageList(parentPageOrdering.childrenIds, dto);
|
||||
await trx
|
||||
.updateTable('pageOrdering')
|
||||
.set(parentPageOrdering)
|
||||
.where('id', '=', parentPageOrdering.id)
|
||||
.execute();
|
||||
}
|
||||
|
||||
// update the parent Id of the moved page
|
||||
await trx
|
||||
.updateTable('pages')
|
||||
.set({
|
||||
parentPageId: movedPage.parentPageId || null,
|
||||
})
|
||||
.where('id', '=', movedPage.id)
|
||||
.execute();
|
||||
},
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
async addPageToOrder(
|
||||
spaceId: string,
|
||||
pageId: string,
|
||||
parentPageId?: string,
|
||||
trx?: KyselyTransaction,
|
||||
) {
|
||||
await executeTx(
|
||||
this.db,
|
||||
async (trx: KyselyTransaction) => {
|
||||
if (parentPageId) {
|
||||
await this.upsertOrdering(
|
||||
parentPageId,
|
||||
OrderingEntity.PAGE,
|
||||
pageId,
|
||||
spaceId,
|
||||
trx,
|
||||
);
|
||||
} else {
|
||||
await this.addToSpacePageOrder(pageId, spaceId, trx);
|
||||
}
|
||||
},
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
async addToSpacePageOrder(
|
||||
pageId: string,
|
||||
spaceId: string,
|
||||
trx: KyselyTransaction,
|
||||
) {
|
||||
await this.upsertOrdering(
|
||||
spaceId,
|
||||
OrderingEntity.SPACE,
|
||||
pageId,
|
||||
spaceId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
async upsertOrdering(
|
||||
entityId: string,
|
||||
entityType: string,
|
||||
childId: string,
|
||||
spaceId: string,
|
||||
trx: KyselyTransaction,
|
||||
) {
|
||||
let ordering = await this.getEntityOrdering(entityId, entityType, trx);
|
||||
console.log(ordering);
|
||||
console.log('oga1');
|
||||
|
||||
if (!ordering) {
|
||||
ordering = await this.createPageOrdering(
|
||||
entityId,
|
||||
entityType,
|
||||
spaceId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
if (!ordering.childrenIds.includes(childId)) {
|
||||
ordering.childrenIds.unshift(childId);
|
||||
console.log(childId);
|
||||
console.log('childId above');
|
||||
await trx
|
||||
.updateTable('pageOrdering')
|
||||
.set(ordering)
|
||||
.where('id', '=', ordering.id)
|
||||
.execute();
|
||||
//await manager.save(PageOrdering, ordering);
|
||||
}
|
||||
}
|
||||
|
||||
async removeFromParent(
|
||||
parentId: string,
|
||||
childId: string,
|
||||
trx: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
await this.removeChildFromOrdering(
|
||||
parentId,
|
||||
OrderingEntity.PAGE,
|
||||
childId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
async removeFromSpacePageOrder(
|
||||
spaceId: string,
|
||||
pageId: string,
|
||||
trx: KyselyTransaction,
|
||||
) {
|
||||
await this.removeChildFromOrdering(
|
||||
spaceId,
|
||||
OrderingEntity.SPACE,
|
||||
pageId,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
async removeChildFromOrdering(
|
||||
entityId: string,
|
||||
entityType: string,
|
||||
childId: string,
|
||||
trx: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
const ordering = await this.getEntityOrdering(entityId, entityType, trx);
|
||||
|
||||
if (ordering && ordering.childrenIds.includes(childId)) {
|
||||
await removeFromArrayAndSave(ordering, 'childrenIds', childId, trx);
|
||||
}
|
||||
}
|
||||
|
||||
async removePageFromHierarchy(
|
||||
page: Page,
|
||||
trx: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
if (page.parentPageId) {
|
||||
await this.removeFromParent(page.parentPageId, page.id, trx);
|
||||
} else {
|
||||
await this.removeFromSpacePageOrder(page.spaceId, page.id, trx);
|
||||
}
|
||||
}
|
||||
|
||||
async getEntityOrdering(
|
||||
entityId: string,
|
||||
entityType: string,
|
||||
trx: KyselyTransaction,
|
||||
): Promise<PageOrdering> {
|
||||
return trx
|
||||
.selectFrom('pageOrdering')
|
||||
.selectAll()
|
||||
.where('entityId', '=', entityId)
|
||||
.where('entityType', '=', entityType)
|
||||
.forUpdate()
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
async createPageOrdering(
|
||||
entityId: string,
|
||||
entityType: string,
|
||||
spaceId: string,
|
||||
trx: KyselyTransaction,
|
||||
): Promise<PageOrdering> {
|
||||
await trx
|
||||
.insertInto('pageOrdering')
|
||||
.values({
|
||||
entityId,
|
||||
entityType,
|
||||
spaceId,
|
||||
childrenIds: [],
|
||||
})
|
||||
.onConflict((oc) => oc.columns(['entityId', 'entityType']).doNothing())
|
||||
.execute();
|
||||
|
||||
// Todo: maybe use returning above
|
||||
return await this.getEntityOrdering(entityId, entityType, trx);
|
||||
}
|
||||
|
||||
async getSpacePageOrder(
|
||||
spaceId: string,
|
||||
): Promise<{ id: string; childrenIds: string[]; spaceId: string }> {
|
||||
return await this.db
|
||||
.selectFrom('pageOrdering')
|
||||
.select(['id', 'childrenIds', 'spaceId'])
|
||||
.where('entityId', '=', spaceId)
|
||||
.where('entityType', '=', OrderingEntity.SPACE)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
async convertToTree(spaceId: string): Promise<TreeNode[]> {
|
||||
const spaceOrder = await this.getSpacePageOrder(spaceId);
|
||||
|
||||
const pageOrder = spaceOrder ? spaceOrder.childrenIds : undefined;
|
||||
const pages = await this.pageService.getSidebarPagesBySpaceId(spaceId);
|
||||
|
||||
const pageMap: { [id: string]: PageWithOrderingDto } = {};
|
||||
pages.forEach((page) => {
|
||||
pageMap[page.id] = page;
|
||||
});
|
||||
|
||||
function buildTreeNode(id: string): TreeNode | undefined {
|
||||
const page = pageMap[id];
|
||||
if (!page) return;
|
||||
|
||||
const node: TreeNode = {
|
||||
id: page.id,
|
||||
title: page.title || '',
|
||||
children: [],
|
||||
};
|
||||
|
||||
if (page.icon) node.icon = page.icon;
|
||||
|
||||
if (page.childrenIds && page.childrenIds.length > 0) {
|
||||
node.children = page.childrenIds
|
||||
.map((childId) => buildTreeNode(childId))
|
||||
.filter(Boolean) as TreeNode[];
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
return pageOrder
|
||||
.map((id) => buildTreeNode(id))
|
||||
.filter(Boolean) as TreeNode[];
|
||||
}
|
||||
}
|
||||
@ -1,25 +1,30 @@
|
||||
import {
|
||||
forwardRef,
|
||||
Inject,
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { CreatePageDto } from '../dto/create-page.dto';
|
||||
import { UpdatePageDto } from '../dto/update-page.dto';
|
||||
import { PageOrderingService } from './page-ordering.service';
|
||||
import { PageWithOrderingDto } from '../dto/page-with-ordering.dto';
|
||||
import { transformPageResult } from '../page.util';
|
||||
import { PageRepo } from '@docmost/db/repos/page/page.repo';
|
||||
import { Page } from '@docmost/db/types/entity.types';
|
||||
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||
import {
|
||||
executeWithPagination,
|
||||
PaginationResult,
|
||||
} from '@docmost/db/pagination/pagination';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { KyselyDB } from '@docmost/db/types/kysely.types';
|
||||
import { generateJitteredKeyBetween } from 'fractional-indexing-jittered';
|
||||
import { MovePageDto } from '../dto/move-page.dto';
|
||||
import { ExpressionBuilder } from 'kysely';
|
||||
import { DB } from '@docmost/db/types/db';
|
||||
import { SidebarPageDto } from '../dto/sidebar-page.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PageService {
|
||||
constructor(
|
||||
private pageRepo: PageRepo,
|
||||
@Inject(forwardRef(() => PageOrderingService))
|
||||
private pageOrderingService: PageOrderingService,
|
||||
@InjectKysely() private readonly db: KyselyDB,
|
||||
) {}
|
||||
|
||||
async findById(
|
||||
@ -27,7 +32,7 @@ export class PageService {
|
||||
includeContent?: boolean,
|
||||
includeYdoc?: boolean,
|
||||
): Promise<Page> {
|
||||
return this.pageRepo.findById(pageId, includeContent, includeYdoc);
|
||||
return this.pageRepo.findById(pageId, { includeContent, includeYdoc });
|
||||
}
|
||||
|
||||
async create(
|
||||
@ -38,33 +43,61 @@ export class PageService {
|
||||
// check if parent page exists
|
||||
if (createPageDto.parentPageId) {
|
||||
// TODO: make sure parent page belongs to same space and user has permissions
|
||||
// make sure user has permission to parent.
|
||||
const parentPage = await this.pageRepo.findById(
|
||||
createPageDto.parentPageId,
|
||||
);
|
||||
if (!parentPage) throw new NotFoundException('Parent page not found');
|
||||
}
|
||||
|
||||
let pageId = undefined;
|
||||
if (createPageDto.pageId) {
|
||||
pageId = createPageDto.pageId;
|
||||
delete createPageDto.pageId;
|
||||
let pagePosition: string;
|
||||
|
||||
const lastPageQuery = this.db
|
||||
.selectFrom('pages')
|
||||
.select(['id', 'position'])
|
||||
.where('spaceId', '=', createPageDto.spaceId)
|
||||
.orderBy('position', 'desc')
|
||||
.limit(1);
|
||||
|
||||
// todo: simplify code
|
||||
if (createPageDto.parentPageId) {
|
||||
// check for children of this page
|
||||
const lastPage = await lastPageQuery
|
||||
.where('parentPageId', '=', createPageDto.parentPageId)
|
||||
.executeTakeFirst();
|
||||
|
||||
if (!lastPage) {
|
||||
pagePosition = generateJitteredKeyBetween(null, null);
|
||||
} else {
|
||||
// if there is an existing page, we should get a position below it
|
||||
pagePosition = generateJitteredKeyBetween(lastPage.position, null);
|
||||
}
|
||||
} else {
|
||||
// for root page
|
||||
const lastPage = await lastPageQuery
|
||||
.where('parentPageId', 'is', null)
|
||||
.executeTakeFirst();
|
||||
|
||||
// if no existing page, make this the first
|
||||
if (!lastPage) {
|
||||
pagePosition = generateJitteredKeyBetween(null, null); // we expect "a0"
|
||||
} else {
|
||||
// if there is an existing page, we should get a position below it
|
||||
pagePosition = generateJitteredKeyBetween(lastPage.position, null);
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: should be in a transaction
|
||||
const createdPage = await this.pageRepo.insertPage({
|
||||
...createPageDto,
|
||||
id: pageId,
|
||||
title: createPageDto.title,
|
||||
position: pagePosition,
|
||||
icon: createPageDto.icon,
|
||||
parentPageId: createPageDto.parentPageId,
|
||||
spaceId: createPageDto.spaceId,
|
||||
creatorId: userId,
|
||||
workspaceId: workspaceId,
|
||||
lastUpdatedById: userId,
|
||||
});
|
||||
|
||||
await this.pageOrderingService.addPageToOrder(
|
||||
createPageDto.spaceId,
|
||||
pageId,
|
||||
createPageDto.parentPageId,
|
||||
);
|
||||
|
||||
return createdPage;
|
||||
}
|
||||
|
||||
@ -103,12 +136,90 @@ export class PageService {
|
||||
);
|
||||
}
|
||||
|
||||
async getSidebarPagesBySpaceId(
|
||||
spaceId: string,
|
||||
limit = 200,
|
||||
): Promise<PageWithOrderingDto[]> {
|
||||
const pages = await this.pageRepo.getSpaceSidebarPages(spaceId, limit);
|
||||
return transformPageResult(pages);
|
||||
withHasChildren(eb: ExpressionBuilder<DB, 'pages'>) {
|
||||
return eb
|
||||
.selectFrom('pages as child')
|
||||
.select((eb) =>
|
||||
eb
|
||||
.case()
|
||||
.when(eb.fn.countAll(), '>', 0)
|
||||
.then(true)
|
||||
.else(false)
|
||||
.end()
|
||||
.as('count'),
|
||||
)
|
||||
.whereRef('child.parentPageId', '=', 'pages.id')
|
||||
.limit(1)
|
||||
.as('hasChildren');
|
||||
}
|
||||
|
||||
async getSidebarPages(
|
||||
dto: SidebarPageDto,
|
||||
pagination: PaginationOptions,
|
||||
): Promise<any> {
|
||||
let query = this.db
|
||||
.selectFrom('pages')
|
||||
.select([
|
||||
'id',
|
||||
'title',
|
||||
'icon',
|
||||
'position',
|
||||
'parentPageId',
|
||||
'spaceId',
|
||||
'creatorId',
|
||||
])
|
||||
.select((eb) => this.withHasChildren(eb))
|
||||
.orderBy('position', 'asc')
|
||||
.where('spaceId', '=', dto.spaceId);
|
||||
|
||||
if (dto.pageId) {
|
||||
query = query.where('parentPageId', '=', dto.pageId);
|
||||
} else {
|
||||
query = query.where('parentPageId', 'is', null);
|
||||
}
|
||||
|
||||
const result = executeWithPagination(query, {
|
||||
page: pagination.page,
|
||||
perPage: 250,
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async movePage(dto: MovePageDto) {
|
||||
// validate position value by attempting to generate a key
|
||||
try {
|
||||
generateJitteredKeyBetween(dto.position, null);
|
||||
} catch (err) {
|
||||
throw new BadRequestException('Invalid move position');
|
||||
}
|
||||
|
||||
const movedPage = await this.pageRepo.findById(dto.pageId);
|
||||
if (!movedPage) throw new NotFoundException('Moved page not found');
|
||||
|
||||
let parentPageId: string;
|
||||
if (movedPage.parentPageId === dto.parentPageId) {
|
||||
parentPageId = undefined;
|
||||
} else {
|
||||
// changing the page's parent
|
||||
if (dto.parentPageId) {
|
||||
const parentPage = await this.pageRepo.findById(dto.parentPageId);
|
||||
if (!parentPage) throw new NotFoundException('Parent page not found');
|
||||
}
|
||||
parentPageId = dto.parentPageId;
|
||||
}
|
||||
|
||||
await this.pageRepo.updatePage(
|
||||
{
|
||||
position: dto.position,
|
||||
parentPageId: parentPageId,
|
||||
},
|
||||
dto.pageId,
|
||||
);
|
||||
|
||||
// TODO
|
||||
// check for duplicates?
|
||||
// permissions
|
||||
}
|
||||
|
||||
async getRecentSpacePages(
|
||||
@ -126,8 +237,8 @@ export class PageService {
|
||||
async forceDelete(pageId: string): Promise<void> {
|
||||
await this.pageRepo.deletePage(pageId);
|
||||
}
|
||||
|
||||
/*
|
||||
}
|
||||
/*
|
||||
// TODO: page deletion and restoration
|
||||
async delete(pageId: string): Promise<void> {
|
||||
await this.dataSource.transaction(async (manager: EntityManager) => {
|
||||
@ -217,4 +328,3 @@ export class PageService {
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user