2025-11-24 14:01:48 +01:00
|
|
|
<script setup>
|
|
|
|
|
import PagedJsWrapper from './components/PagedJsWrapper.vue';
|
2025-11-24 16:51:55 +01:00
|
|
|
import EditorPanel from './components/EditorPanel.vue';
|
|
|
|
|
import StylesheetViewer from './components/StylesheetViewer.vue';
|
|
|
|
|
import ElementPopup from './components/ElementPopup.vue';
|
|
|
|
|
import { onMounted, ref, watch } from 'vue';
|
2025-11-24 17:55:42 +01:00
|
|
|
import { useStylesheetStore } from './stores/stylesheet';
|
|
|
|
|
|
|
|
|
|
const stylesheetStore = useStylesheetStore();
|
2025-11-24 14:01:48 +01:00
|
|
|
|
2025-11-24 16:51:55 +01:00
|
|
|
const previewFrame = ref(null);
|
|
|
|
|
const aboutFontSize = ref(2);
|
|
|
|
|
const aboutFontSizeUnit = ref('rem');
|
2025-11-24 14:01:48 +01:00
|
|
|
|
2025-11-24 16:51:55 +01:00
|
|
|
const popupVisible = ref(false);
|
|
|
|
|
const popupPosition = ref({ x: 0, y: 0 });
|
|
|
|
|
const popupSelector = ref('');
|
|
|
|
|
|
|
|
|
|
// PagedJS print rules
|
|
|
|
|
const printStyles = `
|
|
|
|
|
h2 { break-before: page; }
|
|
|
|
|
|
|
|
|
|
@page {
|
|
|
|
|
size: A4;
|
|
|
|
|
margin: 20mm 15mm 26mm 15mm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@page {
|
|
|
|
|
@bottom-center { content: string(title); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.chapter > h2 { string-set: title content(text); }
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const injectStylesToIframe = () => {
|
|
|
|
|
const iframe = previewFrame.value;
|
|
|
|
|
if (!iframe?.contentDocument) return;
|
2025-11-24 14:01:48 +01:00
|
|
|
|
2025-11-24 16:51:55 +01:00
|
|
|
let styleElement = iframe.contentDocument.getElementById('dynamic-styles');
|
|
|
|
|
if (!styleElement) {
|
|
|
|
|
styleElement = iframe.contentDocument.createElement('style');
|
|
|
|
|
styleElement.id = 'dynamic-styles';
|
|
|
|
|
iframe.contentDocument.head.appendChild(styleElement);
|
2025-11-24 14:01:48 +01:00
|
|
|
}
|
2025-11-24 17:55:42 +01:00
|
|
|
styleElement.textContent = stylesheetStore.content;
|
2025-11-24 16:51:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleIframeClick = (event) => {
|
|
|
|
|
const element = event.target;
|
2025-11-24 14:01:48 +01:00
|
|
|
|
2025-11-24 16:51:55 +01:00
|
|
|
if (element.tagName === 'BODY' || element.tagName === 'HTML') {
|
|
|
|
|
popupVisible.value = false;
|
|
|
|
|
return;
|
2025-11-24 14:01:48 +01:00
|
|
|
}
|
2025-11-24 16:51:55 +01:00
|
|
|
|
|
|
|
|
const selector = element.id
|
|
|
|
|
? `#${element.id}`
|
|
|
|
|
: `.${element.className.split(' ')[0]}`;
|
|
|
|
|
|
|
|
|
|
popupSelector.value = selector;
|
|
|
|
|
|
|
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
|
const iframeRect = previewFrame.value.getBoundingClientRect();
|
|
|
|
|
popupPosition.value = {
|
|
|
|
|
x: iframeRect.left + rect.left,
|
2025-11-24 17:55:42 +01:00
|
|
|
y: iframeRect.top + rect.bottom + 5,
|
2025-11-24 16:51:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
popupVisible.value = true;
|
2025-11-24 14:01:48 +01:00
|
|
|
};
|
|
|
|
|
|
2025-11-24 16:51:55 +01:00
|
|
|
const closePopup = () => {
|
|
|
|
|
popupVisible.value = false;
|
2025-11-24 14:01:48 +01:00
|
|
|
};
|
|
|
|
|
|
2025-11-24 16:51:55 +01:00
|
|
|
watch(aboutFontSize, (newVal) => {
|
2025-11-24 17:55:42 +01:00
|
|
|
stylesheetStore.updateProperty(
|
|
|
|
|
'.about',
|
|
|
|
|
'font-size',
|
|
|
|
|
newVal,
|
|
|
|
|
aboutFontSizeUnit.value
|
|
|
|
|
);
|
2025-11-24 14:01:48 +01:00
|
|
|
});
|
2025-11-24 16:51:55 +01:00
|
|
|
|
2025-11-24 17:55:42 +01:00
|
|
|
watch(() => stylesheetStore.content, injectStylesToIframe);
|
2025-11-24 16:51:55 +01:00
|
|
|
|
|
|
|
|
const renderPreview = async () => {
|
|
|
|
|
const iframe = previewFrame.value;
|
|
|
|
|
if (!iframe) return;
|
|
|
|
|
|
2025-11-24 17:55:42 +01:00
|
|
|
await stylesheetStore.loadStylesheet();
|
2025-11-24 16:51:55 +01:00
|
|
|
|
2025-11-24 17:55:42 +01:00
|
|
|
const initialFontSize = stylesheetStore.extractValue('.about', 'font-size');
|
2025-11-24 16:51:55 +01:00
|
|
|
if (initialFontSize) {
|
|
|
|
|
aboutFontSize.value = initialFontSize.value;
|
|
|
|
|
aboutFontSizeUnit.value = initialFontSize.unit;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 17:55:42 +01:00
|
|
|
iframe.srcdoc = `
|
2025-11-24 16:51:55 +01:00
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<link rel="stylesheet" href="/assets/css/pagedjs-interface.css">
|
2025-11-24 17:55:42 +01:00
|
|
|
<style id="dynamic-styles">${stylesheetStore.content}</style>
|
2025-11-24 16:51:55 +01:00
|
|
|
<style>${printStyles}</style>
|
|
|
|
|
<script src="https://unpkg.com/pagedjs/dist/paged.polyfill.js"><\/script>
|
|
|
|
|
</head>
|
2025-11-24 17:55:42 +01:00
|
|
|
<body>${document.getElementById('content-source').innerHTML}</body>
|
2025-11-24 16:51:55 +01:00
|
|
|
</html>
|
2025-11-24 17:55:42 +01:00
|
|
|
`;
|
2025-11-24 16:51:55 +01:00
|
|
|
|
|
|
|
|
iframe.onload = () => {
|
|
|
|
|
iframe.contentDocument.addEventListener('click', handleIframeClick);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onMounted(renderPreview);
|
2025-11-24 14:01:48 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-11-24 16:51:55 +01:00
|
|
|
<div id="content-source" style="display: none">
|
2025-11-24 14:01:48 +01:00
|
|
|
<PagedJsWrapper />
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-11-24 16:51:55 +01:00
|
|
|
<EditorPanel
|
|
|
|
|
:fontSize="aboutFontSize"
|
|
|
|
|
:unit="aboutFontSizeUnit"
|
|
|
|
|
@update:fontSize="aboutFontSize = $event"
|
|
|
|
|
/>
|
2025-11-24 14:01:48 +01:00
|
|
|
|
2025-11-24 16:51:55 +01:00
|
|
|
<iframe ref="previewFrame" id="preview-frame"></iframe>
|
2025-11-24 14:01:48 +01:00
|
|
|
|
2025-11-24 17:55:42 +01:00
|
|
|
<StylesheetViewer :stylesheet="stylesheetStore.content" />
|
2025-11-24 14:01:48 +01:00
|
|
|
|
2025-11-24 16:51:55 +01:00
|
|
|
<ElementPopup
|
|
|
|
|
:visible="popupVisible"
|
|
|
|
|
:position="popupPosition"
|
|
|
|
|
:selector="popupSelector"
|
|
|
|
|
@close="closePopup"
|
|
|
|
|
/>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
#preview-frame {
|
|
|
|
|
position: fixed;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 250px;
|
|
|
|
|
width: calc(100% - 600px);
|
|
|
|
|
height: 100vh;
|
|
|
|
|
border: none;
|
2025-11-24 14:01:48 +01:00
|
|
|
}
|
|
|
|
|
</style>
|