const name = 'Pages Plugin' const version = '1.0.0' export const sizes = { a4: [ 210, 297 ], a3: [ 297, 420 ], a2: [ 420, 594 ], a1: [ 594, 841 ], a0: [ 841, 1188 ], letter: [ 215.9, 279.4 ], tabloid: [ 279.4, 431.8 ], } const indexLetter = (i) => String.fromCharCode('A'.charCodeAt(0) + i - 1) export const pagesPlugin = ({size='a4', orientation='portrait', margin=10}, outlineStyle = true ) => { let [sheetWidth, sheetHeight] = sizes[size]; sheetWidth -= margin sheetHeight -= margin return basePlugin({sheetWidth, sheetHeight, orientation, outlineStyle}) } export const cutFabricPlugin = (sheetWidth, sheetHeight) => basePlugin({sheetWidth, sheetHeight, boundary: true, partName: "cutFabric", responsiveWidth: false}) const basePlugin = ({sheetWidth, sheetHeight, orientation='portrait', boundary=false, partName="pages", responsiveWidth=true, outlineStyle=false}) => ({ name, version, hooks: { postLayout: function(pattern) { // Add part pattern.parts[partName] = pattern.Part(partName) // Keep part out of layout pattern.parts[partName].layout = false // But add the part to the autoLayout property pattern.autoLayout.parts[partName] = { move: { x: 0, y: 0 } } // Add pages const { macro } = pattern.parts[partName].shorthand() let { height, width } = pattern if (!responsiveWidth) width = sheetWidth; if (pattern.settings.layout?.topLeft) { height += pattern.settings.layout.topLeft.y responsiveWidth && (width += pattern.settings.layout.topLeft.x) } macro('addPages', { size: [sheetWidth, sheetHeight], orientation, height, width }) if (boundary) pattern.parts[partName].boundary(); } }, macros: { addPages: function(so) { const ls = so.orientation === 'landscape' const w = so.size[ls ? 1 : 0] const h = so.size[ls ? 0 : 1] const cols = Math.ceil(so.width / w) const rows = Math.ceil(so.height / h) const { points, Point, paths, Path, macro } = this.shorthand() let x = 0 let y = 0 let count = 0 for (let row=0;row