简单测试 && 完善 && 整合 eruda

This commit is contained in:
Shikong 2023-07-08 22:59:16 +08:00
parent ae71d31c02
commit fce779dfb6
17 changed files with 287 additions and 72 deletions

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<file url="file://$PROJECT_DIR$" libraries="{Node.js Core}" />
</component>
</project>

View File

@ -17,6 +17,7 @@
"dependencies": { "dependencies": {
"@vueuse/core": "^10.2.1", "@vueuse/core": "^10.2.1",
"element-plus": "^2.3.7", "element-plus": "^2.3.7",
"eruda": "^3.0.0",
"pinia": "^2.1.4", "pinia": "^2.1.4",
"vue": "^3.3.4", "vue": "^3.3.4",
"vue-router": "^4.2.2" "vue-router": "^4.2.2"

View File

@ -1 +1 @@
db4d8f94ebd4b8a52b1af9220d2d1d60 f642d4d4c90aca4792a079e99b5747bd

View File

@ -1,74 +1,16 @@
<script setup>
import {Greet} from '../wailsjs/go/main/App';
import {onMounted, reactive} from "vue";
import {useWebNotification} from '@vueuse/core'
const controller = reactive({
input: ""
})
function greet() {
let resultElement = document.getElementById("result");
// Get name
let name = controller.input
// Check if the input is empty
if (name === "") return;
// Call App.Greet(name)
try {
Greet(name)
.then((result) => {
// Update result with data back from App.Greet()
resultElement.innerText = result;
})
.catch((err) => {
console.error(err);
});
} catch (err) {
console.error(err);
}
}
function sendNotify() {
Greet(controller.input)
.then((result) => {
/**
*
* @type {UseWebNotificationOptions}
*/
const options = {
title: '通知测试',
dir: 'auto',
body: result,
lang: 'zh-CN',
renotify: true,
tag: 'notify',
};
const notification = useWebNotification(options);
setTimeout(()=>{
notification.close()
}, 5 * 1000)
notification.show()
})
.catch((err) => {
console.error(err);
});
}
</script>
<template> <template>
<div class="result w-full text-center" id="result">Please enter your name below 👇</div> <router-view/>
<div class="input-box w-full text-center" id="input">
<el-input v-model="controller.input" type="text" autocomplete="off"></el-input>
<el-button class="btn" @click="greet()">Greet</el-button>
<el-button class="btn" @click="sendNotify()">通知</el-button>
</div>
</template> </template>
<style scoped> <script setup>
import {onBeforeUnmount, onMounted} from "vue";
import {createDebugger, destroyDebugger} from "src/utils/debugger/eruda";
</style> onMounted(()=>{
createDebugger()
})
onBeforeUnmount(()=>{
destroyDebugger()
})
</script>

View File

@ -3,8 +3,10 @@ import { createApp } from 'vue'
import ElementPlus from 'element-plus' import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css' import 'element-plus/dist/index.css'
import App from './App.vue' import App from './App.vue'
import Router from "frontend/src/router/router";
const app = createApp(App) const app = createApp(App)
app.use(Router)
app.use(ElementPlus) app.use(ElementPlus)
app.mount('#app') app.mount('#app')

View File

@ -0,0 +1,18 @@
import * as VueRouter from "vue-router"
/**
*
* @type {Readonly<VueRouter.RouteRecordRaw[]>}
*/
const routes = [
{
path: '/',
component: ()=>import("frontend/src/views/Home.vue"),
}
]
const Router = VueRouter.createRouter({
history: VueRouter.createWebHistory(),
routes
})
export default Router

View File

@ -0,0 +1,44 @@
import eruda from 'eruda'
let isCreated = false
export function showDebugger(){
if(!isCreated){
return
}
eruda.show()
}
export function hideDebugger(){
if(!isCreated){
return
}
eruda.hide()
}
export function createDebugger(){
if(!isCreated){
isCreated = true
eruda.init({
useShadowDom: true,
autoScale: true,
defaults: {
displaySize: 50,
transparency: 1,
theme: 'Atom One Light'
}
})
} else {
eruda.show()
}
}
export function destroyDebugger(){
if(!isCreated){
return
}
eruda.destroy()
isCreated = false
}

View File

@ -0,0 +1,88 @@
<script setup>
import {Greet} from 'frontend/wailsjs/go/main/App';
import {reactive} from "vue";
import {useWebNotification} from '@vueuse/core'
import {GetAllEnv} from "frontend/wailsjs/go/env/Env";
import {hideDebugger, showDebugger} from "src/utils/debugger/eruda";
const controller = reactive({
input: ""
})
function greet() {
let resultElement = document.getElementById("result");
// Get name
let name = controller.input
// Check if the input is empty
if (name === "") return;
// Call App.Greet(name)
try {
Greet(name)
.then((result) => {
// Update result with data back from App.Greet()
resultElement.innerText = result;
})
.catch((err) => {
console.error(err);
});
} catch (err) {
console.error(err);
}
}
function sendNotify() {
Greet(controller.input)
.then((result) => {
/**
*
* @type {UseWebNotificationOptions}
*/
const options = {
title: '通知测试',
dir: 'auto',
body: result,
lang: 'zh-CN',
renotify: true,
tag: 'notify',
};
const notification = useWebNotification(options);
setTimeout(()=>{
notification.close()
}, 5 * 1000)
notification.show()
})
.catch((err) => {
console.error(err);
});
}
function getAllEnv(){
GetAllEnv().then(allEnv => {
console.log(allEnv)
})
}
</script>
<template>
<div class="result w-full text-center" id="result">Please enter your name below 👇</div>
<div class="input-box w-full text-center" id="input">
<el-input v-model="controller.input" type="text" autocomplete="off"></el-input>
<el-button class="btn" @click="greet()">Greet</el-button>
<el-button class="btn" @click="sendNotify()">通知</el-button>
</div>
<el-button @click="getAllEnv()">获取所有环境变量</el-button>
<div>
<el-button @click="showDebugger()">显示 debugger</el-button>
<el-button @click="hideDebugger()">隐藏 debugger</el-button>
</div>
</template>
<style scoped>
</style>

View File

@ -0,0 +1,34 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"jsx": "preserve",
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": false,
"esModuleInterop": true,
"lib": [
"ESNext",
"DOM"
],
"baseUrl": ".",
"paths": {
"frontend/*": ["*"],
"src/*": ["src/*"],
}
},
"include": [
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"src/**/*.vue"
],
"references": [
{
"path": "./tsconfig.node.json"
}
]
}

