Add set box brush volume settings command
This commit is contained in:
33
src/commands/set-box-brush-volume-settings-command.js
Normal file
33
src/commands/set-box-brush-volume-settings-command.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { createOpaqueId } from "../core/ids";
|
||||
import { cloneBoxBrushVolumeSettings } from "../document/brushes";
|
||||
import { getBoxBrushOrThrow, replaceBrush } from "./brush-command-helpers";
|
||||
export function createSetBoxBrushVolumeSettingsCommand(options) {
|
||||
const nextVolume = cloneBoxBrushVolumeSettings(options.volume);
|
||||
let previousVolume = null;
|
||||
return {
|
||||
id: createOpaqueId("command"),
|
||||
label: options.label ?? "Set box volume settings",
|
||||
execute(context) {
|
||||
const currentDocument = context.getDocument();
|
||||
const brush = getBoxBrushOrThrow(currentDocument, options.brushId);
|
||||
if (previousVolume === null) {
|
||||
previousVolume = cloneBoxBrushVolumeSettings(brush.volume);
|
||||
}
|
||||
context.setDocument(replaceBrush(currentDocument, {
|
||||
...brush,
|
||||
volume: cloneBoxBrushVolumeSettings(nextVolume)
|
||||
}));
|
||||
},
|
||||
undo(context) {
|
||||
if (previousVolume === null) {
|
||||
return;
|
||||
}
|
||||
const currentDocument = context.getDocument();
|
||||
const brush = getBoxBrushOrThrow(currentDocument, options.brushId);
|
||||
context.setDocument(replaceBrush(currentDocument, {
|
||||
...brush,
|
||||
volume: cloneBoxBrushVolumeSettings(previousVolume)
|
||||
}));
|
||||
}
|
||||
};
|
||||
}
|
||||
51
src/commands/set-box-brush-volume-settings-command.ts
Normal file
51
src/commands/set-box-brush-volume-settings-command.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { createOpaqueId } from "../core/ids";
|
||||
import { cloneBoxBrushVolumeSettings, type BoxBrushVolumeSettings } from "../document/brushes";
|
||||
|
||||
import { getBoxBrushOrThrow, replaceBrush } from "./brush-command-helpers";
|
||||
import type { EditorCommand } from "./command";
|
||||
|
||||
interface SetBoxBrushVolumeSettingsCommandOptions {
|
||||
brushId: string;
|
||||
volume: BoxBrushVolumeSettings;
|
||||
label?: string;
|
||||
}
|
||||
|
||||
export function createSetBoxBrushVolumeSettingsCommand(options: SetBoxBrushVolumeSettingsCommandOptions): EditorCommand {
|
||||
const nextVolume = cloneBoxBrushVolumeSettings(options.volume);
|
||||
let previousVolume: BoxBrushVolumeSettings | null = null;
|
||||
|
||||
return {
|
||||
id: createOpaqueId("command"),
|
||||
label: options.label ?? "Set box volume settings",
|
||||
execute(context) {
|
||||
const currentDocument = context.getDocument();
|
||||
const brush = getBoxBrushOrThrow(currentDocument, options.brushId);
|
||||
|
||||
if (previousVolume === null) {
|
||||
previousVolume = cloneBoxBrushVolumeSettings(brush.volume);
|
||||
}
|
||||
|
||||
context.setDocument(
|
||||
replaceBrush(currentDocument, {
|
||||
...brush,
|
||||
volume: cloneBoxBrushVolumeSettings(nextVolume)
|
||||
})
|
||||
);
|
||||
},
|
||||
undo(context) {
|
||||
if (previousVolume === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentDocument = context.getDocument();
|
||||
const brush = getBoxBrushOrThrow(currentDocument, options.brushId);
|
||||
|
||||
context.setDocument(
|
||||
replaceBrush(currentDocument, {
|
||||
...brush,
|
||||
volume: cloneBoxBrushVolumeSettings(previousVolume)
|
||||
})
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user