/**
* YCat Editor - 文章大纲功能模块
* 自动提取标题生成可点击目录
*/
let outlineHeadings = [];
// 生成文章大纲
function generateOutline() {
const content = getEditorContent();
if (!content || !content.trim()) {
renderOutline([]);
return;
}
// 提取所有标题 (H1-H6)
const headingRegex = /<(h[1-6])[^>]*>(.*?)<\/\1>/gi;
const headings = [];
let match;
let index = 0;
while ((match = headingRegex.exec(content)) !== null && index < 50) {
const tag = match[1].toLowerCase();
const text = match[2]
.replace(/<[^>]+>/g, '') // 移除HTML标签
.replace(/ /g, ' ')
.replace(/</g, '<')
.replace(/>/g, '>')
.trim();
if (text.length === 0) continue;
const level = parseInt(tag.charAt(1));
headings.push({
level: level,
text: text.length > 50 ? text.substring(0, 50) + '...' : text,
index: index
});
index++;
}
outlineHeadings = headings;
renderOutline(headings);
}
// 渲染大纲到侧边栏
function renderOutline(headings) {
const container = document.getElementById('outlineContainer');
if (!container) return;
if (headings.length === 0) {
container.innerHTML = '
暂无标题
使用 H1-H6 标题丰富文章结构
';
return;
}
let html = '';
headings.forEach((heading, idx) => {
const className = `outline-item outline-item-h${heading.level}`;
html += `- ${heading.text}
`;
});
html += '
';
container.innerHTML = html;
}
// 点击大纲项,滚动到对应标题
function scrollToHeading(headingIndex) {
if (!window.editor) return;
try {
// 获取编辑器内容中的元素
const editorContent = document.querySelector('#editor-container .w-e-text');
if (!editorContent) return;
const headings = editorContent.querySelectorAll('h1, h2, h3, h4, h5, h6');
if (headingIndex < headings.length) {
const targetHeading = headings[headingIndex];
// 滚动到标题位置
targetHeading.scrollIntoView({
behavior: 'smooth',
block: 'center'
});
// 高亮效果
targetHeading.style.transition = 'background-color 0.3s';
targetHeading.style.backgroundColor = 'rgba(255, 107, 53, 0.2)';
targetHeading.style.padding = '4px 8px';
targetHeading.style.borderRadius = '4px';
setTimeout(() => {
targetHeading.style.backgroundColor = '';
targetHeading.style.padding = '';
targetHeading.style.borderRadius = '';
}, 1500);
// 更新大纲激活状态
updateOutlineActive(headingIndex);
}
} catch (err) {
console.error('滚动到标题失败:', err);
}
}
// 更新大纲激活状态
function updateOutlineActive(activeIndex) {
const items = document.querySelectorAll('.outline-item');
items.forEach((item, idx) => {
if (idx === activeIndex) {
item.classList.add('active');
} else {
item.classList.remove('active');
}
});
}
// 监听编辑器内容变化,实时更新大纲
let outlineUpdateTimer = null;
function scheduleOutlineUpdate() {
if (outlineUpdateTimer) {
clearTimeout(outlineUpdateTimer);
}
outlineUpdateTimer = setTimeout(() => {
generateOutline();
}, 2000); // 2秒防抖
}
// 初始化大纲功能
function initOutline() {
// 延迟首次生成,确保编辑器已加载
setTimeout(() => {
generateOutline();
}, 1000);
// 监听内容变化
if (window.editor) {
const originalOnChange = window.editor.config.onChange;
window.editor.config.onChange = function(editor) {
if (originalOnChange) originalOnChange(editor);
scheduleOutlineUpdate();
};
}
console.log('✨ 文章大纲功能已初始化');
}
// DOM 加载完成后初始化
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initOutline);
} else {
setTimeout(initOutline, 500);
}