import { getCurrentProjectEditor } from '../app/main'; import colorStyle from '../common/colors.css?inline'; import { addAsChildren, makeElement } from '../common/dom'; import logo from '../common/graphics/logo-icon.svg?raw'; import resetStyle from '../common/resets.css?inline'; import { closeEveryTypeOfDialog, makeModalDialog, showToast } from '../controls/dialogs/dialogs'; import dialogStyle from '../controls/dialogs/dialogs.css?inline'; import { FontPreview } from '../controls/font-preview/font_preview'; import { DisplayCanvas } from '../display_canvas/display_canvas'; import { TextBlockOptions } from '../display_canvas/text_block_options'; import { makeToolButtonSVG } from '../edit_canvas/tools/tools'; import { makePanel_LivePreview } from '../panels/live_preview'; import panelStyle from '../panels/panels.css?inline'; import popOutWindowStyle from './pop-out-window.css?inline'; export function openPopOutWindow() { // log(`openPopOutWindow`, 'start'); const editor = getCurrentProjectEditor(); // @ts-expect-error 'property does exist' editor.popOutWindow = window.open('', 'glyphr-studio-pop-out-live-preview'); // Define custom elements for the Pop Out Window // @ts-expect-error 'property does exist' editor.popOutWindow.customElements.define('display-canvas', DisplayCanvas); // @ts-expect-error 'property does exist' editor.popOutWindow.customElements.define('font-preview', FontPreview); // Init window properties // @ts-expect-error 'property does exist' let popDoc = editor.popOutWindow.document; popDoc.head.appendChild(makeElement({ tag: 'title', content: 'Live Preview - Glyphr Studio' })); const resets = makeElement({ tag: 'style', innerHTML: resetStyle }); popDoc.head.appendChild(resets); const colors = makeElement({ tag: 'style', innerHTML: colorStyle }); popDoc.head.appendChild(colors); const popWindow = makeElement({ tag: 'style', innerHTML: popOutWindowStyle }); popDoc.head.appendChild(popWindow); const dialogs = makeElement({ tag: 'style', innerHTML: dialogStyle }); popDoc.head.appendChild(dialogs); const panels = makeElement({ tag: 'style', innerHTML: panelStyle }); popDoc.head.appendChild(panels); popDoc.body.appendChild( makeElement({ tag: 'div', id: 'pop-out__wrapper', }) ); // @ts-expect-error 'property does exist' editor.popOutWindow.addEventListener('beforeunload', closePopOutWindow); window.addEventListener('beforeunload', closePopOutWindow); // @ts-expect-error 'property does exist' editor.popOutWindow.addEventListener('resize', livePreviewPopOutWindowResize); editor.subscribe({ topic: '*', subscriberID: 'livePreviewPopOutWindow', callback: redrawPopOutWindow, }); let favIcon = makeElement({ tag: 'link', attributes: { rel: 'shortcut icon', href: `data:image/svg+xml,${encodeURI(logo)}` }, }); popDoc.head.appendChild(favIcon); updatePopOutWindowContent(); // log(`openPopOutWindow`, 'end'); } export function updatePopOutWindowContent() { // log(`updatePopOutWindowContent`, 'start'); const editor = getCurrentProjectEditor(); // @ts-expect-error 'property does exist' let popDoc = editor.popOutWindow.document; const popWrapper = popDoc.querySelector('#pop-out__wrapper'); popWrapper.innerHTML = ''; editor.livePreviews.forEach((options, index) => { // index 0 is for the Live Previews Page // the rest are for the Pop Out Window if (index !== 0) { // log(`appending new live preview renderer`); // log(options); options.widthAdjustment = -20; popWrapper.appendChild(makeLivePreviewRenderer(options)); } }); // Preview controls const footer = makeElement({ tag: 'div', className: 'pop-out__footer' }); const editPreviewsButton = makeElement({ tag: 'fancy-button', content: 'Edit live previews', attributes: { minimal: '' }, }); editPreviewsButton.addEventListener('click', showEditLivePreviewDialog); const addPreviewButton = makeElement({ tag: 'fancy-button', content: 'Add a live preview', attributes: { minimal: '' }, }); addPreviewButton.addEventListener('click', () => { // log(`addPreviewButton ONCLICK`, 'start'); let newPreview = new TextBlockOptions({ text: 'new live preview' }); // log(`\n⮟newPreview⮟`); // log(newPreview); const editor = getCurrentProjectEditor(); editor.livePreviews.push(newPreview); selectedLivePreview = editor.livePreviews.length - 1; updatePopOutWindowContent(); showEditLivePreviewDialog(); // log(`addPreviewButton ONCLICK`, 'end'); }); // addAsChildren(footer, [editPreviewsButton, addPreviewButton]); if (editor.livePreviews.length > 1) footer.appendChild(editPreviewsButton); footer.appendChild(addPreviewButton); popWrapper.appendChild(footer); // Update buttons const popButton = document.querySelector('#editor-page__tool__open-live-preview-pop-out'); if (popButton) { popButton.innerHTML = makeToolButtonSVG({ name: 'closeLivePreview', selected: false, }); } // editor.popOutWindow.setTimeout(refreshPopOutWindow, 10); // log(`updatePopOutWindowContent`, 'end'); } /** * Builds the live preview renderer for a single pop-out preview based on its * selected render flavor. 'gs' (Glyphr Studio) uses the canvas-drawn * display-canvas control; 'otf' / 'ttf' use the font-preview control, which * renders editable native text backed by an on-the-fly generated font binary. * @param {Object} livePreviewOptions - TextBlockOptions for the preview * @returns {Element} - the renderer web component */ function makeLivePreviewRenderer(livePreviewOptions) { const flavor = livePreviewOptions.previewFlavor || 'gs'; if (flavor === 'otf' || flavor === 'ttf') { const fontPreview = new FontPreview(livePreviewOptions); // The font-preview control is the live text input for native flavors, // so keep the shared options text in sync as the user types. fontPreview.addEventListener('text-change', (event) => { // @ts-expect-error CustomEvent detail livePreviewOptions.text = event.detail.text; }); return fontPreview; } return new DisplayCanvas(livePreviewOptions); } export function closePopOutWindow(event) { // log(`closePopOutWindow`, 'start'); if (event) event.preventDefault(); const editor = getCurrentProjectEditor(); try { // @ts-expect-error 'property does exist' editor.popOutWindow.close(); } catch { console.warn('Could not close pop-out window'); } editor.popOutWindow = false; editor.unsubscribe({ idToRemove: 'livePreviewPopOutWindow' }); window.removeEventListener('beforeunload', closePopOutWindow); // Update buttons const popButton = document.querySelector('#editor-page__tool__open-live-preview-pop-out'); if (popButton) { popButton.innerHTML = makeToolButtonSVG({ name: 'openLivePreview', selected: false, }); } // log(`closePopOutWindow`, 'end'); return undefined; } function redrawPopOutWindow() { // log(`redrawPopOutWindow`, 'start'); const editor = getCurrentProjectEditor(); // @ts-expect-error 'property does exist' const popDoc = editor.popOutWindow.document; // display-canvas (Glyphr Studio flavor) rebuilds its text block then redraws. popDoc.body.querySelectorAll('display-canvas').forEach((can) => { can.updateTextBlock(); can.redraw(); }); // font-preview (OTF / TTF flavors) regenerates its font binary and redraws. popDoc.body.querySelectorAll('font-preview').forEach((preview) => { preview.rebuildAndRedraw(); }); // log(`redrawPopOutWindow`, 'end'); } export function makeLivePreviewPopOutCard(showBlurb = false) { const card = makeElement({ tag: 'div', className: 'panel__card full-width', innerHTML: '