Skip to content

Commit

Permalink
v0.4 Release (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann authored Aug 19, 2021
1 parent 6ecdbce commit 38a1ed2
Show file tree
Hide file tree
Showing 27 changed files with 398 additions and 73 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ Quickly add new pages or content to your vault.
You can also do a [manual installation](docs/ManualInstallation.md).

## What's new?
### 0.4.0
- Massive improvements to user scripts and the API:
- You can now define settings for user scripts, which can be configured in Obsidian.
- Exposed ``format`` in the API, which will evaluate format syntax like `{{DATE}}`, `{{VALUE}}`, and so on.
- Exposed a new date module in the API with methods for retrieving and formatting dates.
- Added settings for opening files in edit or preview mode.
- Added settings for automatically focusing the opened file.
- Improved the folder selector search in Template choices.
- You can now rename Multis.

### 0.3.21-2
- Added a better search algorithm for input prompts.

Expand Down
13 changes: 13 additions & 0 deletions docs/QuickAddAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ Sets the contents of your clipboard to the given input.
This function is asynchronous. You should ``await`` it.

Syntax: `await quickAddApi.utility.setClipboard();`

## Date module
Formats always default to ``YYYY-MM-DD``.
### ``now(format?: string, offset?: number)``
Gets the current time and formats according to the given format.

Providing an offset will offset the date by number of days. Giving -1 would mean yesterday, and giving 1 would mean tomorrow - and so on.

### ``tomorrow(format?: string)``
Same as ``now`` but with offset set to 1.

### ``yesterday(format?: string)``
Again, same as ``now`` but with offset set to -1.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "quickadd",
"name": "QuickAdd",
"version": "0.3.22",
"version": "0.4.0",
"minAppVersion": "0.12.5",
"description": "Quickly add new pages or content to your vault.",
"author": "Christian B. B. Houmann",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quickadd",
"version": "0.3.22",
"version": "0.4.0",
"description": "Quickly add new pages or content to your vault.",
"main": "main.js",
"scripts": {
Expand Down
11 changes: 6 additions & 5 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
appendToCurrentLine(this.app.fileManager.generateMarkdownLink(file, ''), this.app);

if (this.choice?.openFile) {
if (this.choice?.openFileInNewTab.enabled) {
await openFile(this.app, file, this.choice.openFileInNewTab.direction);
} else {
await openFile(this.app, file);
}
await openFile(this.app, file, {
openInNewTab: this.choice.openFileInNewTab.enabled,
direction: this.choice.openFileInNewTab.direction,
focus: this.choice.openFileInNewTab.focus,
mode: this.choice.openFileInMode
});
}
}
catch (e) {
Expand Down
59 changes: 19 additions & 40 deletions src/engine/MacroChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import GenericSuggester from "../gui/GenericSuggester/genericSuggester";
import type {IChoiceCommand} from "../types/macros/IChoiceCommand";
import type QuickAdd from "../main";
import type {IChoiceExecutor} from "../IChoiceExecutor";
import {getUserScriptMemberAccess, waitFor} from "../utility";
import {getUserScript, getUserScriptMemberAccess, waitFor} from "../utility";
import type {IWaitCommand} from "../types/macros/QuickCommands/IWaitCommand";
import type {INestedChoiceCommand} from "../types/macros/QuickCommands/INestedChoiceCommand";
import type IChoice from "../types/choices/IChoice";
Expand Down Expand Up @@ -59,7 +59,6 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {


protected async executeCommands(commands: ICommand[]) {
let i = 1;
for (const command of commands) {
if (command?.type === CommandType.Obsidian)
await this.executeObsidianCommand(command as IObsidianCommand);
Expand Down Expand Up @@ -87,13 +86,27 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
// Slightly modified from Templater's user script engine:
// https://github.com/SilentVoid13/Templater/blob/master/src/UserTemplates/UserTemplateParser.ts
protected async executeUserScript(command: IUserScript) {
const userScript = await this.getUserScript(command);
const userScript = await getUserScript(command, this.app);
if (!userScript) {
log.logError(`failed to load user script ${command.path}.`);
return;
}

await this.userScriptDelegator(userScript);
if (userScript.settings) {
await this.runScriptWithSettings(userScript, command);
} else {
await this.userScriptDelegator(userScript);
}
}

private async runScriptWithSettings(userScript, command: IUserScript) {
if (!userScript.entry) {
log.logError(`user script '${command.name}' does not have an entry function.`);
return;
}

await this.onExportIsFunction(userScript.entry, command.settings);
return;
}

protected async userScriptDelegator(userScript: any) {
Expand All @@ -115,8 +128,8 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
}
}

private async onExportIsFunction(userScript: any) {
this.output = await userScript(this.params);
private async onExportIsFunction(userScript: any, settings?: {[key: string]: any}) {
this.output = await userScript(this.params, settings);
}

protected async onExportIsObject(obj: object) {
Expand All @@ -130,40 +143,6 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
}
}

protected async getUserScript(command: IUserScript) {
// @ts-ignore
const vaultPath = this.app.vault.adapter.getBasePath();
const file: TAbstractFile = this.app.vault.getAbstractFileByPath(command.path);
if (!file) {
log.logError(`failed to load file ${command.path}.`);
return;
}

if (file instanceof TFile) {
const filePath = `${vaultPath}/${file.path}`;

if (window.require.cache[window.require.resolve(filePath)]) {
delete window.require.cache[window.require.resolve(filePath)];
}

// @ts-ignore
const userScript = await import(filePath);
if (!userScript || !userScript.default) return;

let script = userScript.default;

const {memberAccess} = getUserScriptMemberAccess(command.name);
if (memberAccess && memberAccess.length > 0) {
let member: string;
while(member = memberAccess.shift()) {
script = script[member];
}
}

return script;
}
}

protected executeObsidianCommand(command: IObsidianCommand) {
// @ts-ignore
this.app.commands.executeCommandById(command.commandId);
Expand Down
11 changes: 6 additions & 5 deletions src/engine/TemplateChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ export class TemplateChoiceEngine extends TemplateEngine {
}

if (this.choice.openFile) {
if (!this.choice.openFileInNewTab.enabled) {
await openFile(this.app, createdFile);
} else {
await openFile(this.app, createdFile, this.choice.openFileInNewTab.direction);
}
await openFile(this.app, createdFile, {
openInNewTab: this.choice.openFileInNewTab.enabled,
direction: this.choice.openFileInNewTab.direction,
focus: this.choice.openFileInNewTab.focus,
mode: this.choice.openFileInMode
});
}
}

Expand Down
25 changes: 24 additions & 1 deletion src/gui/ChoiceBuilder/captureChoiceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {FileNameDisplayFormatter} from "../../formatters/fileNameDisplayFormatte
import {GenericTextSuggester} from "../genericTextSuggester";
import {getTemplatePaths} from "../../utility";
import {NewTabDirection} from "../../types/newTabDirection";
import type {FileViewMode} from "../../types/fileViewMode";

export class CaptureChoiceBuilder extends ChoiceBuilder {
choice: ICaptureChoice;
Expand Down Expand Up @@ -280,6 +281,20 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
this.reload();
});
})
.addDropdown(dropdown => {
dropdown.selectEl.style.marginLeft = "10px";

if (!this.choice.openFileInMode)
this.choice.openFileInMode = 'default';

dropdown
.addOption('source', 'Source')
.addOption('preview', 'Preview')
.addOption('default', 'Default')
.setValue(this.choice.openFileInMode)
.onChange(value => this.choice.openFileInMode = (value as FileViewMode))
}
);
}

