进行了一些小修复
This commit is contained in:
@@ -1921,6 +1921,74 @@ tbquire.config({
|
||||
});
|
||||
}
|
||||
|
||||
var postMetaDocumentEventsBound = false;
|
||||
|
||||
function setPostMetaMenuState(menuRoot, open) {
|
||||
var toggle = menuRoot.querySelector(".post-drop-meta__toggle");
|
||||
var menu = menuRoot.querySelector(".post-drop-meta__menu");
|
||||
if (!toggle || !menu) return;
|
||||
|
||||
menuRoot.classList.toggle("is-open", open);
|
||||
toggle.setAttribute("aria-expanded", open ? "true" : "false");
|
||||
menu.setAttribute("aria-hidden", open ? "false" : "true");
|
||||
}
|
||||
|
||||
function closePostMetaMenus(except, restoreFocus) {
|
||||
document.querySelectorAll(".post-drop-meta.is-open").forEach(function (menuRoot) {
|
||||
if (menuRoot === except) return;
|
||||
var toggle = menuRoot.querySelector(".post-drop-meta__toggle");
|
||||
setPostMetaMenuState(menuRoot, false);
|
||||
if (restoreFocus && toggle) toggle.focus();
|
||||
});
|
||||
}
|
||||
|
||||
function initPostMetaMenus(root) {
|
||||
var scope = root || document;
|
||||
var menuRoots = [];
|
||||
|
||||
if (scope.nodeType === 1 && scope.matches(".post-drop-meta")) {
|
||||
menuRoots.push(scope);
|
||||
}
|
||||
scope.querySelectorAll(".post-drop-meta").forEach(function (menuRoot) {
|
||||
menuRoots.push(menuRoot);
|
||||
});
|
||||
|
||||
menuRoots.forEach(function (menuRoot) {
|
||||
var toggle = menuRoot.querySelector(".post-drop-meta__toggle");
|
||||
var menu = menuRoot.querySelector(".post-drop-meta__menu");
|
||||
if (!toggle || !menu || toggle.dataset.postMetaMenuBound) return;
|
||||
|
||||
toggle.dataset.postMetaMenuBound = "1";
|
||||
setPostMetaMenuState(menuRoot, false);
|
||||
|
||||
toggle.addEventListener("click", function () {
|
||||
var open = !menuRoot.classList.contains("is-open");
|
||||
closePostMetaMenus(menuRoot, false);
|
||||
setPostMetaMenuState(menuRoot, open);
|
||||
});
|
||||
|
||||
menu.addEventListener("click", function (event) {
|
||||
if (event.target.closest(".post-drop-meta__item")) {
|
||||
setPostMetaMenuState(menuRoot, false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (menuRoots.length && !postMetaDocumentEventsBound) {
|
||||
postMetaDocumentEventsBound = true;
|
||||
document.addEventListener("click", function (event) {
|
||||
if (!event.target.closest(".post-drop-meta")) {
|
||||
closePostMetaMenus(null, false);
|
||||
}
|
||||
});
|
||||
document.addEventListener("keydown", function (event) {
|
||||
if (event.key === "Escape") {
|
||||
closePostMetaMenus(null, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getBaseStatCount(statName) {
|
||||
var statEl = document.querySelector('[data-stat-count="' + statName + '"][data-base-count]');
|
||||
return statEl ? (parseInt(statEl.dataset.baseCount, 10) || 0) : 0;
|
||||
@@ -2104,6 +2172,7 @@ tbquire.config({
|
||||
|
||||
window.AksrtFrontend.register("aksrt-search-clear", function () { initSearchClear(); });
|
||||
window.AksrtFrontend.register("aksrt-article-tools", function () { initArticleTools(); });
|
||||
window.AksrtFrontend.register("aksrt-post-meta-menus", function (root) { initPostMetaMenus(root); });
|
||||
window.AksrtFrontend.register("aksrt-password-toggles", function (root) { initPasswordToggles(root); });
|
||||
window.AksrtFrontend.register("aksrt-interaction-bar", function (root) { initInteractionBar(root); });
|
||||
})();
|
||||
|
||||
@@ -78,6 +78,74 @@
|
||||
});
|
||||
}
|
||||
|
||||
var postMetaDocumentEventsBound = false;
|
||||
|
||||
function setPostMetaMenuState(menuRoot, open) {
|
||||
var toggle = menuRoot.querySelector(".post-drop-meta__toggle");
|
||||
var menu = menuRoot.querySelector(".post-drop-meta__menu");
|
||||
if (!toggle || !menu) return;
|
||||
|
||||
menuRoot.classList.toggle("is-open", open);
|
||||
toggle.setAttribute("aria-expanded", open ? "true" : "false");
|
||||
menu.setAttribute("aria-hidden", open ? "false" : "true");
|
||||
}
|
||||
|
||||
function closePostMetaMenus(except, restoreFocus) {
|
||||
document.querySelectorAll(".post-drop-meta.is-open").forEach(function (menuRoot) {
|
||||
if (menuRoot === except) return;
|
||||
var toggle = menuRoot.querySelector(".post-drop-meta__toggle");
|
||||
setPostMetaMenuState(menuRoot, false);
|
||||
if (restoreFocus && toggle) toggle.focus();
|
||||
});
|
||||
}
|
||||
|
||||
function initPostMetaMenus(root) {
|
||||
var scope = root || document;
|
||||
var menuRoots = [];
|
||||
|
||||
if (scope.nodeType === 1 && scope.matches(".post-drop-meta")) {
|
||||
menuRoots.push(scope);
|
||||
}
|
||||
scope.querySelectorAll(".post-drop-meta").forEach(function (menuRoot) {
|
||||
menuRoots.push(menuRoot);
|
||||
});
|
||||
|
||||
menuRoots.forEach(function (menuRoot) {
|
||||
var toggle = menuRoot.querySelector(".post-drop-meta__toggle");
|
||||
var menu = menuRoot.querySelector(".post-drop-meta__menu");
|
||||
if (!toggle || !menu || toggle.dataset.postMetaMenuBound) return;
|
||||
|
||||
toggle.dataset.postMetaMenuBound = "1";
|
||||
setPostMetaMenuState(menuRoot, false);
|
||||
|
||||
toggle.addEventListener("click", function () {
|
||||
var open = !menuRoot.classList.contains("is-open");
|
||||
closePostMetaMenus(menuRoot, false);
|
||||
setPostMetaMenuState(menuRoot, open);
|
||||
});
|
||||
|
||||
menu.addEventListener("click", function (event) {
|
||||
if (event.target.closest(".post-drop-meta__item")) {
|
||||
setPostMetaMenuState(menuRoot, false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (menuRoots.length && !postMetaDocumentEventsBound) {
|
||||
postMetaDocumentEventsBound = true;
|
||||
document.addEventListener("click", function (event) {
|
||||
if (!event.target.closest(".post-drop-meta")) {
|
||||
closePostMetaMenus(null, false);
|
||||
}
|
||||
});
|
||||
document.addEventListener("keydown", function (event) {
|
||||
if (event.key === "Escape") {
|
||||
closePostMetaMenus(null, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getBaseStatCount(statName) {
|
||||
var statEl = document.querySelector('[data-stat-count="' + statName + '"][data-base-count]');
|
||||
return statEl ? (parseInt(statEl.dataset.baseCount, 10) || 0) : 0;
|
||||
@@ -261,6 +329,7 @@
|
||||
|
||||
window.AksrtFrontend.register("aksrt-search-clear", function () { initSearchClear(); });
|
||||
window.AksrtFrontend.register("aksrt-article-tools", function () { initArticleTools(); });
|
||||
window.AksrtFrontend.register("aksrt-post-meta-menus", function (root) { initPostMetaMenus(root); });
|
||||
window.AksrtFrontend.register("aksrt-password-toggles", function (root) { initPasswordToggles(root); });
|
||||
window.AksrtFrontend.register("aksrt-interaction-bar", function (root) { initInteractionBar(root); });
|
||||
})();
|
||||
|
||||
@@ -531,6 +531,39 @@
|
||||
});
|
||||
}
|
||||
|
||||
function bindCharacterCount(editor) {
|
||||
var counter = null;
|
||||
|
||||
function getCounter() {
|
||||
var container = editor.getContainer();
|
||||
var statusbar = container && container.querySelector('.mce-statusbar .mce-container-body');
|
||||
if (!statusbar) {
|
||||
return null;
|
||||
}
|
||||
|
||||
counter = counter || statusbar.querySelector('[data-submit-word-count]');
|
||||
if (!counter) {
|
||||
counter = document.createElement('span');
|
||||
counter.className = 'mce-wordcount aksrt-submit-word-count';
|
||||
counter.setAttribute('data-submit-word-count', '');
|
||||
counter.setAttribute('aria-live', 'polite');
|
||||
statusbar.appendChild(counter);
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
function updateCount() {
|
||||
var count = String((editor.getBody() && editor.getBody().textContent) || '')
|
||||
.replace(/\s+/g, '').length;
|
||||
var element = getCounter();
|
||||
if (element) {
|
||||
element.textContent = '字数:' + count;
|
||||
}
|
||||
}
|
||||
|
||||
editor.on('init SetContent input keyup change Undo Redo', updateCount);
|
||||
}
|
||||
|
||||
function openCodeModal(editor) {
|
||||
var selected = editor.dom.getParent(editor.selection.getNode(), 'pre.enlighter-pre');
|
||||
var code = selected ? selected.querySelector('code') : null;
|
||||
@@ -591,6 +624,8 @@
|
||||
}
|
||||
|
||||
window.tinymce.PluginManager.add('aksrt_editor_tools', function (editor) {
|
||||
bindCharacterCount(editor);
|
||||
|
||||
if (editorCfg.canUploadImage) {
|
||||
editor.addButton('aksrt_image', {
|
||||
text: '',
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
*
|
||||
* 提供前台编辑页面的增强功能:
|
||||
* - 自动保存草稿
|
||||
* - 字数统计
|
||||
* - 标签建议
|
||||
* - 快捷键支持
|
||||
*
|
||||
@@ -27,8 +26,6 @@
|
||||
contentSelector: 'textarea[name="content"], textarea[name="topic_content"]',
|
||||
tagsSelector: 'input[name="tags"], input[name="topic_tags"]',
|
||||
autoSaveInterval: 30000, // 30秒
|
||||
maxTitleLength: 200,
|
||||
maxContentLength: 100000,
|
||||
draftKey: '',
|
||||
}, options || {});
|
||||
|
||||
@@ -47,9 +44,6 @@
|
||||
this.contentTextarea = this.form.querySelector(this.options.contentSelector);
|
||||
this.tagsInput = this.form.querySelector(this.options.tagsSelector);
|
||||
|
||||
// 字数统计
|
||||
this.initWordCount();
|
||||
|
||||
// 自动保存草稿
|
||||
this.initAutoSave();
|
||||
|
||||
@@ -73,49 +67,6 @@
|
||||
});
|
||||
};
|
||||
|
||||
PageEdit.prototype.initWordCount = function () {
|
||||
var self = this;
|
||||
|
||||
// 创建字数统计显示
|
||||
this.wordCountEl = document.createElement('div');
|
||||
this.wordCountEl.className = 'aksrt-page-edit__word-count';
|
||||
|
||||
if (this.titleInput) {
|
||||
var titleCount = document.createElement('span');
|
||||
titleCount.className = 'aksrt-page-edit__title-count';
|
||||
this.titleInput.parentNode.appendChild(titleCount);
|
||||
|
||||
this.titleInput.addEventListener('input', function () {
|
||||
var len = self.titleInput.value.length;
|
||||
titleCount.textContent = len + '/' + self.options.maxTitleLength;
|
||||
titleCount.classList.toggle('aksrt-page-edit__count--warn', len > self.options.maxTitleLength * 0.9);
|
||||
titleCount.classList.toggle('aksrt-page-edit__count--error', len > self.options.maxTitleLength);
|
||||
});
|
||||
}
|
||||
|
||||
if (this.contentTextarea) {
|
||||
this.contentTextarea.parentNode.appendChild(this.wordCountEl);
|
||||
this.updateWordCount();
|
||||
|
||||
this.contentTextarea.addEventListener('input', function () {
|
||||
self.updateWordCount();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
PageEdit.prototype.updateWordCount = function () {
|
||||
if (!this.contentTextarea || !this.wordCountEl) return;
|
||||
|
||||
var text = this.contentTextarea.value;
|
||||
var chars = text.length;
|
||||
var words = text.trim() ? text.trim().split(/\s+/).length : 0;
|
||||
var chinese = (text.match(/[一-鿿]/g) || []).length;
|
||||
|
||||
this.wordCountEl.textContent = '字数: ' + chars + ' | 词数: ' + words + ' | 中文: ' + chinese;
|
||||
this.wordCountEl.classList.toggle('aksrt-page-edit__count--warn', chars > this.options.maxContentLength * 0.9);
|
||||
this.wordCountEl.classList.toggle('aksrt-page-edit__count--error', chars > this.options.maxContentLength);
|
||||
};
|
||||
|
||||
PageEdit.prototype.initAutoSave = function () {
|
||||
var self = this;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user