First post here and pretty new to Javascript. I am trying to get familiar with script actions by reimplementing some of the existing actions in a local script in VS Code and then loading this script in the Drafts action.
Assume the following directory structure:
Scripts/
└── Custom Scripts/
├── editing.js
└── utils.js
Is it possible to reuse functions across multiple files and if so, what is the correct way of importing these?
This works:
VS Code:
// editing.js
const insertText = (text, selectionStart) => {
editor.setSelectedText(text);
editor.setSelectedRange(selectionStart + text.length, 0);
};
const pasteClipboard = () => {
const text = app.getClipboard();
const [selectionStart, _] = editor.getSelectedRange();
insertText(text, selectionStart);
};
Drafts action:
require("CustomScripts/editing.js")
pasteClipboard();
This does not work:
VS Code:
// utils.js
export const insertText = (text, selectionStart) => {
editor.setSelectedText(text);
editor.setSelectedRange(selectionStart + text.length, 0);
};
// editing.js
import { insertText } from "./utils.js";
const pasteClipboard = () => {
const text = app.getClipboard();
const [selectionStart, _] = editor.getSelectedRange();
insertText(text, selectionStart);
};
Drafts action:
require("CustomScripts/editing.js")
pasteClipboard(); // Error: "Can't find variable: pasteClipboard"