@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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/vue",
3
3
  "type": "module",
4
- "version": "1.6.2",
4
+ "version": "1.6.4",
5
5
  "description": "Bagel core sdk packages",
6
6
  "author": {
7
7
  "name": "Neveh Allon",
@@ -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 ?? '240px'
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
- emit('update:modelValue', code.value)
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' !== event.key) {return}
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
- emit('update:modelValue', code.value)
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 = target.selectionEnd = start + 2
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
- if (newVal !== undefined && newVal !== code.value) {
72
- code.value = newVal
110
+ const newCodeStr = valueToString(newVal)
111
+ if (newCodeStr !== code.value) {
112
+ code.value = newCodeStr
73
113
  }
74
114
  }, { immediate: true })
75
115
  </script>