完成重构

master
yutent 2023-09-05 14:17:37 +08:00
parent 419a13e4e6
commit 8bde4b078c
7 changed files with 64 additions and 61 deletions

View File

@ -6,9 +6,8 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib, Gio, GObject, GdkPixbuf from gi.repository import Gtk, Gdk, GLib, Gio, GObject, GdkPixbuf
from we.gtk3 import WebEngine, create_setting, create_hmr_server from webengine.gtk3 import WebEngine, create_setting, create_hmr_server
# from webengine.gtk3 import WebEngine, create_setting, create_hmr_server from _window import Window
from window import Window
APP_ID = 'fun.wkit.hosts-switch' APP_ID = 'fun.wkit.hosts-switch'
__dir__ = os.path.dirname(os.path.realpath(__file__)) __dir__ = os.path.dirname(os.path.realpath(__file__))
@ -26,12 +25,12 @@ class Application(Gtk.Application):
self.window = Window() self.window = Window()
web = WebEngine(self.window) web = WebEngine(self.window)
setting = create_setting({"devtools": True})
hmr = create_hmr_server()
web.set_root(web_root) web.set_root(web_root)
web.use(setting).use(hmr) if os.getenv('RUN_ENV') == 'development':
setting = create_setting({"devtools": True})
hmr = create_hmr_server()
web.use(setting).use(hmr)
web.connect('quit', self.quit_all) web.connect('quit', self.quit_all)
web.load() web.load()
@ -61,5 +60,4 @@ if __name__ == "__main__":
app.run(sys.argv) app.run(sys.argv)
except Exception as err: except Exception as err:
print(err) print(err)
pass

View File

@ -11,6 +11,8 @@ import { createApp, createRouter } from 'wkitd'
import store from './store.js' import store from './store.js'
import './components/home.js' import './components/home.js'
import { noop } from './utils/index.js'
createApp({ createApp({
data: { data: {
input: '', input: '',
@ -32,7 +34,7 @@ createApp({
} }
}, },
render() { render() {
return html` <wc-home @contextmenu.prevent2=${function () {}}></wc-home> ` return html` <wc-home @contextmenu.prevent=${noop}></wc-home> `
} }
}) })
.use(store) .use(store)

View File

