@@ -29,7 +29,7 @@ export const LinkedStyles = Extension.create({
2929 const parentNode = findParentNode ( ( node ) => node . type . name === 'paragraph' ) ( tr . selection ) || { } ;
3030 pos = parentNode . pos ;
3131 paragraphNode = parentNode . node ;
32- } ;
32+ }
3333
3434 tr . setNodeMarkup ( pos , undefined , {
3535 ...paragraphNode . attrs ,
@@ -54,15 +54,15 @@ const createLinkedStylesPlugin = (editor) => {
5454 const styles = editor . converter ?. linkedStyles || [ ] ;
5555 return {
5656 styles,
57- decorations : generateDecorations ( editor . state ?. doc , styles ) ,
57+ decorations : generateDecorations ( editor . state , styles ) ,
5858 } ;
5959 } ,
6060 apply ( tr , prev , oldEditorState , newEditorState ) {
6161 if ( ! editor . converter || editor . options . mode !== 'docx' ) return { ...prev } ;
6262 let decorations = prev . decorations || DecorationSet . empty ;
6363 if ( tr . docChanged ) {
6464 const styles = LinkedStylesPluginKey . getState ( editor . state ) . styles ;
65- decorations = generateDecorations ( newEditorState . doc , styles ) ;
65+ decorations = generateDecorations ( newEditorState , styles ) ;
6666 }
6767
6868 return { ...prev , decorations } ;
@@ -79,13 +79,15 @@ const createLinkedStylesPlugin = (editor) => {
7979/**
8080 * Generate style decorations for linked styles
8181 *
82- * @param {Object } doc The current document object
82+ * @param {Object } state Editor state
8383 * @param {Array[Object] } styles The linked styles
8484 * @returns {DecorationSet } The decorations
8585 */
86- const generateDecorations = ( doc , styles ) => {
86+ const generateDecorations = ( state , styles ) => {
8787 const decorations = [ ] ;
8888 let lastStyleId = null ;
89+ const doc = state ?. doc ;
90+
8991 doc . descendants ( ( node , pos ) => {
9092 const { name } = node . type ;
9193
@@ -97,7 +99,10 @@ const generateDecorations = (doc, styles) => {
9799 const linkedStyle = getLinkedStyle ( lastStyleId , styles ) ;
98100 if ( ! linkedStyle ) return ;
99101
100- const styleString = generateLinkedStyleString ( linkedStyle , node ) ;
102+ const $pos = state . doc . resolve ( pos ) ;
103+ const parent = $pos . parent ;
104+
105+ const styleString = generateLinkedStyleString ( linkedStyle , node , parent ) ;
101106 if ( ! styleString ) return ;
102107
103108 const decoration = Decoration . inline ( pos , pos + node . nodeSize , { style : styleString } ) ;
@@ -112,9 +117,10 @@ const generateDecorations = (doc, styles) => {
112117 *
113118 * @param {Object } linkedStyle The linked style object
114119 * @param {Object } node The current node
120+ * @param {Object } parent The parent of current
115121 * @returns {String } The style string
116122 */
117- export const generateLinkedStyleString = ( linkedStyle , node , includeSpacing = true ) => {
123+ export const generateLinkedStyleString = ( linkedStyle , node , parent , includeSpacing = true ) => {
118124 if ( ! linkedStyle ?. definition ?. styles ) return '' ;
119125 const markValue = { } ;
120126
@@ -138,16 +144,19 @@ export const generateLinkedStyleString = (linkedStyle, node, includeSpacing = tr
138144
139145 // Check if this node has the expected mark. If yes, we are not overriding it
140146 const mark = flattenedMarks . find ( ( n ) => n . key === key ) ;
147+ const hasParentIndent = Object . keys ( parent ?. attrs . indent || { } ) ;
148+ const hasParentSpacing = Object . keys ( parent ?. attrs . spacing || { } ) ;
141149
142150 // If no mark already in the node, we override the style
143151 if ( ! mark ) {
144- if ( key === 'spacing' && includeSpacing ) {
152+ if ( key === 'spacing' && includeSpacing && ! hasParentSpacing ) {
145153 const space = getSpacingStyle ( value ) ;
146154 Object . entries ( space ) . forEach ( ( [ k , v ] ) => {
147155 markValue [ k ] = v ;
148156 } ) ;
149- } else if ( key === 'indent' && includeSpacing ) {
157+ } else if ( key === 'indent' && includeSpacing && ! hasParentIndent ) {
150158 const { leftIndent, rightIndent, firstLine } = value ;
159+
151160 if ( leftIndent ) markValue [ 'margin-left' ] = leftIndent + 'px' ;
152161 if ( rightIndent ) markValue [ 'margin-right' ] = rightIndent + 'px' ;
153162 if ( firstLine ) markValue [ 'text-indent' ] = firstLine + 'px' ;
@@ -158,7 +167,7 @@ export const generateLinkedStyleString = (linkedStyle, node, includeSpacing = tr
158167 }
159168 }
160169 } ) ;
161-
170+
162171 return Object . entries ( markValue ) . map ( ( [ key , value ] ) => `${ key } : ${ value } ` ) . join ( ';' ) ;
163172} ;
164173
@@ -174,11 +183,11 @@ const getLinkedStyle = (styleId, styles = []) => {
174183} ;
175184
176185export const getSpacingStyle = ( spacing ) => {
177- const { lineSpaceBefore, lineSpaceAfter, line } = spacing ;
186+ const { lineSpaceBefore, lineSpaceAfter, line, lineRule } = spacing ;
178187 return {
179188 'margin-top' : lineSpaceBefore + 'px' ,
180189 'margin-bottom' : lineSpaceAfter + 'px' ,
181- ...getLineHeightValueString ( line , '' )
190+ ...getLineHeightValueString ( line , '' , lineRule , true )
182191 } ;
183192} ;
184193
0 commit comments