ktxt2 conversion link
This commit is contained in:
@@ -3,6 +3,8 @@ package ktxt2;
|
||||
import kiss.Stream;
|
||||
import kiss.Prelude;
|
||||
|
||||
using haxe.io.Path;
|
||||
|
||||
typedef KTxt2Block = {
|
||||
source:String,
|
||||
output:String,
|
||||
@@ -25,5 +27,13 @@ enum KTxt2Element {
|
||||
Block(block:KTxt2Block);
|
||||
}
|
||||
|
||||
typedef KTxt2Conversion = {
|
||||
sourceType:String,
|
||||
outputType:String,
|
||||
canConvert:String->Bool,
|
||||
convert:String->String,
|
||||
name:String
|
||||
};
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class KTxt2 {}
|
||||
|
@@ -77,4 +77,21 @@
|
||||
(None
|
||||
(break)))
|
||||
(elements.push (Block block))))
|
||||
elements))
|
||||
elements))
|
||||
|
||||
(var :Array<KTxt2Conversion> conversions [])
|
||||
|
||||
(function registerConversion [:KTxt2Conversion conversion] (conversions.push conversion))
|
||||
|
||||
(function :Map<String,KTxt2Conversion> validConversions [sourceFile outputFile source]
|
||||
(let [validMap (new Map)]
|
||||
(doFor c conversions
|
||||
(case c
|
||||
((when (and
|
||||
(= sourceType (sourceFile.extension))
|
||||
(= outputType (outputFile.extension))
|
||||
(canConvert source))
|
||||
(objectWith sourceType outputType canConvert name))
|
||||
(dictSet validMap name c))
|
||||
(otherwise {})))
|
||||
validMap))
|
@@ -109,11 +109,17 @@
|
||||
(vscode.postMessage (object type "replace" text (if newStatus KTxt2.lockedStart KTxt2.unlockedStart) start start end end)))
|
||||
(otherwise (throw "element $element is not a block"))))
|
||||
|
||||
(function tryAutoConvert [element]
|
||||
(case element
|
||||
((Block (objectWith source output outputStart outputEnd))
|
||||
(vscode.postMessage (objectWith [type "tryAutoConvert"] source output outputStart outputEnd)))
|
||||
(otherwise (throw "element $element is not a block"))))
|
||||
|
||||
(function blockElements [source output locked idx]
|
||||
(let [outerDiv (document.createElement "div")
|
||||
sourceDiv (document.createElement "div")
|
||||
outputDiv (document.createElement "div")
|
||||
lockDiv (document.createElement "div")
|
||||
convertLink (document.createElement "a")
|
||||
xLink (document.createElement "a")
|
||||
lockLink (document.createElement "a")]
|
||||
(outerDiv.setAttribute "class" "container")
|
||||
@@ -130,11 +136,6 @@
|
||||
(outputDiv.addEventListener "input"
|
||||
->(addEditTimeout idx ->(replaceOutputBlock (nth ktxt2Elements idx) outputDiv.innerHTML)))
|
||||
(set outputDiv.innerHTML output)
|
||||
(lockDiv.setAttribute "style" "width: 1ch")
|
||||
(if locked
|
||||
(set lockDiv.innerHTML "!")
|
||||
// TODO add a "generate" button and "lock" button
|
||||
)
|
||||
|
||||
// Link that will delete the whole block:
|
||||
(set xLink.innerHTML "x")
|
||||
@@ -144,7 +145,14 @@
|
||||
(outerDiv.appendChild xLink))
|
||||
|
||||
(outerDiv.appendChild sourceDiv)
|
||||
(outerDiv.appendChild lockDiv)
|
||||
|
||||
// Link that attempts automatic conversion of the source in a block:
|
||||
(unless locked
|
||||
(set convertLink.innerHTML "->")
|
||||
(convertLink.addEventListener "click"
|
||||
->(tryAutoConvert (nth ktxt2Elements idx)))
|
||||
(outerDiv.appendChild convertLink))
|
||||
|
||||
(outerDiv.appendChild outputDiv)
|
||||
|
||||
(set lockLink.innerHTML (if locked "unlock" "lock"))
|
||||
|
@@ -10,7 +10,11 @@ typedef MessageFromEditor = {
|
||||
?text:String,
|
||||
?start:kiss.Stream.Position,
|
||||
?end:kiss.Stream.Position,
|
||||
?position:kiss.Stream.Position
|
||||
?position:kiss.Stream.Position,
|
||||
?source:String,
|
||||
?output:String,
|
||||
?outputStart:kiss.Stream.Position,
|
||||
?outputEnd:kiss.Stream.Position
|
||||
};
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
|
@@ -32,18 +32,31 @@
|
||||
->:Void [e]
|
||||
(case (the MessageFromEditor e)
|
||||
((object type "print" text message)
|
||||
(Vscode.window.showInformationMessage message))
|
||||
((object type "replace" text text start start end end)
|
||||
(infoMessage message))
|
||||
((objectWith [type "replace"] text start end)
|
||||
(makeEdit
|
||||
->edit (edit.replace document.uri (rangeFromStartEnd start end) text)))
|
||||
((object type "insert" text text position position)
|
||||
((objectWith [type "insert"] text position)
|
||||
(makeEdit
|
||||
->edit (edit.insert document.uri (streamPosToDocumentPos position) text)))
|
||||
((object type "deleteBlock" start start end end)
|
||||
((objectWith [type "deleteBlock"] start end)
|
||||
(makeEdit
|
||||
->edit (edit.delete document.uri (new Range (streamPosToOffsetDocumentPos start -KTxt2.blockStartEnd.length) (streamPosToOffsetDocumentPos end KTxt2.blockStartEnd.length)))))
|
||||
((objectWith [type "tryAutoConvert"] source output outputStart outputEnd)
|
||||
(let [[base sourceExt outputExt _] (document.fileName.split ".")
|
||||
conversions
|
||||
(KTxt2.validConversions "${base}.${sourceExt}" "${base}.${outputExt}" source)]
|
||||
(if (> 0 (Lambda.count conversions))
|
||||
(let [overwrite (if output "" "Overwrite")
|
||||
chosenConversion (if (= 1 (Lambda.count conversions)) (first (collect (conversions.iterator))) null)]
|
||||
(withValueOrQuickPick overwrite ["Overwrite" "Cancel"]
|
||||
(when (= "Overwrite" overwrite)
|
||||
(withValueOrQuickPickMap chosenConversion conversions
|
||||
(makeEdit
|
||||
->edit (edit.replace document.uri (rangeFromStartEnd outputStart outputEnd) (chosenConversion.convert source)))))))
|
||||
(errorMessage "No automatic conversions are valid for the chosen block."))))
|
||||
(otherwise
|
||||
(Vscode.window.showErrorMessage "bad message $e from KTxt2Editor"))))
|
||||
(errorMessage "bad message $e from KTxt2Editor"))))
|
||||
|
||||
(updateWebview))
|
||||
null)
|
||||
|
Reference in New Issue
Block a user