@ -8,7 +8,6 @@ import { html, css, Component, classMap, nextTick, outsideClick } from 'wkit'
import 'ui/icon/index.js' import 'ui/icon/index.js'
import 'ui/scroll/index.js' import 'ui/scroll/index.js'
import 'ui/layer/index.js' import 'ui/layer/index.js'
// import 'local/layer/index.js'
import 'ui/space/index.js' import 'ui/space/index.js'
import 'ui/form/input.js' import 'ui/form/input.js'
import 'ui/form/switch.js' import 'ui/form/switch.js'
@ -149,14 +148,16 @@ class Home extends Component {
confirmAction(ev) { confirmAction(ev) {
this.$refs.context.close() this.$refs.context.close()
if (ev.target.tagName === 'LI') { if (ev.target.tagName === 'LI') {
var act = ev.target.dataset.act let act = ev.target.dataset.act
let { HOST_DATA, records, domains } = this.$store
let idx = domains.indexOf(this.editDomain)
if (act === 'del') { if (act === 'del') {
layer layer
.confirm(`是否要删除域名「${this.editDomain}」?`) .confirm(`是否要删除域名「${this.editDomain}」?`)
.then(res => { .then(res => {
if (this.editDomain === this.activeDomain) { if (this.editDomain === this.$store.activeDomain) {
if (this.records.length) { if (records.length) {
return layer.toast( return layer.toast(
'该域名下有主机记录, 请先删除主机记录后再删除域名', '该域名下有主机记录, 请先删除主机记录后再删除域名',
'error' 'error'
@ -171,40 +172,46 @@ class Home extends Component {
} }
} }
delete HOST_DATA[this.editDomain] delete HOST_DATA[this.editDomain]
this.domains.remove(this.editDomain)
this.activeDomain = '' domains.splice(idx, 1)
this.editDomain = '' this.editDomain = ''
this.records.clear() this.$store.records = []
this.$store.activeDomain = domains[0]
this.$refs.domain.mounted()
this.save() this.save()
this.toggleDomain(this.domains[0])
}) })
.catch(noop) .catch(noop)
} else if (act === 'edit') { } else if (act === 'edit') {
layer layer
.prompt(`请输入新的名字「${this.editDomain}`, (val, done) => { .prompt(
if (val === this.editDomain || HOST_DATA[val]) { `请输入新的名字「${this.editDomain}`,
return this.editDomain,
} (val, done) => {
if (val === this.editDomain || HOST_DATA[val]) {
return layer.toast(`${val} 域名没有变化, 或已经存在`)
}
if ( if (
val === 'localhost' || val === 'localhost' ||
val === 'local' || val === 'local' ||
/^[\w.]+\.[a-z]+$/.test(val) /^[\w.]+\.[a-z]+$/.test(val)
) { ) {
done() done()
} else { } else {
layer.toast('域名格式错误', 'error') layer.toast('域名格式错误', 'error')
}
} }
}) )
.then(val => { .then(val => {
var idx = this.domains.indexOf(this.editDomain) domains[idx] = val
this.domains.set(idx, val)
HOST_DATA[val] = HOST_DATA[this.editDomain] HOST_DATA[val] = HOST_DATA[this.editDomain]
delete HOST_DATA[this.editDomain] delete HOST_DATA[this.editDomain]
this.activeDomain = ''
this.$store.activeDomain = val
this.editDomain = '' this.editDomain = ''
this.$refs.domain.mounted()
this.save() this.save()
this.toggleDomain(val)
}) })
.catch(noop) .catch(noop)
} }
@ -214,7 +221,7 @@ class Home extends Component {
save() { save() {
let { HOST_DATA, activeDomain, records } = this.$store let { HOST_DATA, activeDomain, records } = this.$store
if (activeDomain) { if (activeDomain) {
HOST_DATA[activeDomain] = records HOST_DATA[activeDomain] = records.filter(it => it.record && it.value)
} }
saveHosts(HOST_DATA) saveHosts(HOST_DATA)
layer.toast('保存成功', 'success') layer.toast('保存成功', 'success')
@ -226,20 +233,15 @@ class Home extends Component {
ref="domain" ref="domain"
@toggle-domain=${_ => (this.$refs.list.scrollTop = 0)} @toggle-domain=${_ => (this.$refs.list.scrollTop = 0)}
@show-menu=${ev => this.showMenu(ev.event)} @show-menu=${ev => this.showMenu(ev.event)}
@save=${this.save}
></wc-sidebar> ></wc-sidebar>
<main class="main noselect"> <main class="main noselect">
<header class="toolbar"> <header class="toolbar">
<wc-button <wc-button size="m" icon="plus" @click=${this.addRecord}
size="m"
icon="plus"
type="primary"
@click=${this.addRecord}
>新增记录</wc-button >新增记录</wc-button
> >
<wc-button size="m" icon="fly" type="info" @click=${this.save} <wc-button size="m" icon="fly" @click=${this.save}>保存</wc-button>
>保存</wc-button
>
</header> </header>
<wc-records class="list" ref="list"></wc-records> <wc-records class="list" ref="list"></wc-records>

View File

@ -88,13 +88,13 @@ class Sidebar extends Component {
} }
}) })
.then(val => { .then(val => {
let { HOST_DATA, records } = this.$store
this.$store.domains.push(val) this.$store.domains.push(val)
// HOST_DATA[val] = [] HOST_DATA[val] = []
// if (!this.$store.activeDomain) { if (!this.$store.activeDomain) {
// this.toggleDomain(null, val) this.toggleDomain(null, val)
// } }
// this.save() this.$emit('save')
// this.$emit('save')
}) })
.catch(noop) .catch(noop)
} }

View File

@ -20,12 +20,10 @@
"imports":{ "imports":{
"es.shim":"app:///lib/es.shim.js", "es.shim":"app:///lib/es.shim.js",
"wkit":"app:///lib/wkit.js", "wkit":"app:///lib/wkit.js",
"wkit":"http://127.0.0.1:9999/src/index.js",
"wkitd":"app:///lib/wkitd.js", "wkitd":"app:///lib/wkitd.js",
"fetch":"app:///lib/fetch.js", "fetch":"app:///lib/fetch.js",
"crypto":"app:///lib/crypto.js", "crypto":"app:///lib/crypto.js",
"ui/":"app:///lib/ui/", "ui/":"app:///lib/ui/"
"local/":"http://127.0.0.1:8090/dist/"
} }
} }
</script> </script>

View File

@ -4,14 +4,14 @@
* @date 2023/09/04 19:18:28 * @date 2023/09/04 19:18:28
*/ */
import { nextTick } from 'wkit'
await native.init() await native.init()
const APP_CONFIG_DIR = `${native.env.CONFIG_DIR}/hosts-switch` const APP_CONFIG_DIR = `${native.env.CONFIG_DIR}/hosts-switch`
const HOST_FILE = `${APP_CONFIG_DIR}/host.cache` const HOST_FILE = `${APP_CONFIG_DIR}/host.cache`
const LOCK_FILE = `${APP_CONFIG_DIR}/lock` const LOCK_FILE = `${APP_CONFIG_DIR}/lock`
let timer
export function noop() {} export function noop() {}
export function checkPermission() { export function checkPermission() {
@ -74,8 +74,7 @@ export async function getHistory() {
} }
export function saveHosts(dict) { export function saveHosts(dict) {
clearTimeout(timer) nextTick(async () => {
timer = setTimeout(() => {
var txt = '' var txt = ''
for (let k in dict) { for (let k in dict) {
for (let it of dict[k]) { for (let it of dict[k]) {
@ -89,8 +88,12 @@ export function saveHosts(dict) {
} }
txt += '\n' txt += '\n'
} }
console.log(txt)
// native.fs.write(HOST_FILE, JSON.stringify(dict)) try {
// native.fs.write('/etc/hosts', txt) await native.fs.write(HOST_FILE, JSON.stringify(dict))
}, 500) await native.fs.write('/etc/hosts', txt)
} catch (err) {
layer.alert(err)
}
})
} }