This commit is contained in:
@@ -5,10 +5,12 @@ import { emitter } from '~/services/Emitter';
|
||||
import { GetWidget, ParseMarkdown } from '~/services/Marker';
|
||||
import Server from '~/services/Server';
|
||||
import { DisplayToast } from '~/services/Toaster';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import TestWidget from '../widgets/TestWidget.vue';
|
||||
|
||||
// import { GetNote, GetContent } from '@/services/Content';
|
||||
const props = defineProps(['text', 'title', 'noteKey']);
|
||||
const { t } = useI18n();
|
||||
const noteContent = ref(null); // Markdown text
|
||||
const renderedContent = ref(null); // Container holding the compiled HTML (v-html)
|
||||
|
||||
@@ -36,20 +38,56 @@ const compiledMarkdown = computed(() => {
|
||||
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() {
|
||||
// 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'];
|
||||
widget_types.forEach((widget_type) => {
|
||||
const nodes = document.querySelectorAll('.vue-component-' + widget_type);
|
||||
const nodes = root.querySelectorAll('.vue-component-' + widget_type);
|
||||
nodes.forEach(el => {
|
||||
const app = createApp(GetWidget(widget_type, el.dataset.component), { content: el.dataset.content });
|
||||
app.mount(el);
|
||||
const widget = GetWidget(widget_type, el.dataset.component);
|
||||
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(){
|
||||
// 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;
|
||||
setTimeout(() => {
|
||||
setupHeadings();
|
||||
@@ -82,6 +120,7 @@ function handleTitleUpdate(data) {
|
||||
onUnmounted(() => {
|
||||
emitter.off('title-updated', handleTitleUpdate);
|
||||
// window.removeEventListener('keydown', handleKeydown);
|
||||
unmountComponents();
|
||||
});
|
||||
|
||||
function handleKeydown(e) {
|
||||
@@ -92,6 +131,10 @@ function handleKeydown(e) {
|
||||
if(!editingMode.value){
|
||||
update();
|
||||
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;
|
||||
}
|
||||
@@ -111,11 +154,17 @@ function SaveNote(){
|
||||
title: title.value,
|
||||
}).then((response) => {
|
||||
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;
|
||||
}
|
||||
savedState = { text: sourceText.value, title: title.value };
|
||||
isDirty.value = false;
|
||||
}).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 rollDice = () => {
|
||||
const result = parse(props.content);
|
||||
resultText.value = result.total;
|
||||
stepsHtml.value = result.steps.map(renderStep).join('');
|
||||
try {
|
||||
const result = parse(props.content);
|
||||
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) => {
|
||||
|
||||
@@ -8,10 +8,13 @@ const container = ref(null);
|
||||
const resultText = ref("");
|
||||
|
||||
const rollDice = () => {
|
||||
console.log(props.content);
|
||||
const result = parse(props.content);
|
||||
console.log(result);
|
||||
resultText.value = result.total;
|
||||
try {
|
||||
const result = parse(props.content);
|
||||
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(() => {
|
||||
|
||||
Reference in New Issue
Block a user