diff --git a/common/space_script.ts b/common/space_script.ts index 3828d270..83da4d4a 100644 --- a/common/space_script.ts +++ b/common/space_script.ts @@ -2,13 +2,31 @@ import { System } from "../lib/plugos/system.ts"; import { ScriptObject } from "../plugs/index/script.ts"; import { AppCommand, CommandDef } from "./hooks/command.ts"; +type FunctionDef = { + name: string; +}; + export class ScriptEnvironment { functions: Record any> = {}; commands: Record = {}; // Public API - registerFunction(name: string, fn: (...args: any[]) => any) { - this.functions[name] = fn; + + // Register function + registerFunction(def: FunctionDef, fn: (...args: any[]) => any): void; + // Legacy invocation + registerFunction(name: string, fn: (...args: any[]) => any): void; + registerFunction( + arg: string | FunctionDef, + fn: (...args: any[]) => any, + ): void { + if (typeof arg === "string") { + console.warn( + "registerFunction with string is deprecated, use `{name: string}` instead", + ); + arg = { name: arg }; + } + this.functions[arg.name] = fn; } registerCommand(command: CommandDef, fn: (...args: any[]) => any) { diff --git a/website/SETTINGS.md b/website/SETTINGS.md index 1d87d1fb..87bc1221 100644 --- a/website/SETTINGS.md +++ b/website/SETTINGS.md @@ -41,8 +41,6 @@ shortcuts: key: "Alt-x" - command: "{[Upload: File]}" priority: 1 # Make sure this appears at the top of the list in the command palette -- command: "{[Open Command Palette]}" - key: "Ctrl-." # Defines files to ignore in a format compatible with .gitignore spaceIgnore: | diff --git a/website/Space Script.md b/website/Space Script.md index 68a9c827..04aec725 100644 --- a/website/Space Script.md +++ b/website/Space Script.md @@ -13,7 +13,7 @@ Space scripts are defined by simply using `space-script` fenced code blocks in y Here is a trivial example: ```space-script -silverbullet.registerFunction("helloYeller", (name) => { +silverbullet.registerFunction({name: "helloYeller"}, (name) => { return `Hello ${name}!`.toUpperCase(); }) ``` @@ -35,7 +35,7 @@ While not very secure, some effort is put into running this code in a clean Java Depending on where code is run (client or server), a slightly different JavaScript API will be available. However, code should ideally primarily rely on the following explicitly exposed APIs: -* `silverbullet.registerFunction(name, callback)`: registers a custom function (see [[#Custom functions]]). +* `silverbullet.registerFunction(definition, callback)`: registers a custom function (see [[#Custom functions]]). * `silverbullet.registerCommand(definition, callback)`: registers a custom command (see [[#Custom commands]]). * `syscall(name, args...)`: invoke a syscall (see [[#Syscalls]]). @@ -51,14 +51,15 @@ Since template rendering happens on the server (except in [[Client Modes#Synced The `silverbullet.registerFunction` API takes two arguments: -* `name`: the function name to register +* `options`: with currently just one option: + * `name`: the name of the function to register * `callback`: the callback function to invoke (can be `async` or not) ## Example Even though a [[Functions#readPage(name)]] function already exist, you could implement it in space script as follows (let’s name it `myReadPage`) using the `syscall` API (detailed further in [[#Syscalls]]): ```space-script -silverbullet.registerFunction("myReadPage", async (name) => { +silverbullet.registerFunction({name: "myReadPage"}, async (name) => { const pageContent = await syscall("space.readPage", name); return pageContent; })