mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: 优化资源加载
This commit is contained in:
1 parent
bc74870a39
commit
167c70c2b1
24 files changed
+167
-188
No files matched your search
@@ -5,37 +5,38 @@ import tpl from 'vj/utils/tpl';
|
||||
import i18n from 'vj/utils/i18n';
|
||||
|
||||
const highlighterPage = new AutoloadPage('highlighterPage', async () => {
|
||||
const [{ default: prismjs }, [success, format]] = await Promise.all([
|
||||
Promise.all([
|
||||
import('./prismjs'),
|
||||
UserContext.formatCode ? load() : [true, null],
|
||||
]);
|
||||
if (!success) Notification.error(`Astyle load fail: ${format}`);
|
||||
function runHighlight($container) {
|
||||
prismjs.highlightBlocks($container, success ? format : null);
|
||||
$container.find('pre code').get().forEach((code) => {
|
||||
const $code = $(code);
|
||||
const $root = $code.parent().parent();
|
||||
const language = ($(code).attr('class') || '').trim();
|
||||
const m = language.match(/language-([a-z0-9]+)/);
|
||||
if (m && m[1] && m[1].toLowerCase().startsWith('input')) {
|
||||
const id = +m[1].substr(5, m[1].length - 5);
|
||||
if (Number.isSafeInteger(id)) {
|
||||
const $output = $container.find(`pre.language-output${id}`);
|
||||
if ($output.length) {
|
||||
const $c = $(document.createElement('div')).addClass('row');
|
||||
$root.after($c);
|
||||
$root.prepend($(tpl`<h2>${i18n('Sample Input')} ${id}</h2>`))
|
||||
.addClass('medium-6 columns sample').appendTo($c);
|
||||
$output.parent().prepend($(tpl`<h2>${i18n('Sample Output')} ${id}</h2>`))
|
||||
.addClass('medium-6 columns sample').appendTo($c);
|
||||
]).then(([{ default: prismjs }, [success, format]]) => {
|
||||
if (!success) Notification.error(`Astyle load fail: ${format}`);
|
||||
function runHighlight($container) {
|
||||
prismjs.highlightBlocks($container, success ? format : null);
|
||||
$container.find('pre code').get().forEach((code) => {
|
||||
const $code = $(code);
|
||||
const $root = $code.parent().parent();
|
||||
const language = ($(code).attr('class') || '').trim();
|
||||
const m = language.match(/language-([a-z0-9]+)/);
|
||||
if (m && m[1] && m[1].toLowerCase().startsWith('input')) {
|
||||
const id = +m[1].substr(5, m[1].length - 5);
|
||||
if (Number.isSafeInteger(id)) {
|
||||
const $output = $container.find(`pre.language-output${id}`);
|
||||
if ($output.length) {
|
||||
const $c = $(document.createElement('div')).addClass('row');
|
||||
$root.after($c);
|
||||
$root.prepend($(tpl`<h2>${i18n('Sample Input')} ${id}</h2>`))
|
||||
.addClass('medium-6 columns sample').appendTo($c);
|
||||
$output.parent().prepend($(tpl`<h2>${i18n('Sample Output')} ${id}</h2>`))
|
||||
.addClass('medium-6 columns sample').appendTo($c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
runHighlight($('body'));
|
||||
$(document).on('vjContentNew', (e) => runHighlight($(e.target)));
|
||||
runHighlight($('body'));
|
||||
$(document).on('vjContentNew', (e) => runHighlight($(e.target)));
|
||||
});
|
||||
});
|
||||
|
||||
export default highlighterPage;
|
||||
Reference in new issue
Block a user