@@ -132,25 +132,23 @@ export const lineNumberRender = (block: HTMLElement, zoom = 1) => {
132132 if ( lineList [ lineList . length - 1 ] === "" && lineList . length > 1 ) {
133133 lineList . pop ( ) ;
134134 }
135- block . firstElementChild . innerHTML = `<span>${ lineList . length } </span>` ;
136- codeElement . style . paddingLeft = `${ block . firstElementChild . clientWidth + 16 } px` ;
137135
138- const codeElementStyle = window . getComputedStyle ( codeElement ) ;
139- const lineNumberTemp = document . createElement ( "div" ) ;
140- lineNumberTemp . className = "hljs" ;
141- // 不能使用 codeElement.clientWidth,被忽略小数点导致宽度不一致
142- lineNumberTemp . setAttribute ( "style" , `padding-left:${ codeElement . style . paddingLeft } ;
136+ let lineNumberHTML = "" ;
137+ if ( codeElement . style . wordBreak === "break-word" ) {
138+ // 代码块开启了换行
139+ const codeElementStyle = window . getComputedStyle ( codeElement ) ;
140+ const lineNumberTemp = document . createElement ( "div" ) ;
141+ lineNumberTemp . className = "hljs" ;
142+ // 不能使用 codeElement.clientWidth,被忽略小数点导致宽度不一致
143+ lineNumberTemp . setAttribute ( "style" , `padding-left:${ codeElement . style . paddingLeft } ;
143144width: ${ codeElement . getBoundingClientRect ( ) . width / zoom } px;
144145white-space:${ codeElementStyle . whiteSpace } ;
145146word-break:${ codeElementStyle . wordBreak } ;
146147font-variant-ligatures:${ codeElementStyle . fontVariantLigatures } ;
147148padding-right:0;max-height: none;box-sizing: border-box;position: absolute;padding-top:0 !important;padding-bottom:0 !important;min-height:auto !important;` ) ;
148- lineNumberTemp . setAttribute ( "contenteditable" , "true" ) ;
149- block . insertAdjacentElement ( "afterend" , lineNumberTemp ) ;
149+ lineNumberTemp . setAttribute ( "contenteditable" , "true" ) ;
150+ block . insertAdjacentElement ( "afterend" , lineNumberTemp ) ;
150151
151- let lineNumberHTML = "" ;
152- if ( codeElement . style . wordBreak === "break-word" ) {
153- // 代码块开启了换行
154152 lineList . map ( ( line ) => {
155153 // windows 下空格高度为 0 https://github.com/siyuan-note/siyuan/issues/12346
156154 lineNumberTemp . textContent = line . trim ( ) ? line : "<br>" ;
@@ -160,12 +158,21 @@ padding-right:0;max-height: none;box-sizing: border-box;position: absolute;paddi
160158 // https://github.com/siyuan-note/siyuan/issues/9140
161159 lineNumberHTML += `<span style="height:${ lineNumberTemp . clientHeight } px"></span>` ;
162160 } ) ;
161+ lineNumberTemp . remove ( ) ;
163162 } else {
164163 lineNumberHTML = "<span></span>" . repeat ( lineList . length ) ;
165164 }
166165
167- lineNumberTemp . remove ( ) ;
168166 block . firstElementChild . innerHTML = lineNumberHTML ;
167+
168+ // 用最后一个行号元素计算宽度
169+ const lastLineNumberElement = block . firstElementChild . lastElementChild as HTMLElement ;
170+ if ( lastLineNumberElement ) {
171+ lastLineNumberElement . textContent = lineList . length . toString ( ) ;
172+ codeElement . style . paddingLeft = `${ lastLineNumberElement . offsetWidth + 16 } px` ;
173+ lastLineNumberElement . textContent = "" ;
174+ }
175+
169176 // https://github.com/siyuan-note/siyuan/issues/12726
170177 if ( block . scrollHeight > block . clientHeight ) {
171178 if ( getSelection ( ) . rangeCount > 0 ) {
0 commit comments