-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.mjs
377 lines (318 loc) · 11.9 KB
/
index.mjs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import _ from 'lodash'
import { toRefs, markRaw, ref, computed, nextTick, onMounted, inject } from 'vue'
import TextElement from './TextElement.vue'
import TextareaElement from './TextareaElement.vue'
import ElementSelectOption from './ElementSelectOption.vue'
export default function () {
return [
() => ({
config(config) {
// Can be used on Text & Textarea elements with `view: 'elementSelector'`
config.theme.templates.TextElement_elementSelector = markRaw(TextElement)
config.theme.templates.TextareaElement_elementSelector = markRaw(TextareaElement)
// Add an extra class to Text & Textarea elements
config.theme.classes.TextElement.inputContainer_elementSelector = 'relative'
config.theme.classes.TextareaElement.inputContainer_elementSelector = 'relative'
let TextElement_$inputContainer = config.theme.classes.TextElement.$inputContainer
let TextareaElement_$inputContainer = config.theme.classes.TextareaElement.$inputContainer
// Add those extra classes to $inputContainer
config.theme.classes.TextElement.$inputContainer = (classes, el$) => {
return TextElement_$inputContainer(classes, el$).concat(
el$.elementSelector ? classes.inputContainer_elementSelector : null
)
}
config.theme.classes.TextareaElement.$inputContainer = (classes, el$) => {
return TextareaElement_$inputContainer(classes, el$).concat(
el$.elementSelector ? classes.inputContainer_elementSelector : null
)
}
return config
},
}),
() => ({
apply: ['TextElement', 'TextareaElement'],
props: {
elementSelector: {
type: Boolean,
required: false,
default: false,
},
elementSelectorOptions: {
type: Object,
required: false,
default: () => ({}),
},
},
setup(props, context, component) {
const { elementSelector, elementSelectorOptions, builder } = toRefs(props)
// If the element does not have elementSelector enabled we return
// the original component with adding any extra features.
if (!elementSelector?.value) {
return component
}
const tags = inject('tags')
// ================ DATA ================
/**
* Whether a field is being selected.
*
* @type {boolean}
* @default {false}
*/
const selectingElement = ref(false)
/**
* The field selector's Vueform component instance.
*
* @type {object}
* @default {null}
*/
const elementSelector$ = ref(null)
/**
* The saved position of the cursor.
*
* @type {integer}
* @default {-1}
*/
const cursorPosition = ref(-1)
// ============== COMPUTED ==============
/**
* The flatten list of form elements.
*
* @returns {array}
*/
const fields = computed(() => {
return flattenTree(component.form$.value.builderPagedTree || [])
})
/**
* Exclude the current item from options.
*
* @returns {boolean}
*/
const excludeSelf = computed(() => {
return elementSelectorOptions.value.excludeSelf || false
})
/**
* The path of the currently selected element.
*
* @returns {string}
*/
const selectedElement = computed(() => {
return component.form$.value.selectedElement
})
/**
* The form options for the field selector.
*
* @returns {object}
*/
const elementSelectorForm = computed(() => {
return {
schema: {
field: {
type: 'select',
inputType: 'search',
canClear: false,
canDeselect: false,
search: true,
items: fields.value,
autocomplete: 'off',
placeholder: tags.element_selector_placeholder,
trackBy: 'searchLabel',
caret: false,
object: true,
noResultsText: tags.element_selector_no_results,
addClass: {
select: {
search: 'vfb-field-input',
dropdown: 'vfb-fields-container vfb-fields-dropdown',
option: fields.value.some(f => f.container && f?.children.length) ? '' : 'vfb-field-container-no-nesting',
optionPointed: 'vfb-field-pointed',
optionSelected: 'vfb-field-selected',
optionSelectedPointed: 'vfb-field-pointed vfb-field-selected',
groupLabel: 'vfb-field-group-label-container',
}
},
slots: {
'option': markRaw(ElementSelectOption),
},
onChange(n) {
selectingElement.value = false
let position = cursorPosition.value + n.path.length
let input = component.input.value
let start = input.value.slice(0, cursorPosition.value)
let end = input.value.slice(cursorPosition.value)
// Text before the original cursor
let value = start
// Add the insertion
value += n.path
// Add space after insertion if next part does not start with } or |
if (['}', '|'].indexOf(end.charAt(0)) === -1) {
value += '}'
}
let scrollToText = value
// Add add text after the original cursor
value += end
// Update the element value with text insertion
component.update(value)
// Refocus the input
component.input.value.focus()
// Set the cursor to the original position
nextTick(() => {
setCaretPosition(component.input.value, position + 1)
if (['text', 'search'].indexOf(component.input.value.type) !== -1) {
const measureSpan = document.createElement('span')
measureSpan.style.visibility = 'hidden'
measureSpan.style.position = 'absolute'
measureSpan.style.whiteSpace = 'nowrap'
measureSpan.style.font = component.input.value.font;
document.body.appendChild(measureSpan)
measureSpan.textContent = scrollToText
component.input.value.scrollLeft = measureSpan.offsetWidth - component.input.value.clientWidth
document.body.removeChild(measureSpan)
} else if (component.autosize) {
component.autosize()
}
})
},
// onClose(a,b) {
// nextTick(() => {
// selectingElement.value = false
// nextTick(() => {
// setCaretPosition(component.input.value, cursorPosition.value)
// })
// })
// }
},
}
}
})
// ============== METHODS ===============
/**
* Set the cursor to a certain position in a text or textarea input.
*
* @param {HTMLElement} el - the HTML element
* @param {integer} pos - the position to set
* @returns {void}
*/
const setCaretPosition = (el, pos) => {
if (el) {
if (el.createTextRange) {
let range = el.createTextRange()
range.move('character', pos)
range.select()
} else {
if (el.selectionStart) {
el.focus()
el.setSelectionRange(pos, pos)
} else {
el.focus()
}
}
}
}
const getCharacterBeforeCursor = (el) => {
let lastChar = null
const cursorPosition = el.selectionStart
const value = el.value
if (cursorPosition > 0) {
lastChar = value[cursorPosition - 1]
}
return lastChar
}
/**
* Flattens the tree to be consumable by SelectElement.
*
* @param {array} elements - elements in the tree
* @param {integer} level - current level of elements
* @returns {array}
*/
const flattenTree = (elements = pagedTree.value, level = 0) => {
let paths = []
_.forEach(elements, (el) => {
let path = el.root ? '__VUEFORM_ROOT__' : el.path.replace(/\.0\b/g, () => '.*')
if (excludeSelf.value && path === selectedElement.value.replace(/\.0\b/g, () => '.*')) {
return
}
let field = {
...el,
path,
level,
value: path,
searchLabel: `${el.primaryLabel || ''} ${el.secondaryLabel || ''}`,
}
paths.push(field)
if (['group', 'object', 'table', 'tabs', 'steps', 'root'].indexOf(el.type) !== -1) {
paths = paths.concat(flattenTree(el.children || [], level + 1) || [])
}
if (el.type === 'list' && el.children[0].type === 'object') {
if (selectedElement.value.match(new RegExp(`^${el.path.replace('.', '\\\.')}\\\.`))) {
paths = paths.concat(flattenTree(el.children[0].children || [], level + 1) || [])
}
}
})
return paths
}
/**
* Close the dropdown on Backspace if it is empy.
*
* @returns {void}
*/
const handleSearchKeydown = (e) => {
if (e.key === 'Backspace' && e.target.value == '') {
e.preventDefault()
selectingElement.value = false
nextTick(() => {
setCaretPosition(component.input.value, cursorPosition.value)
})
}
if (e.key === 'Escape') {
setTimeout(() => {
elementSelector$.value.el$('field').input.focus()
}, 0)
}
}
/**
* Close the dropdown on Escape if it is empy.
*
* @returns {void}
*/
const handleSearchKeyup = (e) => {
if (e.key === 'Escape') {
e.preventDefault()
e.stopPropagation()
selectingElement.value = false
nextTick(() => {
setCaretPosition(component.input.value, cursorPosition.value)
})
}
}
/**
* Handles the input's keydown event and shows the field selector dropdown on `[` key.
*
* @param {Event} e - the keydown Event
* @returns {void}
*/
const handleKeydown = (e) => {
if (e.key === '{' || (e.key === 'ArrowDown' && getCharacterBeforeCursor(e.target) === '{')) {
selectingElement.value = true
cursorPosition.value = e.target.selectionStart + 1
setTimeout(() => {
let field$ = elementSelector$.value.el$('field')
field$.input.focus()
field$.input.open()
field$.input.input.addEventListener('keydown', handleSearchKeydown)
field$.input.input.addEventListener('keyup', handleSearchKeyup)
}, 0)
} else {
context.emit('keydown', e, component.el$)
}
}
return {
...component,
handleKeydown,
elementSelector$,
selectingElement,
elementSelectorForm,
}
}
})
]
}