From 61fb5f6c807af59fd174c4b42dc5ce435cc683f6 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 25 Sep 2023 20:00:52 +0200 Subject: [PATCH] common code / html support --- package-lock.json | 24 ------------------- package.json | 5 ---- public/embed.go | 3 +++ public/index.html | 3 +++ .../components/pages/filebrowser/FileEdit.js | 15 ++++-------- .../pages/filebrowser/Filebrowser.js | 9 +++---- .../js/components/pages/filebrowser/common.js | 20 ++++++++++++++++ 7 files changed, 34 insertions(+), 45 deletions(-) delete mode 100644 package-lock.json delete mode 100644 package.json create mode 100644 public/js/components/pages/filebrowser/common.js diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index a0cdc99c..00000000 --- a/package-lock.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "mtui", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "dependencies": { - "codemirror": "^5.65.15" - } - }, - "node_modules/codemirror": { - "version": "5.65.15", - "resolved": "https://registry.npmjs.org/codemirror/-/codemirror-5.65.15.tgz", - "integrity": "sha512-YC4EHbbwQeubZzxLl5G4nlbLc1T21QTrKGaOal/Pkm9dVDMZXMH7+ieSPEOZCtO9I68i8/oteJKOxzHC2zR+0g==" - } - }, - "dependencies": { - "codemirror": { - "version": "5.65.15", - "resolved": "https://registry.npmjs.org/codemirror/-/codemirror-5.65.15.tgz", - "integrity": "sha512-YC4EHbbwQeubZzxLl5G4nlbLc1T21QTrKGaOal/Pkm9dVDMZXMH7+ieSPEOZCtO9I68i8/oteJKOxzHC2zR+0g==" - } - } -} diff --git a/package.json b/package.json deleted file mode 100644 index 18e22fe6..00000000 --- a/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "dependencies": { - "codemirror": "^5.65.15" - } -} diff --git a/public/embed.go b/public/embed.go index 75cd1acb..4de007a2 100644 --- a/public/embed.go +++ b/public/embed.go @@ -22,6 +22,9 @@ import ( //go:embed node_modules/codemirror/lib/codemirror.* //go:embed node_modules/codemirror/mode/lua/lua.js //go:embed node_modules/codemirror/mode/javascript/javascript.js +//go:embed node_modules/codemirror/mode/htmlmixed/htmlmixed.js +//go:embed node_modules/codemirror/mode/xml/xml.js +//go:embed node_modules/codemirror/mode/css/css.js var Webapp embed.FS const DefaultCss = "node_modules/bootstrap/dist/css/bootstrap.min.css" diff --git a/public/index.html b/public/index.html index 1424925d..635b07f4 100644 --- a/public/index.html +++ b/public/index.html @@ -36,6 +36,9 @@ + + + {{if .Webdev}} {{else}} diff --git a/public/js/components/pages/filebrowser/FileEdit.js b/public/js/components/pages/filebrowser/FileEdit.js index d7ddf309..706d8370 100644 --- a/public/js/components/pages/filebrowser/FileEdit.js +++ b/public/js/components/pages/filebrowser/FileEdit.js @@ -1,6 +1,7 @@ import { START, FILEBROWSER } from "../../Breadcrumb.js"; import DefaultLayout from "../../layouts/DefaultLayout.js"; import { download_text, upload } from "../../../api/filebrowser.js"; +import { get_mode_name } from "./common.js"; export default { props: ["pathMatch"], @@ -18,18 +19,12 @@ export default { download_text(this.pathMatch) .then(t => this.text = t) .then(() => { - - const mode = {}; - if (this.pathMatch.match(/.*(lua)$/i)) { - mode.name = "lua"; - } else if (this.pathMatch.match(/.*(js|json)$/i)) { - mode.name = "javascript"; - } - this.cm = CodeMirror.fromTextArea(this.$refs.textarea, { lineNumbers: true, viewportMargin: Infinity, - mode: mode + mode: { + name: get_mode_name(this.pathMatch) + } }); }); }, @@ -83,4 +78,4 @@ export default { ` -}; \ No newline at end of file +}; diff --git a/public/js/components/pages/filebrowser/Filebrowser.js b/public/js/components/pages/filebrowser/Filebrowser.js index bb68d178..97c09acb 100644 --- a/public/js/components/pages/filebrowser/Filebrowser.js +++ b/public/js/components/pages/filebrowser/Filebrowser.js @@ -4,6 +4,7 @@ import format_size from "../../../util/format_size.js"; import format_time from "../../../util/format_time.js"; import { START, FILEBROWSER } from "../../Breadcrumb.js"; import { get_maintenance } from "../../../service/stats.js"; +import { can_edit } from "./common.js"; export default { props: ["pathMatch"], @@ -90,18 +91,14 @@ export default { } }); }, - can_edit: function(filename) { - return filename.match(/.*(js|lua|txt|conf|cfg|json|md|mt)$/i); - }, + can_edit: can_edit, is_database: function(filename) { return filename.match(/.*(sqlite|sqlite-shm|sqlite-wal)$/i); }, get_icon: function(item) { if (item.is_dir) { return "folder"; - } else if (item.name.match(/.*(txt|conf|cfg|md)$/i)) { - return "file-lines"; - } else if (item.name.match(/.*(js|lua|json)$/i)) { + } else if (can_edit(item.name)) { return "file-code"; } else if (item.name.match(/.*(sqlite)$/i)) { return "database"; diff --git a/public/js/components/pages/filebrowser/common.js b/public/js/components/pages/filebrowser/common.js new file mode 100644 index 00000000..24d6ec81 --- /dev/null +++ b/public/js/components/pages/filebrowser/common.js @@ -0,0 +1,20 @@ +const modes = [{ + name: "lua", match: /.*(lua)$/i +},{ + name: "javascript", match: /.*(js|json)$/i +},{ + name: "htmlmixed", match: /.*(html)$/i +},{ + name: "xml", match: /.*(xml)$/i +},{ + name: "text/css", match: /.*(css)$/i +}]; + +export const can_edit = filename => { + return filename.match(/.*(js|lua|txt|conf|cfg|json|md|mt|html|css)$/i); +}; + +export const get_mode_name = filename => { + const mode = modes.find(m => filename.match(m.match)); + return mode ? mode.name : null; +}; \ No newline at end of file