1- <template >
2- <fragment >
3- <iframe
4- ref =" iframe"
5- class =" one-iframe"
6- />
7- <slot />
8- </fragment >
9- </template >
10-
111<script >
12- import { Fragment } from ' vue-frag '
2+ import Vue from ' vue'
133
144export default {
155 name: ' one-iframe' ,
16- components: {
17- Fragment
18- },
196 props: {
20- globalStyle: String
7+ globalStyle: String ,
8+ live: Boolean
219 },
2210 watch: {
2311 globalStyle (value ) {
@@ -27,41 +15,55 @@ export default {
2715 }
2816 },
2917 beforeDestroy () {
30- if (this .contents ) {
31- this .contents .forEach (node => {
32- this .$refs .iframe .parentNode .appendChild (node)
33- })
34- }
35-
3618 if (this .mo ) {
3719 this .mo .disconnect ()
3820 }
3921 },
4022 mounted () {
4123 const links = document .querySelectorAll (' link[rel=stylesheet]' )
4224 const styles = document .querySelectorAll (' style' )
43- const { iframe } = this .$refs
44- const { body , head } = iframe .contentDocument
45- this .contents = this .$el .frag .filter (node => node !== iframe);
46- [... links, ... styles].forEach (node => {
25+ const { head } = this .$el .contentDocument
26+
27+ ;[... links, ... styles].forEach (node => {
4728 const clone = node .cloneNode (true )
4829 head .appendChild (clone)
4930 })
5031
51- this .contents .forEach (node => {
52- body .appendChild (node)
53- })
54-
5532 if (this .globalStyle ) {
5633 const style = document .createElement (' style' )
5734 style .textContent = this .globalStyle
5835 head .appendChild (style)
5936 this .style = style
6037 }
6138
62- this .watchLiveStyle (head)
39+ this .renderContents ()
40+
41+ if (this .live ) {
42+ this .watchLiveStyle (head)
43+ }
44+ },
45+ beforeUpdate () {
46+ this .app .contents = Object .freeze (this .$slots .default )
6347 },
6448 methods: {
49+ renderContents () {
50+ const contents = this .$slots .default
51+ const body = this .$el .contentDocument .body
52+ const el = document .createElement (' div' )
53+ body .appendChild (el)
54+
55+ this .app = new Vue ({
56+ el,
57+ data () {
58+ return {
59+ contents: Object .freeze (contents)
60+ }
61+ },
62+ render (h ) {
63+ return h (' div' , this .contents )
64+ }
65+ })
66+ },
6567 watchLiveStyle (head ) {
6668 this .liveStyle = document .createComment (' ' )
6769 head .appendChild (this .liveStyle )
@@ -84,6 +86,13 @@ export default {
8486 })
8587 this .mo .observe (document .head , { childList: true })
8688 }
89+ },
90+ render (h ) {
91+ return h (' iframe' , {
92+ class: {
93+ ' one-iframe' : true
94+ }
95+ })
8796 }
8897}
8998 </script >
0 commit comments