This commit is contained in:
iqudoo
2026-06-13 05:34:16 +08:00
parent c25319e0de
commit 2a43df00c6
2 changed files with 213 additions and 32 deletions

View File

@@ -296,15 +296,70 @@ export function buildPrintablePages(
};
}
/** 自动排版时允许的最小页边距 (mm) */
export const AUTO_LAYOUT_MIN_PAGE_MARGIN_MM = 10;
const AUTO_LAYOUT_MIN_MARGIN_STORAGE_KEY = 'label_designer_auto_layout_min_margin_mm';
/** 读取上次自动排版使用的最小页边距 (mm) */
export function loadAutoLayoutMinPageMarginMm(): number {
try {
const raw = localStorage.getItem(AUTO_LAYOUT_MIN_MARGIN_STORAGE_KEY);
if (raw === null) return AUTO_LAYOUT_MIN_PAGE_MARGIN_MM;
const n = Number(raw);
if (Number.isFinite(n) && n >= 0) return n;
} catch {
/* ignore */
}
return AUTO_LAYOUT_MIN_PAGE_MARGIN_MM;
}
/** 保存自动排版使用的最小页边距 (mm) */
export function saveAutoLayoutMinPageMarginMm(value: number): void {
try {
localStorage.setItem(AUTO_LAYOUT_MIN_MARGIN_STORAGE_KEY, String(value));
} catch {
/* ignore */
}
}
/** 按最小页边距居中计算四边页边距 */
export function computeAutoLayoutPaper(
paper: PaperConfig,
template: Pick<LabelTemplate, 'width' | 'height'>,
minMargin: number
): PaperConfig {
const paperW = paper.orientation === 'portrait' ? paper.width : paper.height;
const paperH = paper.orientation === 'portrait' ? paper.height : paper.width;
const lw = template.width;
const lh = template.height;
const usableWInit = paperW - minMargin * 2;
const usableHInit = paperH - minMargin * 2;
const maxCols = Math.floor((usableWInit + paper.columnGap) / (lw + paper.columnGap)) || 1;
const maxRows = Math.floor((usableHInit + paper.rowGap) / (lh + paper.rowGap)) || 1;
const gridWidth = maxCols * lw + (maxCols - 1) * paper.columnGap;
const gridHeight = maxRows * lh + (maxRows - 1) * paper.rowGap;
const balancedLR = Math.max(minMargin, Math.round(((paperW - gridWidth) / 2) * 10) / 10);
const balancedTB = Math.max(minMargin, Math.round(((paperH - gridHeight) / 2) * 10) / 10);
return {
...paper,
marginLeft: balancedLR,
marginRight: balancedLR,
marginTop: balancedTB,
marginBottom: balancedTB,
};
}
export const DEFAULT_PAPER_CONFIG: PaperConfig = {
type: 'A4',
width: 210,
height: 297,
orientation: 'portrait',
marginTop: 10,
marginBottom: 10,
marginLeft: 10,
marginRight: 10,
marginTop: AUTO_LAYOUT_MIN_PAGE_MARGIN_MM,
marginBottom: AUTO_LAYOUT_MIN_PAGE_MARGIN_MM,
marginLeft: AUTO_LAYOUT_MIN_PAGE_MARGIN_MM,
marginRight: AUTO_LAYOUT_MIN_PAGE_MARGIN_MM,
columnGap: 2,
rowGap: 2,
flow: 'left-right-top-bottom',