Add delete interaction link command
This commit is contained in:
50
src/commands/delete-interaction-link-command.ts
Normal file
50
src/commands/delete-interaction-link-command.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { createOpaqueId } from "../core/ids";
|
||||
import { cloneInteractionLink } from "../interactions/interaction-links";
|
||||
|
||||
import type { EditorCommand } from "./command";
|
||||
|
||||
export function createDeleteInteractionLinkCommand(linkId: string): EditorCommand {
|
||||
let previousLink = null as ReturnType<typeof cloneInteractionLink> | null;
|
||||
|
||||
return {
|
||||
id: createOpaqueId("command"),
|
||||
label: "Delete interaction link",
|
||||
execute(context) {
|
||||
const currentDocument = context.getDocument();
|
||||
const currentLink = currentDocument.interactionLinks[linkId];
|
||||
|
||||
if (currentLink === undefined) {
|
||||
throw new Error(`Interaction link ${linkId} does not exist.`);
|
||||
}
|
||||
|
||||
if (previousLink === null) {
|
||||
previousLink = cloneInteractionLink(currentLink);
|
||||
}
|
||||
|
||||
const nextInteractionLinks = {
|
||||
...currentDocument.interactionLinks
|
||||
};
|
||||
delete nextInteractionLinks[linkId];
|
||||
|
||||
context.setDocument({
|
||||
...currentDocument,
|
||||
interactionLinks: nextInteractionLinks
|
||||
});
|
||||
},
|
||||
undo(context) {
|
||||
if (previousLink === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentDocument = context.getDocument();
|
||||
|
||||
context.setDocument({
|
||||
...currentDocument,
|
||||
interactionLinks: {
|
||||
...currentDocument.interactionLinks,
|
||||
[previousLink.id]: cloneInteractionLink(previousLink)
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user