fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
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
<template>
  <a-form :label-col="labelCol" :wrapper-col="wrapperCol">
    <template v-for="(item, i) in renderOptions" :key="i">
      <a-form-item v-if="(item.show as any)(attrForm)" :label="item.label">
        <div :style="getItemStyle(item)">
          <component
            :is="getComponent(item.type)"
            v-model:value="item.value"
            :min="item.min || item.min === 0 ? item.min : -Infinity"
            :max="item.max || item.max === 0 ? item.max : Infinity"
            :step="item.step || 0.1"
            :range="item.range || false"
            :options="item.data || []"
            :units="item.units"
            @change="itemChange(item)"
          >
          </component>
        </div>
        <template v-if="item.extra !== undefined">
          <template v-if="item.extraType === 'string'">{{ item.extra(attrForm) }}</template>
          <component v-else :is="item.extra(attrForm)"></component
        ></template>
      </a-form-item>
    </template>
  </a-form>
</template>
<script setup lang="ts">
import { computed, ref, watchEffect } from "vue"
import { components, GuiItem } from "./index"
 
const props = defineProps<{
  options: GuiItem[]
  labelCol?: number
}>()
 
const emits = defineEmits(["change"])
 
const renderOptions = ref<GuiItem[]>([])
 
const attrForm = computed(() => {
  const modelValues: Record<string, any> = {}
  renderOptions.value.forEach((item) => {
    modelValues[item.field] = item.value
  })
 
  return modelValues
})
 
const labelCol = { span: props.labelCol || 6 }
const wrapperCol = { span: 24 - labelCol.span }
 
watchEffect(() => {
  renderOptions.value = props.options.map((item) => mergeItemOption(item))
})
 
defineExpose({
  // 删除指定 field 或 索引的 元素
  delete: (key: string | number) => {
    let index: number
    if (typeof key === "string") {
      renderOptions.value.forEach((item, i) => {
        if (item.field === key) {
          index = i
        }
      })
    }
    if (typeof key === "number" && key >= 0 && key < renderOptions.value.length) {
      index = key
    }
    if (index !== undefined) {
      renderOptions.value.splice(index, 1)
    }
  },
  // 在指定位置插入 一个 或 多个 元素
  insert(index: number, ...args: GuiItem[]) {
    args.forEach((item) => {
      renderOptions.value.splice(index, 0, mergeItemOption(item))
    })
  },
  updateField(field: string, value) {
    renderOptions.value.forEach((item) => {
      if (item.field === field) {
        item.value = value
      }
    })
  },
  updateExtra(field: string, value) {
    renderOptions.value.forEach((item) => {
      if (item.field === field) {
        item.extra = mergeExtra(value)
      }
    })
  },
  updateFields(fieldObj: any) {
    renderOptions.value.forEach((item) => {
      if (fieldObj[item.field]) {
        item.value = fieldObj[item.field]
      }
    })
  },
  getValue(field: string) {
    const item = renderOptions.value.find((it) => {
      return it.field === field
    })
    if (item) {
      return item.value
    }
    throw new Error("field is not exist")
  },
  getValues() {
    return attrForm.value
  }
})
 
const getItemStyle = ({ extraWidth, extra, label }: GuiItem) => {
  if (!extraWidth && extraWidth !== 0) {
    extraWidth = 100
  }
  return extra !== undefined
    ? {
        width: `calc(100% - ${extraWidth || extraWidth === 0 ? extraWidth : 100}px)`,
        display: "inline-block",
        marginRight: "10px"
      }
    : {
        display: "inline-block",
        width: "100%"
      }
}
 
function getComponent(type: keyof typeof components) {
  return components[type]
}
 
function mergeItemOption(item) {
  // show字段转为function
  if (typeof item.show !== "function") {
    item.show = () => (item.show === undefined ? true : !!item.show)
  }
 
  // extra 字段转为function
  item.extra = mergeExtra(item.extra)
  item.extraType = item.extraType || "string"
  return item
}
 
function mergeExtra(extra) {
  let extraNew = extra
  if (extraNew === undefined || extraNew === null) {
    return undefined
  }
 
  if (typeof extraNew !== "function" && extraNew) {
    extraNew = () => {
      if (typeof extra === "string") {
        let str = extra
        const paramsPattern = /[^{\}]+(?=})/g
        const extractParams = str.match(paramsPattern) || []
        extractParams.forEach((key) => {
          str = str.replace(new RegExp(`{${key}}`, "g"), attrForm.value[key])
        })
        return str
      } else {
        return extra
      }
    }
  }
 
  return extraNew
}
 
function itemChange(item: GuiItem) {
  emits("change", attrForm.value)
  if (item.change) {
    item.change(item.value, attrForm.value)
  }
}
</script>
<script lang="ts">
export default {
  name: "mars-gui"
}
</script>
 
<style lang="less"></style>