11<script setup lang="ts">
2- import { computed , onMounted , onUnmounted , ref } from ' vue'
2+ import { computed , onUnmounted , ref } from ' vue'
33
44const props = defineProps <{
5- code : string
5+ initialSource : string
66}>()
77
88const editor = ref <HTMLElement | null >(null )
99const 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
1613function 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
5234onUnmounted (() => {
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
0 commit comments