From 56d4431b484e6c082068d2081d2f3cf3e109cbcb Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 2 Nov 2021 12:51:22 -0400 Subject: [PATCH] save window.scrollY in hidden ktxt2 editor --- projects/kiss-vscode/src/ktxt2/KTxt2Editor.hx | 6 ++++ .../kiss-vscode/src/ktxt2/KTxt2Editor.kiss | 30 ++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2Editor.hx b/projects/kiss-vscode/src/ktxt2/KTxt2Editor.hx index 0b135578..0978a92d 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2Editor.hx +++ b/projects/kiss-vscode/src/ktxt2/KTxt2Editor.hx @@ -18,5 +18,11 @@ typedef MessageToEditor = { ?text:String }; +typedef EditorState = { + text:String, + scrollY:Float + // TODO active editor, selection & range etc +}; + @:build(kiss.Kiss.build()) class KTxt2Editor {} diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2Editor.kiss b/projects/kiss-vscode/src/ktxt2/KTxt2Editor.kiss index b1d03b03..52f17057 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2Editor.kiss +++ b/projects/kiss-vscode/src/ktxt2/KTxt2Editor.kiss @@ -9,6 +9,14 @@ (var SCROLL_AMOUNT 18) (var &mut elementScrollY 0) +(function :EditorState getState [] + (ifLet [s (the EditorState (vscode.getState))] + s + (object scrollY 0 text ""))) + +(function :Void setState [:EditorState state] + (vscode.setState state)) + (function main [] (set vscode (EditorExterns.acquireVsCodeApi)) (set window EditorExterns.window) @@ -22,12 +30,26 @@ (case (the MessageToEditor event.data) ((object type "update" text text) (whenMonacoIsAvailable ->(updateContent text)) - (vscode.setState (object text text))) - (otherType (throw "bad message $event for KTxt2Editor")))) - (whenLet [state (the Dynamic (vscode.getState))] + // Either create the first EditorState, or get the last one + (let [newState (getState)] + (set newState.text text) + (setState newState))) + (otherType (throw "bad message $event for KTxt2Editor")))) + + (window.addEventListener "scroll" + ->(let [s (getState)] + (set s.scrollY window.scrollY) + (setState s))) + + // Don't use getState helper here because we don't want to force updateContent with blank text + (whenLet [state (the EditorState (vscode.getState))] + // Reload the editor after it has been hidden: // Wait to set up the UI until monaco is loaded from the other scripts: - (whenMonacoIsAvailable ->(updateContent state.text)))) + (whenMonacoIsAvailable ->{ + (updateContent state.text) + (setScrollY state.scrollY) + }))) (var MONACO_CHECK_MILLI 100) (function :Void whenMonacoIsAvailable [:Void->Void doThis]