keyboard shortcuts in ktxt2 editor

This commit is contained in:
2021-11-02 13:56:27 -04:00
parent 56d4431b48
commit eec3af799a
2 changed files with 57 additions and 31 deletions

View File

@@ -20,7 +20,8 @@ typedef MessageToEditor = {
typedef EditorState = { typedef EditorState = {
text:String, text:String,
scrollY:Float scrollY:Float,
elementScrollY:Int
// TODO active editor, selection & range etc // TODO active editor, selection & range etc
}; };

View File

@@ -12,7 +12,7 @@
(function :EditorState getState [] (function :EditorState getState []
(ifLet [s (the EditorState (vscode.getState))] (ifLet [s (the EditorState (vscode.getState))]
s s
(object scrollY 0 text ""))) (object scrollY 0 elementScrollY 0 text "")))
(function :Void setState [:EditorState state] (function :Void setState [:EditorState state]
(vscode.setState state)) (vscode.setState state))
@@ -42,11 +42,24 @@
(set s.scrollY window.scrollY) (set s.scrollY window.scrollY)
(setState s))) (setState s)))
(window.addEventListener "keydown" ->:Void e
(unless activeEditor
(case e.key
("g" (pageTop))
("v" (pageBottom))
("ArrowUp" (pageUp))
("ArrowDown" (pageDown))
("ArrowLeft" (scrollToPageTop))
("ArrowRight" (scrollToPageBottom))
("s" (export))
(otherwise))))
// Don't use getState helper here because we don't want to force updateContent with blank text // Don't use getState helper here because we don't want to force updateContent with blank text
(whenLet [state (the EditorState (vscode.getState))] (whenLet [state (the EditorState (vscode.getState))]
// Reload the editor after it has been hidden: // Reload the editor after it has been hidden:
// Wait to set up the UI until monaco is loaded from the other scripts: // Wait to set up the UI until monaco is loaded from the other scripts:
(whenMonacoIsAvailable ->{ (whenMonacoIsAvailable ->{
(set elementScrollY state.elementScrollY)
(updateContent state.text) (updateContent state.text)
(setScrollY state.scrollY) (setScrollY state.scrollY)
}))) })))
@@ -71,6 +84,7 @@
} }
EDIT_TIMEOUT_MILLI))) EDIT_TIMEOUT_MILLI)))
(var &mut :Dynamic activeEditor)
(function monacoEditor [div style content language readOnly :Dynamic->Void onChange] (function monacoEditor [div style content language readOnly :Dynamic->Void onChange]
(let [:Dynamic e (let [:Dynamic e
(Lib.global.monaco.editor.create div (Lib.global.monaco.editor.create div
@@ -91,12 +105,12 @@
(e.layout) (e.layout)
}] }]
(updateSize) (updateSize)
(e.onDidFocusEditorText ->(set activeEditor e))
(e.onDidBlurEditorText ->(when (= activeEditor e) (set activeEditor null)))
(e.onDidContentSizeChange updateSize) (e.onDidContentSizeChange updateSize)
(e.onDidChangeModelContent ->[&opt _] (onChange e)) (e.onDidChangeModelContent ->[&opt _] (onChange e))
e)) e))
(function replaceComment [element newText] (function replaceComment [element newText]
(case element (case element
((Comment (object text text start start end end)) ((Comment (object text text start start end end))
@@ -202,8 +216,7 @@
->(changeLockStatus (nth ktxt2Elements idx) !locked)) ->(changeLockStatus (nth ktxt2Elements idx) !locked))
(outerDiv.appendChild lockLink) (outerDiv.appendChild lockLink)
(set exportLink.innerHTML "export") (set exportLink.innerHTML "export")
(exportLink.addEventListener "click" (exportLink.addEventListener "click" export)
->(vscode.postMessage (object type "export")))
(outerDiv.appendChild exportLink) (outerDiv.appendChild exportLink)
(content.appendChild outerDiv) (content.appendChild outerDiv)
(content.appendChild (document.createElement "br")) (content.appendChild (document.createElement "br"))
@@ -221,6 +234,14 @@
top y top y
behavior INSTANT))) behavior INSTANT)))
(function :Void changeElementScrollY [:Void->Void changeFunc]
(changeFunc)
(clamp elementScrollY 0 (- ktxt2Elements.length PAGE_SIZE))
(let [s (getState)]
(set s.elementScrollY elementScrollY)
(setState s))
(updateContent))
(function :Void updateContent [&opt text] (function :Void updateContent [&opt text]
(try (try
{ {
@@ -235,18 +256,8 @@
topLink (document.createElement "a")] topLink (document.createElement "a")]
(set upLink.innerHTML "^ ") (set upLink.innerHTML "^ ")
(set topLink.innerHTML "^^^") (set topLink.innerHTML "^^^")
(upLink.addEventListener "click" (upLink.addEventListener "click" pageUp)
->{ (topLink.addEventListener "click" pageTop)
(-= elementScrollY SCROLL_AMOUNT)
(updateContent)
(setScrollY (- document.body.scrollHeight document.documentElement.clientHeight))
})
(topLink.addEventListener "click"
->{
(set elementScrollY 0)
(updateContent)
(setScrollY 0)
})
(content.appendChild upLink) (content.appendChild upLink)
(content.appendChild topLink))) (content.appendChild topLink)))
(doFor [idx element] (.slice (collect (enumerate ktxt2Elements)) elementScrollY (+ elementScrollY PAGE_SIZE)) (doFor [idx element] (.slice (collect (enumerate ktxt2Elements)) elementScrollY (+ elementScrollY PAGE_SIZE))
@@ -260,21 +271,35 @@
bottomLink (document.createElement "a")] bottomLink (document.createElement "a")]
(set downLink.innerHTML "v ") (set downLink.innerHTML "v ")
(set bottomLink.innerHTML "vvv") (set bottomLink.innerHTML "vvv")
(downLink.addEventListener "click" (downLink.addEventListener "click" pageDown)
->{ (bottomLink.addEventListener "click" pageBottom)
(+= elementScrollY SCROLL_AMOUNT)
(updateContent)
(setScrollY 0)
})
(bottomLink.addEventListener "click"
->{
(set elementScrollY (- ktxt2Elements.length PAGE_SIZE))
(updateContent)
(setScrollY (- document.body.scrollHeight document.documentElement.clientHeight))
})
(content.appendChild downLink) (content.appendChild downLink)
(content.appendChild bottomLink))) (content.appendChild bottomLink)))
(set updatingContent false) (set updatingContent false)
} }
(catch [error] (print "Error updating ktxt2 editor: ${error}")))) (catch [error] (print "Error updating ktxt2 editor: ${error}"))))
(function scrollToPageTop []
(setScrollY 0))
(function scrollToPageBottom []
(setScrollY (- document.body.scrollHeight document.documentElement.clientHeight)))
(function pageDown []
(changeElementScrollY ->(+= elementScrollY SCROLL_AMOUNT))
(scrollToPageTop))
(function pageBottom []
(changeElementScrollY ->(set elementScrollY (- ktxt2Elements.length PAGE_SIZE)))
(scrollToPageBottom))
(function pageUp []
(changeElementScrollY ->(-= elementScrollY SCROLL_AMOUNT))
(scrollToPageBottom))
(function pageTop []
(changeElementScrollY ->(set elementScrollY 0))
(scrollToPageTop))
(function export []
(vscode.postMessage (object type "export")))