- 删除 AksrtFrontend registry/rehydrate 双路架构及 build/public.js 拼包入口,
前台唯一初始路径为 PHP 本地栈(jQuery 1.9.1 → Bootstrap 3.4.1)
→ loader_js(_win/aksrtData 唯一注入)→ tbquire(['main']) → runtime/main.min.js
- window.auto_fun = debounce(z_auto_fun, 100) 为唯一重扫入口;所有模块改为
执行一次 + off/on('auto_fun.aksrt-*') 命名空间重扫,data/class 标记幂等
- 局部更新协议统一:HTML 注入 → post_ajax.ed → auto_fun();
远程 modal → loaded.bs.modal → auto_fun();JSON 成功 → zib_ajax.success
- 灯箱恢复 Miniimgbox/imgbox 唯一 owner(imgbox-container、a[data-imgbox]、
imgbox_data),删除 AksrtLightbox/data-aksrt-lightbox 第二实现及 CSS/overlay,
_win.imgbox* 重新接上 post-display 设置
- 死代码清理:runtime 下 11 组被自有实现替代的旧模块副本、无消费 vendored 库、
顶层 grade.min.js、revenue.js、无调用方的 aksrt_get_win_runtime_config(),
以及 loader 中 16 条死 mapping
- PHP 入队改为本地栈 + loader_js,routed/widget/submit 依赖统一换绑;
用户卡片发布按钮恢复 nav_user_newposts_btn 设置门控
- 测试对齐:新增 frontend-lifecycle-contract,重写 navigation/lazyload/toast/
auth-source/comment/page-display/post-visual 等断言为目标架构契约
验证:node --test widget-system+navigation 223 通过 0 失败;
构建脚本重建 main/imgbox min 与 CSS bundles;改动 PHP php -l、JS node --check
全部通过;git diff --check 干净;credits 重入 harness 通过。
223 lines
6.7 KiB
JavaScript
223 lines
6.7 KiB
JavaScript
/**
|
|
* AKSRT Code Block Enhancements
|
|
*
|
|
* Integrates EnlighterJS for syntax highlighting and adds
|
|
* copy-to-clipboard buttons and language labels to code blocks.
|
|
* No jQuery dependency — pure vanilla JS.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
(function () {
|
|
'use strict';
|
|
|
|
var BASE_URI = (window.aksrtData && window.aksrtData.themeUri) || '';
|
|
var STYLE_ID = 'aksrt-enlighter-css';
|
|
var ENLIGHTER_LOADED = false;
|
|
|
|
function getHighlightSettings() {
|
|
return (window.aksrtData && window.aksrtData.codeHighlight) || {};
|
|
}
|
|
|
|
function isDarkTheme(value) {
|
|
if (typeof value === 'boolean') return value;
|
|
return document.documentElement.classList.contains('dark') ||
|
|
document.documentElement.getAttribute('data-theme') === 'dark';
|
|
}
|
|
|
|
function getThemeName(isDark) {
|
|
var settings = getHighlightSettings();
|
|
return isDarkTheme(isDark) ? (settings.darkTheme || 'monokai') : (settings.lightTheme || 'enlighter');
|
|
}
|
|
|
|
function applyEnlighterTheme(isDark) {
|
|
var settings = getHighlightSettings();
|
|
if (settings.enabled === false) return;
|
|
|
|
var theme = getThemeName(isDark);
|
|
document.querySelectorAll('.enlighter-default').forEach(function (el) {
|
|
el.className = el.className.replace(/\benlighter-t-[\w-]+\b/g, '').replace(/\s+/g, ' ').trim();
|
|
el.classList.add('enlighter-t-' + theme);
|
|
});
|
|
}
|
|
|
|
window.AksrtPrecodeApplyTheme = applyEnlighterTheme;
|
|
|
|
// ── Copy Button Styles ──
|
|
|
|
var COPY_STYLE_ID = 'aksrt-precode-style';
|
|
|
|
function injectCopyStyle() {
|
|
if (document.getElementById(COPY_STYLE_ID)) return;
|
|
var style = document.createElement('style');
|
|
style.id = COPY_STYLE_ID;
|
|
style.textContent =
|
|
'.aksrt-code-copy{position:absolute;top:6px;right:8px;border:none;background:rgba(255,255,255,0.08);color:#a6adc8;cursor:pointer;padding:3px 10px;border-radius:6px;font-size:12px;z-index:10;transition:all 0.2s;backdrop-filter:blur(4px);}' +
|
|
'.aksrt-code-copy:hover{background:rgba(255,255,255,0.15);color:#cdd6f4;}' +
|
|
'.aksrt-code-copy.is-copied{color:#a6e3a1;}' +
|
|
'.enlighter-default{position:relative;}' +
|
|
'.enlighter-default .aksrt-code-copy{top:4px;right:4px;}' +
|
|
'pre{position:relative;}' +
|
|
'pre .aksrt-code-copy{top:8px;right:8px;}';
|
|
document.head.appendChild(style);
|
|
}
|
|
|
|
// ── EnlighterJS CSS Injection ──
|
|
|
|
function injectEnlighterCSS() {
|
|
if (document.getElementById(STYLE_ID)) return;
|
|
var link = document.createElement('link');
|
|
link.id = STYLE_ID;
|
|
link.rel = 'stylesheet';
|
|
link.href = BASE_URI + '/assets/js/enlighter/enlighterjs.min.css';
|
|
document.head.appendChild(link);
|
|
}
|
|
|
|
// ── EnlighterJS Loader ──
|
|
|
|
function loadEnlighterJS(callback) {
|
|
if (ENLIGHTER_LOADED && window.EnlighterJS) {
|
|
callback();
|
|
return;
|
|
}
|
|
|
|
injectEnlighterCSS();
|
|
|
|
var script = document.createElement('script');
|
|
script.src = BASE_URI + '/assets/js/enlighter/enlighterjs.min.js';
|
|
script.onload = function () {
|
|
ENLIGHTER_LOADED = true;
|
|
callback();
|
|
};
|
|
script.onerror = function () {
|
|
// Fallback: just add copy buttons without highlighting
|
|
addCopyButtonsPlain();
|
|
};
|
|
document.head.appendChild(script);
|
|
}
|
|
|
|
// ── Copy to Clipboard ──
|
|
|
|
function copyToClipboard(text, btn) {
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
navigator.clipboard.writeText(text).then(function () {
|
|
showCopied(btn);
|
|
});
|
|
} else {
|
|
var ta = document.createElement('textarea');
|
|
ta.value = text;
|
|
ta.style.cssText = 'position:fixed;left:-9999px;';
|
|
document.body.appendChild(ta);
|
|
ta.select();
|
|
try { document.execCommand('copy'); showCopied(btn); } catch (e) { /* ignore */ }
|
|
document.body.removeChild(ta);
|
|
}
|
|
}
|
|
|
|
function showCopied(btn) {
|
|
btn.textContent = '已复制';
|
|
btn.classList.add('is-copied');
|
|
setTimeout(function () {
|
|
btn.textContent = '复制';
|
|
btn.classList.remove('is-copied');
|
|
}, 1500);
|
|
}
|
|
|
|
function createCopyButton(text) {
|
|
var btn = document.createElement('button');
|
|
btn.className = 'aksrt-code-copy';
|
|
btn.textContent = '复制';
|
|
btn.type = 'button';
|
|
btn.addEventListener('click', function () {
|
|
copyToClipboard(text, btn);
|
|
});
|
|
return btn;
|
|
}
|
|
|
|
// ── EnlighterJS Integration ──
|
|
|
|
function initEnlighter() {
|
|
if (!window.EnlighterJS) return;
|
|
|
|
// Get settings from aksrtData or use defaults
|
|
var settings = getHighlightSettings();
|
|
if (settings.enabled === false) return; // 后台关闭代码高亮
|
|
|
|
var lineNumbers = settings.lineNumbers === true;
|
|
var showButtons = settings.showButtons !== false;
|
|
var maxHeight = settings.maxHeight || 400;
|
|
|
|
var currentTheme = getThemeName();
|
|
|
|
// Find all code blocks that haven't been processed
|
|
var blocks = document.querySelectorAll('pre > code:not(.enlighter-origin)');
|
|
if (blocks.length === 0) return;
|
|
|
|
// Process each block
|
|
blocks.forEach(function (code) {
|
|
var pre = code.parentElement;
|
|
if (!pre || pre.dataset.aksrtEnhanced) return;
|
|
|
|
// Get raw text before EnlighterJS processes it
|
|
var rawText = code.textContent;
|
|
|
|
// Apply EnlighterJS
|
|
try {
|
|
EnlighterJS.enlight(code, {
|
|
theme: currentTheme,
|
|
linenumbers: lineNumbers,
|
|
indent: 2,
|
|
textOverflow: 'scroll',
|
|
rawcodeDbclick: true,
|
|
rawButton: showButtons,
|
|
infoButton: showButtons,
|
|
windowButton: showButtons
|
|
});
|
|
} catch (e) {
|
|
// If EnlighterJS fails, keep the plain block
|
|
}
|
|
|
|
// Add copy button after EnlighterJS processing
|
|
var container = pre.querySelector('.enlighter-default') || pre;
|
|
container.appendChild(createCopyButton(rawText));
|
|
pre.dataset.aksrtEnhanced = '1';
|
|
|
|
// Apply max-height
|
|
if (maxHeight > 0) {
|
|
pre.style.maxHeight = maxHeight + 'px';
|
|
pre.style.overflow = 'auto';
|
|
}
|
|
});
|
|
|
|
applyEnlighterTheme();
|
|
}
|
|
|
|
// ── Fallback: Plain code blocks with copy buttons ──
|
|
|
|
function addCopyButtonsPlain() {
|
|
var blocks = document.querySelectorAll('pre > code');
|
|
blocks.forEach(function (code) {
|
|
var pre = code.parentElement;
|
|
if (!pre || pre.dataset.aksrtEnhanced) return;
|
|
pre.style.position = 'relative';
|
|
pre.appendChild(createCopyButton(code.textContent));
|
|
pre.dataset.aksrtEnhanced = '1';
|
|
});
|
|
}
|
|
|
|
// ── Main Init ──
|
|
|
|
function init() {
|
|
injectCopyStyle();
|
|
|
|
// Check if there are any code blocks on the page
|
|
var blocks = document.querySelectorAll('pre > code');
|
|
if (blocks.length === 0) return;
|
|
|
|
// Load EnlighterJS and initialize
|
|
loadEnlighterJS(initEnlighter);
|
|
}
|
|
|
|
jQuery(document).off('auto_fun.aksrt-precode').on('auto_fun.aksrt-precode', function () { init(document); });
|
|
})();
|