This commit is contained in:
@@ -5,10 +5,12 @@ import { emitter } from '~/services/Emitter';
|
|||||||
import { GetWidget, ParseMarkdown } from '~/services/Marker';
|
import { GetWidget, ParseMarkdown } from '~/services/Marker';
|
||||||
import Server from '~/services/Server';
|
import Server from '~/services/Server';
|
||||||
import { DisplayToast } from '~/services/Toaster';
|
import { DisplayToast } from '~/services/Toaster';
|
||||||
|
import { useI18n } from 'vue-i18n';
|
||||||
import TestWidget from '../widgets/TestWidget.vue';
|
import TestWidget from '../widgets/TestWidget.vue';
|
||||||
|
|
||||||
// import { GetNote, GetContent } from '@/services/Content';
|
// import { GetNote, GetContent } from '@/services/Content';
|
||||||
const props = defineProps(['text', 'title', 'noteKey']);
|
const props = defineProps(['text', 'title', 'noteKey']);
|
||||||
|
const { t } = useI18n();
|
||||||
const noteContent = ref(null); // Markdown text
|
const noteContent = ref(null); // Markdown text
|
||||||
const renderedContent = ref(null); // Container holding the compiled HTML (v-html)
|
const renderedContent = ref(null); // Container holding the compiled HTML (v-html)
|
||||||
|
|
||||||
@@ -36,20 +38,56 @@ const compiledMarkdown = computed(() => {
|
|||||||
return ParseMarkdown(sourceText.value);
|
return ParseMarkdown(sourceText.value);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Apps de widget muntades sobre aquest node. Les guardem per desmuntar-les
|
||||||
|
// abans de re-renderitzar o en desmuntar la nota (evita instancies orfes i
|
||||||
|
// que Vue intenti parchar nodes ja eliminats).
|
||||||
|
const mountedApps = [];
|
||||||
|
|
||||||
|
function unmountComponents() {
|
||||||
|
for (const app of mountedApps) {
|
||||||
|
try {
|
||||||
|
app.unmount();
|
||||||
|
} catch (err) {
|
||||||
|
// El contenidor pot ja no ser al DOM; ignorem i netegim igualment.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mountedApps.length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
function mountComponents() {
|
function mountComponents() {
|
||||||
// Should no need more
|
// Només dins d'aquesta nota: evitar muntar (o tornari a muntar) widgets
|
||||||
|
// d'altres notes obertes, que ja tenen la seva app.
|
||||||
|
const root = renderedContent.value;
|
||||||
|
if (!root) return;
|
||||||
|
|
||||||
const widget_types = ['display', 'inline'];
|
const widget_types = ['display', 'inline'];
|
||||||
widget_types.forEach((widget_type) => {
|
widget_types.forEach((widget_type) => {
|
||||||
const nodes = document.querySelectorAll('.vue-component-' + widget_type);
|
const nodes = root.querySelectorAll('.vue-component-' + widget_type);
|
||||||
nodes.forEach(el => {
|
nodes.forEach(el => {
|
||||||
const app = createApp(GetWidget(widget_type, el.dataset.component), { content: el.dataset.content });
|
const widget = GetWidget(widget_type, el.dataset.component);
|
||||||
app.mount(el);
|
if (!widget) return; // widget desconegut: saltem en comptes de petar
|
||||||
|
try {
|
||||||
|
const app = createApp(widget, { content: el.dataset.content });
|
||||||
|
// Evita que un error dins del widget (p. ex. el parser de daus amb una
|
||||||
|
// expressio malformada) es converteixi en un error no controlat.
|
||||||
|
app.config.errorHandler = (err) => {
|
||||||
|
console.error('Widget error', el.dataset.component, err);
|
||||||
|
};
|
||||||
|
app.mount(el);
|
||||||
|
mountedApps.push(app);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to mount widget', el.dataset.component, err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
///
|
///
|
||||||
|
|
||||||
function update(){
|
function update(){
|
||||||
|
// Desmuntem les apps anteriors mentre els seus nodes encara son al DOM;
|
||||||
|
// la reassignacio de displayText (v-html) eliminaria els contenidors i
|
||||||
|
// desmuntar-les despres fallaria sobre nodes desaccelats.
|
||||||
|
unmountComponents();
|
||||||
displayText.value = compiledMarkdown.value;
|
displayText.value = compiledMarkdown.value;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setupHeadings();
|
setupHeadings();
|
||||||
@@ -82,6 +120,7 @@ function handleTitleUpdate(data) {
|
|||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
emitter.off('title-updated', handleTitleUpdate);
|
emitter.off('title-updated', handleTitleUpdate);
|
||||||
// window.removeEventListener('keydown', handleKeydown);
|
// window.removeEventListener('keydown', handleKeydown);
|
||||||
|
unmountComponents();
|
||||||
});
|
});
|
||||||
|
|
||||||
function handleKeydown(e) {
|
function handleKeydown(e) {
|
||||||
@@ -92,6 +131,10 @@ function handleKeydown(e) {
|
|||||||
if(!editingMode.value){
|
if(!editingMode.value){
|
||||||
update();
|
update();
|
||||||
SaveNote(); // Save when switching to display mode
|
SaveNote(); // Save when switching to display mode
|
||||||
|
} else {
|
||||||
|
// Entrant en mode edicio, la vista prèvia (v-else) es destrueix;
|
||||||
|
// desmuntem els widgets per no deixar apps orfes.
|
||||||
|
unmountComponents();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -111,11 +154,17 @@ function SaveNote(){
|
|||||||
title: title.value,
|
title: title.value,
|
||||||
}).then((response) => {
|
}).then((response) => {
|
||||||
if(response.data.status !== 'ok'){
|
if(response.data.status !== 'ok'){
|
||||||
|
// El contingut no s'ha guardat: mantenem el punt de "no desar" i
|
||||||
|
// avisem en lloc d'empassar-nos l'error silenciosament.
|
||||||
|
isDirty.value = true;
|
||||||
|
DisplayToast('red', t(response.data.msg || 'general.notes.save-error'), 3000);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
savedState = { text: sourceText.value, title: title.value };
|
savedState = { text: sourceText.value, title: title.value };
|
||||||
isDirty.value = false;
|
isDirty.value = false;
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
|
isDirty.value = true;
|
||||||
|
DisplayToast('red', t('general.notes.save-error'), 3000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,14 @@ const steps = ref(null);
|
|||||||
const stepsHtml = ref("");
|
const stepsHtml = ref("");
|
||||||
|
|
||||||
const rollDice = () => {
|
const rollDice = () => {
|
||||||
const result = parse(props.content);
|
try {
|
||||||
resultText.value = result.total;
|
const result = parse(props.content);
|
||||||
stepsHtml.value = result.steps.map(renderStep).join('');
|
resultText.value = result.total;
|
||||||
|
stepsHtml.value = result.steps.map(renderStep).join('');
|
||||||
|
} catch (err) {
|
||||||
|
// Expressio de daus malformada: no fem petar el widget, nomes ho registrem.
|
||||||
|
console.error('Invalid roll expression', props.content, err);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderStep = (s) => {
|
const renderStep = (s) => {
|
||||||
|
|||||||
@@ -8,10 +8,13 @@ const container = ref(null);
|
|||||||
const resultText = ref("");
|
const resultText = ref("");
|
||||||
|
|
||||||
const rollDice = () => {
|
const rollDice = () => {
|
||||||
console.log(props.content);
|
try {
|
||||||
const result = parse(props.content);
|
const result = parse(props.content);
|
||||||
console.log(result);
|
resultText.value = result.total;
|
||||||
resultText.value = result.total;
|
} catch (err) {
|
||||||
|
// Expressio de daus malformada: no fem petar el widget, nomes ho registrem.
|
||||||
|
console.error('Invalid roll expression', props.content, err);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
|
|||||||
@@ -13,16 +13,30 @@ const componentCache = {
|
|||||||
display: {}
|
display: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Retorna null si el widget no està registrat (evita cridar
|
||||||
|
// defineAsyncComponent(undefined), que llençaria un TypeError i rebentaria
|
||||||
|
// tot el muntatge de notes).
|
||||||
const GetWidget = (type, name) => {
|
const GetWidget = (type, name) => {
|
||||||
|
const loader = widget_map[type] && widget_map[type][name];
|
||||||
|
if (!loader) {
|
||||||
|
console.warn(`[Marker] Unknown widget "${name}" of type "${type}"; skipping.`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (!componentCache[type][name]) {
|
if (!componentCache[type][name]) {
|
||||||
componentCache[type][name] = defineAsyncComponent(
|
componentCache[type][name] = defineAsyncComponent(loader)
|
||||||
widget_map[type][name]
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return componentCache[type][name]
|
return componentCache[type][name]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Escapa valors per inserir-los de forma segura dins d'un atribut HTML.
|
||||||
|
const escapeAttr = (value) => String(value ?? "")
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")
|
||||||
|
.replace(/"/g, """);
|
||||||
|
|
||||||
|
|
||||||
const marker = new Marked();
|
const marker = new Marked();
|
||||||
|
|
||||||
@@ -45,7 +59,7 @@ const extension = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
renderer(token) {
|
renderer(token) {
|
||||||
return `<div class="vue-component-display" data-component="${token.name}" data-content="${token.text}"></div>`;
|
return `<div class="vue-component-display" data-component="${escapeAttr(token.name)}" data-content="${escapeAttr(token.text)}"></div>`;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -70,7 +84,7 @@ const inlineExtension = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
renderer(token) {
|
renderer(token) {
|
||||||
return `<span class="vue-component-inline" data-component="${token.name}" data-content="${token.text}"></span>`;
|
return `<span class="vue-component-inline" data-component="${escapeAttr(token.name)}" data-content="${escapeAttr(token.text)}"></span>`;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -94,7 +108,7 @@ const linkExtension = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
renderer(token) {
|
renderer(token) {
|
||||||
return `<span class="vue-link" data-href="${token.link}">Link</span>`;
|
return `<span class="vue-link" data-href="${escapeAttr(token.link)}">Link</span>`;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
{
|
{
|
||||||
"save": "Desa"
|
"save": "Desa",
|
||||||
|
"notes": {
|
||||||
|
"saved": "Nota desada",
|
||||||
|
"save-error": "No s'ha pogut desar la nota"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,5 +6,9 @@
|
|||||||
"save": "Save",
|
"save": "Save",
|
||||||
"cancel": "Cancel",
|
"cancel": "Cancel",
|
||||||
"delete": "Delete",
|
"delete": "Delete",
|
||||||
"open": "Open"
|
"open": "Open",
|
||||||
|
"notes": {
|
||||||
|
"saved": "Note saved",
|
||||||
|
"save-error": "Unable to save the note"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
{
|
{
|
||||||
"save": "Guardar"
|
"save": "Guardar",
|
||||||
|
"notes": {
|
||||||
|
"saved": "Nota guardada",
|
||||||
|
"save-error": "No se ha podido guardar la nota"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user