xielang/example/editFile.xie
Copy
// 本例演示编辑一个文本文件 // 用法:xie editFile.xie -file=d:\tmp\a1.txt // 注意使用时将上面参数中的内容替换为自己的实际内容 // This example demonstrates editing a text file // Usage: xie editFile.xie -file=d:\tmp\a1.txt // Please replace the content in the above parameters with your own actual content when using it // 从命令行获取文件名 // Obtain a file name from the command line getSwitch $filePathT $argsG "-file=" "" trim $filePathT $filePathT // 如果没有指定文件名则提示是否要新建一个文件 // 弹出文件选择框来指定文件 // If no file name is specified, prompt whether to create a new file // Pop up a file selection box to specify the file ifEmpty $filePathT :+1 :next1 mt $rs1 $guiG getConfirm "请选择……(please select...)" "文件路径为空,是否创建新文件?(file not exists, create a new file for edit?)" ifNot $rs1 :+1 :+2 exit mt $rs2 $guiG selectFileToSave -confirmOverwrite -title="新建文件……(new file...)" -default=test.txt `-filter=[{"Name":"All files", "Patterns":["*.*"], "CaseFold":true}]` ifNilOrEmpty $rs2 :+1 :+2 fatalf "未选择文件(no file selected)" = $filePathT $rs2 :next1 // 获取内置的用于基于WebView2的图形界面所用的JavaScript文件 // Obtain built-in JavaScript files for WebView2 based graphical interfaces getResource $tmps "js/tkc.js" // 设置图形界面所用的HTML // Set the HTML used for the GUI = $htmlT ` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>编辑文件(edit file)……</title> <script>TX_tkc.js_XT</script> </head> <body style="font-size: 16px;"> <div style="margin-top: 10px;"> <label for="remotePathID" >文件路径(file path): </label><input id="filePathID" type="text" readonly /> </div> <div style="margin-top: 10px; text-align: center;"> <textarea id="mainTextArea" style="width: calc(100% - 10px); height: 80vh; font-size: 16px;"></textarea> </div> <div style="margin-top: 10px;"> <button id="btnLoadID" cmd="reload">重新载入(reload)</button> <button id="btnSaveID" cmd="save">保存(save)</button> <button id="btnSaveAsID" cmd="saveAs">另存为(saveAs)……</button> <button id="btnClose">关闭(close)</button> </div> <script type="text/javascript"> function setFilePath(pathA) { document.getElementById("filePathID").value = tkc.hexDecode(pathA); } function setTextValue(textA) { document.getElementById("mainTextArea").value = tkc.hexDecode(textA); } function sendCmd(e, senderA) { var cmdT = e.target.getAttribute("cmd").trim(); if (cmdT == "save") { var rs = quickDelegateDo(JSON.stringify({cmd: cmdT, value: document.getElementById("mainTextArea").value})); rs.then(res => { alert(res); }); return; } else if (cmdT == "saveAs") { var rs = quickDelegateDo(JSON.stringify({cmd: cmdT, value: document.getElementById("mainTextArea").value})); rs.then(res => { if (res.startsWith("TXERROR:")) { alert(res.substring(8)); return; } document.getElementById("filePathID").value = res; }); return; } var rs = quickDelegateDo(JSON.stringify({cmd: cmdT})); rs.then(res => { console.log(res); if (res != "") { alert(res); } }); } function closeWindow() { quickDelegateDo(JSON.stringify({cmd: "close"})); } document.addEventListener('DOMContentLoaded', function() { document.getElementById("btnClose").addEventListener("click", closeWindow); document.getElementById("btnLoadID").addEventListener("click", sendCmd); document.getElementById("btnSaveID").addEventListener("click", sendCmd); document.getElementById("btnSaveAsID").addEventListener("click", sendCmd); quickDelegateDo(JSON.stringify({cmd: "init"})); }, false); </script> </body> </html> ` // 替换HTML文本中的JavaScript内容 // Replace JavaScript content in HTML text strReplace $htmlT $htmlT "TX_tkc.js_XT" $tmps // 新建图形界面窗口 // Create a new graphical interface window mt $windowT $guiG newWindow "-title=编辑文件(edit file)" -width=640 -height=480 -center -debug // 创建并设定图形界面使用的快速代理对象 // Create and set fast proxy objects for graphical interface use new $dele1 quickDelegate :deleFast1 :deleFast1End mt $rs $windowT setQuickDelegate $dele1 // 设置图形界面所用的HTML // Create and set fast proxy objects for graphical interface use mt $rs $windowT setHtml $htmlT // 运行图形界面 // show the GUI window mt $rs $windowT show ifErrX $rs :+1 :+2 fatalf "运行图形界面失败(failed to run GUI):%v" $rs exit // 快速代理对象的代码 // 即用于在WEB图形界面GUI中的回调函数 // Code for quick delegate Objects // The callback function used in the WEB GUI :deleFast1 // 获取传入参数 // Get incoming parameters getArrayItem $inputT $inputL 0 pl "inputT: %v" $inputT // 本例只允许传入JSON格式的指令,这里将JSON转换为内部对象(这里是一个映射) // This example only allows instructions in JSON format to be passed in, where JSON is converted into an internal object (here is a mapping) fromJson $cmdObjT $inputT // 从映射对象中键名为cmd和value各自对应的键值 // From the mapping object, the key names cmd and value correspond to their respective key values {} $cmdT $cmdObjT "cmd" "" {} $valueT $cmdObjT "value" "" // 根据命令各自处理 // Process according to commands if @`$cmdT == "init"` :+1 :inext1 // 初始化命令 // Initialization command pln "init" // 为了传递进JavaScript的函数,将参数进行16进制编码,以免特殊字符冲突 // To pass in JavaScript functions, encode the parameters in hexadecimal to avoid special character collisions hexEncode $tmps1 $filePathT spr $tmps2 `setFilePath('%v')` $tmps1 // 调用图形界面中的JavaScript函数设置文件路径的显示 // Calling JavaScript functions in the GUI to set the display of file path mt $rs $windowT eval $tmps2 plo $rs // 载入文本内容并显示在textarea中 // Load the text content and display it in the textarea loadText $fcT $filePathT hexEncode $tmps1 $fcT spr $tmps2 `setTextValue('%v')` $tmps1 mt $rs $windowT eval $tmps2 exitL "" :inext1 if @`$cmdT == "save"` :+1 :inext2 // 保存 // save pln "save text" saveText $rs1 $valueT $filePathT ifErrX $rs1 :+1 :inextErr1 spr $rs "保存失败(failed to save):%v" $rs1 exitL $rs :inextErr1 exitL "保存完毕(successfully saved)" :inext2 if @`$cmdT == "saveAs"` :+1 :inext3 // 另存 // save as pln "saveAs" spr $tmps1 -default=%v $filePathT mt $rs2 $guiG selectFileToSave -confirmOverwrite -title="另存为(save as)……" $tmps1 `-filter=[{"Name":"All files", "Patterns":["*.*"], "CaseFold":true}]` ifNilOrEmpty $rs2 :+1 :+2 exitL "TXERROR:未选择文件(no file selected)" = $filePathT $rs2 saveText $rs1 $valueT $filePathT ifErrX $rs1 :+1 :inextErr2 spr $rs "TXERROR:另存失败(failed to save):%v" $rs1 exitL $rs :inextErr2 // 注意,另存后将返回新的文件路径,以便显示在图形界面中 exitL $filePathT :inext3 if @`$cmdT == "reload"` :+1 :inext4 // 重新载入 // reload pln "reload text" loadText $fcT $filePathT hexEncode $tmps1 $fcT spr $tmps2 `setTextValue('%v')` $tmps1 mt $rs $windowT eval $tmps2 exitL "" :inext4 if @`$cmdT == "close"` :+1 :inext5 // 关闭窗口 // close the GUI window pln "close window" mt $rs $windowT close exitL "" :inext5 if @`$cmdT == "prints"` :+1 :inextn // 用于调试的prints命令 // prints command for debug purpose {} $tmps $cmdObjT "value" "" pln $tmps exitL "" :inextn pln "未知命令(unknown command)" exitL "未知命令(unknown command)" :deleFast1End exitL