mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
3.3 KiB
143 lines
3.3 KiB
2 years ago
|
<script setup lang="ts">
|
||
|
import { Modal as AModal } from 'ant-design-vue'
|
||
|
import Editor from '~/components/monaco/Editor.vue'
|
||
2 years ago
|
import { ReadonlyInj, computed, inject, ref, useVModel, watch } from '#imports'
|
||
2 years ago
|
import { EditModeInj, IsFormInj } from '~/context'
|
||
2 years ago
|
|
||
|
interface Props {
|
||
2 years ago
|
modelValue: string | Record<string, any> | undefined
|
||
2 years ago
|
}
|
||
|
|
||
|
interface Emits {
|
||
2 years ago
|
(event: 'update:modelValue', model: string): void
|
||
2 years ago
|
}
|
||
|
|
||
|
const props = defineProps<Props>()
|
||
|
|
||
|
const emits = defineEmits<Emits>()
|
||
|
|
||
2 years ago
|
const editEnabled = inject(EditModeInj, ref(false))
|
||
|
|
||
2 years ago
|
const isForm = inject(IsFormInj, ref(false))
|
||
|
|
||
2 years ago
|
const readonly = inject(ReadonlyInj)
|
||
2 years ago
|
|
||
2 years ago
|
const vModel = useVModel(props, 'modelValue', emits)
|
||
2 years ago
|
|
||
2 years ago
|
const localValueState = ref<string | undefined>()
|
||
|
|
||
2 years ago
|
let error = $ref<string | undefined>()
|
||
|
|
||
|
let isExpanded = $ref(false)
|
||
|
|
||
2 years ago
|
const localValue = computed<string | Record<string, any> | undefined>({
|
||
|
get: () => localValueState.value,
|
||
|
set: (val: undefined | string | Record<string, any>) => {
|
||
|
localValueState.value = typeof val === 'object' ? JSON.stringify(val, null, 2) : val
|
||
2 years ago
|
/** if form and not expanded then sync directly */
|
||
|
if (isForm.value && !isExpanded) {
|
||
|
vModel.value = val
|
||
|
}
|
||
2 years ago
|
},
|
||
|
})
|
||
|
|
||
2 years ago
|
const clear = () => {
|
||
|
error = undefined
|
||
2 years ago
|
|
||
2 years ago
|
isExpanded = false
|
||
2 years ago
|
|
||
2 years ago
|
editEnabled.value = false
|
||
2 years ago
|
|
||
2 years ago
|
localValue.value = vModel.value
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const formatJson = (json: string) => {
|
||
|
try {
|
||
|
return JSON.stringify(JSON.parse(json), null, 2)
|
||
|
} catch (e) {
|
||
|
return json
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
const onSave = () => {
|
||
|
isExpanded = false
|
||
2 years ago
|
|
||
2 years ago
|
editEnabled.value = false
|
||
2 years ago
|
|
||
|
localValue.value = localValue ? formatJson(localValue.value as string) : localValue
|
||
|
|
||
|
vModel.value = localValue.value
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
watch(
|
||
2 years ago
|
vModel,
|
||
2 years ago
|
(val) => {
|
||
2 years ago
|
localValue.value = val
|
||
2 years ago
|
},
|
||
2 years ago
|
{ immediate: true },
|
||
2 years ago
|
)
|
||
|
|
||
2 years ago
|
watch(localValue, (val) => {
|
||
2 years ago
|
try {
|
||
2 years ago
|
JSON.parse(val as string)
|
||
|
|
||
2 years ago
|
error = undefined
|
||
|
} catch (e: any) {
|
||
|
error = e
|
||
|
}
|
||
|
})
|
||
2 years ago
|
|
||
2 years ago
|
watch(editEnabled, () => {
|
||
|
isExpanded = false
|
||
2 years ago
|
|
||
|
localValue.value = vModel.value
|
||
2 years ago
|
})
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<component :is="isExpanded ? AModal : 'div'" v-model:visible="isExpanded" :closable="false" centered :footer="null">
|
||
2 years ago
|
<div v-if="editEnabled && !readonly" class="flex flex-col w-full">
|
||
2 years ago
|
<div class="flex flex-row justify-between pt-1 pb-2">
|
||
|
<a-button type="text" size="small" @click="isExpanded = !isExpanded">
|
||
2 years ago
|
<CilFullscreenExit v-if="isExpanded" class="h-2.5" />
|
||
|
|
||
|
<CilFullscreen v-else class="h-2.5" />
|
||
2 years ago
|
</a-button>
|
||
2 years ago
|
|
||
2 years ago
|
<div v-if="!isForm || isExpanded" class="flex flex-row">
|
||
2 years ago
|
<a-button type="text" size="small" :onclick="clear"><div class="text-xs">Cancel</div></a-button>
|
||
2 years ago
|
|
||
2 years ago
|
<a-button type="primary" size="small" :disabled="!!error || localValue === vModel">
|
||
|
<div class="text-xs" :onclick="onSave">Save</div>
|
||
|
</a-button>
|
||
2 years ago
|
</div>
|
||
|
</div>
|
||
2 years ago
|
|
||
2 years ago
|
<Editor
|
||
|
:model-value="localValue"
|
||
|
class="min-w-full w-80"
|
||
|
:class="{ 'expanded-editor': isExpanded, 'editor': !isExpanded }"
|
||
|
:hide-minimap="true"
|
||
2 years ago
|
:disable-deep-compare="true"
|
||
2 years ago
|
@update:model-value="localValue = $event"
|
||
|
/>
|
||
2 years ago
|
|
||
2 years ago
|
<span v-if="error" class="text-xs w-full py-1 text-red-500">
|
||
2 years ago
|
{{ error.toString() }}
|
||
2 years ago
|
</span>
|
||
|
</div>
|
||
2 years ago
|
|
||
2 years ago
|
<span v-else>{{ vModel }}</span>
|
||
|
</component>
|
||
|
</template>
|
||
|
|
||
|
<style scoped lang="scss">
|
||
|
.expanded-editor {
|
||
|
min-height: min(600px, 80vh);
|
||
|
}
|
||
|
|
||
|
.editor {
|
||
|
min-height: min(200px, 10vh);
|
||
|
}
|
||
|
</style>
|