|
@@ -1,54 +1,90 @@
|
|
|
+import { CompSizeOpts } from "@/modules/editor/objects/DesignTemp/creates/CompSize";
|
|
|
import { InputNumber, Select } from "ant-design-vue";
|
|
|
-import { defineComponent, reactive } from "vue";
|
|
|
+import { defineComponent } from "vue";
|
|
|
import { any } from "vue-types";
|
|
|
|
|
|
-const labels = ["宽度", "高度"];
|
|
|
const selectOptions = [
|
|
|
- { value: "px", label: "像素" },
|
|
|
- { value: "%", label: "百分比" },
|
|
|
+ { value: "px", label: "像素", options: { step: 1 } },
|
|
|
+ { value: "%", label: "比例", options: { step: 0.1, max: 100, min: 0 } },
|
|
|
];
|
|
|
|
|
|
export const Size = defineComponent({
|
|
|
props: {
|
|
|
- value: any<number[]>().def([]),
|
|
|
+ value: any<[number, number, string?, string?]>().def([0, 0]),
|
|
|
},
|
|
|
emits: ["change"],
|
|
|
setup(props, { emit }) {
|
|
|
- const state = reactive({
|
|
|
- unit: "px",
|
|
|
- });
|
|
|
+ const CompSizeOptsList = [CompSizeOpts.w, CompSizeOpts.h];
|
|
|
|
|
|
- function changeVal(index: number, v: any) {
|
|
|
- // const { value } = props;
|
|
|
- // value[index] = parseInt(v) || 0;
|
|
|
- // emit("change", value);
|
|
|
+ function changeVal(unit: any, index: number, v: any) {
|
|
|
+ const { value } = props;
|
|
|
+ value[index] = parseFloat(v) || 0;
|
|
|
+ if (unit === "%") {
|
|
|
+ value[index] = CompSizeOptsList[index].getPxSize(
|
|
|
+ value[index] as number
|
|
|
+ );
|
|
|
+ }
|
|
|
+ emit("change", value);
|
|
|
+ }
|
|
|
+
|
|
|
+ function changeUnit(index: number, unit: any) {
|
|
|
+ const { value } = props;
|
|
|
+ value[index + 2] = unit;
|
|
|
+ emit("change", value);
|
|
|
+ }
|
|
|
+
|
|
|
+ function fmtVal(val: number, unit: string, index: number) {
|
|
|
+ switch (unit) {
|
|
|
+ case "px":
|
|
|
+ return val.toFixed(0);
|
|
|
+ case "%":
|
|
|
+ return CompSizeOptsList[index].getVSize(val).toFixed(1);
|
|
|
+ default:
|
|
|
+ return "";
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return () => {
|
|
|
- let values = [...props.value];
|
|
|
+ const values = props.value.slice(0, 2) as number[];
|
|
|
+ const units = props.value.slice(2) as string[];
|
|
|
|
|
|
return (
|
|
|
<div class="flex space-x-10px">
|
|
|
- {values.map((value, i) => (
|
|
|
- <div key={i}>
|
|
|
- <div class="mb-5px">{labels[i]}</div>
|
|
|
+ {values.map((value, i) => {
|
|
|
+ const unit = units[i] || selectOptions[0].value;
|
|
|
+ const inputOpts =
|
|
|
+ selectOptions.find((d) => d.value === unit)?.options || {};
|
|
|
+ return (
|
|
|
<InputNumber
|
|
|
- controls={false}
|
|
|
- value={value}
|
|
|
- onChange={changeVal.bind(null, i)}
|
|
|
- />
|
|
|
- </div>
|
|
|
- ))}
|
|
|
- <div>
|
|
|
- <div class="mb-5px">单位</div>
|
|
|
- <Select
|
|
|
- value={state.unit}
|
|
|
- class="min-w-85px"
|
|
|
- options={selectOptions}
|
|
|
- // @ts-ignore
|
|
|
- onChange={(v) => (state.unit = v)}
|
|
|
- ></Select>
|
|
|
- </div>
|
|
|
+ key={i + unit}
|
|
|
+ value={fmtVal(value as number, unit, i)}
|
|
|
+ onChange={changeVal.bind(null, unit, i)}
|
|
|
+ {...inputOpts}
|
|
|
+ >
|
|
|
+ {{
|
|
|
+ addonBefore() {
|
|
|
+ return (
|
|
|
+ <span class="-mx-8px">
|
|
|
+ {CompSizeOptsList[i].label}
|
|
|
+ <Select
|
|
|
+ value={unit}
|
|
|
+ onChange={changeUnit.bind(null, i)}
|
|
|
+ >
|
|
|
+ {selectOptions.map((d) => {
|
|
|
+ return (
|
|
|
+ <Select.Option key={d.value}>
|
|
|
+ {d.value === "%" ? CompSizeOptsList[i].vUnit : d.value}
|
|
|
+ </Select.Option>
|
|
|
+ );
|
|
|
+ })}
|
|
|
+ </Select>
|
|
|
+ </span>
|
|
|
+ );
|
|
|
+ },
|
|
|
+ }}
|
|
|
+ </InputNumber>
|
|
|
+ );
|
|
|
+ })}
|
|
|
</div>
|
|
|
);
|
|
|
};
|