wui-print
Version:
前端打印插件,包含打印设计器、打印表单、打印API
129 lines (128 loc) • 4.19 kB
JavaScript
import { camelToKebab } from "../print-designer-utils";
const createTableLayoutStyle = ({ styleConfig } = {}) => {
if (!styleConfig) {
styleConfig = {};
}
let styleText = "";
Object.keys(styleConfig).forEach((key, index) => {
if (key.indexOf("_") >= 0) {
return false;
}
if (!!styleConfig[key] || styleConfig[key] === 0) {
styleText += `${camelToKebab(key)}:${styleConfig[key]};`;
}
});
return styleText;
};
const createTableLayoutTdStyle = ({ tableLayoutStyleConfig, tdStyleConfig } = {}) => {
if (!tableLayoutStyleConfig) {
tableLayoutStyleConfig = {};
}
if (!tdStyleConfig) {
tdStyleConfig = {};
}
let styleText = "";
let styleObj = {};
Object.keys(tableLayoutStyleConfig).forEach((key, index) => {
if (key.indexOf("cell_") < 0 || key.indexOf("cell_container_") >= 0) {
return false;
}
let newKey = key.replace("cell_", "");
if (!!tableLayoutStyleConfig[key] || tableLayoutStyleConfig[key] === 0) {
styleObj[newKey] = tableLayoutStyleConfig[key];
}
});
if (!!tdStyleConfig) {
Object.keys(tdStyleConfig).forEach((key, index) => {
if (key.indexOf("container_") >= 0) {
return false;
}
if (!!tdStyleConfig[key] || tdStyleConfig[key] === 0) {
styleObj[key] = tdStyleConfig[key];
}
});
}
Object.keys(styleObj).forEach((key, index) => {
styleText += `${camelToKebab(key)}:${styleObj[key]};`;
});
return styleText;
};
const createTableLayoutTdContainerStyle = ({ tableLayoutStyleConfig, tdStyleConfig } = {}) => {
if (!tableLayoutStyleConfig) {
tableLayoutStyleConfig = {};
}
if (!tdStyleConfig) {
tdStyleConfig = {};
}
let styleText = "";
let styleObj = {};
Object.keys(tableLayoutStyleConfig).forEach((key, index) => {
if (key.indexOf("cell_container_") < 0) {
return false;
}
let newKey = key.replace("cell_container_", "");
if (!!tableLayoutStyleConfig[key] || tableLayoutStyleConfig[key] === 0) {
styleObj[newKey] = tableLayoutStyleConfig[key];
}
});
Object.keys(tdStyleConfig).forEach((key, index) => {
if (key.indexOf("custom_") >= 0 || key.indexOf("container_") < 0) {
return false;
}
let newKey = key.replace("container_", "");
if (!!tdStyleConfig[key] || tdStyleConfig[key] === 0) {
styleObj[newKey] = tdStyleConfig[key];
}
});
// flex布局生效时,水平居中和垂直居中将失效
if (!!tdStyleConfig.container_custom_isFLex) {
styleObj.display = "flex";
} else {
if (!!tdStyleConfig.container_custom_centerHorizontally || !!tdStyleConfig.container_custom_centerVertical) {
styleObj.display = "flex";
}
if (!!tdStyleConfig.container_custom_centerHorizontally) {
styleObj.justifyContent = "center";
}
if (!!tdStyleConfig.container_custom_centerVertical) {
styleObj.alignItems = "center";
}
}
Object.keys(styleObj).forEach((key, index) => {
styleText += `${camelToKebab(key)}:${styleObj[key]};`;
});
return styleText;
};
const createTableLayoutTdSlashStyle = ({ width = 0, height = 0, direction } = {}) => {
if (!width || !height) {
return "";
}
if (!direction) {
direction = "top-left";
}
let styleText = "";
let styleObj = {};
let slashWidth = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
let slashDeg = (Math.atan(height / width) * 180) / Math.PI;
styleObj.width = `${slashWidth}px`;
styleObj.transformOrigin = `top left`;
if (direction == "top-left") {
styleObj.transform = `rotate(${slashDeg}deg)`;
} else {
styleObj.transform = `rotate(-${slashDeg}deg)`;
}
Object.keys(styleObj).forEach((key, index) => {
styleText += `${camelToKebab(key)}:${styleObj[key]};`;
});
return styleText;
};
const getTableLayoutStyleRulesId = ({ rowIndex, colIndex } = {}) => {
return `${rowIndex}-${colIndex}`;
};
export default {
createTableLayoutStyle,
createTableLayoutTdStyle,
createTableLayoutTdContainerStyle,
createTableLayoutTdSlashStyle,
getTableLayoutStyleRulesId
};