Skip to content

Commit c14afde

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

2 files changed

Lines changed: 15 additions & 40 deletions

File tree

src/.vuepress/components/EditorFrame.vue

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
<script setup lang="ts">
2-
import { computed, onMounted, onUnmounted, ref } from 'vue'
2+
import { computed, onUnmounted, ref } from 'vue'
33
44
const props = defineProps<{
5-
code: string
5+
initialSource: string
66
}>()
77
88
const editor = ref<HTMLElement | null>(null)
99
const isMaximized = ref(false)
10-
const iframeSrc = ref('data:text/html;base64,')
1110
12-
const editorSrc = computed(() => `/editor.html#${props.code}`)
13-
14-
let readyStateListener: (() => void) | null = null
11+
const iframeSrc = computed(() => `/editor.html#${props.initialSource}`)
1512
1613
function updateBodyOverflow(): void {
1714
document.body.style.overflow = isMaximized.value ? 'hidden' : 'auto'
@@ -22,41 +19,20 @@ function toggleEditor(): void {
2219
updateBodyOverflow()
2320
2421
if (!isMaximized.value) {
25-
editor.value?.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
22+
editor.value?.scrollIntoView({
23+
block: 'nearest',
24+
behavior: 'smooth',
25+
})
2626
}
2727
}
2828
29-
onMounted(() => {
30-
const setIframeSrc = (): void => {
31-
iframeSrc.value = editorSrc.value
32-
}
33-
34-
if (document.readyState === 'complete') {
35-
setIframeSrc()
36-
return
37-
}
38-
39-
readyStateListener = () => {
40-
if (document.readyState === 'complete') {
41-
setIframeSrc()
42-
if (readyStateListener) {
43-
document.removeEventListener('readystatechange', readyStateListener)
44-
readyStateListener = null
45-
}
46-
}
47-
}
48-
49-
document.addEventListener('readystatechange', readyStateListener, { once: true })
50-
})
29+
function closeEditor(): void {
30+
isMaximized.value = false
31+
document.body.style.overflow = 'auto'
32+
}
5133
5234
onUnmounted(() => {
53-
if (readyStateListener) {
54-
document.removeEventListener('readystatechange', readyStateListener)
55-
}
56-
57-
if (isMaximized.value) {
58-
document.body.style.overflow = 'auto'
59-
}
35+
if (isMaximized.value) closeEditor()
6036
})
6137
</script>
6238

@@ -70,7 +46,7 @@ onUnmounted(() => {
7046
>
7147
{{ isMaximized ? '◲' : '◰' }}
7248
</button>
73-
<iframe :src="iframeSrc" title="Editor" loading="lazy" />
49+
<iframe :src="iframeSrc" title="Editor" />
7450
</div>
7551
</template>
7652

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)