View File

@ -0,0 +1,12 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": [
"wails-plugin.ts",
"vite.config.ts"
]
}

View File

@ -1,8 +1,15 @@
import {defineConfig} from "vite"; import {defineConfig} from "vite";
import vuePlugin from "@vitejs/plugin-vue"; import vuePlugin from "@vitejs/plugin-vue";
import path from "path"
export default defineConfig({ export default defineConfig({
plugins: [ plugins: [
vuePlugin() vuePlugin()
] ],
resolve: {
alias: {
"frontend": path.resolve(__dirname, "./"),
"src": path.resolve(__dirname, "./src")
}
}
}) })

View File

@ -0,0 +1,4 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function GetAllEnv():Promise<{[key: string]: string}>;

View File

@ -0,0 +1,7 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function GetAllEnv() {
return window['go']['env']['Env']['GetAllEnv']();
}

View File

@ -529,6 +529,11 @@ element-plus@^2.3.7:
memoize-one "^6.0.0" memoize-one "^6.0.0"
normalize-wheel-es "^1.2.0" normalize-wheel-es "^1.2.0"
eruda@^3.0.0:
version "3.0.0"
resolved "https://registry.npmmirror.com/eruda/-/eruda-3.0.0.tgz#d54b2c9c727f1604796a82c4b62975d1e9988ef1"
integrity sha512-6L8A8aBHOQv0rqeBlNdJEl/hl6OAdLVRtJlVmBIlIJ6Fe1a92HFXO58jHLC0vFyuKV0deTjYjRaWwLo9lJ9K9A==
esbuild@^0.17.5: esbuild@^0.17.5:
version "0.17.19" version "0.17.19"
resolved "https://registry.npmmirror.com/esbuild/-/esbuild-0.17.19.tgz#087a727e98299f0462a3d0bcdd9cd7ff100bd955" resolved "https://registry.npmmirror.com/esbuild/-/esbuild-0.17.19.tgz#087a727e98299f0462a3d0bcdd9cd7ff100bd955"

25
app/wails/lib/env/env.go vendored Normal file
View File

@ -0,0 +1,25 @@
package env
import (
"os"
"strings"
)
type Env struct {
}
func (e *Env) GetAllEnv() map[string]string {
m := make(map[string]string, 0)
for _, e := range os.Environ() {
envs := strings.SplitN(e, "=", 2)
if len(envs) != 2 {
m[envs[0]] = ""
} else {
m[envs[0]] = envs[1]
}
}
return m
}

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/gen2brain/beeep" "github.com/gen2brain/beeep"
"os"
"os/exec" "os/exec"
"strings" "strings"
"testing" "testing"
@ -39,3 +40,20 @@ func TestBeeepNotify(t *testing.T) {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }
func TestEnv(t *testing.T) {
for _, e := range os.Environ() {
parts := strings.SplitN(e, "=", 2)
if len(parts) != 2 {
continue
} else {
println(parts[0], parts[1])
}
}
}
func TestJavaEnv(t *testing.T) {
fmt.Println(os.Getenv("java_home"))
fmt.Println(os.Getenv("JAVA_HOME"))
}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"changeme/lib/env"
"embed" "embed"
"github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2"
@ -27,6 +28,7 @@ func main() {
OnStartup: app.startup, OnStartup: app.startup,
Bind: []interface{}{ Bind: []interface{}{
app, app,
&env.Env{},
}, },
Debug: options.Debug{ Debug: options.Debug{
OpenInspectorOnStartup: true, OpenInspectorOnStartup: true,