@@ -1731,17 +1731,25 @@ function prepareCheckboxAnnotation(params) {
17311731 */
17321732function prepareHtmlAnnotation ( params ) {
17331733 const {
1734- node : { attrs = { } } ,
1734+ node : { attrs = { } , marks = [ ] } ,
1735+ editorSchema,
17351736 } = params ;
17361737
17371738 const parser = new window . DOMParser ( ) ;
17381739 const paragraphHtml = parser . parseFromString ( attrs . rawHtml || attrs . displayLabel , 'text/html' ) ;
1740+ const marksFromAttrs = translateFieldAttrsToMarks ( attrs ) ;
1741+ const allMarks = [ ...marks , ...marksFromAttrs ]
17391742
1740- const state = EditorState . create ( {
1741- doc : PMDOMParser . fromSchema ( params . editorSchema ) . parse ( paragraphHtml ) ,
1743+ let state = EditorState . create ( {
1744+ doc : PMDOMParser . fromSchema ( editorSchema ) . parse ( paragraphHtml ) ,
17421745 } ) ;
17431746
1747+ if ( allMarks . length ) {
1748+ state = applyMarksToHtmlAnnotation ( state , allMarks ) ;
1749+ }
1750+
17441751 const htmlAnnotationNode = state . doc . toJSON ( ) ;
1752+
17451753 return {
17461754 name : 'htmlAnnotation' ,
17471755 elements : translateChildNodes ( {
@@ -1896,6 +1904,20 @@ function translateFieldAnnotation(params) {
18961904 'w:val' : attrs . multipleImage ,
18971905 } ,
18981906 } ,
1907+ {
1908+ name : 'w:fieldFontFamily' ,
1909+ attributes : {
1910+ 'xmlns:w' : customXmlns ,
1911+ 'w:val' : attrs . fontFamily ,
1912+ } ,
1913+ } ,
1914+ {
1915+ name : 'w:fieldFontSize' ,
1916+ attributes : {
1917+ 'xmlns:w' : customXmlns ,
1918+ 'w:val' : attrs . fontSize ,
1919+ } ,
1920+ } ,
18991921 ] ,
19001922 } ,
19011923 {
@@ -2103,3 +2125,46 @@ function resizeKeepAspectRatio(width, height, maxWidth) {
21032125 }
21042126 return { width, height } ;
21052127}
2128+
2129+ function applyMarksToHtmlAnnotation ( state , marks ) {
2130+ const { tr, doc, schema } = state ;
2131+ const allowedMarks = [ 'fontFamily' , 'fontSize' ] ;
2132+
2133+ if (
2134+ ! marks . some ( ( m ) => allowedMarks . includes ( m . type ) )
2135+ ) {
2136+ return state ;
2137+ }
2138+
2139+ const fontFamily = marks . find ( ( m ) => m . type === 'fontFamily' ) ;
2140+ const fontSize = marks . find ( ( m ) => m . type === 'fontSize' ) ;
2141+
2142+ doc . descendants ( ( node , pos ) => {
2143+ if ( ! node . isText ) return ;
2144+
2145+ const found = node . marks . find ( ( m ) => m . type . name === 'textStyle' ) ;
2146+ const textStyleType = schema . marks . textStyle ;
2147+
2148+ if ( ! found ) {
2149+ tr . addMark ( pos , pos + node . nodeSize , textStyleType . create ( {
2150+ ...fontFamily ?. attrs ,
2151+ ...fontSize ?. attrs ,
2152+ } ) ) ;
2153+ return ;
2154+ }
2155+
2156+ if ( ! found ?. attrs . fontFamily && fontFamily ) {
2157+ tr . addMark ( pos , pos + node . nodeSize , textStyleType . create ( {
2158+ ...found ?. attrs ,
2159+ ...fontFamily . attrs ,
2160+ } ) ) ;
2161+ } else if ( ! found ?. attrs . fontSize && fontSize ) {
2162+ tr . addMark ( pos , pos + node . nodeSize , textStyleType . create ( {
2163+ ...found ?. attrs ,
2164+ ...fontSize . attrs ,
2165+ } ) ) ;
2166+ }
2167+ } ) ;
2168+
2169+ return state . apply ( tr ) ;
2170+ } ;
0 commit comments