Messages in/out of QuickWebview
This commit is contained in:
@@ -9,6 +9,7 @@ import vscode.*;
|
||||
import kiss.Prelude;
|
||||
|
||||
import kiss_vscode_api.QuickWebviewScript;
|
||||
using StringTools;
|
||||
|
||||
typedef QuickWebviewSetup = (Document) -> Void;
|
||||
|
||||
|
@@ -1,3 +1,5 @@
|
||||
(loadFrom "kiss-vscode-api" "src/Util.kiss")
|
||||
|
||||
(defMacro quickWebviewScriptJs [&builder b]
|
||||
(assertProcess "haxe" ["QuickWebviewScript.hxml"] null null (libPath "kiss-vscode-api"))
|
||||
(b.str (File.getContent "$(libPath "kiss-vscode-api")/src/kiss_vscode_api/QuickWebviewScript.js")))
|
||||
@@ -12,8 +14,15 @@
|
||||
(let [panel (Vscode.window.createWebviewPanel "kissVscodeQuickWebview" title (or column ViewColumn.Beside) (object enableScripts ?kissScript))]
|
||||
(set panel.webview.html document.documentElement.innerHTML)
|
||||
|
||||
(panel.webview.onDidReceiveMessage
|
||||
->:Void [:Dynamic m]
|
||||
(case m.type
|
||||
("error" (errorMessage (Std.string m.data)))
|
||||
("print" (print m.data))
|
||||
(otherwise)))
|
||||
|
||||
(when kissScript
|
||||
(panel.webview.postMessage (RunScript (Prelude.convertToHScript kissScript))))
|
||||
(panel.webview.postMessage (object type "runScript" data (Prelude.convertToHScript kissScript))))
|
||||
|
||||
// setInterval of the update function and allow for stopping it by disposing the panel or calling a close function passed to update
|
||||
(let [&mut :Function close null
|
||||
|
@@ -1,26 +1,38 @@
|
||||
package kiss_vscode_api;
|
||||
|
||||
import kiss.KissInterp;
|
||||
import kiss.Prelude;
|
||||
import kiss_vscode_api.WebviewExterns;
|
||||
|
||||
enum QuickWebviewMessage {
|
||||
RunScript(hscript:String);
|
||||
Message(m:Any);
|
||||
}
|
||||
typedef QuickWebviewMessage = {
|
||||
type:String,
|
||||
data:Any
|
||||
};
|
||||
|
||||
class QuickWebviewScript {
|
||||
static function main() {
|
||||
var vscode = WebviewExterns.acquireVsCodeApi();
|
||||
Prelude.printStr = s -> vscode.postMessage({type: "print", data: s});
|
||||
|
||||
var interp = new KissInterp();
|
||||
|
||||
interp.variables["vscode"] = vscode;
|
||||
interp.variables["window"] = WebviewExterns.window;
|
||||
interp.variables["document"] = WebviewExterns.window.document;
|
||||
var window = WebviewExterns.window;
|
||||
var document = WebviewExterns.window.document;
|
||||
|
||||
WebviewExterns.window.addEventListener("message", event -> {
|
||||
switch (event.data) {
|
||||
case RunScript(hscript):
|
||||
interp.evalHaxe(hscript);
|
||||
interp.variables["vscode"] = vscode;
|
||||
interp.variables["window"] = window;
|
||||
interp.variables["document"] = document;
|
||||
|
||||
window.addEventListener("message", event -> {
|
||||
switch (event.data.type) {
|
||||
case "runScript":
|
||||
try {
|
||||
interp.evalHaxe(event.data.data);
|
||||
return;
|
||||
} catch (e:Any) {
|
||||
vscode.postMessage({ type: "error", data: e});
|
||||
return;
|
||||
}
|
||||
default:
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user