diff --git a/frontend/app/assets/css/main.scss b/frontend/app/assets/css/main.scss index 70874325..75d74498 100644 --- a/frontend/app/assets/css/main.scss +++ b/frontend/app/assets/css/main.scss @@ -363,4 +363,41 @@ span.artifact { .green { color: var(--color-green); +} + +/* Seccions col·lapsables (h1/h2/h3) a la vista prèvia d'una nota */ +.fold-handle { + position: relative; + cursor: pointer; + user-select: none; + padding-left: 0.95em; +} + +.fold-handle::before { + content: ""; + position: absolute; + left: 0.1em; + top: 0.55em; + width: 0.3em; + height: 0.3em; + box-sizing: border-box; + border-right: 1px solid currentColor; + border-bottom: 1px solid currentColor; + opacity: 0; + transition: transform 0.15s ease, opacity 0.15s ease; + transform: rotate(45deg); + transform-origin: center; +} + +.fold-handle:hover::before { + opacity: 0.7; +} + +.fold.is-collapsed > .fold-handle::before { + opacity: 0.7; + transform: rotate(-45deg); +} + +.fold.is-collapsed > .fold-body { + display: none; } \ No newline at end of file diff --git a/frontend/app/components/viewer/content/Note.vue b/frontend/app/components/viewer/content/Note.vue index 7413296f..36c645ef 100644 --- a/frontend/app/components/viewer/content/Note.vue +++ b/frontend/app/components/viewer/content/Note.vue @@ -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; + } +} + @@ -183,7 +279,7 @@ function setupCallout() {

{{ title }}

-
+