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 ], } /** get a letter to represent an index */ const indexLetter = (i) => String.fromCharCode('A'.charCodeAt(0) + i - 1) /** * A plugin to add printer pages * */ export const pagesPlugin = ({size='a4', orientation='portrait', margin=10}, printStyle = false /** should the pages be rendered for printing or for screen viewing? */ ) => { const ls = orientation === 'landscape' let sheetHeight = sizes[size][ls ? 1 : 0] let sheetWidth = sizes[size][ls ? 0 : 1] sheetWidth -= margin sheetHeight -= margin return basePlugin({sheetWidth, sheetHeight, orientation, printStyle}) } /** * The base plugin for adding a layout helper part like pages or fabric * sheetWidth: the width of the helper part * sheetHeight: the height of the helper part * boundary: should the helper part calculate its boundary? * responsiveColumns: should the part make more columns if the pattern exceed its width? (for pages you want this, for fabric you don't) * printStyle: hould the pages be rendered for printing or for screen viewing? * */ const basePlugin = ({ sheetWidth, sheetHeight, boundary=false, partName="pages", responsiveColumns=true, printStyle=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 (!responsiveColumns) width = sheetWidth; if (pattern.settings.layout?.topLeft) { height += pattern.settings.layout.topLeft.y responsiveColumns && (width += pattern.settings.layout.topLeft.x) } macro('addPages', { size: [sheetWidth, sheetHeight], height, width }) if (boundary) pattern.parts[partName].boundary(); } }, macros: { /** draft the pages */ addPages: function(so) { const [h,w] = so.size 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