完成重构

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 we.gtk3 import WebEngine, create_setting, create_hmr_server
# from webengine.gtk3 import WebEngine, create_setting, create_hmr_server
from window import Window
from webengine.gtk3 import WebEngine, create_setting, create_hmr_server
from _window import Window
APP_ID = 'fun.wkit.hosts-switch'
__dir__ = os.path.dirname(os.path.realpath(__file__))
@ -26,12 +25,12 @@ class Application(Gtk.Application):
self.window = Window()
web = WebEngine(self.window)
setting = create_setting({"devtools": True})
hmr = create_hmr_server()
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.load()
@ -61,5 +60,4 @@ if __name__ == "__main__":
app.run(sys.argv)
except Exception as err:
print(err)
pass

View File

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

View File

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

View File

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

View File

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

View File

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