Skip to content

Commit 7278951

Browse files
committed
minor fixes & cleanups for editor
1 parent 9848906 commit 7278951

2 files changed

Lines changed: 21 additions & 37 deletions

File tree

src/.vuepress/components/EditorFrame.vue

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
<script setup lang="ts">
2-
import { computed, onMounted, onUnmounted, ref } from 'vue'
2+
import { computed, onActivated, onDeactivated, onUnmounted, ref } from 'vue'
33
44
const props = defineProps<{
5-
code: string
5+
initialSource: string
66
}>()
77
88
const editor = ref<HTMLElement | null>(null)
9+
const iframeKey = ref(0)
910
const isMaximized = ref(false)
10-
const iframeSrc = ref('data:text/html;base64,')
1111
12-
const editorSrc = computed(() => `/editor.html#${props.code}`)
13-
14-
let readyStateListener: (() => void) | null = null
12+
const iframeSrc = computed(() => `/editor.html#${props.initialSource}`)
1513
1614
function updateBodyOverflow(): void {
1715
document.body.style.overflow = isMaximized.value ? 'hidden' : 'auto'
@@ -22,41 +20,28 @@ function toggleEditor(): void {
2220
updateBodyOverflow()
2321
2422
if (!isMaximized.value) {
25-
editor.value?.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
23+
editor.value?.scrollIntoView({
24+
block: 'nearest',
25+
behavior: 'smooth',
26+
})
2627
}
2728
}
2829
29-
onMounted(() => {
30-
const setIframeSrc = (): void => {
31-
iframeSrc.value = editorSrc.value
32-
}
33-
34-
if (document.readyState === 'complete') {
35-
setIframeSrc()
36-
return
37-
}
30+
function closeEditor(): void {
31+
isMaximized.value = false
32+
document.body.style.overflow = 'auto'
33+
}
3834
39-
readyStateListener = () => {
40-
if (document.readyState === 'complete') {
41-
setIframeSrc()
42-
if (readyStateListener) {
43-
document.removeEventListener('readystatechange', readyStateListener)
44-
readyStateListener = null
45-
}
46-
}
47-
}
35+
onActivated(() => {
36+
iframeKey.value += 1
37+
})
4838
49-
document.addEventListener('readystatechange', readyStateListener, { once: true })
39+
onDeactivated(() => {
40+
if (isMaximized.value) closeEditor()
5041
})
5142
5243
onUnmounted(() => {
53-
if (readyStateListener) {
54-
document.removeEventListener('readystatechange', readyStateListener)
55-
}
56-
57-
if (isMaximized.value) {
58-
document.body.style.overflow = 'auto'
59-
}
44+
if (isMaximized.value) closeEditor()
6045
})
6146
</script>
6247

@@ -70,7 +55,7 @@ onUnmounted(() => {
7055
>
7156
{{ isMaximized ? '◲' : '◰' }}
7257
</button>
73-
<iframe :src="iframeSrc" title="Editor" loading="lazy" />
58+
<iframe :key="iframeKey" :src="iframeSrc" title="Editor" />
7459
</div>
7560
</template>
7661

src/.vuepress/config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { defaultTheme } from '@vuepress/theme-default'
1111
import { defineUserConfig } from 'vuepress'
1212

1313
import type { Plugin } from '@vuepress/core'
14-
import type { Markdown } from '@vuepress/markdown'
1514

1615
import navbar from './nav'
1716
import sidebar from './sidebar'
@@ -97,7 +96,7 @@ export default defineUserConfig({
9796
function assemblyScriptEditorPlugin(): Plugin {
9897
return {
9998
name: 'as-editor',
100-
extendsMarkdown(md: Markdown) {
99+
extendsMarkdown(md) {
101100
const defaultFence = md.renderer.rules.fence
102101

103102
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
@@ -111,7 +110,7 @@ function assemblyScriptEditorPlugin(): Plugin {
111110
}
112111

113112
const encoded = Buffer.from(token.content, 'utf8').toString('base64')
114-
return `<EditorFrame code="${encoded}" />`
113+
return `<EditorFrame initialSource="${encoded}" />`
115114
}
116115
},
117116
}

0 commit comments

Comments
 (0)