mirror of
https://github.com/zsviczian/obsidian-excalidraw-plugin.git
synced 2025-08-06 05:46:28 +00:00
onload-script, isExcalidrawView
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "obsidian-excalidraw-plugin",
|
||||
"version": "1.6.26",
|
||||
"version": "1.6.26-6",
|
||||
"description": "This is an Obsidian.md plugin that lets you view and edit Excalidraw drawings",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
|
||||
@@ -1554,6 +1554,15 @@ export class ExcalidrawAutomate implements ExcalidrawAutomateInterface {
|
||||
return manifest.version >= requiredVersion;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if view is instance of ExcalidrawView
|
||||
* @param view
|
||||
* @returns
|
||||
*/
|
||||
isExcalidrawView(view: any): boolean {
|
||||
return view instanceof ExcalidrawView;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets selection in view
|
||||
* @param elements
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
fileid,
|
||||
REG_BLOCK_REF_CLEAN,
|
||||
FRONTMATTER_KEY_LINKBUTTON_OPACITY,
|
||||
FRONTMATTER_KEY_ONLOAD_SCRIPT,
|
||||
} from "./Constants";
|
||||
import { _measureText } from "./ExcalidrawAutomate";
|
||||
import ExcalidrawPlugin from "./main";
|
||||
@@ -1267,6 +1268,17 @@ export class ExcalidrawData {
|
||||
return opacity;
|
||||
}
|
||||
|
||||
public getOnLoadScript(): string {
|
||||
const fileCache = this.app.metadataCache.getFileCache(this.file);
|
||||
if (
|
||||
fileCache?.frontmatter &&
|
||||
fileCache.frontmatter[FRONTMATTER_KEY_ONLOAD_SCRIPT] != null
|
||||
) {
|
||||
return fileCache.frontmatter[FRONTMATTER_KEY_ONLOAD_SCRIPT];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private setLinkPrefix(): boolean {
|
||||
const linkPrefix = this.linkPrefix;
|
||||
const fileCache = this.app.metadataCache.getFileCache(this.file);
|
||||
|
||||
@@ -1234,6 +1234,10 @@ export default class ExcalidrawView extends TextFileView {
|
||||
}
|
||||
}
|
||||
await this.loadDrawing(true);
|
||||
const script = this.excalidrawData.getOnLoadScript();
|
||||
if(script) {
|
||||
this.plugin.scriptEngine.executeScript(this,script,this.file.basename + "-onlaod-script");
|
||||
}
|
||||
this.isLoaded = true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -170,7 +170,12 @@ export class ScriptEngine {
|
||||
}
|
||||
const view = this.plugin.app.workspace.activeLeaf.view;
|
||||
if (view instanceof ExcalidrawView) {
|
||||
this.executeScript(view, f);
|
||||
(async()=>{
|
||||
const script = await this.plugin.app.vault.read(f);
|
||||
if(script) {
|
||||
this.executeScript(view, script, scriptName);
|
||||
}
|
||||
})()
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -206,18 +211,13 @@ export class ScriptEngine {
|
||||
delete app.commands.commands[commandId];
|
||||
}
|
||||
|
||||
async executeScript(view: ExcalidrawView, f: TFile) {
|
||||
if (!view || !f) {
|
||||
async executeScript(view: ExcalidrawView, script: string, title: string) {
|
||||
if (!view || !script || !title) {
|
||||
return;
|
||||
}
|
||||
this.plugin.ea.reset();
|
||||
this.plugin.ea.setView(view);
|
||||
const script = await this.plugin.app.vault.read(f);
|
||||
if (!script) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.plugin.ea.activeScript = this.getScriptName(f);
|
||||
this.plugin.ea.activeScript = title;
|
||||
|
||||
//https://stackoverflow.com/questions/45381204/get-asyncfunction-constructor-in-typescript changed tsconfig to es2017
|
||||
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction
|
||||
|
||||
@@ -30,6 +30,7 @@ export const FRONTMATTER_KEY_EXPORT_PNGSCALE = "excalidraw-export-pngscale";
|
||||
export const FRONTMATTER_KEY_CUSTOM_PREFIX = "excalidraw-link-prefix";
|
||||
export const FRONTMATTER_KEY_CUSTOM_URL_PREFIX = "excalidraw-url-prefix";
|
||||
export const FRONTMATTER_KEY_CUSTOM_LINK_BRACKETS = "excalidraw-link-brackets";
|
||||
export const FRONTMATTER_KEY_ONLOAD_SCRIPT = "excalidraw-onload-script";
|
||||
export const FRONTMATTER_KEY_LINKBUTTON_OPACITY = "excalidraw-linkbutton-opacity";
|
||||
export const FRONTMATTER_KEY_DEFAULT_MODE = "excalidraw-default-mode";
|
||||
export const FRONTMATTER_KEY_FONT = "excalidraw-font";
|
||||
|
||||
@@ -532,6 +532,12 @@ export const FRONTMATTER_KEYS_INFO: SuggesterInfo[] = [
|
||||
"Valid values are between 0 and 1, where 0 means the button is transparent.",
|
||||
after: ": 0.5",
|
||||
},
|
||||
{
|
||||
field: "onload-script",
|
||||
code: null,
|
||||
desc: "The value of this field will be executed as javascript code using the Script Engine environment. Use this to initiate custom actions or logic when loading your drawing.",
|
||||
after: ': "new Notice(`Hello World!\\n\\nFile: ${ea.targetView.file.basename}`);"',
|
||||
},
|
||||
{
|
||||
field: "font",
|
||||
code: null,
|
||||
|
||||
@@ -520,13 +520,14 @@ export class ToolsPanel extends React.Component<PanelProps, PanelState> {
|
||||
)
|
||||
: this.state.scriptIconMap[key].name
|
||||
}
|
||||
action={() => {
|
||||
action={async () => {
|
||||
const f =
|
||||
this.props.view.app.vault.getAbstractFileByPath(key);
|
||||
if (f && f instanceof TFile) {
|
||||
this.props.view.plugin.scriptEngine.executeScript(
|
||||
this.props.view,
|
||||
f,
|
||||
await this.props.view.plugin.app.vault.read(f),
|
||||
this.props.view.plugin.scriptEngine.getScriptName(f)
|
||||
);
|
||||
}
|
||||
}}
|
||||
|
||||
1
src/types.d.ts
vendored
1
src/types.d.ts
vendored
@@ -198,6 +198,7 @@ export interface ExcalidrawAutomateInterface {
|
||||
//recommended use:
|
||||
//if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion("1.5.20")) {new Notice("message");return;}
|
||||
verifyMinimumPluginVersion(requiredVersion: string): boolean;
|
||||
isExcalidrawView(view: any): boolean;
|
||||
selectElementsInView(elements: ExcalidrawElement[]): void; //sets selection in view
|
||||
generateElementId(): string; //returns an 8 character long random id
|
||||
cloneElement(element: ExcalidrawElement): ExcalidrawElement; //Returns a clone of the element with a new id
|
||||
|
||||
Reference in New Issue
Block a user