Carets for folding headers
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 53s
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 53s
This commit is contained in:
@@ -10,6 +10,7 @@ import TestWidget from '../widgets/TestWidget.vue';
|
||||
// import { GetNote, GetContent } from '@/services/Content';
|
||||
const props = defineProps(['text', 'title', 'noteKey']);
|
||||
const noteContent = ref(null); // Markdown text
|
||||
const renderedContent = ref(null); // Container holding the compiled HTML (v-html)
|
||||
|
||||
const sourceText = ref(''); // Original markdown source, used for editing
|
||||
const displayText = ref(''); // Compiled HTML from markdown
|
||||
@@ -51,6 +52,7 @@ function mountComponents() {
|
||||
function update(){
|
||||
displayText.value = compiledMarkdown.value;
|
||||
setTimeout(() => {
|
||||
setupHeadings();
|
||||
setupCallout()
|
||||
mountComponents();
|
||||
}, 0);
|
||||
@@ -162,6 +164,100 @@ function setupCallout() {
|
||||
}
|
||||
}
|
||||
|
||||
// --- Col·lapse de seccions (h1, h2, h3) a la vista prèvia ---
|
||||
// Agrupa el contingut sota cada capçalera fins la seguent del mateix nivell o
|
||||
// superior, de manera que en fer clic sobre la capçalera es plegui tota la seva
|
||||
// secció (les sub-seccions s'hi inclouen, estil Obsidian).
|
||||
function headingLevel(el) {
|
||||
const tag = el && el.tagName;
|
||||
if (tag === 'H1') return 1;
|
||||
if (tag === 'H2') return 2;
|
||||
if (tag === 'H3') return 3;
|
||||
return 0; // h4+ i la resta es tracten com a contingut normal
|
||||
}
|
||||
|
||||
function buildFoldTree(nodes) {
|
||||
const pos = { i: 0 };
|
||||
|
||||
function parseSection(level) {
|
||||
const heading = nodes[pos.i];
|
||||
pos.i++;
|
||||
const body = [];
|
||||
while (pos.i < nodes.length) {
|
||||
const lvl = headingLevel(nodes[pos.i]);
|
||||
if (lvl && lvl <= level) break; // seguent secció del mateix nivell o superior
|
||||
if (lvl) {
|
||||
body.push(parseSection(lvl)); // sub-secció més profunda
|
||||
} else {
|
||||
body.push({ node: nodes[pos.i] });
|
||||
pos.i++;
|
||||
}
|
||||
}
|
||||
return { heading, body, level };
|
||||
}
|
||||
|
||||
const top = [];
|
||||
while (pos.i < nodes.length) {
|
||||
const lvl = headingLevel(nodes[pos.i]);
|
||||
if (lvl) {
|
||||
top.push(parseSection(lvl));
|
||||
} else {
|
||||
top.push({ node: nodes[pos.i] });
|
||||
pos.i++;
|
||||
}
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
function renderFoldTree(items, target) {
|
||||
for (const item of items) {
|
||||
if (item.node) {
|
||||
target.appendChild(item.node);
|
||||
continue;
|
||||
}
|
||||
const wrapper = document.createElement('div');
|
||||
wrapper.className = 'fold fold-h' + item.level;
|
||||
item.heading.classList.add('fold-handle');
|
||||
wrapper.appendChild(item.heading);
|
||||
|
||||
const body = document.createElement('div');
|
||||
body.className = 'fold-body';
|
||||
renderFoldTree(item.body, body);
|
||||
wrapper.appendChild(body);
|
||||
|
||||
target.appendChild(wrapper);
|
||||
}
|
||||
}
|
||||
|
||||
function handleFoldClick(e) {
|
||||
// No plegar quan es fa clic en un enllaç o se selecciona text
|
||||
if (e.target.closest('a')) return;
|
||||
const selection = window.getSelection();
|
||||
if (selection && selection.toString()) return;
|
||||
|
||||
const handle = e.target.closest('.fold-handle');
|
||||
if (!handle) return;
|
||||
const wrapper = handle.parentElement;
|
||||
if (!wrapper || !wrapper.classList.contains('fold')) return;
|
||||
wrapper.classList.toggle('is-collapsed');
|
||||
}
|
||||
|
||||
function setupHeadings() {
|
||||
const container = renderedContent.value;
|
||||
if (!container) return;
|
||||
|
||||
const nodes = Array.from(container.children);
|
||||
if (!nodes.some((el) => headingLevel(el))) return;
|
||||
|
||||
const tree = buildFoldTree(nodes);
|
||||
renderFoldTree(tree, container);
|
||||
|
||||
if (!container._foldBound) {
|
||||
container.addEventListener('click', handleFoldClick);
|
||||
container._foldBound = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
@@ -183,7 +279,7 @@ function setupCallout() {
|
||||
<textarea v-model="sourceText" class="full-editor" v-if="editingMode"></textarea>
|
||||
<div v-else class="note-content" ref="noteContent">
|
||||
<h1>{{ title }}</h1>
|
||||
<div ref="noteContent" v-html="displayText"></div>
|
||||
<div ref="renderedContent" v-html="displayText"></div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user