-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathFormField.vue
111 lines (92 loc) · 2.94 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
<template>
<DefaultField
:field="field"
:errors="errors"
:show-help-text="showHelpText"
:full-width-content="true"
@keydown.stop
>
<template #field>
<div
:id="`editor-js-${field.attribute}`"
ref="input"
class="editor-js"
/>
</template>
</DefaultField>
</template>
<script>
import { DependentFormField, FormField, HandlesValidationErrors } from 'laravel-nova';
export default {
mixins: [DependentFormField, FormField, HandlesValidationErrors],
props: ['resourceName', 'resourceId', 'field'],
methods: {
/*
* Set the initial, internal value for the field.
*/
setInitialValue() {
this.value = this.field.value;
const self = this;
const currentContent = (typeof self.field.value === 'object')
? self.field.value
: JSON.parse(self.field.value);
const editor = NovaEditorJS.getInstance({
/**
* Wrapper of Editor
*/
holder: `editor-js-${self.field.attribute}`,
/**
* This Tool will be used as default
*/
defaultBlock: self.field.editorSettings.initialBlock,
/**
* Default placeholder
*/
placeholder: self.field.editorSettings.placeholder,
/**
* Enable autofocus
*/
autofocus: self.field.editorSettings.autofocus,
/**
* Internalization config
*/
i18n: {
/**
* Text direction. If not set, uses ltr
*/
direction: (self.field.editorSettings.rtl ?? false) ? 'rtl' : 'ltr',
},
/**
* Initial Editor data
*/
data: currentContent,
readOnly: self.currentlyIsReadonly,
/**
* Min height of editor
*/
minHeight: 35,
onReady() {
},
onChange() {
editor.save().then((savedData) => {
self.handleChange(savedData);
});
},
}, self.field);
},
/**
* Fill the given FormData object with the field's internal value.
*/
fill(formData) {
const value = typeof this.value === 'string' ? this.value : JSON.stringify(this.value);
formData.append(this.field.attribute, value || '');
},
/**
* Update the field's internal value.
*/
handleChange(value) {
this.value = JSON.stringify(value);
},
},
};
</script>