Skip to content

Commit d333a3c

Browse files
committed
Minor fixes and improvements
- Throw error if insert after line can't be found - #22 - Add drag & drop to macro commands - Support user script member access in macros
1 parent c9e3b43 commit d333a3c

File tree

10 files changed

+92
-23
lines changed

10 files changed

+92
-23
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "quickadd",
33
"name": "QuickAdd",
4-
"version": "0.2.5",
4+
"version": "0.2.6",
55
"minAppVersion": "0.12.00",
66
"description": "Quickly add new pages or content to your vault.",
77
"author": "Christian B. B. Houmann",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quickadd",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "Quickly add new pages or content to your vault.",
55
"main": "main.js",
66
"scripts": {

src/engine/MacroChoiceEngine.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import type QuickAdd from "../main";
1616
import type {IChoiceExecutor} from "../IChoiceExecutor";
1717
import {ChoiceType} from "../types/choices/choiceType";
1818
import type IMultiChoice from "../types/choices/IMultiChoice";
19+
import {getUserScriptMemberAccess} from "../utility";
1920

2021
export class MacroChoiceEngine extends QuickAddChoiceEngine {
2122
public choice: IMacroChoice;
23+
public params = {app: this.app, quickAddApi: QuickAddApi.GetApi(this.app), variables: {}};
2224
protected output: string;
2325
protected macros: IMacro[];
2426
protected choiceExecutor: IChoiceExecutor;
25-
public params = {app: this.app, quickAddApi: QuickAddApi.GetApi(this.app), variables: {}};
2627
protected readonly plugin: QuickAdd;
2728

2829
constructor(app: App, plugin: QuickAdd, choice: IMacroChoice, macros: IMacro[], choiceExecutor: IChoiceExecutor, variables: Map<string, string>) {
@@ -59,12 +60,12 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
5960
// https://github.com/SilentVoid13/Templater/blob/master/src/UserTemplates/UserTemplateParser.ts
6061
protected async executeUserScript(command: IUserScript) {
6162
const userScript = await this.getUserScript(command);
62-
if (!userScript || !userScript.default) {
63+
if (!userScript) {
6364
log.logError(`failed to load user script ${command.path}.`);
6465
return;
6566
}
6667

67-
await this.userScriptDelegator(userScript.default);
68+
await this.userScriptDelegator(userScript);
6869
}
6970

7071
protected async userScriptDelegator(userScript: any) {
@@ -118,7 +119,20 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
118119
}
119120

120121
// @ts-ignore
121-
return await import(filePath);
122+
const userScript = await import(filePath);
123+
if (!userScript || !userScript.default) return;
124+
125+
let script = userScript.default;
126+
127+
const {memberAccess} = getUserScriptMemberAccess(command.name);
128+
if (memberAccess && memberAccess.length > 0) {
129+
let member: string;
130+
while(member = memberAccess.shift()) {
131+
script = script[member];
132+
}
133+
}
134+
135+
return script;
122136
}
123137
}
124138

src/engine/SingleMacroEngine.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {IMacro} from "../types/macros/IMacro";
33
import {MacroChoiceEngine} from "./MacroChoiceEngine";
44
import type QuickAdd from "../main";
55
import type {IChoiceExecutor} from "../IChoiceExecutor";
6+
import {getUserScriptMemberAccess} from "../utility";
67

78
export class SingleMacroEngine extends MacroChoiceEngine {
89
private memberAccess: string[];
@@ -12,12 +13,12 @@ export class SingleMacroEngine extends MacroChoiceEngine {
1213
}
1314

1415
public async runAndGetOutput(macroName: string): Promise<string> {
15-
const splitName: string[] = macroName.split('::');
16-
const macro = this.macros.find(macro => macro.name === splitName[0]);
16+
const {basename, memberAccess} = getUserScriptMemberAccess(macroName);
17+
const macro = this.macros.find(macro => macro.name === basename);
1718
if (!macro) return;
1819

19-
if (splitName.length > 1) {
20-
this.memberAccess = splitName.slice(1);
20+
if (memberAccess && memberAccess.length > 0) {
21+
this.memberAccess = memberAccess;
2122
}
2223

2324
await this.executeCommands(macro.commands)

src/formatters/captureChoiceFormatter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ export class CaptureChoiceFormatter extends CompleteFormatter {
3939
if (this.choice.insertAfter.enabled) {
4040
const targetRegex = new RegExp(`\s*${this.choice.insertAfter.after}\s*`)
4141
const targetPosition = this.fileContent.split("\n").findIndex(line => targetRegex.test(line));
42-
if (!targetPosition) {
43-
log.logError(`unable to find insert after line in file.`);
44-
return formatted;
42+
if (targetPosition === -1) {
43+
log.logError("unable to find insert after line in file.")
4544
}
4645

4746
return this.insertTextAfterPositionInBody(formatted, this.fileContent, targetPosition);

src/gui/CommandList.svelte

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,79 @@
11
<script lang="ts">
2-
import {faTrash} from "@fortawesome/free-solid-svg-icons";
2+
import {faTrash, faBars} from "@fortawesome/free-solid-svg-icons";
33
import Icon from "svelte-awesome/components/Icon.svelte";
44
import type {ICommand} from "../types/macros/ICommand";
5+
import {DndEvent, dndzone, SOURCES, SHADOW_PLACEHOLDER_ITEM_ID} from "svelte-dnd-action";
56
67
export let commands: ICommand[];
78
export let deleteCommand: (command: ICommand) => void;
9+
let dragDisabled: boolean = true;
810
911
export const updateCommandList = (newCommands: ICommand[]) => {
1012
commands = newCommands;
1113
}
14+
15+
function handleConsider(e: CustomEvent<DndEvent>) {
16+
let {items: newItems} = e.detail;
17+
commands = newItems as ICommand[];
18+
}
19+
20+
function handleSort(e: CustomEvent<DndEvent>) {
21+
let {items: newItems, info: {source}} = e.detail;
22+
23+
commands = newItems as ICommand[];
24+
25+
if (source === SOURCES.POINTER) {
26+
dragDisabled = true;
27+
}
28+
}
29+
30+
function startDrag(e: CustomEvent<DndEvent>) {
31+
e.preventDefault()
32+
dragDisabled = false;
33+
}
1234
</script>
1335

14-
<div class="quickAddCommandList">
15-
{#each commands as command, i}
36+
<ol class="quickAddCommandList"
37+
use:dndzone={{items:commands, dragDisabled, dropTargetStyle: {}, type: "command"}}
38+
on:consider={handleConsider}
39+
on:finalize={handleSort}
40+
>
41+
{#each commands.filter(c => c.id !== SHADOW_PLACEHOLDER_ITEM_ID) as command(command.id)}
1642
<div class="quickAddCommandListItem">
17-
<span>{i+1}. {command.name}</span>
18-
<span on:click={() => deleteCommand(command)} class="clickable">
19-
<Icon data="{faTrash}" />
20-
</span>
43+
<li>{command.name}</li>
44+
<div>
45+
<span on:click={() => deleteCommand(command)} class="clickable">
46+
<Icon data="{faTrash}" />
47+
</span>
48+
<span on:mousedown={startDrag} on:touchstart={startDrag}
49+
aria-label="Drag-handle"
50+
style="{dragDisabled ? 'cursor: grab' : 'cursor: grabbing'};"
51+
tabindex={dragDisabled ? 0 : -1}
52+
>
53+
<Icon data={faBars} />
54+
</span>
55+
</div>
2156
</div>
2257
{/each}
23-
</div>
58+
</ol>
2459

2560
<style>
2661
.quickAddCommandListItem {
2762
display: flex;
63+
flex: 1 1 auto;
2864
align-items: center;
2965
justify-content: space-between;
3066
}
3167
3268
.quickAddCommandList {
69+
display: grid;
70+
grid-template-columns: auto;
71+
width: auto;
72+
border: 0 solid black;
73+
overflow-y: auto;
74+
height: auto;
3375
margin-bottom: 8px;
76+
padding: 20px;
3477
}
3578
3679
.clickable {

src/gui/MacroBuilder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import CommandList from "./CommandList.svelte"
1111
import type IChoice from "../types/choices/IChoice";
1212
import {Choice} from "../types/choices/Choice";
1313
import {ChoiceCommand} from "../types/macros/ChoiceCommand";
14+
import {getUserScriptMemberAccess} from "../utility";
1415

1516
export class MacroBuilder extends Modal {
1617
public macro: IMacro;
@@ -85,7 +86,9 @@ export class MacroBuilder extends Modal {
8586
searchComponent.inputEl.addEventListener('keypress', (e: KeyboardEvent) => {
8687
if (e.key === 'Enter') {
8788
const value: string = searchComponent.getValue();
88-
const file = this.javascriptFiles.find(f => f.basename === value);
89+
const scriptBasename = getUserScriptMemberAccess(value).basename;
90+
91+
const file = this.javascriptFiles.find(f => f.basename === scriptBasename);
8992
if (!file) return;
9093

9194
this.macro.commands.push(new UserScript(value, file.path));

src/logger/logManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class LogManager {
1111

1212
logError(message: string) {
1313
LogManager.loggers.forEach(logger => logger.logError(message));
14+
throw new Error();
1415
}
1516

1617
logWarning(message: string) {

src/utility.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,12 @@ export function getAllFolders(app: App): string[] {
8989
return app.vault.getAllLoadedFiles()
9090
.filter(f => f instanceof TFolder)
9191
.map(folder => folder.path);
92+
}
93+
94+
export function getUserScriptMemberAccess(fullMemberPath: string): {basename: string | undefined, memberAccess: string[] | undefined} {
95+
const fullMemberArray: string[] = fullMemberPath.split("::");
96+
return {
97+
basename: fullMemberArray[0],
98+
memberAccess: fullMemberArray.slice(1)
99+
}
92100
}

versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"0.2.5": "0.12.4"
2+
"0.2.6": "0.12.4"
33
}

0 commit comments

Comments
 (0)