fix: copy page labels when duplicating pages

This commit is contained in:
Philipinho
2026-09-30 01:15:11 +01:00
parent 01a139c3d1
commit 45c9ca9f1c
2 changed files with 35 additions and 1 deletions
@@ -56,6 +56,7 @@ import { markdownToHtml } from '@docmost/editor-ext';
import { WatcherService } from '../../watcher/watcher.service';
import { sql } from 'kysely';
import { TransclusionService } from '../transclusion/transclusion.service';
import { LabelRepo } from '@docmost/db/repos/label/label.repo';
@Injectable()
export class PageService {
@@ -74,6 +75,7 @@ export class PageService {
private collaborationGateway: CollaborationGateway,
private readonly watcherService: WatcherService,
private readonly transclusionService: TransclusionService,
private readonly labelRepo: LabelRepo,
) {}
async findById(
@@ -715,7 +717,13 @@ export class PageService {
}),
);
await this.db.insertInto('pages').values(insertablePages).execute();
await executeTx(this.db, async (trx) => {
await trx.insertInto('pages').values(insertablePages).execute();
await this.labelRepo.copyLabelsToPages(
new Map([...pageMap].map(([oldId, entry]) => [oldId, entry.newPageId])),
trx,
);
});
// Extract transclusions from every duplicated page and persist them in
// one statement. Duplication bypasses Yjs onStoreDocument; brand-new
@@ -176,6 +176,32 @@ export class LabelRepo {
.execute();
}
async copyLabelsToPages(
pageIdMap: Map<string, string>,
trx?: KyselyTransaction,
): Promise<void> {
if (pageIdMap.size === 0) return;
const db = dbOrTx(this.db, trx);
const sourceLabels = await db
.selectFrom('pageLabels')
.select(['pageId', 'labelId'])
.where('pageId', 'in', [...pageIdMap.keys()])
.execute();
if (sourceLabels.length === 0) return;
await db
.insertInto('pageLabels')
.values(
sourceLabels.map((row) => ({
pageId: pageIdMap.get(row.pageId),
labelId: row.labelId,
})),
)
.onConflict((oc) => oc.doNothing())
.execute();
}
async removeLabelFromPage(
pageId: string,
labelId: string,