Skip to content

Commit 123fdbc

Browse files
committed
feat(usePluginEnter): ✨增加usePluginEnter
1 parent 3d3532a commit 123fdbc

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

src/App.vue

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@
88
<NButton @click="theme = null">浅色</NButton> -->
99
</template>
1010
<script setup lang="ts">
11-
import { onMounted, computed } from "vue";
1211
import { darkTheme } from "naive-ui";
13-
import { BuiltInGlobalTheme } from "naive-ui/lib/themes/interface";
12+
import usePluginEnter from "./composables/usePluginEnter";
1413
const isDark = ref(false);
15-
const theme = computed<BuiltInGlobalTheme | null>(() => {
14+
const theme = computed<typeof darkTheme | null>(() => {
1615
return isDark.value ? darkTheme : null
1716
})
18-
let utools = null;
19-
onMounted(() => {
20-
if (window["utools"]) {
21-
utools = window["utools"];
22-
utools.onPluginEnter(() => {
23-
isDark.value = utools.isDarkColors();
24-
});
25-
}
17+
usePluginEnter(()=> {
18+
isDark.value = window?.utools.isDarkColors()
2619
})
2720
</script>

src/components/home.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
</template>
6565
<script setup lang="ts">
6666
import { useMessage} from "naive-ui";
67+
import usePluginEnter from "src/composables/usePluginEnter";
6768
const message = useMessage();
6869
6970
const { ctrl_alt } = useMagicKeys();
@@ -81,7 +82,7 @@ const handleCopy = async () => {
8182
await copy(content);
8283
message.success("复制成功");
8384
nextTick(() => {
84-
utools && utools.hideMainWindow();
85+
utools.value?.hideMainWindow();
8586
});
8687
};
8788
// https://github.com/conventional-changelog/commitlint/blob/master/%40commitlint/config-conventional/index.js
@@ -186,12 +187,11 @@ const handleClear = () => {
186187
body.value = "";
187188
type.value = typeOptions.value[0].value;
188189
};
189-
let utools = null;
190-
onMounted(() => {
191-
if (window["utools"]) {
192-
utools = window["utools"];
193-
}
194-
});
190+
const utools = ref<UToolsApi | null>(null);
191+
192+
usePluginEnter(() => {
193+
utools.value = window?.utools
194+
})
195195
196196
</script>
197197
<style scoped>

src/composables/usePluginEnter.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface IPluginEnterAction {
2+
code: string;
3+
type: string;
4+
payload: any;
5+
}
6+
7+
export default function usePluginEnter(
8+
hook: (action: IPluginEnterAction) => unknown
9+
) {
10+
onMounted(() => {
11+
window?.utools.onPluginEnter((action) => {
12+
return hook(action);
13+
});
14+
});
15+
}

0 commit comments

Comments
 (0)