Links to insert blocks in Ktxt2

This commit is contained in:
2021-10-21 23:25:59 -04:00
parent 8c4943cf77
commit fe5bfa9e10
4 changed files with 35 additions and 5 deletions

View File

@@ -8,6 +8,7 @@
(var unlockedStart "|>|")
(var lockedStart "|!|")
(var outputStarts [unlockedStart lockedStart])
(var emptyBlock "${blockStartEnd}${unlockedStart}${blockStartEnd}")
// Return [sourceFile outputFile]
(function :Array<String> splitFileContents [:Stream fileStream]

View File

@@ -37,14 +37,27 @@
}
EDIT_TIMEOUT_MILLI)))
(function pElement [text idx]
(let [p (document.createElement "p")]
(function commentElements [text idx]
(let [p (document.createElement "p")
blockLinkBefore (document.createElement "a")
blockLinkAfter (document.createElement "a")]
// Links that allow inserting a block between existing blocks:
(set blockLinkBefore.innerHTML "+")
(blockLinkBefore.addEventListener "click"
->(insertBlockBeforeComment (nth ktxt2Elements idx)))
(set blockLinkAfter.innerHTML "+")
(blockLinkAfter.addEventListener "click"
->(insertBlockAfterComment (nth ktxt2Elements idx)))
// Paragraph displaying and allowing editing the comment
(set p.innerHTML text)
(p.setAttribute "style" "white-space: pre;")
(p.setAttribute "contenteditable" "true")
(p.addEventListener "input"
->(addEditTimeout idx ->(replaceComment (nth ktxt2Elements idx) p.innerHTML)))
(content.appendChild p)))
(content.appendChild blockLinkBefore)
(content.appendChild p)
(content.appendChild blockLinkAfter)))
(function toPlaintext [:String text]
(.htmlUnescape
@@ -60,6 +73,18 @@
(vscode.postMessage (object type "replace" text (toPlaintext newText) start start end end)))
(otherwise (throw "element $element is not a comment"))))
(function insertBlockBeforeComment [element]
(case element
((Comment (object start position))
(vscode.postMessage (object type "insert" text "${KTxt2.emptyBlock}\n" position position)))
(otherwise (throw "element $element is not a comment"))))
(function insertBlockAfterComment [element]
(case element
((Comment (object end position))
(vscode.postMessage (object type "insert" text "${KTxt2.emptyBlock}\n" position position)))
(otherwise (throw "element $element is not a comment"))))
(function replaceSourceBlock [element newText]
(case element
((Block (object source text sourceStart start sourceEnd end))
@@ -113,7 +138,7 @@
(case element
// TODO make an objectWith macro for case that duplicates parameter names with the match expressions:
((Comment (object text text))
(pElement text idx))
(commentElements text idx))
((Block (object source source output output outputLocked outputLocked))
(blockElements source output outputLocked idx))))
}

View File

@@ -9,7 +9,8 @@ typedef MessageFromEditor = {
type:String,
?text:String,
?start:kiss.Stream.Position,
?end:kiss.Stream.Position
?end:kiss.Stream.Position,
?position:kiss.Stream.Position
};
@:build(kiss.Kiss.build())

View File

@@ -33,6 +33,9 @@
((object type "replace" text text start start end end)
(makeEdit
->edit (edit.replace document.uri (rangeFromStartEnd start end) text)))
((object type "insert" text text position position)
(makeEdit
->edit (edit.insert document.uri (streamPosToDocumentPos position) text)))
(otherwise
(Vscode.window.showErrorMessage "bad message $e from KTxt2Editor"))))