Repository navigation
Expand file tree
/
Copy pathApp.vue
More file actions
156 lines (134 loc) · 3.85 KB
/
Copy pathApp.vue
File metadata and controls
156 lines (134 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<template>
<div v-if="loading" class="app-loading">
<t-loading :loading="true" size="large" text="加载中..." />
</div>
<template v-else>
<titleBar v-if="isElectron" />
<t-config-provider :global-config="globalConfig">
<router-view></router-view>
</t-config-provider>
</template>
</template>
<script setup lang="ts">
import settingStore from "@/stores/setting";
import { merge } from "lodash";
import zhConfig from "tdesign-vue-next/es/locale/zh_CN";
import enConfig from "tdesign-vue-next/es/locale/en_US";
import { cachedLocale, languageList } from "@/locales";
import { initTheme } from "@/utils/theme";
import { type GlobalConfigProvider } from "tdesign-vue-next";
import { useI18n } from "vue-i18n";
const { locale } = useI18n();
const { baseUrl, isElectron } = storeToRefs(settingStore());
import { config } from "md-editor-v3";
const loading = ref(true);
watch(
() => isElectron.value,
(newVal) => {
if (newVal) {
document.body.classList.add("is-electron");
} else {
document.body.classList.remove("is-electron");
}
},
{ immediate: true },
);
onBeforeMount(() => {
document.addEventListener("keydown", function (event) {
if (event.key === "F8") {
event.preventDefault();
debugger;
}
});
});
// 初始化主题
onMounted(async () => {
getPort();
});
async function handleLinkClick(event: MouseEvent) {
event.preventDefault();
event.stopPropagation();
const target = event.currentTarget as HTMLAnchorElement | null;
const url = target?.getAttribute("data-link") || target?.getAttribute("href");
if (!url) return false;
if (isElectron.value) {
await fetch(`toonflow://openurlwithbrowser?url=${encodeURIComponent(url)}`);
} else {
window.open(url, "_blank", "noopener,noreferrer");
}
return false;
}
onMounted(() => {
(window as any).handleLinkClick = handleLinkClick;
});
async function getPort() {
await nextTick();
await nextTick();
await nextTick();
await nextTick();
try {
const res = await fetch("toonflow://getAppUrl");
const data = await res.json();
if (data?.url) {
baseUrl.value = data.url;
isElectron.value = true;
}
} catch (error) {}
loading.value = false;
config({
markdownItConfig(md) {
// 自定义链接渲染
const defaultRender =
md.renderer.rules.link_open ||
function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
const token = tokens[idx];
const href = token.attrGet("href");
if (href) {
// 添加 target="_blank" 在新窗口打开
token.attrSet("target", "_blank");
token.attrSet("rel", "noopener noreferrer");
// 或者添加自定义点击事件的标识
token.attrSet("data-link", href);
token.attrSet("onclick", "return handleLinkClick(event)");
}
return defaultRender(tokens, idx, options, env, self);
};
},
});
try {
const language = navigator.language;
if (language && languageList.some((item) => item.value === language)) {
cachedLocale.value = language;
locale.value = language;
}
} catch (e) {
console.error("获取语言失败", e);
}
}
const tdesignLocaleMap: Record<string, object> = {
"zh-CN": zhConfig,
en: enConfig,
};
const customConfig: GlobalConfigProvider = {
calendar: {},
table: {},
pagination: {},
};
const globalConfig = computed<GlobalConfigProvider>(() => merge({}, tdesignLocaleMap[cachedLocale.value] || zhConfig, customConfig));
onBeforeMount(() => {
initTheme();
});
</script>
<style lang="scss">
.app-loading {
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
background: var(--td-bg-color-page, #f5f5f5);
}
</style>