Files
dragonroll/frontend/app/components/viewer/content/Note.vue
Aran Roig d9ef18e82d
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 53s
Carets for folding headers
2026-10-12 00:30:25 +02:00

401 lines
10 KiB
Vue

<script setup>
import { onMounted, onUnmounted, ref, createApp } from 'vue';
import ToastManager from '~/components/managers/ToastManager.vue';
import { emitter } from '~/services/Emitter';
import { GetWidget, ParseMarkdown } from '~/services/Marker';
import Server from '~/services/Server';
import { DisplayToast } from '~/services/Toaster';
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
const editingMode = ref(false);
const title = ref(props.title);
const isDirty = ref(false);
const showClose = ref(false);
let savedState = { text: '', title: '' };
function markDirty() {
isDirty.value = sourceText.value !== savedState.text || title.value !== savedState.title;
}
function closeNote(){
// 'close-note' only closes the opened note; 'delete-note' is reserved for
// actual deletions (sidebar removal happens on that event instead)
emitter.emit('close-note', props.noteKey);
}
const compiledMarkdown = computed(() => {
return ParseMarkdown(sourceText.value);
});
function mountComponents() {
// Should no need more
const widget_types = ['display', 'inline'];
widget_types.forEach((widget_type) => {
const nodes = document.querySelectorAll('.vue-component-' + widget_type);
nodes.forEach(el => {
const app = createApp(GetWidget(widget_type, el.dataset.component), { content: el.dataset.content });
app.mount(el);
});
});
}
///
function update(){
displayText.value = compiledMarkdown.value;
setTimeout(() => {
setupHeadings();
setupCallout()
mountComponents();
}, 0);
}
watch(sourceText, markDirty);
watch(title, markDirty);
onMounted(() => {
sourceText.value = props.text;
title.value = props.title;
savedState = { text: props.text, title: props.title };
emitter.on('title-updated', handleTitleUpdate);
// window.addEventListener('keydown', handleKeydown);
setTimeout(() => setupCallout(), 0);
update();
});
function handleTitleUpdate(data) {
if (data.key !== props.noteKey) return;
title.value = data.title;
savedState.title = data.title;
}
onUnmounted(() => {
emitter.off('title-updated', handleTitleUpdate);
// window.removeEventListener('keydown', handleKeydown);
});
function handleKeydown(e) {
// Check for Ctrl + E (or Cmd + E on Mac)
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'e') {
e.preventDefault(); // prevent browser default behavior
editingMode.value = !editingMode.value;
if(!editingMode.value){
update();
SaveNote(); // Save when switching to display mode
}
return;
}
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 's') {
e.preventDefault(); // prevent browser default behavior
// Save the note (you can emit an event or call a method here)
SaveNote();
return;
}
}
function SaveNote(){
Server().post('/note/update', {
id: props.noteKey,
content: sourceText.value,
title: title.value,
}).then((response) => {
if(response.data.status !== 'ok'){
return;
}
savedState = { text: sourceText.value, title: title.value };
isDirty.value = false;
}).catch((error) => {
});
}
function toggleCallout() {
const outerBlock = this.parentElement;
outerBlock.classList.toggle("is-collapsed")
const collapsed = outerBlock.classList.contains("is-collapsed")
const height = collapsed ? this.scrollHeight : outerBlock.scrollHeight
outerBlock.style.maxHeight = height + "px"
// walk and adjust height of all parents
let current = outerBlock
let parent = outerBlock.parentElement
while (parent) {
if (!parent.classList.contains("callout")) {
return
}
const collapsed = parent.classList.contains("is-collapsed")
const height = collapsed ? parent.scrollHeight : parent.scrollHeight + current.scrollHeight
parent.style.maxHeight = height + "px"
current = parent
parent = parent.parentElement
}
}
function setupCallout() {
if (!noteContent.value) {
return;
}
const collapsible = noteContent.value.getElementsByClassName(
`callout is-collapsible`,
);
for (const div of collapsible) {
const title = div.firstElementChild;
if (title) {
title.addEventListener("click", toggleCallout)
const collapsed = div.classList.contains("is-collapsed")
const height = collapsed ? title.scrollHeight : div.scrollHeight
div.style.maxHeight = height + "px"
}
}
}
// --- 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>
<template>
<div class="note" @keydown="handleKeydown" tabindex="0">
<div class="note-stunt">
<div class="unsaved-wrapper" v-if="isDirty">
<div class="unsaved-dot"></div>
<div class="close-button close-hidden" v-on:click="closeNote">
<img class="icon" src="/icons/Pixelarticons/white/close.svg" alt="My Happy SVG"/>
</div>
</div>
<div class="close-button" v-else v-on:click="closeNote">
<img class="icon" src="/icons/Pixelarticons/white/close.svg" alt="My Happy SVG"/>
</div>
<span>{{ title }}</span>
</div>
<div class="note-content-container">
<textarea v-model="sourceText" class="full-editor" v-if="editingMode"></textarea>
<div v-else class="note-content" ref="noteContent">
<h1>{{ title }}</h1>
<div ref="renderedContent" v-html="displayText"></div>
</div>
</div>
</div>
</template>
<style scoped>
.note-stunt {
writing-mode: vertical-lr;
position: sticky;
top: 0px;
left: 0px;
bottom: 0px;
padding: 10px;
display: flex;
user-select: none;
}
.unsaved-wrapper {
position: relative;
margin-bottom: 5px;
width: 20px;
height: 20px;
}
.unsaved-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #ffffff;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
pointer-events: none;
}
.close-hidden {
opacity: 0;
visibility: hidden;
position: absolute;
top: 0;
left: 0;
width: 20px;
height: 20px;
margin-bottom: 0;
transition: opacity 0.15s ease, visibility 0.15s ease;
}
.unsaved-wrapper:hover .close-hidden {
opacity: 1;
visibility: visible;
}
.unsaved-wrapper:hover .unsaved-dot {
opacity: 0;
}
.full-editor {
width: 100%;
height: 100%;
box-sizing: border-box;
border: none;
outline: none;
resize: none;
padding: 20px;
font-size: 16px;
font-family: monospace; /* optional, gives document/editor feel */
padding-bottom: 400px; /* Small bottom margin */
}
.close-button {
height: 20px;
width: 20px;
margin-bottom: 5px;
display: flex;
justify-content: center;
cursor: pointer;
filter: invert(var(--color-icon-invert));
}
.note {
min-width: 700px;
max-width: 700px;
overflow-y: auto;
border-color: var(--color-note-border);
border-width: 0px;
border-right-width: 1px;
border-style: solid;
display: flex;
background-color: var(--color-background);
position: sticky;
top: 0px;
}
/* Contingut de cada nota */
.note-content {
padding-bottom: 400px;
overflow-y: auto;
max-width: 600px;
padding: 20px;
}
.note-content-container {
width: 100%;
}
.note-content :deep(img) {
max-width: 100%;
height: auto;
display: block; /* optional: avoids inline spacing issues */
}
</style>