Add step by step macro execution

This commit is contained in:
Joaquín Bernal 2024-04-03 00:29:25 +02:00
parent 180c9086c5
commit 97175f40ae
No known key found for this signature in database
GPG key ID: 138E479EAA74283F
5 changed files with 63 additions and 27 deletions

View file

@ -15,7 +15,7 @@ import {
PageHeaderManager,
StatusBarManager,
} from "./manager/commands";
import { Action, CommanderSettings, Macro } from "./types";
import { Action, CommanderSettings, Macro, MacroItem } from "./types";
import CommanderSettingTab from "./ui/settingTab";
import SettingTabModal from "./ui/settingTabModal";
@ -54,29 +54,37 @@ export default class CommanderPlugin extends Plugin {
const macro = this.settings.macros[id];
if (!macro) throw new Error("Macro not found");
for (const command of macro.macro) {
switch (command.action) {
case Action.COMMAND: {
await app.commands.executeCommandById(command.commandId);
continue;
}
case Action.DELAY: {
await new Promise((resolve) =>
setTimeout(resolve, command.delay)
);
continue;
}
case Action.EDITOR: {
continue;
}
case Action.LOOP: {
for (let i = 0; i < command.times; i++) {
await app.commands.executeCommandById(
command.commandId
);
}
continue;
if (macro.stepByStep){
const nextCommandIndex = macro.nextCommandIndex || 0;
const command = macro.macro[nextCommandIndex];
macro.nextCommandIndex = (nextCommandIndex + 1) % macro.macro.length;
await this.executeMacroCommand(command);
}
else {
for (const command of macro.macro) {
await this.executeMacroCommand(command);
}
}
}
private async executeMacroCommand(command: MacroItem): Promise<void> {
switch (command.action) {
case Action.COMMAND: {
app.commands.executeCommandById(command.commandId);
break;
}
case Action.DELAY: {
await new Promise((resolve) => setTimeout(resolve, command.delay));
break;
}
case Action.EDITOR: {
break;
}
case Action.LOOP: {
for (let i = 0; i < command.times; i++) {
app.commands.executeCommandById(command.commandId);
}
break;
}
}
}

View file

@ -497,6 +497,15 @@
}
}
.cmdr-macro-label {
display: inline-block;
margin-left: 1em;
padding: 1px 5px;
border:1px solid #00000030;
border-radius: 4px;
font-size: 90%;
}
.cmdr-mm-item {
display: flex;
flex-direction: row !important;

View file

@ -19,6 +19,8 @@ export interface Macro {
startup?: boolean;
enterFullscreen?: boolean;
exitFullscreen?: boolean;
stepByStep?: boolean;
nextCommandIndex?: number;
macro: MacroItem[];
}

View file

@ -24,6 +24,7 @@ export default function ({
const [startup, setStartup] = useState(macro.startup || false);
const [enterFullscreen, setEnterFullscreen] = useState(macro.enterFullscreen || false);
const [exitFullscreen, setExitFullscreen] = useState(macro.exitFullscreen || false);
const [stepByStep, setStepByStep] = useState(macro.stepByStep || false);
const [macroCommands, setMacroCommands] = useState<MacroItem[]>(
JSON.parse(JSON.stringify(macro.macro)) || []
);
@ -77,6 +78,18 @@ export default function ({
</div>
<div className="setting-item cmrd-mm-checkboxes">
<div>
<input
type="checkbox"
id="checkbox-step-by-step"
checked={stepByStep}
onChange={({ target }) => {
//@ts-expect-error
setStepByStep(target?.checked ?? false);
}}
/>
<label htmlFor="checkbox-step-by-step">Execute just one step sequentially for each run</label>
</div>
<div>
<input
type="checkbox"
@ -87,7 +100,7 @@ export default function ({
setStartup(target?.checked ?? false);
}}
/>
<label htmlFor="checkbox-startup">Auto-Run on Startup</label>
<label htmlFor="checkbox-startup">Auto-run on startup</label>
</div>
<div>
<input
@ -99,7 +112,7 @@ export default function ({
setEnterFullscreen(target?.checked ?? false);
}}
/>
<label htmlFor="checkbox-enter-fullscreen">Auto-Run on Entering Fullscreen</label>
<label htmlFor="checkbox-enter-fullscreen">Auto-run on entering fullscreen</label>
</div>
<div>
<input
@ -111,7 +124,7 @@ export default function ({
setExitFullscreen(target?.checked ?? false);
}}
/>
<label htmlFor="checkbox-exit-fullscreen">Auto-Run on Exiting Fullscreen</label>
<label htmlFor="checkbox-exit-fullscreen">Auto-run on exiting fullscreen</label>
</div>
</div>
@ -284,7 +297,7 @@ export default function ({
disabled={macroCommands.length === 0}
onClick={() =>
macroCommands.length &&
onSave({ macro: macroCommands, name, icon, startup, enterFullscreen, exitFullscreen })
onSave({ macro: macroCommands, name, icon, startup, enterFullscreen, exitFullscreen, stepByStep })
}
>
Save

View file

@ -52,6 +52,10 @@ export default function MacroViewer({
<div className="setting-item-name">{item.name}</div>
<div className="setting-item-description">
{item.macro.length} Actions
{item.startup && (<span class="cmdr-macro-label">Startup</span>)}
{item.enterFullscreen && (<span class="cmdr-macro-label">Enter fullscreen</span>)}
{item.exitFullscreen && (<span class="cmdr-macro-label">Exit fullscreen</span>)}
{item.stepByStep && (<span class="cmdr-macro-label">Step by step</span>)}
</div>
</div>
<div className="setting-item-control">