|
@@ -1,277 +0,0 @@
|
|
|
-import { defaultLayer } from "@/constant";
|
|
|
-import { getBaseItem } from "@/core/components/util";
|
|
|
-import { getEmptyStoreData, StoreData } from "@/core/store/store";
|
|
|
-import { getFixPosition } from "@/utils/bound";
|
|
|
-import { Size } from "@/utils/math";
|
|
|
-import { getImage } from "@/utils/resource";
|
|
|
-import { TabCover, tableCoverHeight, tableCoverKey, tableCoverScaleKey, tableCoverWidth, tableTableKey, tableTitleKey } from "../../store";
|
|
|
-import { matResponse, TableData } from "@/core/components/table";
|
|
|
-import { TextData } from "@/core/components/text";
|
|
|
-import { ImageData } from "@/core/components/image";
|
|
|
-import { Transform } from "konva/lib/Util";
|
|
|
-import { paperConfigs } from "@/example/components/slide/actions";
|
|
|
-
|
|
|
-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 setCoverScaleTextPosition = (cover: ImageData, title: TextData) => {
|
|
|
- const x = cover.mat[4] - (title.width || 0) / 2;
|
|
|
- const y = cover.mat[5] + cover.height / 2 + 10;
|
|
|
- title.mat[4] = x;
|
|
|
- title.mat[5] = y;
|
|
|
-};
|
|
|
-
|
|
|
-export const getCoverPaperScale = (cover: ImageData, paperKey: string) => {
|
|
|
- let pixelScale =
|
|
|
- (cover.widthRaw! / cover.width) * cover.proportion!.scale;
|
|
|
- const realPixelScale = paperConfigs[paperKey as "a4"].scale;
|
|
|
- return Math.round(realPixelScale * pixelScale);
|
|
|
-}
|
|
|
-export const setCoverPaperScale = (cover: ImageData, paperKey: string, scale: number) => {
|
|
|
- const realPixelScale = paperConfigs[paperKey as "a4"].scale;
|
|
|
- cover.width = cover.widthRaw! / ((scale / realPixelScale) /cover.proportion!.scale)
|
|
|
- cover.height = (cover.heightRaw! / cover.widthRaw!) * cover.width
|
|
|
- console.log(cover.width / cover.height)
|
|
|
-}
|
|
|
-
|
|
|
-const genDefaultCover = async (cover: TabCover, paperKey: string) => {
|
|
|
- const image = await getImage(cover.url);
|
|
|
- const rectScale = image.width / image.height
|
|
|
- const tableCoverScale = tableCoverWidth / tableCoverHeight
|
|
|
-
|
|
|
- let width: number, height: number;
|
|
|
- if (rectScale > tableCoverScale) {
|
|
|
- width = tableCoverWidth;
|
|
|
- height = width / rectScale;
|
|
|
- } else {
|
|
|
- height = tableCoverHeight;
|
|
|
- width = rectScale * height;
|
|
|
- }
|
|
|
- console.log(width / height, rectScale, cover.width / cover.height)
|
|
|
-
|
|
|
- const coverData = {
|
|
|
- ...getBaseItem(),
|
|
|
- cornerRadius: 0,
|
|
|
- strokeWidth: 0,
|
|
|
- url: cover.url,
|
|
|
- key: tableCoverKey,
|
|
|
- proportion: cover.proportion,
|
|
|
- disableTransformer: true,
|
|
|
- widthRaw: cover.width,
|
|
|
- showScale: true,
|
|
|
- heightRaw: cover.height,
|
|
|
- zIndex: -1,
|
|
|
- width,
|
|
|
- height,
|
|
|
- mat: [1, 0, 0, 1, 0, 0],
|
|
|
- };
|
|
|
-
|
|
|
- // 缩放比例默认给10的倍数
|
|
|
- let wScale = getCoverPaperScale(coverData, paperKey)
|
|
|
- setCoverPaperScale(coverData, paperKey, Math.round(wScale / 10) * 10)
|
|
|
- return coverData;
|
|
|
-};
|
|
|
-
|
|
|
-const genDefaultCoverScaleText = (cover: ImageData) => {
|
|
|
- const scale = (cover.widthRaw! / cover.width) * cover.proportion!.scale;
|
|
|
- return {
|
|
|
- ...getBaseItem(),
|
|
|
- content: `1:${scale}`,
|
|
|
- width: 100,
|
|
|
- heihgt: 16,
|
|
|
- fontSize: 12,
|
|
|
- disableEditText: true,
|
|
|
- key: tableCoverScaleKey,
|
|
|
- disableDelete: true,
|
|
|
- align: "center",
|
|
|
- mat: [1, 0, 0, 1, 0, 0],
|
|
|
- };
|
|
|
-}
|
|
|
-
|
|
|
-const genDefaultTable = () => {
|
|
|
- const nameColl = {
|
|
|
- width: 140,
|
|
|
- height: 40,
|
|
|
- padding: 13,
|
|
|
- content: "",
|
|
|
- align: "center",
|
|
|
- fontSize: 14,
|
|
|
- fontColor: "#000000",
|
|
|
- };
|
|
|
- const valueColl = { ...nameColl, width: 200 };
|
|
|
-
|
|
|
- const data = {
|
|
|
- ...getBaseItem(),
|
|
|
- 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],
|
|
|
- };
|
|
|
- return matResponse({ data, mat: new Transform() })
|
|
|
-};
|
|
|
-
|
|
|
-export const genDefaultTitle = () => {
|
|
|
- return {
|
|
|
- ...getBaseItem(),
|
|
|
- content: "默认标题",
|
|
|
- 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[],
|
|
|
- paperKey: string,
|
|
|
- cover?: TabCover | 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 (cover) {
|
|
|
- const imageData = await genDefaultCover(cover, paperKey);
|
|
|
- setCoverPosition(size, margin, imageData);
|
|
|
- layer.image = [imageData];
|
|
|
- layer.text!.push(genDefaultCoverScaleText(imageData))
|
|
|
- }
|
|
|
- } else {
|
|
|
- layer.image = layer.image || [];
|
|
|
- const coverNdx = layer.image.findIndex(
|
|
|
- (item) => item.key === tableCoverKey
|
|
|
- );
|
|
|
- layer.text = layer.text || []
|
|
|
- const coverScaleNdx = layer.text.findIndex(
|
|
|
- (item) => item.key === tableCoverScaleKey
|
|
|
- );
|
|
|
-
|
|
|
- if (cover) {
|
|
|
- const imageData = await genDefaultCover(cover, paperKey);
|
|
|
- const imageScaleData = genDefaultCoverScaleText(imageData)
|
|
|
- if (!~coverNdx) {
|
|
|
- setCoverPosition(size, margin, imageData);
|
|
|
- layer.image.push(imageData);
|
|
|
- } else {
|
|
|
- layer.image[coverNdx].url = imageData.url
|
|
|
- layer.image[coverNdx].width = imageData.width
|
|
|
- layer.image[coverNdx].height = imageData.height
|
|
|
- layer.image[coverNdx].widthRaw = imageData.widthRaw
|
|
|
- layer.image[coverNdx].heightRaw = imageData.heightRaw
|
|
|
- layer.image[coverNdx].proportion = imageData.proportion
|
|
|
- }
|
|
|
-
|
|
|
- if (!~coverScaleNdx) {
|
|
|
- setCoverScaleTextPosition(imageData, imageScaleData)
|
|
|
- } else {
|
|
|
- layer.text[coverScaleNdx].content = imageScaleData.content
|
|
|
- setCoverScaleTextPosition(imageData, layer.text[coverScaleNdx])
|
|
|
- }
|
|
|
- } else {
|
|
|
- ~coverNdx && layer.image.splice(coverNdx, 1);
|
|
|
- ~coverScaleNdx && layer.image.splice(coverScaleNdx, 1);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (place) {
|
|
|
- const imageData = layer.image?.find(item => item.key === tableCoverKey)
|
|
|
- imageData && setCoverPosition(size, margin, imageData)
|
|
|
- const imageScaleData = layer.text?.find(item => item.key === tableCoverScaleKey)
|
|
|
- imageScaleData && imageData && setCoverScaleTextPosition(imageData, imageScaleData)
|
|
|
- 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,
|
|
|
- },
|
|
|
- config: {
|
|
|
- ...getEmptyStoreData(),
|
|
|
- ...store.config,
|
|
|
- }
|
|
|
- };
|
|
|
- } else {
|
|
|
- return {
|
|
|
- ...store,
|
|
|
- config: {
|
|
|
- ...getEmptyStoreData(),
|
|
|
- ...store.config,
|
|
|
- },
|
|
|
- layers: { [defaultLayer]: layer },
|
|
|
- };
|
|
|
- }
|
|
|
-};
|