|
@@ -0,0 +1,190 @@
|
|
|
+import { defaultLayer } from "@/constant";
|
|
|
+import { getBaseItem } from "@/core/components/util";
|
|
|
+import { StoreData } from "@/core/store/store";
|
|
|
+import { getFixPosition } from "@/utils/bound";
|
|
|
+import { Size } from "@/utils/math";
|
|
|
+import { getImage } from "@/utils/resource";
|
|
|
+import { tableCoverKey, tableCoverWidth, tableTableKey, tableTitleKey } from "../../store";
|
|
|
+import { TableData } from "@/core/components/table";
|
|
|
+import { TextData } from "@/core/components/text";
|
|
|
+import { ImageData } from "@/core/components/image";
|
|
|
+
|
|
|
+const setCoverPosition = (
|
|
|
+ size: Size,
|
|
|
+ margin: number | number[],
|
|
|
+ cover: ImageData
|
|
|
+) => {
|
|
|
+ if (!Array.isArray(margin)) {
|
|
|
+ margin = [margin, margin, margin, margin];
|
|
|
+ }
|
|
|
+ const imagePos = getFixPosition(
|
|
|
+ {
|
|
|
+ left: cover.width / 2 + margin[3] + 70,
|
|
|
+ bottom: -cover.height / 2 + margin[2] + 70,
|
|
|
+ },
|
|
|
+ cover,
|
|
|
+ size
|
|
|
+ );
|
|
|
+ cover.mat[4] = imagePos.x;
|
|
|
+ cover.mat[5] = imagePos.y;
|
|
|
+};
|
|
|
+
|
|
|
+const setTablePosition = (
|
|
|
+ size: Size,
|
|
|
+ margin: number | number[],
|
|
|
+ table: TableData
|
|
|
+) => {
|
|
|
+ if (!Array.isArray(margin)) {
|
|
|
+ margin = [margin, margin, margin, margin];
|
|
|
+ }
|
|
|
+ const tablePos = getFixPosition(
|
|
|
+ { right: 40 + margin[1], bottom: 40 + margin[2] },
|
|
|
+ table,
|
|
|
+ size
|
|
|
+ );
|
|
|
+ table.mat[4] = tablePos.x;
|
|
|
+ table.mat[5] = tablePos.y;
|
|
|
+};
|
|
|
+
|
|
|
+const setTitlePosition = (
|
|
|
+ size: Size,
|
|
|
+ margin: number | number[],
|
|
|
+ title: TextData
|
|
|
+) => {
|
|
|
+ if (!Array.isArray(margin)) {
|
|
|
+ margin = [margin, margin, margin, margin];
|
|
|
+ }
|
|
|
+
|
|
|
+ const titlePos = {
|
|
|
+ x: (size.width - margin[3]) / 2 - 150 + margin[3],
|
|
|
+ y: 40 + margin[0],
|
|
|
+ };
|
|
|
+ title.mat[4] = titlePos.x;
|
|
|
+ title.mat[5] = titlePos.y;
|
|
|
+};
|
|
|
+
|
|
|
+const genDefaultCover = async (cover: string) => {
|
|
|
+ const image = await getImage(cover);
|
|
|
+ const coverData = {
|
|
|
+ ...getBaseItem(),
|
|
|
+ cornerRadius: 0,
|
|
|
+ strokeWidth: 1,
|
|
|
+ url: cover,
|
|
|
+ lock: true,
|
|
|
+ key: tableCoverKey,
|
|
|
+ zIndex: -1,
|
|
|
+ width: tableCoverWidth,
|
|
|
+ height: (image.height / image.width) * tableCoverWidth,
|
|
|
+ mat: [1, 0, 0, 1, 0, 0],
|
|
|
+ };
|
|
|
+ return coverData;
|
|
|
+};
|
|
|
+
|
|
|
+const genDefaultTable = () => {
|
|
|
+ const nameColl = {
|
|
|
+ width: 140,
|
|
|
+ height: 40,
|
|
|
+ padding: 13,
|
|
|
+ content: "",
|
|
|
+ align: "center",
|
|
|
+ fontSize: 14,
|
|
|
+ fontColor: "#000000",
|
|
|
+ };
|
|
|
+ const valueColl = { ...nameColl, width: 200 };
|
|
|
+ return {
|
|
|
+ ...getBaseItem(),
|
|
|
+ lock: true,
|
|
|
+ content: [
|
|
|
+ [{ ...nameColl, content: "案发时间" }, valueColl],
|
|
|
+ [{ ...nameColl, content: "案发地点" }, valueColl],
|
|
|
+ [{ ...nameColl, content: "绘图单位" }, valueColl],
|
|
|
+ [{ ...nameColl, content: "绘图人" }, valueColl],
|
|
|
+ [{ ...nameColl, content: "绘图时间" }, valueColl],
|
|
|
+ ],
|
|
|
+ key: tableTableKey,
|
|
|
+ width: nameColl.width + valueColl.width,
|
|
|
+ height: nameColl.height * 5,
|
|
|
+ mat: [1, 0, 0, 1, 0, 0],
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
+export const genDefaultTitle = () => {
|
|
|
+ return {
|
|
|
+ ...getBaseItem(),
|
|
|
+ content: "默认标题",
|
|
|
+ lock: true,
|
|
|
+ width: 300,
|
|
|
+ heihgt: 42,
|
|
|
+ fontSize: 38,
|
|
|
+ key: tableTitleKey,
|
|
|
+ align: "center",
|
|
|
+ mat: [1, 0, 0, 1, 0, 0],
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
+export const getRepTabulationStore = async (
|
|
|
+ store: StoreData,
|
|
|
+ size: Size,
|
|
|
+ margin: number | number[],
|
|
|
+ coverUrl?: string | null,
|
|
|
+ place = true
|
|
|
+) => {
|
|
|
+ const layers = store?.layers;
|
|
|
+ let layer = layers && layers[defaultLayer];
|
|
|
+
|
|
|
+ if (!layer) {
|
|
|
+ const titleData = genDefaultTitle();
|
|
|
+ const tableData = genDefaultTable();
|
|
|
+ setTitlePosition(size, margin, titleData);
|
|
|
+ setTablePosition(size, margin, tableData);
|
|
|
+ layer = { text: [titleData], table: [tableData] };
|
|
|
+
|
|
|
+ if (coverUrl) {
|
|
|
+ const imageData = await genDefaultCover(coverUrl);
|
|
|
+ setCoverPosition(size, margin, imageData);
|
|
|
+ layer.image = [imageData];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ layer.image = layer.image || [];
|
|
|
+ const coverNdx = layer.image.findIndex(
|
|
|
+ (item) => item.key === tableCoverKey
|
|
|
+ );
|
|
|
+
|
|
|
+ if (coverUrl) {
|
|
|
+ if (!~coverNdx) {
|
|
|
+ const imageData = await genDefaultCover(coverUrl);
|
|
|
+ setCoverPosition(size, margin, imageData);
|
|
|
+ layer.image.push(imageData);
|
|
|
+ } else {
|
|
|
+ layer.image[coverNdx].url = coverUrl;
|
|
|
+ }
|
|
|
+ } else if (~coverNdx) {
|
|
|
+ layer.image.splice(coverNdx, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (place) {
|
|
|
+ const imageData = layer.image?.find(item => item.key === tableCoverKey)
|
|
|
+ imageData && setCoverPosition(size, margin, imageData)
|
|
|
+ const titleData = layer.text?.find(item => item.key === tableTitleKey)
|
|
|
+ titleData && setTitlePosition(size, margin, titleData)
|
|
|
+ const tableData = layer.table?.find(item => item.key === tableTableKey)
|
|
|
+ tableData && setTablePosition(size, margin, tableData)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (layers) {
|
|
|
+ return {
|
|
|
+ ...store,
|
|
|
+ layers: {
|
|
|
+ ...layers,
|
|
|
+ [defaultLayer]: layer,
|
|
|
+ },
|
|
|
+ };
|
|
|
+ } else {
|
|
|
+ return {
|
|
|
+ ...store,
|
|
|
+ layers: { [defaultLayer]: layer },
|
|
|
+ };
|
|
|
+ }
|
|
|
+};
|