private addOpenFileInNewTabSetting(): void {
Expand All @@ -292,7 +307,7 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
})
.addDropdown(dropdown => {
if (!this.choice?.openFileInNewTab) {
this.choice.openFileInNewTab = {enabled: false, direction: NewTabDirection.vertical};
this.choice.openFileInNewTab = { enabled: false, direction: NewTabDirection.vertical, focus: true };
}

dropdown.selectEl.style.marginLeft = "10px";
Expand All @@ -301,5 +316,13 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
dropdown.setValue(this.choice?.openFileInNewTab?.direction);
dropdown.onChange(value => this.choice.openFileInNewTab.direction = <NewTabDirection>value);
});

new Setting(this.contentEl)
.setName("Focus new pane")
.setDesc("Focus the opened tab immediately")
.addToggle(toggle => toggle
.setValue(this.choice.openFileInNewTab.focus)
.onChange(value => this.choice.openFileInNewTab.focus = value)
);
}
}
23 changes: 23 additions & 0 deletions src/gui/ChoiceBuilder/templateChoiceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {log} from "../../logger/logManager";
import {getAllFolderPathsInVault, getTemplatePaths} from "../../utility";
import {GenericTextSuggester} from "../genericTextSuggester";
import type QuickAdd from "../../main";
import type {FileViewMode} from "../../types/fileViewMode";

export class TemplateChoiceBuilder extends ChoiceBuilder {
choice: ITemplateChoice;
Expand Down Expand Up @@ -209,6 +210,20 @@ export class TemplateChoiceBuilder extends ChoiceBuilder {
this.reload();
});
})
.addDropdown(dropdown => {
dropdown.selectEl.style.marginLeft = "10px";

if (!this.choice.openFileInMode)
this.choice.openFileInMode = 'default';

dropdown
.addOption('source', 'Source')
.addOption('preview', 'Preview')
.addOption('default', 'Default')
.setValue(this.choice.openFileInMode)
.onChange(value => this.choice.openFileInMode = (value as FileViewMode))
}
);
}

