Optimize loading speed (#45)
This commit is contained in:
1 parent
a18db299ee
commit
a0bcce5cf1
9 files changed
+92
-33
No files matched your search
@@ -5,9 +5,14 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue';
|
||||
import VueMarkdown from './VueMarkdown.vue';
|
||||
import { highlight } from '@/utils';
|
||||
import 'highlight.js/styles/night-owl.css';
|
||||
|
||||
export default defineComponent({
|
||||
name: 'ApiCode',
|
||||
directives: {
|
||||
highlight
|
||||
},
|
||||
components: {
|
||||
VueMarkdown
|
||||
},
|
||||
|
||||
+6
-3
@@ -84,9 +84,12 @@ export const loadLocaleMessages = async (i18n: I18n, locale: string) => {
|
||||
'chatdoc'
|
||||
];
|
||||
|
||||
const promises = names.map((name) => loadLocalResource(name, locale));
|
||||
const resources = await Promise.all(promises);
|
||||
|
||||
const messages: any = {};
|
||||
for (const name of names) {
|
||||
const resource = await loadLocalResource(name, locale);
|
||||
names.forEach((name, index) => {
|
||||
const resource = resources[index];
|
||||
console.log('resource', resource);
|
||||
for (const key in resource) {
|
||||
if (resource.hasOwnProperty(key)) {
|
||||
@@ -94,7 +97,7 @@ export const loadLocaleMessages = async (i18n: I18n, locale: string) => {
|
||||
messages[`${name}.${key}`] = element.message;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// set locale and locale message
|
||||
i18n.global.setLocaleMessage(locale, messages);
|
||||
|
||||
-19
@@ -9,9 +9,6 @@ import 'mac-scrollbar/dist/mac-scrollbar.css';
|
||||
import dayjs from './plugins/dayjs';
|
||||
import './plugins/font-awesome';
|
||||
import { vLoading } from 'element-plus';
|
||||
import hl from 'highlight.js';
|
||||
import 'highlight.js/styles/night-owl.css';
|
||||
import copyToClipboard from 'copy-to-clipboard';
|
||||
import { initializeCookies, initializeFavicon, initializeTitle } from './utils/initializer';
|
||||
import config from './plugins/config';
|
||||
|
||||
@@ -33,22 +30,6 @@ const main = async () => {
|
||||
app.mount('#app');
|
||||
console.debug('app mounted');
|
||||
|
||||
app.directive('highlight', async (el) => {
|
||||
const blocks = el.querySelectorAll('pre code');
|
||||
blocks.forEach((block: HTMLPreElement) => {
|
||||
// create the copy button
|
||||
const copy = document.createElement('button');
|
||||
copy.innerHTML = i18n.global.t('common.button.copy').toString();
|
||||
// add the event listener to each click
|
||||
copy.addEventListener('click', () => {
|
||||
copyToClipboard(block.innerText);
|
||||
});
|
||||
// append the copy button to each code block
|
||||
block.parentElement?.prepend(copy);
|
||||
hl.highlightBlock(block);
|
||||
});
|
||||
});
|
||||
|
||||
// make app available globally
|
||||
// @ts-ignore
|
||||
window.app = app;
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import i18n from '@/i18n';
|
||||
import copyToClipboard from 'copy-to-clipboard';
|
||||
|
||||
export const highlight = async (el: HTMLElement) => {
|
||||
const hl = (await import('highlight.js')).default;
|
||||
console.debug('highlight.js loaded');
|
||||
const blocks = el.querySelectorAll('pre code');
|
||||
blocks.forEach((block) => {
|
||||
// create the copy button
|
||||
const copy = document.createElement('button');
|
||||
copy.innerHTML = i18n.global.t('common.button.copy').toString();
|
||||
// add the event listener to each click
|
||||
copy.addEventListener('click', () => {
|
||||
// @ts-ignore
|
||||
copyToClipboard(block.innerText);
|
||||
});
|
||||
// append the copy button to each code block
|
||||
// check already has copy button
|
||||
if (!block.parentElement?.querySelector('button')) {
|
||||
block.parentElement?.prepend(copy);
|
||||
}
|
||||
// @ts-ignore
|
||||
hl.highlightBlock(block);
|
||||
});
|
||||
};
|
||||
@@ -1,3 +1,6 @@
|
||||
export * from './baseUrl';
|
||||
export * from './mode';
|
||||
export * from './log';
|
||||
export * from './initializer';
|
||||
export * from './highlight';
|
||||
export * from './is';
|
||||
Reference in new issue
Block a user