Fix issue #20: Add notes to a map
This commit is contained in:
122
frontend/app/components/windows/PinnedNoteWindow.vue
Normal file
122
frontend/app/components/windows/PinnedNoteWindow.vue
Normal file
@@ -0,0 +1,122 @@
|
||||
<script setup>
|
||||
import { createApp, onBeforeUnmount, onMounted, ref } from 'vue';
|
||||
import { SetupHandle, SetSize, ResetPosition, ClearWindow, Top } from '@/services/Windows';
|
||||
import WindowHandle from './partials/WindowHandle.vue';
|
||||
import { GetWidget, ParseMarkdown } from '~/services/Marker';
|
||||
import Server from '~/services/Server';
|
||||
|
||||
// The note a map pin points at (see MapViewerPane.vue). The note is fetched here instead of being
|
||||
// carried over from the map, so a pin works whatever the note column shows and always reads fresh.
|
||||
const props = defineProps(['data']);
|
||||
const id = props.data.id;
|
||||
const noteId = props.data.note;
|
||||
const handle = ref(null);
|
||||
const wrapper = ref(null);
|
||||
const renderedContent = ref(null);
|
||||
const note = ref(null);
|
||||
const displayText = ref('');
|
||||
const loading = ref(true);
|
||||
const failed = ref(false);
|
||||
|
||||
// Widgets mounted over this note, kept to unmount them again the way Note.vue does: re-rendering
|
||||
// would otherwise leave the apps of the nodes it replaced behind.
|
||||
const mountedApps = [];
|
||||
|
||||
function unmountWidgets() {
|
||||
for (const app of mountedApps) {
|
||||
try {
|
||||
app.unmount();
|
||||
} catch (error) {
|
||||
// The container may already be off the DOM; ignore and drop it all the same.
|
||||
}
|
||||
}
|
||||
mountedApps.length = 0;
|
||||
}
|
||||
|
||||
function mountWidgets() {
|
||||
const root = renderedContent.value;
|
||||
if (!root) return;
|
||||
|
||||
['display', 'inline'].forEach((widgetType) => {
|
||||
root.querySelectorAll('.vue-component-' + widgetType).forEach((el) => {
|
||||
const widget = GetWidget(widgetType, el.dataset.component);
|
||||
if (!widget) return;
|
||||
try {
|
||||
const app = createApp(widget, { content: el.dataset.content });
|
||||
app.config.errorHandler = (error) => console.error('Widget error', el.dataset.component, error);
|
||||
app.mount(el);
|
||||
mountedApps.push(app);
|
||||
} catch (error) {
|
||||
console.error('Failed to mount widget', el.dataset.component, error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchNote() {
|
||||
loading.value = true;
|
||||
failed.value = false;
|
||||
try {
|
||||
const response = await Server().get(`/note/retrieve/${noteId}`);
|
||||
if (response.data.status !== 'ok' || !response.data.note) {
|
||||
failed.value = true;
|
||||
return;
|
||||
}
|
||||
note.value = response.data.note;
|
||||
unmountWidgets();
|
||||
displayText.value = ParseMarkdown(response.data.note.content ?? '');
|
||||
setTimeout(() => mountWidgets(), 0);
|
||||
} catch (error) {
|
||||
failed.value = true;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
Top(wrapper);
|
||||
SetupHandle(id, handle);
|
||||
SetSize(id, {
|
||||
width: Math.max(360, Math.min(720, window.innerWidth - 24)),
|
||||
height: Math.max(360, Math.min(720, window.innerHeight - 32))
|
||||
});
|
||||
ResetPosition(id, 'center');
|
||||
fetchNote();
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
unmountWidgets();
|
||||
ClearWindow({ type: 'pinned_note' });
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="window-wrapper pinned-note-window" :id="'window-wrapper-' + id" ref="wrapper">
|
||||
<WindowHandle :window="id" ref="handle" />
|
||||
<div class="body">
|
||||
<div v-if="loading" class="note-state">{{ $t('campaigns.states.loading') }}</div>
|
||||
<div v-else-if="failed" class="note-state">{{ $t('campaigns.map.pins.note-error') }}</div>
|
||||
<template v-else>
|
||||
<h1 v-if="note.title" class="note-title">{{ note.title }}</h1>
|
||||
<div ref="renderedContent" class="note-content" v-html="displayText"></div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.window-wrapper { display: flex; align-items: center; flex-direction: column; }
|
||||
/* Stretch so the outer margin insets the content instead of overflowing the window. */
|
||||
.body {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
align-self: stretch;
|
||||
box-sizing: border-box;
|
||||
margin: 10px;
|
||||
padding: 8px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.note-title { margin: 0 0 10px; font-family: MrEavesRemake; overflow-wrap: anywhere; }
|
||||
.note-state { padding: 10px; font-size: 13px; opacity: .8; }
|
||||
.note-content :deep(img) { max-width: 100%; height: auto; display: block; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user