geoproject-app/src/App.vue

92 lines
2.4 KiB
Vue
Raw Normal View History

<script setup>
import PagedJsWrapper from './components/PagedJsWrapper.vue';
import EditorPanel from './components/editor/EditorPanel.vue';
import ElementPopup from './components/ElementPopup.vue';
import { onMounted, ref, watch } from 'vue';
import { useStylesheetStore } from './stores/stylesheet';
const stylesheetStore = useStylesheetStore();
const previewFrame = ref(null);
const elementPopup = ref(null);
let savedScrollPercentage = 0;
const renderPreview = async (shouldReloadFromFile = false) => {
const iframe = previewFrame.value;
if (!iframe) return;
if (iframe.contentWindow && iframe.contentDocument) {
const scrollTop = iframe.contentWindow.scrollY || 0;
const scrollHeight = iframe.contentDocument.documentElement.scrollHeight;
const clientHeight = iframe.contentWindow.innerHeight;
const maxScroll = scrollHeight - clientHeight;
savedScrollPercentage = maxScroll > 0 ? scrollTop / maxScroll : 0;
}
if (shouldReloadFromFile || !stylesheetStore.content) {
await stylesheetStore.loadStylesheet();
}
iframe.srcdoc = `
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/assets/css/pagedjs-interface.css">
<style id="dynamic-styles">${stylesheetStore.content}</style>
<script src="https://unpkg.com/pagedjs/dist/paged.polyfill.js"><\/script>
</head>
<body>${document.getElementById('content-source').innerHTML}</body>
</html>
`;
iframe.onload = () => {
iframe.contentDocument.addEventListener(
'click',
elementPopup.value.handleIframeClick
);
setTimeout(() => {
const scrollHeight = iframe.contentDocument.documentElement.scrollHeight;
const clientHeight = iframe.contentWindow.innerHeight;
const maxScroll = scrollHeight - clientHeight;
const targetScroll = savedScrollPercentage * maxScroll;
iframe.contentWindow.scrollTo(0, targetScroll);
}, 500);
};
};
watch(
() => stylesheetStore.content,
() => {
renderPreview();
}
);
onMounted(() => renderPreview(true));
</script>
<template>
<div id="content-source" style="display: none">
<PagedJsWrapper />
</div>
<EditorPanel />
<iframe ref="previewFrame" id="preview-frame"></iframe>
<ElementPopup ref="elementPopup" :iframeRef="previewFrame" />
</template>
<style>
#preview-frame {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
border: none;
}
</style>