-
Notifications
You must be signed in to change notification settings - Fork 8
/
FormField.vue
191 lines (164 loc) · 6.34 KB
/
FormField.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<template>
<default-field :field="field" :errors="errors" :full-width-content="true">
<template slot="field">
<div class="unlayerControls flex">
<button
id="fullscreenToggleButton"
class="text-xs bg-90 hover:bg-black text-white font-semibold rounded-sm px-4 py-1 m-1 form-input-bordered"
@click="toggleFullscreen"
type="button">
{{ fullscreenButtonText.on }}
</button>
</div>
<div :id=containerId :style="{height: field.height || '800px'}" class="form-input-bordered"></div>
<p v-if="hasError" class="my-2 text-danger">
{{ firstError }}
</p>
</template>
</default-field>
</template>
<script>
import { FormField, HandlesValidationErrors } from 'laravel-nova'
export default {
mixins: [FormField, HandlesValidationErrors],
props: ['resourceName', 'resourceId', 'field'],
data: () => ({
fullscreenButtonText: {
on: '▶ Enter fullscreen',
off: '✖︎ Exit fullscreen',
},
}),
created() {
this.injectUnlayerScript(this.initEditor);
},
computed: {
containerId: function () {
return `${this.field.attribute}--editorContainer`;
}
},
methods: {
toggleFullscreen() {
// toggle scrolling of the page
document.body.classList.toggle('overflow-hidden');
const unlayerEditorContainer = this.$el.querySelector(`#${this.containerId}`);
unlayerEditorContainer.classList.toggle('z-50');
unlayerEditorContainer.classList.toggle('fullscreen');
const controls = this.$el.querySelector('.unlayerControls');
controls.classList.toggle('stickyControls');
const toggleButton = controls.querySelector(`#fullscreenToggleButton`);
unlayerEditorContainer.classList.contains('fullscreen')
? toggleButton.innerText = this.fullscreenButtonText.off
: toggleButton.innerText = this.fullscreenButtonText.on;
},
/**
* Set the initial, internal value for the field.
*/
setInitialValue() {
this.value = this.field.value ? JSON.parse(this.field.value) : {};
},
/**
* Fill the given FormData object with the field's internal value.
* @property {FormData} formData
*/
fill(formData) {
formData.append(this.field.attribute, JSON.stringify(this.value));
formData.append(`${this.field.attribute}_html`, this.finalHtml);
},
/**
* Update the field's internal value.
*/
handleChange(value) {
this.value = value
},
/**
* @param {Function} onLoadCallback
*/
injectUnlayerScript(onLoadCallback) {
const unlayerScript = document.createElement('script');
unlayerScript.setAttribute('src', '//editor.unlayer.com/embed.js');
unlayerScript.onload = onLoadCallback;
document.head.appendChild(unlayerScript);
},
/**
* Init unlayer editor and add event listeners
*/
initEditor() {
const unlayerConfig = this.field.config;
unlayerConfig.id = this.containerId;
const editExistDesign = this.value && Object.keys(this.value).length;
if (editExistDesign && unlayerConfig.templateId) {
this.templateId = unlayerConfig.templateId;
delete unlayerConfig.templateId;
}
window.unlayer.init(unlayerConfig);
if (editExistDesign) {
window.unlayer.loadDesign(this.value);
}
/** @see https://docs.unlayer.com/docs/events */
window.unlayer.addEventListener('design:loaded', this.designLoaded);
window.unlayer.addEventListener('design:updated', this.designUpdated);
window.unlayer.addEventListener('onImageUpload', this.imageUpload);
},
/**
* @param {{design: Object}} loadedDesign
*/
designLoaded(loadedDesign) {
Nova.$emit('unlayer:design:loaded', {
inputName: this.field.attribute,
payload: loadedDesign,
});
window.unlayer.exportHtml((editorData) => {
this.finalHtml = editorData.html;
this.value = editorData.design;
});
},
/**
* @param {{item: Object, type: string}} changeLog
*/
designUpdated(changeLog) {
Nova.$emit('unlayer:design:updated', {
inputName: this.field.attribute,
payload: changeLog,
});
this.exportHtml();
},
/**
* Build HTML based on current JSON config
*/
exportHtml() {
window.unlayer.exportHtml((editorData) => {
this.finalHtml = editorData.html;
this.value = editorData.design;
Nova.$emit('unlayer:html:exported', {
inputName: this.field.attribute,
payload: editorData,
});
});
},
/**
* @param {Object} imageData
*/
imageUpload(imageData) {
Nova.$emit('unlayer:image:uploaded', {
inputName: this.field.attribute,
payload: imageData,
});
},
},
}
</script>
<style scoped>
.fullscreen {
position: fixed;
left: 1vw;
top: 1vh;
width: 98vw !important;
height: 98vh !important;
}
.stickyControls {
position: fixed;
left: 1vw;
top: 1vh;
z-index: 51;
}
</style>