Skip to content

Commit d2c8201

Browse files
committed
refactor(composable): ♻️重构暗色模式
增加useDark 和 useUtools
1 parent 123fdbc commit d2c8201

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

src/App.vue

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99
</template>
1010
<script setup lang="ts">
1111
import { darkTheme } from "naive-ui";
12-
import usePluginEnter from "./composables/usePluginEnter";
13-
const isDark = ref(false);
12+
const isDark = useDark()
1413
const theme = computed<typeof darkTheme | null>(() => {
1514
return isDark.value ? darkTheme : null
1615
})
17-
usePluginEnter(()=> {
18-
isDark.value = window?.utools.isDarkColors()
19-
})
16+
2017
</script>

src/components/home.vue

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@
6464
</template>
6565
<script setup lang="ts">
6666
import { useMessage} from "naive-ui";
67-
import usePluginEnter from "src/composables/usePluginEnter";
67+
import useUtools from "../composeables/useUtools";
6868
const message = useMessage();
69+
const utools = useUtools()
6970
7071
const { ctrl_alt } = useMagicKeys();
7172
@@ -82,7 +83,7 @@ const handleCopy = async () => {
8283
await copy(content);
8384
message.success("复制成功");
8485
nextTick(() => {
85-
utools.value?.hideMainWindow();
86+
utools?.hideMainWindow();
8687
});
8788
};
8889
// https://github.com/conventional-changelog/commitlint/blob/master/%40commitlint/config-conventional/index.js
@@ -187,11 +188,6 @@ const handleClear = () => {
187188
body.value = "";
188189
type.value = typeOptions.value[0].value;
189190
};
190-
const utools = ref<UToolsApi | null>(null);
191-
192-
usePluginEnter(() => {
193-
utools.value = window?.utools
194-
})
195191
196192
</script>
197193
<style scoped>

src/composeables/useDark.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Ref } from "vue";
2+
import usePluginEnter from "./usePluginEnter";
3+
4+
export default function useDark(): Ref<boolean> {
5+
const isDark = ref<boolean>(false);
6+
usePluginEnter(() => {
7+
isDark.value = window?.utools.isDarkColors();
8+
});
9+
return isDark;
10+
}

src/composeables/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+
}

src/composeables/useUtools.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import usePluginEnter from "./usePluginEnter";
2+
3+
export default function useUtools(){
4+
let tools = reactive<UToolsApi>(window?.utools);
5+
usePluginEnter(() => {
6+
tools = window!.utools;
7+
});
8+
return tools;
9+
}
10+

0 commit comments

Comments
 (0)