Simplified movepageup/down to movepage in multitool. undo now works for any index change

This commit is contained in:
Connor Yoh 2025-02-28 16:40:54 +00:00
parent 167c85e73f
commit 3234e136ec
3 changed files with 16 additions and 69 deletions

View File

@ -35,8 +35,7 @@ class PdfActionsManager {
const sibling = imgContainer.previousSibling; const sibling = imgContainer.previousSibling;
if (sibling) { if (sibling) {
let movePageCommand = this.movePageTo(imgContainer, sibling, true, true); this.movePageTo(imgContainer, sibling, true);
this._pushUndoClearRedo(movePageCommand);
} }
} }
@ -44,12 +43,11 @@ class PdfActionsManager {
var imgContainer = this.getPageContainer(e.target); var imgContainer = this.getPageContainer(e.target);
const sibling = imgContainer.nextSibling; const sibling = imgContainer.nextSibling;
if (sibling) { if (sibling) {
let movePageCommand = this.movePageTo( this.movePageTo(
imgContainer, imgContainer,
sibling.nextSibling, sibling.nextSibling,
true true
); );
this._pushUndoClearRedo(movePageCommand);
} }
} }

View File

@ -1,4 +1,4 @@
import { MovePageUpCommand, MovePageDownCommand } from './commands/move-page.js'; import { MovePageCommand } from './commands/move-page.js';
import { RemoveSelectedCommand } from './commands/remove.js'; import { RemoveSelectedCommand } from './commands/remove.js';
import { RotateAllCommand, RotateElementCommand } from './commands/rotate.js'; import { RotateAllCommand, RotateElementCommand } from './commands/rotate.js';
import { SplitAllCommand } from './commands/split.js'; import { SplitAllCommand } from './commands/split.js';
@ -109,27 +109,17 @@ class PdfContainer {
downloadBtn.disabled = true; downloadBtn.disabled = true;
} }
movePageTo(startElement, endElement, scrollTo = false, moveUp = false) { movePageTo(startElement, endElement, scrollTo = false) {
let movePageCommand; let movePageCommand = new MovePageCommand(
if (moveUp) { startElement,
movePageCommand = new MovePageUpCommand( endElement,
startElement, this.pagesContainer,
endElement, this.pagesContainerWrapper,
this.pagesContainer, scrollTo
this.pagesContainerWrapper, );
scrollTo
);
} else {
movePageCommand = new MovePageDownCommand(
startElement,
endElement,
this.pagesContainer,
this.pagesContainerWrapper,
scrollTo
);
}
movePageCommand.execute(); movePageCommand.execute();
this.undoManager.pushUndoClearRedo(movePageCommand);
return movePageCommand; return movePageCommand;
} }

View File

@ -1,6 +1,6 @@
import {Command} from './command.js'; import {Command} from './command.js';
export class AbstractMovePageCommand extends Command { export class MovePageCommand extends Command {
constructor(startElement, endElement, pagesContainer, pagesContainerWrapper, scrollTo = false) { constructor(startElement, endElement, pagesContainer, pagesContainerWrapper, scrollTo = false) {
super(); super();
@ -16,7 +16,6 @@ export class AbstractMovePageCommand extends Command {
this.scrollTo = scrollTo; this.scrollTo = scrollTo;
this.pagesContainerWrapper = pagesContainerWrapper; this.pagesContainerWrapper = pagesContainerWrapper;
} }
execute() { execute() {
// Check & remove page number elements here too if they exist because Firefox doesn't fire the relevant event on page move. // Check & remove page number elements here too if they exist because Firefox doesn't fire the relevant event on page move.
const pageNumberElement = this.startElement.querySelector('.page-number'); const pageNumberElement = this.startElement.querySelector('.page-number');
@ -42,51 +41,11 @@ export class AbstractMovePageCommand extends Command {
} }
undo() { undo() {
// Requires overriding in child classes
}
redo() {
this.execute();
}
}
export class MovePageUpCommand extends AbstractMovePageCommand {
constructor(startElement, endElement, pagesContainer, pagesContainerWrapper, scrollTo = false) {
super(startElement, endElement, pagesContainer, pagesContainerWrapper, scrollTo);
}
undo() {
if (this.endElement) {
this.pagesContainer.removeChild(this.endElement);
this.startElement.insertAdjacentElement('beforebegin', this.endElement);
}
if (this.scrollTo) {
const {width} = this.startElement.getBoundingClientRect();
const vector = this.endIndex === -1 || this.startIndex <= this.endIndex ? 0 - width : width;
this.pagesContainerWrapper.scroll({
left: this.pagesContainerWrapper.scrollLeft - vector,
});
}
}
redo() {
this.execute();
}
}
export class MovePageDownCommand extends AbstractMovePageCommand {
constructor(startElement, endElement, pagesContainer, pagesContainerWrapper, scrollTo = false) {
super(startElement, endElement, pagesContainer, pagesContainerWrapper, scrollTo);
}
undo() {
let previousElement = this.startElement.previousSibling;
if (this.startElement) { if (this.startElement) {
this.pagesContainer.removeChild(this.startElement); this.pagesContainer.removeChild(this.startElement);
previousElement.insertAdjacentElement('beforebegin', this.startElement); let previousNeighbour = Array.from(this.pagesContainer.childNodes)[this.startIndex];
previousNeighbour?.insertAdjacentElement('beforebegin', this.startElement)
?? this.pagesContainer.append(this.startElement);
} }
if (this.scrollTo) { if (this.scrollTo) {