Skip to content

Commit

Permalink
chore: infra for script support
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgurney committed Oct 20, 2024
1 parent 38851f1 commit 5630487
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/Adapters/DataviewAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class DataviewAdapter implements Adapter {
return this.functions;
}

async useFunction(config: ScriptConfig): Promise<string | undefined> {
async use(config: ScriptConfig): Promise<string | undefined> {

let result;

Expand Down Expand Up @@ -114,7 +114,7 @@ export default class DataviewAdapter implements Adapter {
break;
}

return result ? result : `Nothing to return`;
return result ? result : '';

}

Expand Down Expand Up @@ -222,6 +222,7 @@ export default class DataviewAdapter implements Adapter {
container.empty();
let dataviewLocalApi = this.dataviewPlugin.localApi(activeFile.path, this.plugin, container);
// from dv.view: may directly render, in which case it will likely return undefined or null
// TODO: try: input should be provided as a string that's read in as JSON; note other script types need to support this as well
let result = await Promise.resolve(func(dataviewLocalApi, input));
if (result) {
await this.dataviewApi.renderValue(
Expand Down
2 changes: 1 addition & 1 deletion src/Adapters/JsEngineAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class JsEngineAdapter implements Adapter {
return this.functions;
}

async useFunction(config: ScriptConfig): Promise<string | undefined> {
async use(config: ScriptConfig): Promise<string | undefined> {
let result;

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/Adapters/TemplaterAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class TemplaterAdapter implements Adapter {
return this.functions;
}

async useFunction(config: ScriptConfig): Promise<string | undefined> {
async use(config: ScriptConfig): Promise<string | undefined> {
let result;

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/Settings/NoteToolbarSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export interface ScriptConfig {
expression?: string;
sourceFile?: string;
sourceFunction?: string;
sourceArgs?: any[];
sourceArgs?: string;
outputContainer?: string;
};

Expand Down
4 changes: 2 additions & 2 deletions src/Settings/UI/Modals/ToolbarSettingsModal.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { App, ButtonComponent, Menu, MenuItem, Modal, Notice, Platform, Setting, TFile, TFolder, debounce, getIcon, normalizePath, setIcon, setTooltip } from 'obsidian';
import { App, ButtonComponent, DropdownComponent, Menu, MenuItem, Modal, Notice, Platform, Setting, TFile, TFolder, debounce, getIcon, normalizePath, setIcon, setTooltip } from 'obsidian';
import { arraymove, debugLog, getElementPosition, hasVars, removeComponentVisibility, addComponentVisibility, moveElement, getUUID } from 'Utils/Utils';
import { emptyMessageFr, learnMoreFr, createToolbarPreviewFr, displayHelpSection, showWhatsNewIfNeeded, pluginLinkFr } from "../Utils/SettingsUIUtils";
import NoteToolbarPlugin from 'main';
import { DEFAULT_STYLE_OPTIONS, ItemType, MOBILE_STYLE_OPTIONS, POSITION_OPTIONS, PositionType, DEFAULT_STYLE_DISCLAIMERS, ToolbarItemSettings, ToolbarSettings, MOBILE_STYLE_DISCLAIMERS, LINK_OPTIONS, ComponentType, t, DEFAULT_ITEM_VISIBILITY_SETTINGS, COMMAND_DOES_NOT_EXIST } from 'Settings/NoteToolbarSettings';
import { DEFAULT_STYLE_OPTIONS, ItemType, MOBILE_STYLE_OPTIONS, POSITION_OPTIONS, PositionType, DEFAULT_STYLE_DISCLAIMERS, ToolbarItemSettings, ToolbarSettings, MOBILE_STYLE_DISCLAIMERS, LINK_OPTIONS, ComponentType, t, DEFAULT_ITEM_VISIBILITY_SETTINGS, COMMAND_DOES_NOT_EXIST, ScriptConfig } from 'Settings/NoteToolbarSettings';
import { NoteToolbarSettingTab } from 'Settings/UI/NoteToolbarSettingTab';
import { confirmWithModal } from 'Settings/UI/Modals/ConfirmModal';
import { CommandSuggester } from 'Settings/UI/Suggesters/CommandSuggester';
Expand Down
2 changes: 1 addition & 1 deletion src/Types/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ScriptConfig } from "Settings/NoteToolbarSettings";

export interface Adapter {
getFunctions(): AdapterFunction[]; // returns all functions for this adapter
useFunction(config: ScriptConfig): Promise<any>; // executes the function with provided config
use(config: ScriptConfig): Promise<any>; // executes the function with provided config
}

export interface AdapterFunction {
Expand Down
5 changes: 1 addition & 4 deletions src/Utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ function hasVisibleComponents(platform: { allViews?: { components: ComponentType
* @param app App
* @param textToInsert string to insert
*/
export function insertTextAtCursor(app: App, textToInsert?: string) {
if (!textToInsert) {
return;
}
export function insertTextAtCursor(app: App, textToInsert: string) {
const activeLeaf = app.workspace.getActiveViewOfType(MarkdownView);
const editor = activeLeaf ? activeLeaf.editor : null;
if (editor) {
Expand Down
13 changes: 8 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -948,21 +948,22 @@ export default class NoteToolbarPlugin extends Plugin {
* @param file optional TFile if handling links outside of the active file
*/
async handleItemLink(item: ToolbarItemSettings, event?: MouseEvent | KeyboardEvent, file?: TFile) {
await this.handleLink(item.link, item.linkAttr.type, item.linkAttr.hasVars, item.linkAttr.commandId, event, file);
await this.handleLink(item.uuid, item.link, item.linkAttr.type, item.linkAttr.hasVars, item.linkAttr.commandId, event, file);
}

/**
* Handles the link provided.
* @param linkHref What the link is for.
* @param uuid ID of the item
* @param linkHref what the link is for
* @param type: ItemType
* @param hasVars: boolean
* @param commandId: string or null
* @param event MouseEvent or KeyboardEvent from where link is activated
* @param file optional TFile if handling links outside of the active file
*/
async handleLink(linkHref: string, type: ItemType, hasVars: boolean, commandId: string | null, event?: MouseEvent | KeyboardEvent, file?: TFile) {
async handleLink(uuid: string, linkHref: string, type: ItemType, hasVars: boolean, commandId: string | null, event?: MouseEvent | KeyboardEvent, file?: TFile) {

// debugLog("handleLink()", linkHref, type, hasVars, commandId, event);
// debugLog("handleLink", uuid, linkHref, type, hasVars, commandId, event);
this.app.workspace.trigger("note-toolbar:item-activated", 'test');

let activeFile = this.app.workspace.getActiveFile();
Expand Down Expand Up @@ -1272,6 +1273,8 @@ export default class NoteToolbarPlugin extends Plugin {

if (linkHref != null) {

const itemUuid = clickedEl.id;

let linkType = clickedEl.getAttribute("data-toolbar-link-attr-type") as ItemType;
linkType ? (Object.values(ItemType).includes(linkType) ? event.preventDefault() : undefined) : undefined

Expand All @@ -1289,7 +1292,7 @@ export default class NoteToolbarPlugin extends Plugin {
await this.removeFocusStyle();
}

await this.handleLink(linkHref, linkType, linkHasVars, linkCommandId, event);
await this.handleLink(itemUuid, linkHref, linkType, linkHasVars, linkCommandId, event);

}

Expand Down

0 comments on commit 5630487

Please sign in to comment.