@@ -162,7 +162,7 @@ export function parseDiffCode(container: HTMLElement, code: string, outputFormat
162162 const trList = diffHtmlStr . match ( TableTrReg ) as RegExpMatchArray ;
163163 const trListLength = trList . length ;
164164 let newTrStr = '' ;
165- const offset = trListLength / 2 ;
165+ const offset = Math . floor ( trListLength / 2 ) ;
166166 for ( let i = 0 ; i < trListLength / 2 ; i ++ ) {
167167 const leftTdList = trList [ i ] . match ( TableTdReg ) ;
168168 const rightTdList = trList [ i + offset ] . match ( TableTdReg ) ;
@@ -197,10 +197,37 @@ export function setLineNumberInDataset(trNode: HTMLElement, prevL: number, prevR
197197
198198// 中间行展开后,折叠行数小于阈值时,将向上向下展开按钮更新为全部展开
199199export function updateExpandUpDownButton ( trNode : HTMLElement ) {
200- trNode . children [ 0 ] . children [ 0 ] . remove ( ) ;
201- trNode . children [ 0 ] . children [ 0 ] . classList . remove ( 'up-expand' ) ;
202- trNode . children [ 0 ] . children [ 0 ] . classList . add ( 'all-expand' ) ;
203- trNode . children [ 0 ] . children [ 0 ] . innerHTML = AllExpandIcon ( ) ;
200+ trNode . children [ 0 ] ?. children [ 0 ] ?. remove ( ) ;
201+ trNode . children [ 0 ] ?. children [ 0 ] ?. classList . remove ( 'up-expand' ) ;
202+ trNode . children [ 0 ] ?. children [ 0 ] ?. classList . add ( 'all-expand' ) ;
203+ trNode . children [ 0 ] ?. children [ 0 ] && ( trNode . children [ 0 ] . children [ 0 ] . innerHTML = AllExpandIcon ( ) ) ;
204+ }
205+
206+ /**
207+ * 查找给定节点的前一个同级元素节点。
208+ * 适用于下一行是新增行或者删除行的情况
209+ *
210+ * @param _node 如果折叠行后一行左边获取不到行数,则寻找第一个左边有行数的行。
211+ * @returns 返回找到的后一个同级元素节点,如果没有找到则返回null。
212+ */
213+ export function findNextElement ( _node : Element ) : Element {
214+ if ( _node . children [ 0 ] ?. innerText ) {
215+ return _node ;
216+ }
217+ return findNextElement ( _node . nextElementSibling ) ;
218+ }
219+ /**
220+ * 查找给定节点的前一个同级元素节点。
221+ * 适用于上一行是新增行或者删除行或者评论行的情况
222+ *
223+ * @param _node 如果折叠行后一行左边获取不到行数,则寻找第一个左边有行数的行。
224+ * @returns 返回找到的前一个同级元素节点,如果没有找到则返回null。
225+ */
226+ export function findPrevElement ( _node : Element ) : Element {
227+ if ( _node . children [ 0 ] ?. innerText ) {
228+ return _node ;
229+ }
230+ return findPrevElement ( _node . previousElementSibling ) ;
204231}
205232
206233/*
@@ -221,20 +248,20 @@ export function updateLineNumberInDatasetForDoubleColumn(
221248 let nextR : number ;
222249 let prevR : number ;
223250 if ( position === 'top' ) {
224- const nextLineNode = trNode . nextElementSibling as HTMLElement ;
251+ const nextLineNode = findNextElement ( trNode . nextElementSibling ) as HTMLElement ;
225252 nextL = parseInt ( ( nextLineNode . children [ 0 ] as HTMLElement ) . innerText ) - 1 ;
226253 prevL = Math . max ( nextL - expandThreshold + 1 , 1 ) ;
227254 nextR = parseInt ( ( nextLineNode . children [ 2 ] as HTMLElement ) . innerText ) - 1 ;
228255 prevR = Math . max ( nextR - expandThreshold + 1 , 1 ) ;
229256 } else if ( position === 'bottom' ) {
230- const prevLineNode = trNode . previousElementSibling as HTMLElement ;
257+ const prevLineNode = findPrevElement ( trNode . previousElementSibling ) as HTMLElement ;
231258 prevL = parseInt ( ( prevLineNode ?. children [ 0 ] as HTMLElement ) ?. innerText ) + 1 ;
232259 nextL = prevL + expandThreshold - 1 ;
233260 prevR = parseInt ( ( prevLineNode ?. children [ 2 ] as HTMLElement ) ?. innerText ) + 1 ;
234261 nextR = prevR + expandThreshold - 1 ;
235262 } else {
236- const prevLineNode = trNode . previousElementSibling as HTMLElement ;
237- const nextLineNode = trNode . nextElementSibling as HTMLElement ;
263+ const prevLineNode = findPrevElement ( trNode . previousElementSibling ) as HTMLElement ;
264+ const nextLineNode = findNextElement ( trNode . nextElementSibling ) as HTMLElement ;
238265 const prevLineNumber = parseInt ( ( prevLineNode . children [ 0 ] as HTMLElement ) . innerText ) ;
239266 const nextLineNumber = parseInt ( ( nextLineNode . children [ 0 ] as HTMLElement ) . innerText ) ;
240267 prevL = prevLineNumber + 1 ;
@@ -243,6 +270,7 @@ export function updateLineNumberInDatasetForDoubleColumn(
243270 nextR = parseInt ( ( nextLineNode . children [ 2 ] as HTMLElement ) . innerText ) - 1 ;
244271 const isExpandAll = nextLineNumber - prevLineNumber <= expandThreshold ;
245272 if ( isExpandAll && updateExpandButton ) {
273+ trNode . style . display = 'none' ;
246274 updateExpandUpDownButton ( trNode ) ;
247275 }
248276 }
0 commit comments