private addOpenFileInNewTabSetting(): void {
Expand All @@ -226,5 +241,13 @@ export class TemplateChoiceBuilder extends ChoiceBuilder {
dropdown.setValue(this.choice.openFileInNewTab.direction);
dropdown.onChange(value => this.choice.openFileInNewTab.direction = <NewTabDirection>value);
});

new Setting(this.contentEl)
.setName("Focus new pane")
.setDesc("Focus the opened tab immediately")
.addToggle(toggle => toggle
.setValue(this.choice.openFileInNewTab.focus)
.onChange(value => this.choice.openFileInNewTab.focus = value)
);
}
}
19 changes: 19 additions & 0 deletions src/gui/MacroGUIs/CommandList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import type IChoice from "../../types/choices/IChoice";
import {App} from "obsidian";
import QuickAdd from "../../main";
import UserScriptCommand from "./Components/UserScriptCommand.svelte";
import type {IUserScript} from "../../types/macros/IUserScript";
import {UserScriptSettingsModal} from "./UserScriptSettingsModal";
import {getUserScript} from "../../utility";
import {log} from "../../logger/logManager";
export let commands: ICommand[];
export let deleteCommand: (command: ICommand) => Promise<void>;
Expand Down Expand Up @@ -81,6 +86,18 @@
break;
}
}
async function configureScript(e: CustomEvent) {
const command: IUserScript = e.detail;
const userScript = await getUserScript(command, app);
if (!userScript.settings) {
log.logWarning(`${command.name} has no settings.`);
return;
}
new UserScriptSettingsModal(app, command, userScript.settings).open();
}
</script>

<ol class="quickAddCommandList"
Expand All @@ -93,6 +110,8 @@
<WaitCommand bind:command bind:dragDisabled bind:startDrag={startDrag} on:deleteCommand={async e => await deleteCommand(e.detail)} on:updateCommand={updateCommandFromEvent} />
{:else if command.type === CommandType.NestedChoice}
<NestedChoiceCommand bind:command bind:dragDisabled bind:startDrag={startDrag} on:deleteCommand={async e => await deleteCommand(e.detail)} on:updateCommand={updateCommandFromEvent} on:configureChoice={configureChoice} />
{:else if command.type === CommandType.UserScript}
<UserScriptCommand bind:command bind:dragDisabled bind:startDrag on:deleteCommand={async e => await deleteCommand(e.detail)} on:updateCommand={updateCommandFromEvent} on:configureScript={configureScript} />
{:else}
<StandardCommand bind:command bind:dragDisabled bind:startDrag={startDrag} on:deleteCommand={async e => await deleteCommand(e.detail)} on:updateCommand={updateCommandFromEvent} />
{/if}
Expand Down
43 changes: 43 additions & 0 deletions src/gui/MacroGUIs/Components/UserScriptCommand.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script lang="ts">
import {faTrash, faBars, faCog} from "@fortawesome/free-solid-svg-icons";
import Icon from "svelte-awesome/components/Icon.svelte";
import {createEventDispatcher} from "svelte";
import {DndEvent} from "svelte-dnd-action";
import {IUserScript} from "../../../types/macros/IUserScript";
export let command: IUserScript;
export let startDrag: (e: CustomEvent<DndEvent>) => void;
export let dragDisabled: boolean;
const dispatch = createEventDispatcher();
function deleteCommand() {
dispatch('deleteCommand', command.id);
}
function configureChoice() {
dispatch('configureScript', command);
}
</script>

<div class="quickAddCommandListItem">
<li>{command.name}</li>
<div>
<span on:click={() => configureChoice()} class="clickable">
<Icon data="{faCog}" />
</span>
<span on:click={() => deleteCommand()} class="clickable">
<Icon data="{faTrash}" />
</span>
<span on:mousedown={startDrag} on:touchstart={startDrag}
aria-label="Drag-handle"
style="{dragDisabled ? 'cursor: grab' : 'cursor: grabbing'};"
tabindex={dragDisabled ? 0 : -1}
>
<Icon data={faBars} />
</span>
</div>
</div>

<style lang="css">
</style>
1 change: 0 additions & 1 deletion src/gui/MacroGUIs/MacroBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {CopyCommand} from "../../types/macros/EditorCommands/CopyCommand";
import {CutCommand} from "../../types/macros/EditorCommands/CutCommand";
import {PasteCommand} from "../../types/macros/EditorCommands/PasteCommand";
import {log} from "../../logger/logManager";
import {EditorCommand} from "../../types/macros/EditorCommands/EditorCommand";
import {SelectActiveLineCommand} from "../../types/macros/EditorCommands/SelectActiveLineCommand";
import {SelectLinkOnActiveLineCommand} from "../../types/macros/EditorCommands/SelectLinkOnActiveLineCommand";
import GenericYesNoPrompt from "../GenericYesNoPrompt/GenericYesNoPrompt";
Expand Down
Loading

0 comments on commit 38a1ed2

Please sign in to comment.