open as worspace implementation
This commit is contained in:
@@ -23,6 +23,10 @@ export function createWorkspaceId(): string {
|
||||
return createId("workspace");
|
||||
}
|
||||
|
||||
export function createPdfId(): string {
|
||||
return createId('pdf');
|
||||
}
|
||||
|
||||
export function defaultWorkspaceNameFromPdfName(pdfName: string): string {
|
||||
return pdfName.replace(/\.pdf$/i, "") || "Untitled workspace";
|
||||
}
|
||||
|
||||
39
src/workspace/workspaceSelection.test.ts
Normal file
39
src/workspace/workspaceSelection.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { PageRef } from '../pdf/pdfTypes';
|
||||
import {
|
||||
createSelectionPdfName,
|
||||
createSelectionWorkspaceName,
|
||||
getSelectedPagesInVisualOrder,
|
||||
} from './workspaceSelection';
|
||||
|
||||
function page(id: string, sourcePageIndex: number): PageRef {
|
||||
return { id, sourcePageIndex, rotation: 0 };
|
||||
}
|
||||
|
||||
describe('workspaceSelection', () => {
|
||||
it('returns selected pages in current visual order', () => {
|
||||
const pages = [page('page-3', 2), page('page-1', 0), page('page-2', 1)];
|
||||
|
||||
expect(getSelectedPagesInVisualOrder(pages, ['page-2', 'page-3'])).toEqual([
|
||||
pages[0],
|
||||
pages[2],
|
||||
]);
|
||||
});
|
||||
|
||||
it('ignores selected ids that are no longer present', () => {
|
||||
const pages = [page('page-1', 0), page('page-2', 1)];
|
||||
|
||||
expect(getSelectedPagesInVisualOrder(pages, ['missing', 'page-2'])).toEqual(
|
||||
[pages[1]]
|
||||
);
|
||||
});
|
||||
|
||||
it('creates readable derived workspace and PDF names', () => {
|
||||
expect(createSelectionWorkspaceName('contract.pdf', 3)).toBe(
|
||||
'contract - 3-page-selection'
|
||||
);
|
||||
expect(createSelectionPdfName('contract.pdf', 1)).toBe(
|
||||
'contract - 1-page-selection.pdf'
|
||||
);
|
||||
});
|
||||
});
|
||||
31
src/workspace/workspaceSelection.ts
Normal file
31
src/workspace/workspaceSelection.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { PageRef } from '../pdf/pdfTypes';
|
||||
|
||||
export function getSelectedPagesInVisualOrder(
|
||||
pages: PageRef[],
|
||||
selectedPageIds: string[]
|
||||
): PageRef[] {
|
||||
if (pages.length === 0 || selectedPageIds.length === 0) return [];
|
||||
|
||||
const selectedSet = new Set(selectedPageIds);
|
||||
return pages.filter((page) => selectedSet.has(page.id));
|
||||
}
|
||||
|
||||
export function createSelectionWorkspaceName(
|
||||
pdfName: string,
|
||||
selectedPageCount: number
|
||||
): string {
|
||||
const baseName = pdfName.replace(/\.pdf$/i, '') || 'Untitled';
|
||||
const suffix =
|
||||
selectedPageCount === 1
|
||||
? '1-page-selection'
|
||||
: `${selectedPageCount}-page-selection`;
|
||||
|
||||
return `${baseName} - ${suffix}`;
|
||||
}
|
||||
|
||||
export function createSelectionPdfName(
|
||||
pdfName: string,
|
||||
selectedPageCount: number
|
||||
): string {
|
||||
return `${createSelectionWorkspaceName(pdfName, selectedPageCount)}.pdf`;
|
||||
}
|
||||
Reference in New Issue
Block a user