|
| 1 | +<template> |
| 2 | + <default-field :field="field" :errors="errors"> |
| 3 | + <template slot="field"> |
| 4 | + <div :id=containerId :style="{height: field.height || '800px'}"></div> |
| 5 | + <p v-if="hasError" class="my-2 text-danger"> |
| 6 | + {{ firstError }} |
| 7 | + </p> |
| 8 | + </template> |
| 9 | + </default-field> |
| 10 | +</template> |
| 11 | + |
| 12 | +<script> |
| 13 | + import { FormField, HandlesValidationErrors } from 'laravel-nova' |
| 14 | +
|
| 15 | + export default { |
| 16 | + mixins: [FormField, HandlesValidationErrors], |
| 17 | +
|
| 18 | + props: ['resourceName', 'resourceId', 'field'], |
| 19 | +
|
| 20 | + created() { |
| 21 | + this.injectUnlayerScript(this.initEditor); |
| 22 | + }, |
| 23 | +
|
| 24 | + computed: { |
| 25 | + containerId: function () { |
| 26 | + return `${this.field.attribute}--editorContainer`; |
| 27 | + } |
| 28 | + }, |
| 29 | +
|
| 30 | + methods: { |
| 31 | + /* |
| 32 | + * Set the initial, internal value for the field. |
| 33 | + */ |
| 34 | + setInitialValue() { |
| 35 | + this.value = JSON.parse(this.field.value) || {}; |
| 36 | + }, |
| 37 | +
|
| 38 | + /** |
| 39 | + * Fill the given FormData object with the field's internal value. |
| 40 | + * @property {FormData} formData |
| 41 | + */ |
| 42 | + fill(formData) { |
| 43 | + formData.append(this.field.attribute, JSON.stringify(this.value)); |
| 44 | + formData.append(`${this.field.attribute}_html`, this.finalHtml); |
| 45 | + }, |
| 46 | +
|
| 47 | + /** |
| 48 | + * Update the field's internal value. |
| 49 | + */ |
| 50 | + handleChange(value) { |
| 51 | + this.value = value |
| 52 | + }, |
| 53 | +
|
| 54 | + /** |
| 55 | + * @param {Function} onLoadCallback |
| 56 | + */ |
| 57 | + injectUnlayerScript(onLoadCallback) { |
| 58 | + const unlayerScript = document.createElement('script'); |
| 59 | + unlayerScript.setAttribute('src', '//editor.unlayer.com/embed.js'); |
| 60 | + unlayerScript.onload = onLoadCallback; |
| 61 | + document.head.appendChild(unlayerScript); |
| 62 | + }, |
| 63 | + initEditor() { |
| 64 | + const unlayerConfig = this.field.config; |
| 65 | + unlayerConfig.id = this.containerId; |
| 66 | + const editExistDesign = this.value && Object.keys(this.value).length; |
| 67 | + if (editExistDesign && unlayerConfig.templateId) { |
| 68 | + this.templateId = unlayerConfig.templateId; |
| 69 | + delete unlayerConfig.templateId; |
| 70 | + } |
| 71 | +
|
| 72 | + window.unlayer.init(unlayerConfig); |
| 73 | +
|
| 74 | + if (editExistDesign) { |
| 75 | + window.unlayer.loadDesign(this.value); |
| 76 | + } |
| 77 | +
|
| 78 | + window.unlayer.addEventListener('design:loaded', this.designLoaded); |
| 79 | + window.unlayer.addEventListener('design:updated', this.designUpdated); |
| 80 | + }, |
| 81 | +
|
| 82 | + /** |
| 83 | + * @param {{design: Object}} loadedDesign |
| 84 | + */ |
| 85 | + designLoaded(loadedDesign) { |
| 86 | + window.unlayer.exportHtml((editorData) => { |
| 87 | + this.finalHtml = editorData.html; |
| 88 | + this.value = editorData.design; |
| 89 | + }); |
| 90 | + }, |
| 91 | +
|
| 92 | + /** |
| 93 | + * @param {{item: Object, type: string}} changeLog |
| 94 | + */ |
| 95 | + designUpdated(changeLog) { |
| 96 | + window.unlayer.exportHtml((editorData) => { |
| 97 | + this.finalHtml = editorData.html; |
| 98 | + this.value = editorData.design; |
| 99 | + }); |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | +</script> |
0 commit comments