Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snippets): add html & css renderer to snippets preview #126

Merged
merged 3 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions config/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ export default defineConfig({
resolvers: [
IconsResolver({
prefix: '',
customCollections: ['unicons']
customCollections: ['unicons', 'svg']
})
]
}),
Icons({
customCollections: {
unicons: FileSystemIconLoader(
'./node_modules/@iconscout/unicons/svg/line'
)
),
svg: FileSystemIconLoader(pathSrc + '/assets/svg')
}
})
],
Expand Down
9 changes: 9 additions & 0 deletions src/main/menu/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ const editorMenu: MenuItemConstructorOptions[] = [
'main-menu:preview-markdown'
)
}
},
{
label: 'Preview Code',
accelerator: 'Shift+CommandOrControl+P',
click: () => {
BrowserWindow.getFocusedWindow()?.webContents.send(
'main-menu:preview-code'
)
}
}
]

Expand Down
4 changes: 4 additions & 0 deletions src/renderer/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ ipc.on('main-menu:preview-markdown', async () => {
}
})

ipc.on('main-menu:preview-code', () => {
snippetStore.isCodePreview = !snippetStore.isCodePreview
})

ipc.on('main-menu:copy-snippet', () => {
onCopySnippet()
})
Expand Down
19 changes: 19 additions & 0 deletions src/renderer/assets/scss/base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ h6 {
z-index: 10;
}
}
.gutter-line-horizontal {
position: absolute;
top: 0;
left: 0;
right: 0;
width: 100%;
height: 1px;
background-color: var(--color-border);
cursor: row-resize;
&::after {
content: '';
display: block;
height: 8px;
width: 100%;
position: absolute;
top: -2px;
z-index: 10;
}
}

