@@ -5,10 +5,19 @@ import { emuToPixels } from '../../helpers.js';
55 */
66export const handleDrawingNode = ( params ) => {
77 const { nodes, filename } = params ;
8- if ( nodes . length === 0 || nodes [ 0 ] . name !== 'w:drawing' ) {
8+
9+ const validNodes = [ 'w:drawing' , 'w:p' ] ;
10+ if ( nodes . length === 0 || ! validNodes . includes ( nodes [ 0 ] . name ) ) {
911 return { nodes : [ ] , consumed : 0 } ;
1012 }
11- const node = nodes [ 0 ] ;
13+
14+ const mainNode = nodes [ 0 ] ;
15+ let node ;
16+
17+ if ( mainNode . name === 'w:drawing' ) node = mainNode ;
18+ else node = mainNode . elements . find ( ( el ) => el . name === 'w:drawing' ) ;
19+
20+ if ( ! node ) return { nodes : [ ] , consumed : 0 } ;
1221
1322 let result ;
1423 const { elements } = node ;
@@ -22,6 +31,7 @@ export const handleDrawingNode = (params) => {
2231 // Others, wp:inline
2332 const inlineImage = elements . find ( ( el ) => el . name === 'wp:inline' ) ;
2433 if ( inlineImage ) result = handleImageImport ( inlineImage , currentFileName , params ) ;
34+
2535 return { nodes : result ? [ result ] : [ ] , consumed : 1 } ;
2636} ;
2737
@@ -43,6 +53,11 @@ export function handleImageImport(node, currentFileName, params) {
4353
4454 const graphic = node . elements . find ( ( el ) => el . name === 'a:graphic' ) ;
4555 const graphicData = graphic . elements . find ( ( el ) => el . name === 'a:graphicData' ) ;
56+ const { uri } = graphicData ?. attributes ;
57+ const shapeURI = "http://schemas.microsoft.com/office/word/2010/wordprocessingShape" ;
58+ if ( ! ! uri && uri === shapeURI ) {
59+ return handleShapeDrawing ( params , node , graphicData ) ;
60+ } ;
4661
4762 const picture = graphicData . elements . find ( ( el ) => el . name === 'pic:pic' ) ;
4863 if ( ! picture || ! picture . elements ) return null ;
@@ -97,6 +112,68 @@ export function handleImageImport(node, currentFileName, params) {
97112 } ;
98113}
99114
115+ const handleShapeDrawing = ( params , node , graphicData ) => {
116+ const wsp = graphicData . elements . find ( ( el ) => el . name === 'wps:wsp' ) ;
117+ const textBox = wsp . elements . find ( ( el ) => el . name === 'wps:txbx' ) ;
118+ const textBoxContent = textBox ?. elements ?. find ( ( el ) => el . name === 'w:txbxContent' ) ;
119+
120+ const isGraphicContainer = node . elements . find ( ( el ) => el . name === 'wp:docPr' ) ;
121+ const spPr = wsp . elements . find ( ( el ) => el . name === 'wps:spPr' ) ;
122+ const prstGeom = spPr ?. elements . find ( ( el ) => el . name === 'a:prstGeom' ) ;
123+
124+ if ( ! ! prstGeom && prstGeom . attributes [ 'prst' ] === 'rect' ) {
125+ return getRectangleShape ( params , spPr ) ;
126+ }
127+
128+ if ( ! textBoxContent ) {
129+ return null ;
130+ }
131+
132+ const { nodeListHandler } = params ;
133+ const translatedElement = nodeListHandler . handler ( {
134+ ...params ,
135+ node : textBoxContent . elements [ 0 ] ,
136+ nodes : textBoxContent . elements
137+ } ) ;
138+
139+ return translatedElement [ 0 ] ;
140+ } ;
141+
142+ const getRectangleShape = ( params , node ) => {
143+ const schemaAttrs = { } ;
144+
145+ const [ drawingNode ] = params . nodes ;
146+
147+ if ( drawingNode ?. name === 'w:drawing' ) {
148+ schemaAttrs . drawingContent = drawingNode ;
149+ }
150+
151+ const xfrm = node . elements . find ( ( el ) => el . name === 'a:xfrm' ) ;
152+ const start = xfrm . elements . find ( ( el ) => el . name === 'a:off' ) ;
153+ const size = xfrm . elements . find ( ( el ) => el . name === 'a:ext' ) ;
154+ const outline = node . elements . find ( ( el ) => el . name === 'a:ln' ) ;
155+ const solidFill = node . elements . find ( ( el ) => el . name === 'a:solidFill' ) ;
156+
157+ const rectangleSize = {
158+ top : emuToPixels ( start . attributes [ 'y' ] ) ,
159+ left : emuToPixels ( start . attributes [ 'x' ] ) ,
160+ width : emuToPixels ( size . attributes [ 'cx' ] ) ,
161+ height : emuToPixels ( size . attributes [ 'cy' ] ) ,
162+ } ;
163+ schemaAttrs . size = rectangleSize ;
164+
165+ const background = solidFill ?. elements [ 0 ] ?. attributes [ 'val' ] ;
166+
167+ if ( background ) {
168+ schemaAttrs . background = '#' + background ;
169+ }
170+
171+ return {
172+ type : 'contentBlock' ,
173+ attrs : schemaAttrs ,
174+ } ;
175+ } ;
176+
100177/**
101178 * @type {import("docxImporter").NodeHandlerEntry }
102179 */
0 commit comments