@@ -16,6 +16,9 @@ export default {
16
16
data () {
17
17
return {
18
18
loading: true ,
19
+ token: ' public' ,
20
+ unlocked: false ,
21
+ snapshot: {},
19
22
}
20
23
},
21
24
components: {
@@ -26,42 +29,106 @@ export default {
26
29
this .$refs .iframe .contentWindow .postMessage (data, ' *' )
27
30
},
28
31
initEditor () {
32
+ this .snapshot = {... this .snippet }
29
33
this .$refs .iframe .onload = () => {
30
- this .send ({
31
- action: ' custom' ,
32
- value: [{
33
- id: ' save' ,
34
- icon: ' content-save' ,
35
- }, {
36
- id: ' fullscreen' ,
37
- icon: ' fullscreen' ,
38
- align: ' right'
39
- }]
40
- })
41
- this .send ({ action: ' config' , field: ' title' , value: this .snippet .title })
34
+ this .updateControls ()
35
+ this .send ({ action: ' title' , value: this .snippet .title })
36
+ this .send ({ action: ' author' , value: this .snippet .author })
42
37
this .send ({ action: ' code' , value: this .snippet .code })
38
+ this .send ({ action: ' config' , field: ' readonly' , value: this .locked })
43
39
this .send ({ action: ' run' })
44
40
this .loading = false
45
41
}
46
42
},
43
+ updateControls () {
44
+ const controls = []
45
+
46
+ if (this .new ) {
47
+ controls .push ({
48
+ id: ' publish' ,
49
+ icon: ' cloud-upload'
50
+ })
51
+ } else if (this .unsaved ) {
52
+ controls .push ({
53
+ id: ' save' ,
54
+ icon: ' content-save' ,
55
+ })
56
+ }
57
+
58
+ controls .push ({
59
+ id: ' lock' ,
60
+ icon: this .public
61
+ ? ' earth'
62
+ : this .unlocked
63
+ ? ' lock-open-variant-outline'
64
+ : ' lock-outline' ,
65
+ align: ' right'
66
+ })
67
+
68
+ if (! this .new ){
69
+ controls .push ({
70
+ id: ' fork' ,
71
+ icon: ' source-fork' ,
72
+ align: ' right'
73
+ })
74
+ }
75
+
76
+ controls .push ({
77
+ id: ' fullscreen' ,
78
+ icon: ' fullscreen' ,
79
+ align: ' right'
80
+ })
81
+
82
+ controls .push ({
83
+ id: ' close' ,
84
+ icon: ' close' ,
85
+ align: ' right'
86
+ })
87
+
88
+ this .send ({
89
+ action: ' custom' ,
90
+ field: ' set' ,
91
+ value: controls,
92
+ })
93
+ },
47
94
registerListener () {
48
95
window .addEventListener (' message' , this .onListener )
49
96
},
50
97
unregisterListener (){
51
98
window .removeEventListener (' message' , this .onListener )
52
99
},
53
100
onListener (e ) {
54
- const { action , source , id } = e .data
101
+ const { action , source , id , value } = e .data
55
102
if (source !== ' wenyan-ide' )
56
103
return
57
- if (action === ' custom' ){
104
+ if (action === ' info' ) {
105
+ this .snapshot .code = value .code
106
+ this .snapshot .title = value .title
107
+ this .snapshot .author = value .author
108
+ this .updateControls ()
109
+ }
110
+ else if (action === ' custom' ){
58
111
switch (id){
59
112
case ' fullscreen' :
60
113
this .$emit (' fullscreen' , true )
114
+ break
61
115
}
62
116
}
63
117
}
64
118
},
119
+ computed: {
120
+ public () {
121
+ return this .snippet .token === ' public'
122
+ },
123
+ new () {
124
+ return this .snippet .id == null
125
+ },
126
+ unsaved () {
127
+ return this .snippet .code !== this .snapshot .code
128
+ || this .snippet .title !== this .snapshot .title
129
+ || this .snippet .author !== this .snapshot .author
130
+ }
131
+ },
65
132
watch: {
66
133
snippet () {
67
134
this .initEditor ()
0 commit comments