|
| 1 | +import { handleParagraphNode } from './paragraphNodeImporter.js'; |
| 2 | +import { defaultNodeListHandler } from './docxImporter.js'; |
| 3 | + |
| 4 | +export const handlePictNode = (params) => { |
| 5 | + const { nodes } = params; |
| 6 | + |
| 7 | + if (!nodes.length || nodes[0].name !== 'w:p') { |
| 8 | + return { nodes: [], consumed: 0 }; |
| 9 | + } |
| 10 | + |
| 11 | + const [pNode] = nodes; |
| 12 | + const run = pNode.elements?.find((el) => el.name === 'w:r'); |
| 13 | + const pict = run?.elements?.find((el) => el.name === 'w:pict'); |
| 14 | + |
| 15 | + // if there is no pict, then process as a paragraph or list. |
| 16 | + if (!pict) { |
| 17 | + return { nodes: [], consumed: 0 }; |
| 18 | + } |
| 19 | + |
| 20 | + const node = pict; |
| 21 | + const shape = node.elements?.find((el) => el.name === 'v:shape'); |
| 22 | + const shapetype = node.elements?.find((el) => el.name === 'v:shapetype'); |
| 23 | + const group = node.elements?.find((el) => el.name === 'v:group'); |
| 24 | + |
| 25 | + // such a case probably shouldn't exist. |
| 26 | + if (!shape && !group) { |
| 27 | + return { nodes: [], consumed: 0 }; |
| 28 | + } |
| 29 | + |
| 30 | + let result = null; |
| 31 | + |
| 32 | + const isGroup = group && !shape; |
| 33 | + |
| 34 | + if (isGroup) { |
| 35 | + // there should be a group of shapes being processed here (skip for now). |
| 36 | + result = null; |
| 37 | + } else { |
| 38 | + const textbox = shape.elements?.find((el) => el.name === 'v:textbox'); |
| 39 | + |
| 40 | + // process shapes with textbox. |
| 41 | + if (textbox) { |
| 42 | + result = handleShapTextboxImport({ |
| 43 | + pict, |
| 44 | + pNode, |
| 45 | + shape, |
| 46 | + params, |
| 47 | + }); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return { nodes: result ? [result] : [], consumed: 1 }; |
| 52 | +}; |
| 53 | + |
| 54 | +export function handleShapTextboxImport({ |
| 55 | + pict, |
| 56 | + pNode, |
| 57 | + shape, |
| 58 | + params, |
| 59 | +}) { |
| 60 | + const schemaAttrs = {}; |
| 61 | + const schemaTextboxAttrs = {}; |
| 62 | + |
| 63 | + const shapeAttrs = shape.attributes || {}; |
| 64 | + |
| 65 | + schemaAttrs.attributes = shapeAttrs; |
| 66 | + |
| 67 | + if (shapeAttrs.fillcolor) { |
| 68 | + schemaAttrs.fillcolor = shapeAttrs.fillcolor; |
| 69 | + } |
| 70 | + |
| 71 | + const parsedStyle = parseInlineStyles(shapeAttrs.style); |
| 72 | + const shapeStyle = buildStyles(parsedStyle); |
| 73 | + |
| 74 | + if (shapeStyle) { |
| 75 | + schemaAttrs.style = shapeStyle; |
| 76 | + } |
| 77 | + |
| 78 | + const textbox = shape.elements?.find((el) => el.name === 'v:textbox'); |
| 79 | + const wrap = shape.elements?.find((el) => el.name === 'w10:wrap'); |
| 80 | + |
| 81 | + if (wrap?.attributes) { |
| 82 | + schemaAttrs.wrapAttributes = wrap.attributes; |
| 83 | + } |
| 84 | + |
| 85 | + if (textbox?.attributes) { |
| 86 | + schemaTextboxAttrs.attributes = textbox.attributes; |
| 87 | + } |
| 88 | + |
| 89 | + const textboxContent = textbox?.elements?.find((el) => el.name === 'w:txbxContent'); |
| 90 | + const textboxContentElems = textboxContent?.elements || []; |
| 91 | + |
| 92 | + const content = textboxContentElems.map((elem) => handleParagraphNode({ |
| 93 | + nodes: [elem], |
| 94 | + docx: params.docx, |
| 95 | + nodeListHandler: defaultNodeListHandler(), |
| 96 | + })); |
| 97 | + const contentNodes = content.reduce((acc, current) => ( |
| 98 | + [...acc, ...current.nodes] |
| 99 | + ), []); |
| 100 | + |
| 101 | + const shapeTextbox = { |
| 102 | + type: 'shapeTextbox', |
| 103 | + attrs: schemaTextboxAttrs, |
| 104 | + content: contentNodes, |
| 105 | + }; |
| 106 | + |
| 107 | + const shapeContainer = { |
| 108 | + type: 'shapeContainer', |
| 109 | + attrs: schemaAttrs, |
| 110 | + content: [shapeTextbox], |
| 111 | + }; |
| 112 | + |
| 113 | + return shapeContainer; |
| 114 | +} |
| 115 | + |
| 116 | +function parseInlineStyles(styleString) { |
| 117 | + if (!styleString) return {}; |
| 118 | + return styleString |
| 119 | + .split(';') |
| 120 | + .filter((style) => !!style.trim()) |
| 121 | + .reduce((acc, style) => { |
| 122 | + const [prop, value] = style.split(':').map((str) => str.trim()); |
| 123 | + if (prop && value) acc[prop] = value; |
| 124 | + return acc; |
| 125 | + }, {}); |
| 126 | +} |
| 127 | + |
| 128 | +function buildStyles(styleObject) { |
| 129 | + const allowed = [ |
| 130 | + 'width', |
| 131 | + 'height', |
| 132 | + |
| 133 | + // these styles should probably work relative to the page, |
| 134 | + // since in the doc it is positioned absolutely. |
| 135 | + // 'margin-left', |
| 136 | + // 'margin-right', |
| 137 | + |
| 138 | + // causes pagination issues. |
| 139 | + // 'margin-top', |
| 140 | + // 'margin-bottom', |
| 141 | + |
| 142 | + // styleObject - also contains other word styles (mso-). |
| 143 | + ]; |
| 144 | + |
| 145 | + let style = ''; |
| 146 | + for (const [prop, value] of Object.entries(styleObject)) { |
| 147 | + if (allowed.includes(prop)) { |
| 148 | + style += `${prop}: ${value};`; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return style; |
| 153 | +} |
| 154 | + |
| 155 | +export const pictNodeHandlerEntity = { |
| 156 | + handlerName: 'handlePictNode', |
| 157 | + handler: handlePictNode, |
| 158 | +}; |
0 commit comments