|
| 1 | +<template> |
| 2 | + <div |
| 3 | + ref="previewRef" |
| 4 | + class="editor-preview" |
| 5 | + > |
| 6 | + <div class="tools"> |
| 7 | + <div class="left"> |
| 8 | + <AppCheckbox |
| 9 | + v-model="appStore.codePreview.darkMode" |
| 10 | + name="Dark mode" |
| 11 | + label="Dark mode" |
| 12 | + /> |
| 13 | + </div> |
| 14 | + <div class="right"> |
| 15 | + <AppActionButton @click="onSaveToHtml"> |
| 16 | + <UniconsFileDownload /> |
| 17 | + </AppActionButton> |
| 18 | + </div> |
| 19 | + </div> |
| 20 | + <div class="body"> |
| 21 | + <iframe |
| 22 | + :srcDoc="srcDoc" |
| 23 | + frameborder="0" |
| 24 | + height="100%" |
| 25 | + width="100%" |
| 26 | + /> |
| 27 | + </div> |
| 28 | + <div |
| 29 | + ref="gutterRef" |
| 30 | + class="gutter-line-horizontal" |
| 31 | + /> |
| 32 | + </div> |
| 33 | +</template> |
| 34 | + |
| 35 | +<script setup lang="ts"> |
| 36 | +import { useSnippetStore } from '@/store/snippets' |
| 37 | +import { computed, ref, onMounted, watch } from 'vue' |
| 38 | +import interact from 'interactjs' |
| 39 | +import { useAppStore } from '@/store/app' |
| 40 | +
|
| 41 | +const snippetStore = useSnippetStore() |
| 42 | +const appStore = useAppStore() |
| 43 | +const srcDoc = ref() |
| 44 | +const height = computed(() => appStore.sizes.codePreviewHeight + 'px') |
| 45 | +
|
| 46 | +const previewRef = ref() |
| 47 | +const gutterRef = ref() |
| 48 | +
|
| 49 | +snippetStore.$subscribe(() => { |
| 50 | + setSrcDoc() |
| 51 | +}) |
| 52 | +
|
| 53 | +watch( |
| 54 | + () => appStore.codePreview.darkMode, |
| 55 | + () => { |
| 56 | + setSrcDoc() |
| 57 | + } |
| 58 | +) |
| 59 | +
|
| 60 | +const setSrcDoc = () => { |
| 61 | + const html = snippetStore.selected?.content.find( |
| 62 | + i => i.language === 'html' |
| 63 | + )?.value |
| 64 | + const css = snippetStore.selected?.content.find( |
| 65 | + i => i.language === 'css' |
| 66 | + )?.value |
| 67 | +
|
| 68 | + const htmlDefault = |
| 69 | + '<div>Add fragments with HTML & CSS languages to view result.</div>' |
| 70 | + const bg = appStore.codePreview.darkMode ? 'background: #263238;' : '' |
| 71 | + const cssDefault = ` |
| 72 | + body { |
| 73 | + display: flex; |
| 74 | + align-items: center; |
| 75 | + justify-content: center; |
| 76 | + height: 100vh; |
| 77 | + margin: 0; |
| 78 | + ${bg} |
| 79 | + } |
| 80 | + ` |
| 81 | +
|
| 82 | + srcDoc.value = `<html> |
| 83 | + <body>${html || htmlDefault}<body> |
| 84 | + <style>${cssDefault + css}<style> |
| 85 | + </html> |
| 86 | + ` |
| 87 | +} |
| 88 | +setSrcDoc() |
| 89 | +
|
| 90 | +const onSaveToHtml = () => { |
| 91 | + const a = document.createElement('a') |
| 92 | +
|
| 93 | + a.href = `data:text/plain;charset=utf-8, ${encodeURIComponent(srcDoc.value)}` |
| 94 | + console.log(a) |
| 95 | + a.download = `${snippetStore.selected?.name}.html` |
| 96 | + a.click() |
| 97 | +} |
| 98 | +
|
| 99 | +onMounted(() => { |
| 100 | + interact(previewRef.value).resizable({ |
| 101 | + edges: { top: true }, |
| 102 | + onmove: e => { |
| 103 | + const { pageY } = e |
| 104 | + appStore.sizes.codePreviewHeight = Math.floor(window.innerHeight - pageY) |
| 105 | + } |
| 106 | + }) |
| 107 | +}) |
| 108 | +</script> |
| 109 | + |
| 110 | +<style lang="scss" scoped> |
| 111 | +.editor-preview { |
| 112 | + position: relative; |
| 113 | + .body { |
| 114 | + height: calc(v-bind(height) - 34px); |
| 115 | + } |
| 116 | + .tools { |
| 117 | + height: 34px; |
| 118 | + display: flex; |
| 119 | + justify-content: space-between; |
| 120 | + align-items: center; |
| 121 | + padding: 4px var(--spacing-xs); |
| 122 | + border-bottom: 1px solid var(--color-border); |
| 123 | + } |
| 124 | + iframe { |
| 125 | + background-color: #fff; |
| 126 | + } |
| 127 | +} |
| 128 | +</style> |
0 commit comments