Files
wordpress-theme-aksrt/assets/js/page-edit.js
T
kate522 1f4a39d8f8 feat(runtime): 全站前端脚本统一接入 AksrtFrontend 全局 DOM 生命周期
- rehydrate.js 新增 boot()/aksrt:ready 事件,支持 boot 后晚注册立即以 init 触发
- aksrt-runtime.js 初始加载改走 AksrtFrontend.boot,tooltip/倒计时注册为组件
- 约 30 个前端脚本(首页、评论、支付、路由、页面叶子、widgets)
  从直接 DOMContentLoaded 迁移为 AksrtFrontend.register,并加幂等标记
- section-navs 依赖改为 aksrt-runtime,保证注册时序
- 清理死代码:删除孤儿 loader.js/main.js 及 runtime/page-navs 遗留副本
- loader.js 移除已迁移模块的 tbquire 路径映射,main.js 相应瘦身
- build-public-assets.mjs 增加 runtime/main.min.js 自动压缩步骤
- 新增 4 个契约测试(homepage-dom/rehydrate-runtime/global-dom-init/
  public-bundle-parity),3 个既有 harness 适配注册表初始化方式
2026-09-02 21:48:56 +08:00

205 lines
5.6 KiB
JavaScript

/**
* AKSRT Page Edit — 前台编辑增强
*
* 提供前台编辑页面的增强功能:
* - 自动保存草稿
* - 标签建议
* - 快捷键支持
*
* @package AKSRT
*/
(function () {
'use strict';
var DRAFT_KEY_PREFIX = 'aksrt_draft_';
/**
* 初始化页面编辑增强
*
* @param {Object} options 配置项
*/
function PageEdit(options) {
this.options = Object.assign({
form: null,
formSelector: 'form[data-aksrt-edit]',
titleSelector: 'input[name="title"], input[name="topic_title"]',
contentSelector: 'textarea[name="content"], textarea[name="topic_content"]',
tagsSelector: 'input[name="tags"], input[name="topic_tags"]',
autoSaveInterval: 30000, // 30秒
draftKey: '',
}, options || {});
this.form = this.options.form || document.querySelector(this.options.formSelector);
if (!this.form) return;
this.autoSaveTimer = null;
this.lastSavedContent = '';
this.init();
}
PageEdit.prototype.init = function () {
var self = this;
this.titleInput = this.form.querySelector(this.options.titleSelector);
this.contentTextarea = this.form.querySelector(this.options.contentSelector);
this.tagsInput = this.form.querySelector(this.options.tagsSelector);
// 自动保存草稿
this.initAutoSave();
// 快捷键
this.initShortcuts();
// 标签输入增强
this.initTagInput();
// 表单提交时清除草稿
this.form.addEventListener('submit', function () {
self.clearDraft();
});
// 页面离开提醒
window.addEventListener('beforeunload', function (e) {
if (self.hasUnsavedChanges()) {
e.preventDefault();
e.returnValue = '您有未保存的修改,确定要离开吗?';
}
});
};
PageEdit.prototype.initAutoSave = function () {
var self = this;
if (!this.options.draftKey) {
// 从页面 URL 生成 draft key
this.options.draftKey = DRAFT_KEY_PREFIX + (window.location.pathname.replace(/\//g, '_') || 'page');
}
// 恢复草稿
this.restoreDraft();
// 定时自动保存
if (this.options.autoSaveInterval > 0) {
this.autoSaveTimer = setInterval(function () {
self.saveDraft();
}, this.options.autoSaveInterval);
}
};
PageEdit.prototype.saveDraft = function () {
if (!this.form) return;
var data = {};
if (this.titleInput) data.title = this.titleInput.value;
if (this.contentTextarea) data.content = this.contentTextarea.value;
if (this.tagsInput) data.tags = this.tagsInput.value;
var content = JSON.stringify(data);
if (content === this.lastSavedContent) return;
try {
localStorage.setItem(this.options.draftKey, content);
this.lastSavedContent = content;
// 显示保存提示
var toast = document.getElementById('aksrt-draft-toast');
if (!toast) {
toast = document.createElement('div');
toast.id = 'aksrt-draft-toast';
toast.className = 'aksrt-page-edit__toast';
document.body.appendChild(toast);
}
toast.textContent = '草稿已自动保存';
toast.classList.add('aksrt-page-edit__toast--visible');
setTimeout(function () {
toast.classList.remove('aksrt-page-edit__toast--visible');
}, 2000);
} catch (e) {
console.error('[AKSRT PageEdit] 保存草稿失败:', e);
}
};
PageEdit.prototype.restoreDraft = function () {
try {
var saved = localStorage.getItem(this.options.draftKey);
if (!saved) return;
var data = JSON.parse(saved);
var hasContent = false;
if (this.titleInput && data.title && !this.titleInput.value) {
this.titleInput.value = data.title;
hasContent = true;
}
if (this.contentTextarea && data.content && !this.contentTextarea.value) {
this.contentTextarea.value = data.content;
hasContent = true;
}
if (this.tagsInput && data.tags && !this.tagsInput.value) {
this.tagsInput.value = data.tags;
hasContent = true;
}
if (hasContent) {
this.lastSavedContent = saved;
}
} catch (e) {
console.error('[AKSRT PageEdit] 恢复草稿失败:', e);
}
};
PageEdit.prototype.clearDraft = function () {
try {
localStorage.removeItem(this.options.draftKey);
} catch (e) {}
};
PageEdit.prototype.hasUnsavedChanges = function () {
if (!this.form) return false;
var data = {};
if (this.titleInput) data.title = this.titleInput.value;
if (this.contentTextarea) data.content = this.contentTextarea.value;
if (this.tagsInput) data.tags = this.tagsInput.value;
return JSON.stringify(data) !== this.lastSavedContent;
};
PageEdit.prototype.initShortcuts = function () {
var self = this;
this.form.addEventListener('keydown', function (e) {
// Ctrl+S / Cmd+S 保存草稿
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault();
self.saveDraft();
}
});
};
PageEdit.prototype.initTagInput = function () {
if (!this.tagsInput) return;
// 回车键添加标签
this.tagsInput.addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
e.preventDefault();
}
});
};
// 自动初始化
function autoInit(root) {
var form = root && root.nodeType === 1 && root.matches('form[data-aksrt-edit]')
? root : (root || document).querySelector('form[data-aksrt-edit]');
if (form) {
new PageEdit({ form: form });
}
}
window.AksrtFrontend.register('aksrt-page-edit', autoInit);
window.AksrtPageEdit = PageEdit;
})();