|
@@ -25,7 +25,7 @@
|
|
|
class="w-full ml-0!"
|
|
class="w-full ml-0!"
|
|
|
size="small"
|
|
size="small"
|
|
|
type="primary"
|
|
type="primary"
|
|
|
- @click="emit('bind')"
|
|
|
|
|
|
|
+ @click="handleOpenVariableSelector"
|
|
|
><LuLink size="12" /> 绑定变量</el-button
|
|
><LuLink size="12" /> 绑定变量</el-button
|
|
|
>
|
|
>
|
|
|
<el-button
|
|
<el-button
|
|
@@ -33,48 +33,72 @@
|
|
|
class="w-full ml-0!"
|
|
class="w-full ml-0!"
|
|
|
size="small"
|
|
size="small"
|
|
|
type="primary"
|
|
type="primary"
|
|
|
- @click="emit('unbind')"
|
|
|
|
|
|
|
+ @click="handleUnbindVariable"
|
|
|
><LuUnlink size="12" /> 解绑变量</el-button
|
|
><LuUnlink size="12" /> 解绑变量</el-button
|
|
|
>
|
|
>
|
|
|
</div>
|
|
</div>
|
|
|
</el-popover>
|
|
</el-popover>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
+
|
|
|
|
|
+ <!-- 变量选择弹窗 -->
|
|
|
|
|
+ <VariableSelectorDialog
|
|
|
|
|
+ v-model="showVariableSelector"
|
|
|
|
|
+ :all-variables="
|
|
|
|
|
+ getAllVariables(projectStore.project?.variables || [], projectStore.activePage?.variables)
|
|
|
|
|
+ "
|
|
|
|
|
+ :current-var-id="currentBoundVarId"
|
|
|
|
|
+ :support-type="variableConfig?.type"
|
|
|
|
|
+ @confirm="handleVariableBindConfirm"
|
|
|
|
|
+ @cancel="showVariableSelector = false"
|
|
|
|
|
+ />
|
|
|
</template>
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
<script setup lang="ts">
|
|
|
import { computed, ref } from 'vue'
|
|
import { computed, ref } from 'vue'
|
|
|
import { ElMessageBox, ElMessage } from 'element-plus'
|
|
import { ElMessageBox, ElMessage } from 'element-plus'
|
|
|
-import { isVariableBound, findVariableById, getAllVariables } from '@/utils/variableBinding'
|
|
|
|
|
|
|
+import {
|
|
|
|
|
+ isVariableBound,
|
|
|
|
|
+ findVariableById,
|
|
|
|
|
+ getAllVariables,
|
|
|
|
|
+ bindVariableToProperty
|
|
|
|
|
+} from '@/utils/variableBinding'
|
|
|
import { useProjectStore } from '@/store/modules/project'
|
|
import { useProjectStore } from '@/store/modules/project'
|
|
|
import { LuPlus, LuLink, LuUnlink } from 'vue-icons-plus/lu'
|
|
import { LuPlus, LuLink, LuUnlink } from 'vue-icons-plus/lu'
|
|
|
import { v4 } from 'uuid'
|
|
import { v4 } from 'uuid'
|
|
|
import { TbVariable } from 'vue-icons-plus/tb'
|
|
import { TbVariable } from 'vue-icons-plus/tb'
|
|
|
|
|
+import VariableSelectorDialog from './VariableSelectorDialog.vue'
|
|
|
|
|
|
|
|
import type { Variable, VariableType } from '@/types/variables'
|
|
import type { Variable, VariableType } from '@/types/variables'
|
|
|
import type { PopoverInstance } from 'element-plus'
|
|
import type { PopoverInstance } from 'element-plus'
|
|
|
|
|
|
|
|
-const props = defineProps<{
|
|
|
|
|
- modelValue: any
|
|
|
|
|
- fieldPath: string
|
|
|
|
|
- canBindVariable?: boolean
|
|
|
|
|
- variableConfig?: {
|
|
|
|
|
- type: VariableType
|
|
|
|
|
- enumMap?: Record<number, string>
|
|
|
|
|
|
|
+const props = withDefaults(
|
|
|
|
|
+ defineProps<{
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 是否允许绑定变量
|
|
|
|
|
+ */
|
|
|
|
|
+ canBindVariable?: boolean
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 变量配置
|
|
|
|
|
+ */
|
|
|
|
|
+ variableConfig?: {
|
|
|
|
|
+ type: VariableType
|
|
|
|
|
+ enumMap?: Record<number, string>
|
|
|
|
|
+ }
|
|
|
|
|
+ }>(),
|
|
|
|
|
+ {
|
|
|
|
|
+ canBindVariable: true
|
|
|
}
|
|
}
|
|
|
-}>()
|
|
|
|
|
|
|
+)
|
|
|
|
|
|
|
|
-const emit = defineEmits<{
|
|
|
|
|
- 'update:modelValue': [value: any]
|
|
|
|
|
- bind: []
|
|
|
|
|
- unbind: []
|
|
|
|
|
-}>()
|
|
|
|
|
|
|
+const modelValue = defineModel<any>()
|
|
|
|
|
|
|
|
const projectStore = useProjectStore()
|
|
const projectStore = useProjectStore()
|
|
|
|
|
|
|
|
const popoverRef = ref<PopoverInstance>()
|
|
const popoverRef = ref<PopoverInstance>()
|
|
|
|
|
+const showVariableSelector = ref(false)
|
|
|
|
|
|
|
|
const isBound = computed(() => {
|
|
const isBound = computed(() => {
|
|
|
- return isVariableBound(props.modelValue)
|
|
|
|
|
|
|
+ return isVariableBound(modelValue.value)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
const boundVariableName = computed(() => {
|
|
const boundVariableName = computed(() => {
|
|
@@ -85,7 +109,7 @@ const boundVariableName = computed(() => {
|
|
|
projectStore.activePage?.variables
|
|
projectStore.activePage?.variables
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- const variable = findVariableById(props.modelValue.varId, allVars)
|
|
|
|
|
|
|
+ const variable = findVariableById(modelValue.value?.varId, allVars)
|
|
|
return variable?.name || ''
|
|
return variable?.name || ''
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -107,12 +131,12 @@ const addPageVariables = () => {
|
|
|
inputErrorMessage: '变量名称只能包含字母、数字和下划线,且必须以字母或下划线开头'
|
|
inputErrorMessage: '变量名称只能包含字母、数字和下划线,且必须以字母或下划线开头'
|
|
|
})
|
|
})
|
|
|
.then(({ value }) => {
|
|
.then(({ value }) => {
|
|
|
- let val = props.modelValue
|
|
|
|
|
|
|
+ let val = modelValue.value
|
|
|
// 如果是枚举
|
|
// 如果是枚举
|
|
|
if (props.variableConfig?.type === 'enum' && props.variableConfig.enumMap) {
|
|
if (props.variableConfig?.type === 'enum' && props.variableConfig.enumMap) {
|
|
|
// 尝试将当前值转换为枚举的key
|
|
// 尝试将当前值转换为枚举的key
|
|
|
const enumKey = Object.keys(props.variableConfig.enumMap).find(
|
|
const enumKey = Object.keys(props.variableConfig.enumMap).find(
|
|
|
- (key) => props.variableConfig?.enumMap?.[key] === props.modelValue
|
|
|
|
|
|
|
+ (key) => props.variableConfig?.enumMap?.[key] === modelValue.value
|
|
|
)
|
|
)
|
|
|
val = enumKey ?? val
|
|
val = enumKey ?? val
|
|
|
}
|
|
}
|
|
@@ -125,7 +149,7 @@ const addPageVariables = () => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
projectStore.activePage?.variables?.push(newVar)
|
|
projectStore.activePage?.variables?.push(newVar)
|
|
|
- emit('update:modelValue', { varId: newVar.id, originValue: val })
|
|
|
|
|
|
|
+ modelValue.value = { varId: newVar.id, originValue: val }
|
|
|
})
|
|
})
|
|
|
.catch(() => {
|
|
.catch(() => {
|
|
|
ElMessage({
|
|
ElMessage({
|
|
@@ -133,6 +157,41 @@ const addPageVariables = () => {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+/**当前变量绑定的变量id */
|
|
|
|
|
+const currentBoundVarId = computed(() => {
|
|
|
|
|
+ const currentValue = modelValue.value
|
|
|
|
|
+ return currentValue && typeof currentValue === 'object' && currentValue.varId
|
|
|
|
|
+ ? currentValue.varId
|
|
|
|
|
+ : ''
|
|
|
|
|
+})
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 变量选择确认后的处理
|
|
|
|
|
+ * @param varId
|
|
|
|
|
+ */
|
|
|
|
|
+const handleVariableBindConfirm = (varId: string) => {
|
|
|
|
|
+ const boundValue = bindVariableToProperty(modelValue.value, varId)
|
|
|
|
|
+ modelValue.value = boundValue
|
|
|
|
|
+ showVariableSelector.value = false
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 解绑变量
|
|
|
|
|
+ */
|
|
|
|
|
+const handleUnbindVariable = () => {
|
|
|
|
|
+ const currentValue = modelValue.value
|
|
|
|
|
+ if (currentValue && typeof currentValue === 'object' && currentValue.varId) {
|
|
|
|
|
+ modelValue.value = currentValue.originValue
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 打开变量选择器
|
|
|
|
|
+ */
|
|
|
|
|
+const handleOpenVariableSelector = () => {
|
|
|
|
|
+ showVariableSelector.value = true
|
|
|
|
|
+}
|
|
|
</script>
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped lang="less">
|
|
<style scoped lang="less">
|