From 24ada1c76a4378a76d0c277aa8e67bd5a27e19cd Mon Sep 17 00:00:00 2001 From: Nimaoth Date: Thu, 4 Jan 2024 16:51:27 +0100 Subject: [PATCH] remove js scripting --- README.md | 1 - src/app.nim | 14 ++----- src/scripting/scripting_js.nim | 71 --------------------------------- src/scripting/scripting_nim.nim | 2 +- 4 files changed, 5 insertions(+), 83 deletions(-) delete mode 100644 src/scripting/scripting_js.nim diff --git a/README.md b/README.md index 0bbc5974..28b9d5bf 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,6 @@ Absytree supports different scripting languages/mechanisms. The editor API is ex At startup Absytree loads the following scripts in order: - `config/absytree_config_wasm.wasm` - `config/absytree_config_wasm.nim` (NimScript, only in desktop version) -- `config/absytree_config.js` (Only in browser version) At the moment `config/absytree_config_wasm.wasm` is generated from `config/absytree_config_wasm.nim` by compiling it to wasm using `config/config.nims` (uses Emscripten). So `absytree_config_wasm.nim` can be used as NimScript or as wasm plugin. diff --git a/src/app.nim b/src/app.nim index a6f7a038..44cd12da 100644 --- a/src/app.nim +++ b/src/app.nim @@ -11,8 +11,6 @@ import input, events, document, document_editor, popup, dispatch_tables, theme, when not defined(js): import scripting/scripting_nim -else: - import scripting/scripting_js import scripting/scripting_wasm @@ -604,9 +602,7 @@ proc initScripting(self: App) {.async.} = if path.hasPrefix("app://", rest): path = fs.getApplicationFilePath(rest) - when defined(js): - self.scriptContext = new ScriptContextJs - else: + when not defined(js): self.scriptContext = await createScriptContext("./config/absytree_config.nim", searchPaths) withScriptContext self, self.scriptContext: @@ -1489,7 +1485,7 @@ proc setGithubAccessToken*(self: App, token: string) {.expose("editor").} = proc reloadConfig*(self: App) {.expose("editor").} = defer: self.platform.requestRender() - if self.scriptContext.isNil.not: + if self.scriptContext.isNotNil: try: self.scriptContext.reload() if not self.initializeCalled: @@ -1708,10 +1704,8 @@ else: return getActiveEditor() proc loadCurrentConfig*(self: App) {.expose("editor").} = - ## Javascript backend only! - ## Opens the config file in a new view. - when defined(js): - discard self.createAndAddView(newTextDocument(self.asConfigProvider, "./config/absytree_config.js", fs.loadApplicationFile("./config/absytree_config.js"), true)) + ## Opens the default config file in a new view. + discard self.createAndAddView(newTextDocument(self.asConfigProvider, "./config/absytree_config.nim", fs.loadApplicationFile("./config/absytree_config.nim"), true)) proc logRootNode*(self: App) {.expose("editor").} = let str = self.platform.builder.root.dump(true) diff --git a/src/scripting/scripting_js.nim b/src/scripting/scripting_js.nim deleted file mode 100644 index 8b8d2da2..00000000 --- a/src/scripting/scripting_js.nim +++ /dev/null @@ -1,71 +0,0 @@ -when not defined(js): - {.error: "scripting_js.nim does not work in non-js backend. Use scripting_nim.nim instead.".} - -import std/[macros, dom, json] -import misc/[custom_logger, custom_async] -import platform/filesystem -import scripting_base, popup, document_editor - -export scripting_base - -logCategory "scripting-js" - -type ScriptContextJs* = ref object of ScriptContext - discard - -macro invoke*(self: ScriptContext; pName: untyped; args: varargs[typed]; returnType: typedesc): untyped = - result = quote do: - default(`returnType`) - -proc loadScriptJs(url: cstring): Future[Element] {.importjs: "jsLoadScript(#)".} -proc loadScriptContentJs(content: cstring): Future[Element] {.importjs: "jsLoadScriptContent(#)".} -# proc evalJs(str: cstring) {.importjs("eval(#)").} -proc confirmJs(msg: cstring): bool {.importjs("confirm(#)").} -proc hasLocalStorage(key: cstring): bool {.importjs("(window.localStorage.getItem(#) !== null)").} - -proc initAsync(self: ScriptContextJs): Future[void] {.async.} = - discard await loadScriptJs("./scripting_runtime.js") - - const configFilePath = "./config/absytree_config.js" - if hasLocalStorage(configFilePath): - let config = fs.loadApplicationFile(configFilePath) - - let contentStrict = "\"use strict\";\n" & config - echo contentStrict - - let allowEval = confirmJs("You are about to eval() some javascript (./config/absytree_config.js). Look in the console to see what's in there.") - - if allowEval: - # evalJs(contentStrict.cstring) - discard await loadScriptContentJs(config.cstring) - else: - log(lvlWarn, fmt"Did not load config file because user declined.") - else: - discard await loadScriptJs("./config/absytree_config.js") - -method init*(self: ScriptContextJs, path: string): Future[void] = - return self.initAsync() - -method reload*(self: ScriptContextJs) = discard - -method handleUnknownPopupAction*(self: ScriptContextJs, popup: Popup, action: string, arg: JsonNode): bool = - let action = action.cstring - let arg = ($arg).cstring - {.emit: ["return window.handleUnknownPopupAction ? window.handleUnknownPopupAction(", popup, ", ", action, ", JSON.parse(", arg, ")) : false;"].} - -method handleUnknownDocumentEditorAction*(self: ScriptContextJs, editor: DocumentEditor, action: string, arg: JsonNode): bool = - let action = action.cstring - let arg = ($arg).cstring - {.emit: ["return window.handleUnknownDocumentEditorAction ? window.handleUnknownDocumentEditorAction(", editor, ", ", action, ", JSON.parse(", arg, ")) : false;"].} - -method handleGlobalAction*(self: ScriptContextJs, action: string, arg: JsonNode): bool = - let action = action.cstring - let arg = ($arg).cstring - {.emit: ["return window.handleGlobalAction ? window.handleGlobalAction(", action, ", JSON.parse(", arg, ")) : false;"].} - -method postInitialize*(self: ScriptContextJs): bool = - {.emit: ["return window.postInitialize ? window.postInitialize() : false;"].} - -method handleCallback*(self: ScriptContextJs, id: int, arg: JsonNode): bool = - let arg = ($arg).cstring - {.emit: ["return window.handleCallback ? window.handleCallback(", id, ", JSON.parse(", arg, ")) : false;"].} \ No newline at end of file diff --git a/src/scripting/scripting_nim.nim b/src/scripting/scripting_nim.nim index 38726743..b757e4cd 100644 --- a/src/scripting/scripting_nim.nim +++ b/src/scripting/scripting_nim.nim @@ -1,5 +1,5 @@ when defined(js): - {.error: "scripting_nim.nim does not work in js backend. Use scripting_js.nim instead.".} + {.error: "scripting_nim.nim does not work in js backend.".} import std/[os, osproc, tables, strformat, json, strutils, macrocache, macros, genasts, sugar] from logging import nil