.desc {
font-size: var(--text-sm);
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/assets/svg/arrow-slash.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
128 changes: 128 additions & 0 deletions src/renderer/components/editor/EditorPreview.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<template>
<div
ref="previewRef"
class="editor-preview"
>
<div class="tools">
<div class="left">
<AppCheckbox
v-model="appStore.codePreview.darkMode"
name="Dark mode"
label="Dark mode"
/>
</div>
<div class="right">
<AppActionButton @click="onSaveToHtml">
<UniconsFileDownload />
</AppActionButton>
</div>
</div>
<div class="body">
<iframe
:srcDoc="srcDoc"
frameborder="0"
height="100%"
width="100%"
/>
</div>
<div
ref="gutterRef"
class="gutter-line-horizontal"
/>
</div>
</template>

<script setup lang="ts">
import { useSnippetStore } from '@/store/snippets'
import { computed, ref, onMounted, watch } from 'vue'
import interact from 'interactjs'
import { useAppStore } from '@/store/app'

const snippetStore = useSnippetStore()
const appStore = useAppStore()
const srcDoc = ref()
const height = computed(() => appStore.sizes.codePreviewHeight + 'px')

const previewRef = ref()
const gutterRef = ref()

snippetStore.$subscribe(() => {
setSrcDoc()
})

watch(
() => appStore.codePreview.darkMode,
() => {
setSrcDoc()
}
)

const setSrcDoc = () => {
const html = snippetStore.selected?.content.find(
i => i.language === 'html'
)?.value
const css = snippetStore.selected?.content.find(
i => i.language === 'css'
)?.value

const htmlDefault =
'<div>Add fragments with HTML & CSS languages to view result.</div>'
const bg = appStore.codePreview.darkMode ? 'background: #263238;' : ''
const cssDefault = `
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
${bg}
}
`

srcDoc.value = `<html>
<body>${html || htmlDefault}<body>
<style>${cssDefault + css}<style>
</html>
`
}
setSrcDoc()

const onSaveToHtml = () => {
const a = document.createElement('a')

a.href = `data:text/plain;charset=utf-8, ${encodeURIComponent(srcDoc.value)}`
console.log(a)
a.download = `${snippetStore.selected?.name}.html`
a.click()
}

onMounted(() => {
interact(previewRef.value).resizable({
edges: { top: true },
onmove: e => {
const { pageY } = e
appStore.sizes.codePreviewHeight = Math.floor(window.innerHeight - pageY)
}
})
})
</script>

<style lang="scss" scoped>
.editor-preview {
position: relative;
.body {
height: calc(v-bind(height) - 34px);
}
.tools {
height: 34px;
display: flex;
justify-content: space-between;
align-items: center;
padding: 4px var(--spacing-xs);
border-bottom: 1px solid var(--color-border);
}
iframe {
background-color: #fff;
}
}
</style>
6 changes: 5 additions & 1 deletion src/renderer/components/editor/TheEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ const editorHeight = computed(() => {
forceRefresh.value

let result =
appStore.sizes.editor.titleHeight +
appStore.sizes.titlebar +
appStore.sizes.editor.titleHeight +
appStore.sizes.editor.footerHeight

if (snippetStore.isFragmentsShow) {
Expand All @@ -99,6 +99,10 @@ const editorHeight = computed(() => {
result += appStore.sizes.editor.descriptionHeight
}

if (snippetStore.isCodePreview) {
result += appStore.sizes.codePreviewHeight
}

return window.innerHeight - result + 'px'
})

Expand Down
10 changes: 8 additions & 2 deletions src/renderer/components/snippets/SnippetHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
<UniconsCamera v-if="!snippetStore.isScreenshotPreview" />
<UniconsCameraSlash v-else />
</AppActionButton>
<AppActionButton @click="onCopySnippet">
<UniconsArrow />
<AppActionButton @click="onCodePreview">
<SvgArrowSlash v-if="snippetStore.isCodePreview" />
<UniconsArrow v-else />
</AppActionButton>
<AppActionButton @click="onAddDescription">
<UniconsText />
Expand Down Expand Up @@ -53,6 +54,7 @@ import { useSnippetStore } from '@/store/snippets'
import { useDebounceFn } from '@vueuse/core'
import { computed, onUnmounted, ref } from 'vue'
import { useAppStore } from '@/store/app'
import { track } from '@/electron'

const snippetStore = useSnippetStore()
const appStore = useAppStore()
Expand All @@ -77,6 +79,10 @@ const onClickMarkdownPreview = () => {
const onClickScreenshotPreview = () => {
snippetStore.isScreenshotPreview = !snippetStore.isScreenshotPreview
}
const onCodePreview = () => {
snippetStore.isCodePreview = !snippetStore.isCodePreview
track('snippets/code-preview')
}

emitter.on('snippet:focus-name', () => {
inputRef.value?.select()
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/components/snippets/SnippetsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
>
<template v-if="snippetStore.selected">
<SnippetHeader />
<!-- TODO: упростить условия отображения -->
<TheEditor
v-if="
!snippetStore.isMarkdownPreview && !snippetStore.isScreenshotPreview
Expand All @@ -18,6 +19,13 @@
:is-search-mode="isSearchMode"
:fragments="snippetStore.isFragmentsShow"
/>
<EditorPreview
v-if="
snippetStore.isCodePreview &&
!snippetStore.isScreenshotPreview &&
!snippetStore.isMarkdownPreview
"
/>
<TheMarkdown
v-if="
snippetStore.isMarkdownPreview && !snippetStore.isScreenshotPreview
Expand Down
7 changes: 7 additions & 0 deletions src/renderer/store/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { platform, store } from '@/electron'
import type {
CodePreviewSettings,
EditorSettings,
ScreenshotSettings,
State,
Expand Down Expand Up @@ -29,6 +30,10 @@ const SCREENSHOT_DEFAULTS: ScreenshotSettings = {
width: 600
}

const CODE_PREVIEW_DEFAULTS: CodePreviewSettings = {
darkMode: false
}

export const useAppStore = defineStore('app', {
state: (): State => ({
isInit: false,
Expand All @@ -38,6 +43,7 @@ export const useAppStore = defineStore('app', {
titlebar: 15,
sidebar: 180,
snippetList: 250,
codePreviewHeight: 200,
editor: {
titleHeight: 34,
fragmentsHeight: 25,
Expand All @@ -48,6 +54,7 @@ export const useAppStore = defineStore('app', {
},
editor: EDITOR_DEFAULTS,
screenshot: SCREENSHOT_DEFAULTS,
codePreview: CODE_PREVIEW_DEFAULTS,
selectedPreferencesMenu: 'storage',
version,
platform: platform()
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/store/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const useSnippetStore = defineStore('snippets', {
sort: 'updatedAt',
isContextState: false,
isMarkdownPreview: false,
isScreenshotPreview: false
isScreenshotPreview: false,
isCodePreview: false
}),

getters: {
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/main/analytics.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type SnippetEvents =
| 'search'
| 'set-language'
| 'create-screenshot'
| 'code-preview'
type FolderEvents = 'add-new' | 'delete' | 'set-language'
type TagEvents = 'add-new' | 'delete'
type AppEvents =
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/main/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type MainMenuAction =
| 'new-snippet'
| 'preferences'
| 'preview-markdown'
| 'preview-code'
| 'search'
| 'sort-snippets'

Expand Down
6 changes: 6 additions & 0 deletions src/shared/types/renderer/store/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface AppSizes {
titlebar: number
sidebar: number
snippetList: number
codePreviewHeight: number
editor: {
titleHeight: number
fragmentsHeight: number
Expand Down Expand Up @@ -54,6 +55,10 @@ export interface ScreenshotSettings {
width: number
}

export interface CodePreviewSettings {
darkMode: boolean
}

export interface State {
platform: NodeJS.Platform
theme: Theme
Expand All @@ -63,5 +68,6 @@ export interface State {
editor: EditorSettings
selectedPreferencesMenu: string
screenshot: ScreenshotSettings
codePreview: CodePreviewSettings
isInit: boolean
}
1 change: 1 addition & 0 deletions src/shared/types/renderer/store/snippets.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface State {
isContextState: boolean
isMarkdownPreview: boolean
isScreenshotPreview: boolean
isCodePreview: boolean
}