FillAreaSetDialog.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <a-modal v-model:open="visible" :width="334" centered>
  3. <template #title> 填涂区设置 </template>
  4. <a-form
  5. ref="formRef"
  6. :model="formData"
  7. :rules="rules"
  8. :label-col="{ style: { width: '60px' } }"
  9. >
  10. <a-form-item label="未填涂">
  11. <div
  12. class="color-block color-unfill"
  13. :style="{ backgroundColor: formData.unfillColor }"
  14. @click="onEditColor('unfillColor')"
  15. ></div>
  16. <a-checkbox v-model:checked="formData.unfillShow">显示</a-checkbox>
  17. </a-form-item>
  18. <a-form-item label="已填涂">
  19. <div
  20. class="color-block color-fill"
  21. :style="{ backgroundColor: formData.fillColor }"
  22. @click="onEditColor('fillColor')"
  23. ></div>
  24. <a-checkbox v-model:checked="formData.fillShow">显示</a-checkbox>
  25. </a-form-item>
  26. <a-form-item name="borderWidth" label="宽度">
  27. <a-input-number
  28. v-model:value="formData.borderWidth"
  29. style="width: 40px; margin-right: 4px"
  30. :min="1"
  31. :max="9"
  32. :precision="0"
  33. :controls="false"
  34. />
  35. <span class="input-tips">像素</span>
  36. </a-form-item>
  37. </a-form>
  38. <template #footer>
  39. <div class="box-justify">
  40. <a-button type="link" @click="initSet">恢复默认值</a-button>
  41. <div>
  42. <a-button type="text" @click="close">取消</a-button>
  43. <a-button type="primary" @click="confirm">确认</a-button>
  44. </div>
  45. </div>
  46. </template>
  47. </a-modal>
  48. <ColorPickerDialog
  49. ref="colorPickerDialogRef"
  50. :color="curColor"
  51. @confirm="onColorConfirm"
  52. />
  53. </template>
  54. <script setup lang="ts">
  55. import { reactive, ref, watch, UnwrapRef } from "vue";
  56. import { message } from "ant-design-vue";
  57. import useModal from "@/hooks/useModal";
  58. import { useUserStore } from "@/store";
  59. import { objModifyAssign } from "@/utils/tool";
  60. import ColorPickerDialog from "./ColorPickerDialog.vue";
  61. defineOptions({
  62. name: "FillAreaSetDialog",
  63. });
  64. /* modal */
  65. const { visible, open, close } = useModal();
  66. defineExpose({ open, close });
  67. const userStore = useUserStore();
  68. const defaultFormData = {
  69. fillColor: "#f53f3f",
  70. fillShow: true,
  71. unfillColor: "#165DFF",
  72. unfillShow: false,
  73. borderWidth: 2,
  74. };
  75. const emit = defineEmits(["modified"]);
  76. type FormDataType = typeof defaultFormData;
  77. const formRef = ref();
  78. const formData: UnwrapRef<FormDataType> = reactive({
  79. ...defaultFormData,
  80. });
  81. const rules: FormRules<keyof FormDataType> = {
  82. borderWidth: [
  83. {
  84. required: true,
  85. message: "请输入",
  86. trigger: "change",
  87. },
  88. ],
  89. };
  90. function initSet() {
  91. objModifyAssign(formData, defaultFormData);
  92. }
  93. async function confirm() {
  94. const valid = await formRef.value?.validate().catch(() => false);
  95. if (!valid) return;
  96. userStore.setRecogFillSet(formData);
  97. message.success("保存成功!");
  98. emit("modified", formData);
  99. close();
  100. }
  101. // color picker
  102. type EditType = "fillColor" | "unfillColor";
  103. const curColor = ref("");
  104. const colorPickerDialogRef = ref();
  105. const editType = ref<EditType>("fillColor");
  106. function onEditColor(type: EditType) {
  107. curColor.value =
  108. type === "fillColor" ? formData.fillColor : formData.unfillColor;
  109. editType.value = type;
  110. colorPickerDialogRef.value?.open();
  111. }
  112. function onColorConfirm(val: string) {
  113. if (editType.value === "fillColor") {
  114. formData.fillColor = val;
  115. } else {
  116. formData.unfillColor = val;
  117. }
  118. }
  119. /* init modal */
  120. watch(
  121. () => visible.value,
  122. (val) => {
  123. if (val) {
  124. modalOpenHandle();
  125. }
  126. },
  127. {
  128. immediate: true,
  129. }
  130. );
  131. function modalOpenHandle() {
  132. objModifyAssign(formData, userStore.recogFillSet || defaultFormData);
  133. }
  134. </script>
  135. <style lang="less" scoped>
  136. .color-block {
  137. display: inline-block;
  138. vertical-align: middle;
  139. width: 32px;
  140. height: 32px;
  141. border-radius: 6px;
  142. margin-right: 8px;
  143. cursor: pointer;
  144. &:hover {
  145. opacity: 0.8;
  146. }
  147. }
  148. .input-tips {
  149. display: inline-block;
  150. vertical-align: middle;
  151. margin-top: 3px;
  152. }
  153. </style>