From 36428283e725b90887623fc625a4764a6e4600cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=AB=8B=E5=B8=AE?= <3294713004@qq.com> Date: Sun, 18 Aug 2024 01:52:21 +0800 Subject: [PATCH] =?UTF-8?q?Update:=20=E8=B0=83=E6=95=B4Ampy=E6=9D=BF?= =?UTF-8?q?=E5=8D=A1=E6=96=87=E4=BB=B6=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 本次更新用以配置Ampy板卡文件管理使其仅支持编辑文本文件 --- .../mixly-modules/common/editor-ace.js | 21 ++- .../modules/mixly-modules/common/file-tree.js | 12 +- .../mixly-modules/common/statusbar-ampy.js | 52 ++---- .../modules/mixly-modules/common/workspace.js | 2 +- .../modules/mixly-modules/web/burn-upload.js | 2 +- common/msg/mixly/en.json | 1 + common/msg/mixly/zh-hans.json | 1 + common/msg/mixly/zh-hant.json | 1 + common/templates/json/ace-mode-map.json | 171 ++++++++++++++++++ 9 files changed, 219 insertions(+), 44 deletions(-) create mode 100644 common/templates/json/ace-mode-map.json diff --git a/common/modules/mixly-modules/common/editor-ace.js b/common/modules/mixly-modules/common/editor-ace.js index c31a4dbf..0728297f 100644 --- a/common/modules/mixly-modules/common/editor-ace.js +++ b/common/modules/mixly-modules/common/editor-ace.js @@ -23,7 +23,8 @@ class EditorAce extends EditorBase { static { this.CTRL_BTNS = ['resetFontSize', 'increaseFontSize', 'decreaseFontSize']; this.CTRL_BTN_TEMPLATE = '
'; - + this.MODE_MAP = goog.getJSON(path.join(Env.templatePath, 'json/ace-mode-map.json')); + HTMLTemplate.add( 'html/editor/editor-code.html', new HTMLTemplate(goog.get(path.join(Env.templatePath, 'html/editor/editor-code.html'))) @@ -279,6 +280,24 @@ class EditorAce extends EditorBase { this.#editor_.setReadOnly(status); } + setMode(type) { + this.#editor_.session.setMode(`ace/mode/${type}`); + } + + setFileMode(extname) { + const mode = this.getFileMode(extname) ?? 'text'; + this.setMode(mode); + } + + getFileMode(extname) { + for (const [mode, extensions] of Object.entries(EditorAce.MODE_MAP)) { + if (extensions.includes(extname)) { + return mode; + } + } + return null; + } + cut() { const { selection, session } = this.#editor_; const cutLine = selection.isEmpty(); diff --git a/common/modules/mixly-modules/common/file-tree.js b/common/modules/mixly-modules/common/file-tree.js index 2d0badaa..5c14cca2 100644 --- a/common/modules/mixly-modules/common/file-tree.js +++ b/common/modules/mixly-modules/common/file-tree.js @@ -130,7 +130,7 @@ class FileTree extends Component { }); this.#jstree_ = this.#$fileTree_.jstree(true); this.addEventsType([ - 'selectLeaf', 'afterOpenNode', 'afterCloseNode', 'afterRefreshNode', + 'beforeSelectLeaf', 'afterSelectLeaf', 'afterOpenNode', 'afterCloseNode', 'afterRefreshNode', 'afterCreateNode', 'afterDeleteNode', 'afterRenameNode' ]); this.#addEventsListener_(); @@ -202,8 +202,14 @@ class FileTree extends Component { if (selected[0].id === this.#selected_) { return; } - this.#selected_ = selected[0].id; - this.runEvent('selectLeaf', selected); + const result = this.runEvent('beforeSelectLeaf', selected); + if ((result.length && result[0]) || !result.length) { + this.#selected_ = selected[0].id; + this.runEvent('afterSelectLeaf', selected); + } else { + this.deselect(selected[0].id); + this.reselect(); + } }) .on('refresh.jstree', (e, data) => { this.runEvent('afterRefreshNode', data.node); diff --git a/common/modules/mixly-modules/common/statusbar-ampy.js b/common/modules/mixly-modules/common/statusbar-ampy.js index 4267ae11..8acdb791 100644 --- a/common/modules/mixly-modules/common/statusbar-ampy.js +++ b/common/modules/mixly-modules/common/statusbar-ampy.js @@ -89,9 +89,20 @@ class StatusBarAmpy extends PageBase { this.openFS(); }); - this.#fileTree_.bind('selectLeaf', async (selected) => { - this.#fileTree_.showProgress(); + this.#fileTree_.bind('beforeSelectLeaf', (selected) => { const filePath = selected[0].id; + const mode = this.#editor_.getFileMode(path.extname(filePath)); + if (!mode) { + layer.msg(Msg.Lang['statusbar.ampy.cannotEdit'], { time: 1000 }); + return false; + } + this.#editor_.setMode(mode); + return true; + }); + + this.#fileTree_.bind('afterSelectLeaf', async (selected) => { + const filePath = selected[0].id; + this.#fileTree_.showProgress(); const fs = this.#fileTree_.getFS(); const [error, result] = await fs.readFile(filePath); if (error) { @@ -103,9 +114,6 @@ class StatusBarAmpy extends PageBase { this.#editor_.scrollToTop(); this.#editor_.focus(); this.setStatus(false); - this.#editor_.getEditor().session.setMode( - `ace/mode/${this.getLanguageByExt(path.extname(filePath))}` - ); } this.#fileTree_.hideProgress(); }); @@ -173,7 +181,7 @@ class StatusBarAmpy extends PageBase { this.#fileTree_.refreshFolder(id); } else { const nodes = this.#fileTree_.getSelectedNodes(); - this.#fileTree_.runEvent('selectLeaf', nodes); + this.#fileTree_.runEvent('afterSelectLeaf', nodes); } } } @@ -439,38 +447,6 @@ class StatusBarAmpy extends PageBase { } } - getLanguageByExt(ext) { - let language = 'text'; - switch(ext) { - case '.json': - language = 'json'; - break; - case '.c': - case '.cpp': - case '.h': - case '.hpp': - case '.ino': - language = 'c_cpp'; - break; - case '.js': - language = 'javascript'; - break; - case '.py': - language = 'python'; - break; - case '.lua': - language = 'lua'; - break; - case '.md': - case '.mdx': - language = 'markdown'; - break; - default: - language = 'text'; - } - return language; - } - dispose() { this.#editor_.dispose(); this.#fileTree_.dispose(); diff --git a/common/modules/mixly-modules/common/workspace.js b/common/modules/mixly-modules/common/workspace.js index f4dc4b54..afa80e41 100644 --- a/common/modules/mixly-modules/common/workspace.js +++ b/common/modules/mixly-modules/common/workspace.js @@ -103,7 +103,7 @@ class Workspace extends Component { #addEventsListenerForFileTree_() { const leftSideBarLocalStorage = this.getLeftSideBarsManager().get('local_storage'); const fileTree = leftSideBarLocalStorage.getFileTree(); - fileTree.bind('selectLeaf', (selected) => { + fileTree.bind('afterSelectLeaf', (selected) => { const tabs = this.#editorsManager_.getTabs(); tabs.addTab({ name: selected[0].text, diff --git a/common/modules/mixly-modules/web/burn-upload.js b/common/modules/mixly-modules/web/burn-upload.js index daab601d..c4744879 100644 --- a/common/modules/mixly-modules/web/burn-upload.js +++ b/common/modules/mixly-modules/web/burn-upload.js @@ -137,7 +137,7 @@ BU.burnByUSB = () => { const hex2Blob = new Blob([ hexStr ], { type: 'text/plain' }); const buffer = await hex2Blob.arrayBuffer(); if (!buffer) { - layer.msg(Msg.Lang['固件读取出错'], { time: 1000 }); + layer.msg(Msg.Lang['shell.bin.readFailed'], { time: 1000 }); return; } BU.burning = true; diff --git a/common/msg/mixly/en.json b/common/msg/mixly/en.json index 05a7acf8..0062176b 100644 --- a/common/msg/mixly/en.json +++ b/common/msg/mixly/en.json @@ -244,6 +244,7 @@ "statusbar.ampy.loadBoardFS": "Load board FS", "statusbar.ampy.refresh": "Refresh", "statusbar.ampy.exit": "Exit", + "statusbar.ampy.cannotEdit": "This file type does not support editing", "statusbar.dropdownMenu.noOptions": "No options", "statusbar.fs.newMapFolder": "Create a new mapping directory", "statusbar.fs.localFolderNotExist": "Local mapping directory does not exist", diff --git a/common/msg/mixly/zh-hans.json b/common/msg/mixly/zh-hans.json index 8b86e95c..4129be56 100644 --- a/common/msg/mixly/zh-hans.json +++ b/common/msg/mixly/zh-hans.json @@ -244,6 +244,7 @@ "statusbar.ampy.loadBoardFS": "载入板卡文件夹", "statusbar.ampy.refresh": "刷新", "statusbar.ampy.exit": "退出", + "statusbar.ampy.cannotEdit": "该文件类型不支持编辑", "statusbar.dropdownMenu.noOptions": "无选项", "statusbar.fs.newMapFolder": "新建映射目录", "statusbar.fs.localFolderNotExist": "本地映射目录不存在", diff --git a/common/msg/mixly/zh-hant.json b/common/msg/mixly/zh-hant.json index 708d4359..a7acd468 100644 --- a/common/msg/mixly/zh-hant.json +++ b/common/msg/mixly/zh-hant.json @@ -244,6 +244,7 @@ "statusbar.ampy.loadBoardFS": "載入卡卡資料夾", "statusbar.ampy.refresh": "刷新", "statusbar.ampy.exit": "退出", + "statusbar.ampy.cannotEdit": "該文件類型不支援編輯", "statusbar.dropdownMenu.noOptions": "無選項", "statusbar.fs.newMapFolder": "新映射目錄", "statusbar.fs.localFolderNotExist": "本機映射目錄不存在", diff --git a/common/templates/json/ace-mode-map.json b/common/templates/json/ace-mode-map.json new file mode 100644 index 00000000..1ca780bd --- /dev/null +++ b/common/templates/json/ace-mode-map.json @@ -0,0 +1,171 @@ +{ + "abap": [".abap"], + "abc": [".abc"], + "actionscript": [".as"], + "ada": [".ada", ".adb", ".ads"], + "alda": [".alda"], + "apex": [".apex"], + "aql": [".aql"], + "asciidoc": [".adoc", ".asciidoc"], + "asl": [".dsl", ".asl"], + "assembly_x86": [".asm", ".a", ".s"], + "autohotkey": [".ahk"], + "batchfile": [".bat", ".cmd"], + "bro": [".bro"], + "c9search": [".c9search_results"], + "c_cpp": [".c", ".cpp", ".cc", ".cxx", ".h", ".hh", ".hpp", ".ino"], + "cirru": [".cirru", ".cr"], + "clojure": [".clj", ".cljs"], + "cobol": [".cbl", ".cob"], + "coffee": [".coffee", ".cf", ".cson", ".iced"], + "coldfusion": [".cfm"], + "crystal": [".cr"], + "csharp": [".cs"], + "csound_document": [".csd"], + "csound_orchestra": [".orc"], + "csound_score": [".sco"], + "css": [".css"], + "curly": [".curl"], + "d": [".d", ".di"], + "dart": [".dart"], + "diff": [".diff", ".patch"], + "dockerfile": [".dockerfile"], + "dot": [".dot"], + "drools": [".drl"], + "edifact": [".edi"], + "eiffel": [".e"], + "ejs": [".ejs"], + "elixir": [".ex", ".exs"], + "elm": [".elm"], + "erlang": [".erl", ".hrl"], + "forth": [".frt", ".fs", ".ldr", ".fth"], + "fortran": [".f", ".f77", ".f90", ".f95", ".f03", ".for", ".fpp"], + "fsharp": [".fs", ".fsi", ".ml", ".mli"], + "gcode": [".gcode"], + "gherkin": [".feature"], + "gitignore": [".gitignore"], + "glsl": [".glsl", ".frag", ".vert"], + "gobstones": [".gbs"], + "golang": [".go"], + "graphqlschema": [".gql", ".graphql"], + "groovy": [".groovy"], + "haml": [".haml"], + "handlebars": [".hbs", ".handlebars"], + "haskell": [".hs"], + "haskell_cabal": [".cabal"], + "haxe": [".hx"], + "hjson": [".hjson"], + "html": [".html", ".htm", ".xhtml"], + "ini": [".ini", ".cfg", ".prefs"], + "io": [".io"], + "jack": [".jack"], + "jade": [".jade"], + "java": [".java"], + "javascript": [".js", ".jsm", ".jsx"], + "json": [".json"], + "json5": [".json5"], + "jsoniq": [".jq"], + "jsp": [".jsp"], + "jssm": [".jssm", ".jssm_state"], + "jsx": [".jsx"], + "julia": [".jl"], + "kotlin": [".kt", ".kts"], + "latex": [".tex", ".latex", ".ltx", ".bib"], + "less": [".less"], + "liquid": [".liquid"], + "lisp": [".lisp"], + "livescript": [".ls"], + "logiql": [".logic", ".lql"], + "logtalk": [".logtalk"], + "lsl": [".lsl"], + "lua": [".lua"], + "luapage": [".lp"], + "lucene": [".lucene"], + "makefile": [".makefile", ".gnumakefile", ".ocamlmakefile", ".make"], + "markdown": [".md", ".markdown"], + "mask": [".mask"], + "matlab": [".matlab"], + "maze": [".mz"], + "mediawiki": [".wiki", ".mediawiki"], + "mel": [".mel"], + "mips": [".s", ".asm"], + "mithril": [".mjs"], + "moonscript": [".moon"], + "mysql": [".mysql"], + "nginx": [".nginx", ".conf"], + "nim": [".nim"], + "nix": [".nix"], + "nsis": [".nsi", ".nsh"], + "nunjucks": [".njk", ".nunjucks"], + "objectivec": [".m", ".mm"], + "ocaml": [".ml", ".mli"], + "pascal": [".pas", ".pp"], + "perl": [".pl", ".pm"], + "pgsql": [".pgsql"], + "php": [".php", ".inc"], + "php_laravel_blade": [".blade.php"], + "pig": [".pig"], + "powershell": [".ps1"], + "praat": [".praat", ".praatscript", ".psc", ".proc"], + "prisma": [".prisma"], + "prolog": [".plg", ".prolog"], + "properties": [".properties"], + "protobuf": [".proto"], + "puppet": [".pp"], + "purescript": [".purs"], + "python": [".py"], + "q": [".q"], + "qml": [".qml"], + "r": [".r"], + "raku": [".raku", ".rakumod", ".rakutest", ".pl6", ".pm6"], + "razor": [".cshtml", ".razor"], + "rdoc": [".rdoc"], + "red": [".red", ".reds"], + "rhtml": [".erb", ".rhtml"], + "robot": [".robot"], + "rst": [".rst"], + "ruby": [".rb"], + "rust": [".rs"], + "sass": [".sass"], + "scad": [".scad"], + "scala": [".scala"], + "scheme": [".scm", ".sm", ".rkt", ".oak"], + "scrypt": [".scrypt"], + "scss": [".scss"], + "sh": [".sh", ".bash", ".bats", ".cgi", ".command", ".fcgi", ".ksh", ".sh.in", ".tmux", ".tool", ".zsh"], + "sjs": [".sjs"], + "slim": [".slim"], + "smarty": [".tpl"], + "smithy": [".smithy"], + "snippets": [".snippets"], + "soy_template": [".soy"], + "space": [".space"], + "sparql": [".rq"], + "sql": [".sql"], + "sqlserver": [".sqlserver"], + "stylus": [".styl", ".stylus"], + "svg": [".svg"], + "swift": [".swift"], + "tcl": [".tcl"], + "terraform": [".tf", ".tfvars", ".terragrunt"], + "tex": [".tex"], + "text": [".txt"], + "textile": [".textile"], + "toml": [".toml"], + "tsx": [".tsx"], + "turtle": [".ttl"], + "twig": [".twig"], + "typescript": [".ts"], + "vala": [".vala"], + "vbscript": [".vbs"], + "velocity": [".vm"], + "verilog": [".v", ".vh", ".sv", ".svh"], + "vhdl": [".vhd", ".vhdl"], + "visualforce": [".vfp", ".component", ".page"], + "wollok": [".wlk", ".wpgm"], + "xml": [".xml", ".rdf", ".rss", ".wsdl", ".xslt", ".atom", ".mathml", ".mml", ".xul", ".xbl", ".xaml"], + "xquery": [".xq"], + "yaml": [".yaml", ".yml"], + "zeek": [".zeek"], + "zig": [".zig"] +} \ No newline at end of file