@bagelink/vue 1.6.2 → 1.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/form/inputs/CodeEditor/Index.vue.d.ts +3 -3
- package/dist/components/form/inputs/CodeEditor/Index.vue.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +37 -21
- package/dist/style.css +1 -1
- package/package.json +1 -1
- package/src/components/form/inputs/CodeEditor/Index.vue +49 -9
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@ import { useHighlight } from './useHighlight'
|
|
|
7
7
|
const props = defineProps({
|
|
8
8
|
language: { type: String as PropType<Language>, default: 'html' },
|
|
9
9
|
readonly: { type: Boolean, default: false },
|
|
10
|
-
modelValue: { type: String, default: '' },
|
|
10
|
+
modelValue: { type: [String, Object] as PropType<string | Record<string, any>>, default: '' },
|
|
11
11
|
autodetect: { type: Boolean, default: true },
|
|
12
12
|
ignoreIllegals: { type: Boolean, default: true },
|
|
13
13
|
label: { type: String, default: '' },
|
|
@@ -17,13 +17,26 @@ const props = defineProps({
|
|
|
17
17
|
})
|
|
18
18
|
|
|
19
19
|
const emit = defineEmits(['update:modelValue'])
|
|
20
|
+
|
|
21
|
+
// Track whether the original modelValue was an object
|
|
22
|
+
const isObjectMode = computed(() => typeof props.modelValue === 'object')
|
|
23
|
+
|
|
24
|
+
// Helper to convert value to string for display
|
|
25
|
+
function valueToString(value: string | Record<string, any>): string {
|
|
26
|
+
if (typeof value === 'string') return value
|
|
27
|
+
if (typeof value === 'object') {
|
|
28
|
+
return JSON.stringify(value, null, 2)
|
|
29
|
+
}
|
|
30
|
+
return ''
|
|
31
|
+
}
|
|
32
|
+
|
|
20
33
|
// State
|
|
21
|
-
const code = ref(props.modelValue
|
|
34
|
+
const code = ref(valueToString(props.modelValue))
|
|
22
35
|
const editorRef = ref<HTMLDivElement>()
|
|
23
36
|
const { loaded, loadHighlight, highlightCode, setTheme } = useHighlight(props.theme)
|
|
24
37
|
|
|
25
38
|
const maxHeight = computed(() => {
|
|
26
|
-
const h = props.height
|
|
39
|
+
const h = props.height
|
|
27
40
|
return h.match(/^\d+$/) ? `${h}px` : h
|
|
28
41
|
})
|
|
29
42
|
|
|
@@ -39,11 +52,24 @@ const formattedCode = computed(() => {
|
|
|
39
52
|
function handleInput(e: Event) {
|
|
40
53
|
const target = e.target as HTMLTextAreaElement
|
|
41
54
|
code.value = target.value
|
|
42
|
-
|
|
55
|
+
|
|
56
|
+
// If originally an object, try to parse and emit object
|
|
57
|
+
if (isObjectMode.value) {
|
|
58
|
+
try {
|
|
59
|
+
const parsed = JSON.parse(code.value)
|
|
60
|
+
emit('update:modelValue', parsed)
|
|
61
|
+
} catch {
|
|
62
|
+
// If parsing fails, emit the string as-is (user is still typing)
|
|
63
|
+
// This allows intermediate invalid JSON states while editing
|
|
64
|
+
emit('update:modelValue', code.value)
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
emit('update:modelValue', code.value)
|
|
68
|
+
}
|
|
43
69
|
}
|
|
44
70
|
|
|
45
71
|
function handleTab(event: KeyboardEvent) {
|
|
46
|
-
if ('Tab'
|
|
72
|
+
if (event.key === 'Tab') { return }
|
|
47
73
|
|
|
48
74
|
event.preventDefault()
|
|
49
75
|
const target = event.target as HTMLTextAreaElement
|
|
@@ -53,11 +79,24 @@ function handleTab(event: KeyboardEvent) {
|
|
|
53
79
|
// Add tab or indent selected text
|
|
54
80
|
const newValue = `${code.value.substring(0, start)} ${code.value.substring(end)}`
|
|
55
81
|
code.value = newValue
|
|
56
|
-
|
|
82
|
+
|
|
83
|
+
// If originally an object, try to parse and emit object
|
|
84
|
+
if (isObjectMode.value) {
|
|
85
|
+
try {
|
|
86
|
+
const parsed = JSON.parse(code.value)
|
|
87
|
+
emit('update:modelValue', parsed)
|
|
88
|
+
} catch {
|
|
89
|
+
// If parsing fails, emit the string as-is
|
|
90
|
+
emit('update:modelValue', code.value)
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
emit('update:modelValue', code.value)
|
|
94
|
+
}
|
|
57
95
|
|
|
58
96
|
// Move cursor position after the inserted tab
|
|
59
97
|
setTimeout(() => {
|
|
60
|
-
target.selectionStart =
|
|
98
|
+
target.selectionStart = start + 2
|
|
99
|
+
target.selectionEnd = start + 2
|
|
61
100
|
}, 0)
|
|
62
101
|
}
|
|
63
102
|
|
|
@@ -68,8 +107,9 @@ onMounted(async () => {
|
|
|
68
107
|
|
|
69
108
|
watch(() => props.theme, (t) => { setTheme(t) })
|
|
70
109
|
watch(() => props.modelValue, (newVal) => {
|
|
71
|
-
|
|
72
|
-
|
|
110
|
+
const newCodeStr = valueToString(newVal)
|
|
111
|
+
if (newCodeStr !== code.value) {
|
|
112
|
+
code.value = newCodeStr
|
|
73
113
|
}
|
|
74
114
|
}, { immediate: true })
|
|
75
115
|
</script>
|