mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-26 14:19:24 +00:00

# Description of Changes A new universal file context rather than the splintered ones for the main views, tools and manager we had before (manager still has its own but its better integreated with the core context) File context has been split it into a handful of different files managing various file related issues separately to reduce the monolith - FileReducer.ts - State management fileActions.ts - File operations fileSelectors.ts - Data access patterns lifecycle.ts - Resource cleanup and memory management fileHooks.ts - React hooks interface contexts.ts - Context providers Improved thumbnail generation Improved indexxedb handling Stopped handling files as blobs were not necessary to improve performance A new library handling drag and drop https://github.com/atlassian/pragmatic-drag-and-drop (Out of scope yes but I broke the old one with the new filecontext and it needed doing so it was a might as well) A new library handling virtualisation on page editor @tanstack/react-virtual, as above. Quickly ripped out the last remnants of the old URL params stuff and replaced with the beginnings of what will later become the new URL navigation system (for now it just restores the tool name in url behavior) Fixed selected file not regestered when opening a tool Fixed png thumbnails Closes #(issue_number) --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: Reece Browne <you@example.com>
335 lines
8.8 KiB
TypeScript
335 lines
8.8 KiB
TypeScript
import { Command, CommandSequence } from '../hooks/useUndoRedo';
|
|
import { PDFDocument, PDFPage } from '../types/pageEditor';
|
|
|
|
// Base class for page operations
|
|
abstract class PageCommand implements Command {
|
|
protected pdfDocument: PDFDocument;
|
|
protected setPdfDocument: (doc: PDFDocument) => void;
|
|
protected previousState: PDFDocument;
|
|
|
|
constructor(
|
|
pdfDocument: PDFDocument,
|
|
setPdfDocument: (doc: PDFDocument) => void
|
|
) {
|
|
this.pdfDocument = pdfDocument;
|
|
this.setPdfDocument = setPdfDocument;
|
|
this.previousState = JSON.parse(JSON.stringify(pdfDocument)); // Deep clone
|
|
}
|
|
|
|
abstract execute(): void;
|
|
abstract description: string;
|
|
|
|
undo(): void {
|
|
this.setPdfDocument(this.previousState);
|
|
}
|
|
}
|
|
|
|
// Rotate pages command
|
|
export class RotatePagesCommand extends PageCommand {
|
|
private pageIds: string[];
|
|
private rotation: number;
|
|
|
|
constructor(
|
|
pdfDocument: PDFDocument,
|
|
setPdfDocument: (doc: PDFDocument) => void,
|
|
pageIds: string[],
|
|
rotation: number
|
|
) {
|
|
super(pdfDocument, setPdfDocument);
|
|
this.pageIds = pageIds;
|
|
this.rotation = rotation;
|
|
}
|
|
|
|
execute(): void {
|
|
const updatedPages = this.pdfDocument.pages.map(page => {
|
|
if (this.pageIds.includes(page.id)) {
|
|
return { ...page, rotation: page.rotation + this.rotation };
|
|
}
|
|
return page;
|
|
});
|
|
|
|
this.setPdfDocument({
|
|
...this.pdfDocument,
|
|
pages: updatedPages,
|
|
totalPages: updatedPages.length
|
|
});
|
|
}
|
|
|
|
get description(): string {
|
|
const direction = this.rotation > 0 ? 'right' : 'left';
|
|
return `Rotate ${this.pageIds.length} page(s) ${direction}`;
|
|
}
|
|
}
|
|
|
|
// Delete pages command
|
|
export class DeletePagesCommand extends PageCommand {
|
|
private pageIds: string[];
|
|
private deletedPages: PDFPage[];
|
|
private deletedPositions: Map<string, number>;
|
|
|
|
constructor(
|
|
pdfDocument: PDFDocument,
|
|
setPdfDocument: (doc: PDFDocument) => void,
|
|
pageIds: string[]
|
|
) {
|
|
super(pdfDocument, setPdfDocument);
|
|
this.pageIds = pageIds;
|
|
this.deletedPages = [];
|
|
this.deletedPositions = new Map();
|
|
}
|
|
|
|
execute(): void {
|
|
// Store deleted pages and their positions for undo
|
|
this.deletedPages = this.pdfDocument.pages.filter(page =>
|
|
this.pageIds.includes(page.id)
|
|
);
|
|
|
|
this.deletedPages.forEach(page => {
|
|
const index = this.pdfDocument.pages.findIndex(p => p.id === page.id);
|
|
this.deletedPositions.set(page.id, index);
|
|
});
|
|
|
|
const updatedPages = this.pdfDocument.pages
|
|
.filter(page => !this.pageIds.includes(page.id))
|
|
.map((page, index) => ({ ...page, pageNumber: index + 1 }));
|
|
|
|
this.setPdfDocument({
|
|
...this.pdfDocument,
|
|
pages: updatedPages,
|
|
totalPages: updatedPages.length
|
|
});
|
|
}
|
|
|
|
undo(): void {
|
|
// Simply restore to the previous state (before deletion)
|
|
this.setPdfDocument(this.previousState);
|
|
}
|
|
|
|
get description(): string {
|
|
return `Delete ${this.pageIds.length} page(s)`;
|
|
}
|
|
}
|
|
|
|
// Move pages command
|
|
export class MovePagesCommand extends PageCommand {
|
|
private pageIds: string[];
|
|
private targetIndex: number;
|
|
private originalIndices: Map<string, number>;
|
|
|
|
constructor(
|
|
pdfDocument: PDFDocument,
|
|
setPdfDocument: (doc: PDFDocument) => void,
|
|
pageIds: string[],
|
|
targetIndex: number
|
|
) {
|
|
super(pdfDocument, setPdfDocument);
|
|
this.pageIds = pageIds;
|
|
this.targetIndex = targetIndex;
|
|
this.originalIndices = new Map();
|
|
}
|
|
|
|
execute(): void {
|
|
// Store original positions
|
|
this.pageIds.forEach(pageId => {
|
|
const index = this.pdfDocument.pages.findIndex(p => p.id === pageId);
|
|
this.originalIndices.set(pageId, index);
|
|
});
|
|
|
|
let newPages = [...this.pdfDocument.pages];
|
|
const pagesToMove = this.pageIds
|
|
.map(id => this.pdfDocument.pages.find(p => p.id === id))
|
|
.filter((page): page is PDFPage => page !== undefined);
|
|
|
|
// Remove pages to move
|
|
newPages = newPages.filter(page => !this.pageIds.includes(page.id));
|
|
|
|
// Insert pages at target position
|
|
newPages.splice(this.targetIndex, 0, ...pagesToMove);
|
|
|
|
// Update page numbers
|
|
newPages = newPages.map((page, index) => ({
|
|
...page,
|
|
pageNumber: index + 1
|
|
}));
|
|
|
|
this.setPdfDocument({
|
|
...this.pdfDocument,
|
|
pages: newPages,
|
|
totalPages: newPages.length
|
|
});
|
|
}
|
|
|
|
get description(): string {
|
|
return `Move ${this.pageIds.length} page(s)`;
|
|
}
|
|
}
|
|
|
|
// Reorder single page command (for drag-and-drop)
|
|
export class ReorderPageCommand extends PageCommand {
|
|
private pageId: string;
|
|
private targetIndex: number;
|
|
private originalIndex: number;
|
|
|
|
constructor(
|
|
pdfDocument: PDFDocument,
|
|
setPdfDocument: (doc: PDFDocument) => void,
|
|
pageId: string,
|
|
targetIndex: number
|
|
) {
|
|
super(pdfDocument, setPdfDocument);
|
|
this.pageId = pageId;
|
|
this.targetIndex = targetIndex;
|
|
this.originalIndex = pdfDocument.pages.findIndex(p => p.id === pageId);
|
|
}
|
|
|
|
execute(): void {
|
|
const newPages = [...this.pdfDocument.pages];
|
|
const [movedPage] = newPages.splice(this.originalIndex, 1);
|
|
newPages.splice(this.targetIndex, 0, movedPage);
|
|
|
|
// Update page numbers
|
|
const updatedPages = newPages.map((page, index) => ({
|
|
...page,
|
|
pageNumber: index + 1
|
|
}));
|
|
|
|
this.setPdfDocument({
|
|
...this.pdfDocument,
|
|
pages: updatedPages,
|
|
totalPages: updatedPages.length
|
|
});
|
|
}
|
|
|
|
get description(): string {
|
|
return `Reorder page ${this.originalIndex + 1} to position ${this.targetIndex + 1}`;
|
|
}
|
|
}
|
|
|
|
// Toggle split markers command
|
|
export class ToggleSplitCommand extends PageCommand {
|
|
private pageIds: string[];
|
|
private previousSplitStates: Map<string, boolean>;
|
|
|
|
constructor(
|
|
pdfDocument: PDFDocument,
|
|
setPdfDocument: (doc: PDFDocument) => void,
|
|
pageIds: string[]
|
|
) {
|
|
super(pdfDocument, setPdfDocument);
|
|
this.pageIds = pageIds;
|
|
this.previousSplitStates = new Map();
|
|
}
|
|
|
|
execute(): void {
|
|
// Store previous split states
|
|
this.pageIds.forEach(pageId => {
|
|
const page = this.pdfDocument.pages.find(p => p.id === pageId);
|
|
if (page) {
|
|
this.previousSplitStates.set(pageId, !!page.splitBefore);
|
|
}
|
|
});
|
|
|
|
const updatedPages = this.pdfDocument.pages.map(page => {
|
|
if (this.pageIds.includes(page.id)) {
|
|
return { ...page, splitBefore: !page.splitBefore };
|
|
}
|
|
return page;
|
|
});
|
|
|
|
this.setPdfDocument({
|
|
...this.pdfDocument,
|
|
pages: updatedPages,
|
|
totalPages: updatedPages.length
|
|
});
|
|
}
|
|
|
|
undo(): void {
|
|
const updatedPages = this.pdfDocument.pages.map(page => {
|
|
if (this.pageIds.includes(page.id)) {
|
|
const previousState = this.previousSplitStates.get(page.id);
|
|
return { ...page, splitBefore: previousState };
|
|
}
|
|
return page;
|
|
});
|
|
|
|
this.setPdfDocument({
|
|
...this.pdfDocument,
|
|
pages: updatedPages,
|
|
totalPages: updatedPages.length
|
|
});
|
|
}
|
|
|
|
get description(): string {
|
|
return `Toggle split markers for ${this.pageIds.length} page(s)`;
|
|
}
|
|
}
|
|
|
|
// Add pages command (for inserting new files)
|
|
export class AddPagesCommand extends PageCommand {
|
|
private newPages: PDFPage[];
|
|
private insertIndex: number;
|
|
|
|
constructor(
|
|
pdfDocument: PDFDocument,
|
|
setPdfDocument: (doc: PDFDocument) => void,
|
|
newPages: PDFPage[],
|
|
insertIndex: number = -1 // -1 means append to end
|
|
) {
|
|
super(pdfDocument, setPdfDocument);
|
|
this.newPages = newPages;
|
|
this.insertIndex = insertIndex === -1 ? pdfDocument.pages.length : insertIndex;
|
|
}
|
|
|
|
execute(): void {
|
|
const newPagesArray = [...this.pdfDocument.pages];
|
|
newPagesArray.splice(this.insertIndex, 0, ...this.newPages);
|
|
|
|
// Update page numbers for all pages
|
|
const updatedPages = newPagesArray.map((page, index) => ({
|
|
...page,
|
|
pageNumber: index + 1
|
|
}));
|
|
|
|
this.setPdfDocument({
|
|
...this.pdfDocument,
|
|
pages: updatedPages,
|
|
totalPages: updatedPages.length
|
|
});
|
|
}
|
|
|
|
undo(): void {
|
|
const updatedPages = this.pdfDocument.pages
|
|
.filter(page => !this.newPages.some(newPage => newPage.id === page.id))
|
|
.map((page, index) => ({ ...page, pageNumber: index + 1 }));
|
|
|
|
this.setPdfDocument({
|
|
...this.pdfDocument,
|
|
pages: updatedPages,
|
|
totalPages: updatedPages.length
|
|
});
|
|
}
|
|
|
|
get description(): string {
|
|
return `Add ${this.newPages.length} page(s)`;
|
|
}
|
|
}
|
|
|
|
// Command sequence for bulk operations
|
|
export class PageCommandSequence implements CommandSequence {
|
|
commands: Command[];
|
|
description: string;
|
|
|
|
constructor(commands: Command[], description?: string) {
|
|
this.commands = commands;
|
|
this.description = description || `Execute ${commands.length} operations`;
|
|
}
|
|
|
|
execute(): void {
|
|
this.commands.forEach(command => command.execute());
|
|
}
|
|
|
|
undo(): void {
|
|
// Undo in reverse order
|
|
[...this.commands].reverse().forEach(command => command.undo());
|
|
}
|
|
} |