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