Skip to content

Commit

Permalink
Restore bézier edit target on undo
Browse files Browse the repository at this point in the history
Add infrastructure for tools to restore state on undo/redo
  • Loading branch information
daem-on committed Apr 28, 2024
1 parent 73cd9a9 commit 37a4182
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ToolEventMap = {
"wheel": WheelEvent,
"activate": void,
"deactivate": void,
"restore": void,
};

type EventHandler<T extends keyof ToolEventMap> = (event: ToolEventMap[T]) => void
Expand Down
33 changes: 30 additions & 3 deletions src/tools/bezier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const bezier = defineTool({

function createNewPath() {
path = createPath();
markTarget(path);

const settings = getSettings(path) as LineSettings;
const options = bezierOptions.value;
Expand All @@ -76,6 +77,7 @@ export const bezier = defineTool({

function finish() {
clearSelection();
unmarkTarget(path);
undo.snapshot("bezierFinish");
path = null;
}
Expand All @@ -100,6 +102,7 @@ export const bezier = defineTool({

} else {
path = hitResult.item;
markTarget(path);
if (!hitResult.item.closed && findHandle(path, event.point) && isEndingSegment(hitResult.segment)) {
mode = "continue";
currentSegment = hitResult.segment;
Expand Down Expand Up @@ -140,6 +143,7 @@ export const bezier = defineTool({
// always connects to first segment)
if (!hitResult.segment.isFirst()) hoverPath.reverse();
path.join(hoverPath);
unmarkTarget(path);
path = null;
unselectSegment();
undo.snapshot("bezierJoin");
Expand Down Expand Up @@ -181,16 +185,24 @@ export const bezier = defineTool({
on("mouseup", event => {
if (event.event.button > 0) return; // only first mouse button

if (mode === "add") {
undo.snapshot("bezierAdd");
}
if (mode === "add") undo.snapshot("bezierAdd");
else if (path && path.closed) finish();
mode = null;
});

on("deactivate", () => {
for (const item of getMarkedItems())
unmarkTarget(item as paper.Path);
path = null;
});

on("restore", () => {
const paths = getMarkedItems();
if (paths.length) {
path = paths[0] as paper.Path;
path.selected = true;
}
});
},
});

Expand Down Expand Up @@ -222,3 +234,18 @@ const findHandle = function(path: paper.Path, point: paper.Point) {
function isEndingSegment(segment: paper.Segment) {
return segment.isLast() || segment.isFirst();
}

function markTarget(path: paper.Path) {
path.data.bezierTarget = true;
}

function unmarkTarget(path: paper.Path) {
delete path.data.bezierTarget;
}

function getMarkedItems(): paper.Path[] {
return paper.project.getItems({
match: item => item.data?.bezierTarget,
recursive: true,
}) as paper.Path[];
}
2 changes: 2 additions & 0 deletions src/undo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { updateWindow } from "./objectSettings/objectOptionPanel";
import { triggers } from "./triggers";
import { compressToUint8Array, decompressFromUint8Array } from "lz-string";
import { get } from "./filesio/configManagement";
import { activeToolRef } from "./tools";

type UndoState = {
type: string,
Expand Down Expand Up @@ -93,6 +94,7 @@ function restore(entry: UndoState) {
paper.project.importJSON(text);
layer.reinitLayers(activeLayerID);
updateWindow(true);
activeToolRef.value.emit("restore", undefined);
}

export function clear() {
Expand Down

0 comments on commit 37a4182

Please sign in to comment.