1- import { create } from '../utilities/dom.ts' ;
1+ import { create , update , serialize } from '../utilities/dom.ts' ;
22import { Length } from '../utilities/length.ts' ;
33import { QNS } from '../utilities/namespaces.ts' ;
4- import { evaluateXPathToMap } from '../utilities/xquery.ts' ;
4+ import * as slimdom from 'https://esm.sh/[email protected] ?pin=v121' ; 5+ import { evaluateXPathToArray , evaluateXPathToFirstNode , evaluateXPathToMap } from '../utilities/xquery.ts' ;
56
67/**
78 * All the formatting options that can be given on a text run (inline text).
@@ -17,8 +18,7 @@ export type SectionProperties = {
1718 numberOfColumns : null | number ;
1819 equalWidth : null | boolean ;
1920 separator : null | boolean ;
20- columnSpacing ?: null | Length ;
21- columns ?: { columnSize : Length , columnSpacing : Length | null } [ ]
21+ columns ?: { columnWidth : Length | null , columnSpace ?: Length | null } [ ] | null ;
2222 } ;
2323
2424 /**
@@ -65,70 +65,59 @@ export type SectionProperties = {
6565export function sectionPropertiesFromNode ( node ?: Node | null ) : SectionProperties {
6666 if ( ! node ) {
6767 return { } ;
68- } ;
69- const data = evaluateXPathToMap < SectionProperties > (
70- `let $propsMap := map {
71- "headers": map {
72- "first": ./${ QNS . w } headerReference[@${ QNS . w } type = 'first']/@${ QNS . r } id/string(),
73- "even": ./${ QNS . w } headerReference[@${ QNS . w } type = 'even']/@${ QNS . r } id/string(),
74- "odd": ./${ QNS . w } headerReference[@${ QNS . w } type = 'default']/@${ QNS . r } id/string()
75- },
76- "footers": map {
77- "first": ./${ QNS . w } footerReference[@${ QNS . w } type = 'first']/@${ QNS . r } id/string(),
78- "even": ./${ QNS . w } footerReference[@${ QNS . w } type = 'even']/@${ QNS . r } id/string(),
79- "odd": ./${ QNS . w } footerReference[@${ QNS . w } type = 'default']/@${ QNS . r } id/string()
80- },
81- "columns": map {
82- "numberOfColumns": ./${ QNS . w } cols/@${ QNS . w } num/number(),
83- "separator": docxml:st-on-off(./${ QNS . w } cols/@${ QNS . w } sep),
84- "equalWidth": docxml:st-on-off(./${ QNS . w } cols/@${ QNS . w } equalWidth)
85- },
86- "pageWidth": docxml:length(${ QNS . w } pgSz/@${ QNS . w } w, 'twip'),
87- "pageHeight": docxml:length(${ QNS . w } pgSz/@${ QNS . w } h, 'twip'),
88- "pageOrientation": ./${ QNS . w } pgSz/@${ QNS . w } orient/string(),
89- "pageMargin": map {
90- "top": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } top, 'twip'),
91- "right": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } right, 'twip'),
92- "bottom": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } bottom, 'twip'),
93- "left": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } left, 'twip'),
94- "header": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } header, 'twip'),
95- "footer": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } footer, 'twip'),
96- "gutter": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } gutter, 'twip')
97- },
98- "isTitlePage": exists(./${ QNS . w } titlePg) and (not(./${ QNS . w } titlePg/@${ QNS . w } val) or docxml:st-on-off(./${ QNS . w } titlePg/@${ QNS . w } val))
99- }
100- let $colSpacing := if (exists(./${ QNS . w } cols/@${ QNS . w } space)) then (docxml:length(./${ QNS . w } cols/@${ QNS . w } space, 'twip')) else ()
101- let $cols := ./${ QNS . w } cols/${ QNS . w } col
102- let $colMap :=
103- if (exists($cols))
104- then (
105- for-each(
106- $cols,
107- function($c) {
108- map {
109- "columnsSize": docxml:length($c/@${ QNS . w } w, 'twip'),
110- "columnSpacing": docxml:length($c/@${ QNS . w } space, 'twip')
111- }
112- }
113- )
114- )
115- else()
68+ } ;
11669
117- return (
118- if (exists($colSpacing))
119- then (
120- map:put($propsMap('columns'), 'columnSpacing', $colSpacing)
121- )
122- else(),
123- map:put($propsMap('columns'), 'columns', $colMap)
124- )` ,
70+ const data = evaluateXPathToMap < SectionProperties > (
71+ `map {
72+ "headers": map {
73+ "first": ./${ QNS . w } headerReference[@${ QNS . w } type = 'first']/@${ QNS . r } id/string(),
74+ "even": ./${ QNS . w } headerReference[@${ QNS . w } type = 'even']/@${ QNS . r } id/string(),
75+ "odd": ./${ QNS . w } headerReference[@${ QNS . w } type = 'default']/@${ QNS . r } id/string()
76+ },
77+ "footers": map {
78+ "first": ./${ QNS . w } footerReference[@${ QNS . w } type = 'first']/@${ QNS . r } id/string(),
79+ "even": ./${ QNS . w } footerReference[@${ QNS . w } type = 'even']/@${ QNS . r } id/string(),
80+ "odd": ./${ QNS . w } footerReference[@${ QNS . w } type = 'default']/@${ QNS . r } id/string()
81+ },
82+ "columns": map {
83+ "numberOfColumns": ./${ QNS . w } cols/@${ QNS . w } num/number(),
84+ "separator": docxml:st-on-off(./${ QNS . w } cols/@${ QNS . w } sep),
85+ "equalWidth": docxml:st-on-off(./${ QNS . w } cols/@${ QNS . w } equalwidth),
86+ "columns": array{}
87+ },
88+ "pageWidth": docxml:length(${ QNS . w } pgSz/@${ QNS . w } w, 'twip'),
89+ "pageHeight": docxml:length(${ QNS . w } pgSz/@${ QNS . w } h, 'twip'),
90+ "pageOrientation": ./${ QNS . w } pgSz/@${ QNS . w } orient/string(),
91+ "pageMargin": map {
92+ "top": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } top, 'twip'),
93+ "right": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } right, 'twip'),
94+ "bottom": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } bottom, 'twip'),
95+ "left": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } left, 'twip'),
96+ "header": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } header, 'twip'),
97+ "footer": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } footer, 'twip'),
98+ "gutter": docxml:length(./${ QNS . w } pgMar/@${ QNS . w } gutter, 'twip')
99+ },
100+ "isTitlePage": exists(./${ QNS . w } titlePg) and (not(./${ QNS . w } titlePg/@${ QNS . w } val) or docxml:st-on-off(./${ QNS . w } titlePg/@${ QNS . w } val))
101+ }` ,
125102 node ,
126103 ) ;
104+
105+ evaluateXPathToArray ( `array{./${ QNS . w } cols/${ QNS . w } col/map{
106+ "columnWidth": if (@${ QNS . w } w) then docxml:length(@${ QNS . w } w, 'twip') else (),
107+ "columnSpace": if (@${ QNS . w } space) then docxml:length(@${ QNS . w } space, 'twip') else ()
108+ }}` , node ) . forEach ( ( { columnWidth, columnSpace} ) => data . columns ?. columns ?. push (
109+ { columnWidth : columnWidth || null , columnSpace : columnSpace || null }
110+ ) ) ;
111+
112+ // console.log(data);
127113 return data ;
128114}
129115
130116export function sectionPropertiesToNode ( data : SectionProperties = { } ) : Node {
131- const query = create (
117+
118+ // console.log(data.columns?.columns?.forEach((col) => { console.log(col.columnWidth)}))
119+
120+ const newNode = create (
132121 `element ${ QNS . w } sectPr {
133122 if (exists($headers('first'))) then element ${ QNS . w } headerReference {
134123 attribute ${ QNS . r } id { $headers('first') },
@@ -154,23 +143,10 @@ export function sectionPropertiesToNode(data: SectionProperties = {}): Node {
154143 attribute ${ QNS . r } id { $footers('odd') },
155144 attribute ${ QNS . w } type { 'default' }
156145 } else (),
157- if (exists($columns)) then element ${ QNS . w } cols {
146+ if (exists($columns) and $columns('numberOfColumns') > 0 ) then element ${ QNS . w } cols {
158147 attribute ${ QNS . w } sep { $columns('separator') },
159- attribute ${ QNS . w } equalWidth { $columns('equalWidth') },
160- attribute ${ QNS . w } num { $columns('numberOfColumns') },
161- if (exists($columns('columnSpacing')))
162- then (
163- attribute ${ QNS . w } columnSpacing { $columns('columnSpacing')}
164- )
165- else (),
166- if (exists($columns('columns')))
167- then (
168- element columns {
169- attribute ${ QNS . w } w { round($columns('columns')('columnSize')('twip')) },
170- attribute ${ QNS . w } space { round($columns('columns')('columnSpacing')('twip')) }
171- }
172- )
173- else()
148+ attribute ${ QNS . w } equalwidth { $columns('equalWidth') },
149+ attribute ${ QNS . w } num { $columns('numberOfColumns') }
174150 } else (),
175151 if (exists($pageWidth) or exists($pageHeight) or $pageOrientation) then element ${ QNS . w } pgSz {
176152 if (exists($pageWidth)) then attribute ${ QNS . w } w {
@@ -218,16 +194,33 @@ export function sectionPropertiesToNode(data: SectionProperties = {}): Node {
218194 columns : {
219195 numberOfColumns : data . columns ?. numberOfColumns ,
220196 separator : data . columns ?. separator ,
221- equalWidth : data . columns ?. equalWidth ,
222- columnSpacing : data . columns ?. columnSpacing ,
223- columns : data . columns ?. columns ,
224- } ,
197+ equalWidth : data . columns ?. equalWidth } ,
225198 pageWidth : data . pageWidth || null ,
226199 pageHeight : data . pageHeight || null ,
227200 pageMargin : data . pageMargin || null ,
228201 pageOrientation : data . pageOrientation || null ,
229202 isTitlePage : data . isTitlePage || null ,
230- } ,
231- ) ;
232- return query ;
203+ }
204+ ) ;
205+ data . columns ?. columns ?. forEach ( ( col ) => {
206+ // console.log("COL");
207+ if ( col . columnWidth != undefined ) {
208+ update (
209+ newNode ,
210+ `let $cols := ./${ QNS . w } sectPr/${ QNS . w } cols
211+ return (
212+ if (exists($cols))
213+ then (
214+ insert node element ${ QNS . w } col {
215+ attribute ${ QNS . w } w { round(${ col . columnWidth ?. twip . toString ( ) } ) },
216+ attribute ${ QNS . w } space { round(${ col . columnSpace ?. twip . toString ( ) } ) }
217+ } as last into $cols
218+ ) else ()
219+ )
220+ `
221+ )
222+ }
223+ } ) ;
224+
225+ return newNode ;
233226}
0 commit comments