11import { debounce , Debouncer , MarkdownRenderChild , MarkdownView } from "obsidian" ;
22
3- import { getLanguageIcon , createCodeblockIcon , createCodeblockLang , getLanguageSpecificColorClass , getDefaultParameters , getCurrentMode , createContainer , createFileName , createCodeblockCollapse , getBorderColorByLanguage , getPropertyFromLanguageSpecificColors , isSpecificHeader , determineDefaultFoldState , getDisplayLanguageName } from "../Utils" ;
3+ import { getLanguageIcon , createCodeblockIcon , createCodeblockLang , getLanguageSpecificColorClass , getDefaultParameters , getCurrentMode , createContainer , createFileName , createCodeblockCollapse , getBorderColorByLanguage , getPropertyFromLanguageSpecificColors , isSpecificHeader , isFoldDisabled , getDisplayLanguageName } from "../Utils" ;
44import { createButtons , toggleFold , reassignFadeOutClasses } from "./ReadingViewUtils" ;
5- import { fadeOutLineCount } from "../Const" ;
5+ import { DEFAULT_COLLAPSE_TEXT , fadeOutLineCount , UNCOLLAPSE_EVENT } from "../Const" ;
66import CodeBlockCustomizerPlugin from "../main" ;
77import { FoldingState } from "../EditorView/EditorEffects" ;
88import { FoldingScope , TabPersistence } from "../Settings" ;
@@ -95,6 +95,14 @@ export class GroupedCodeBlockRenderChild extends MarkdownRenderChild {
9595 private processGroup ( group : HTMLPreElement [ ] , groupName : string ) {
9696 const firstBlock = group [ 0 ] ;
9797
98+ for ( const block of group ) {
99+ const foldAsTab = block . dataset . foldAsTab ;
100+ if ( foldAsTab ) {
101+ delete block . dataset . foldAsTab ;
102+ block . classList . add ( foldAsTab === 'semi' ? 'codeblock-customizer-codeblock-semi-collapsed' : 'codeblock-customizer-codeblock-collapsed' , 'codeblock-customizer-codeblock-default-collapse' ) ;
103+ }
104+ }
105+
98106 this . hideGroupedCodeBlocks ( group ) ;
99107
100108 const parameters = this . getParametersFromElement ( firstBlock ) ;
@@ -125,8 +133,8 @@ export class GroupedCodeBlockRenderChild extends MarkdownRenderChild {
125133 this . updateHeaderLanguageSpecificClasses ( header , currentParameters . language ) ;
126134 languageIconElement = this . updateHeaderLanguageIcon ( header , languageIconElement , currentParameters . displayLanguage ) ;
127135 this . updateHeaderButtons ( groupButtonsContainer , currentParameters , currentBlock ) ;
128- collapseIconElement = this . updateHeaderCollapseIcon ( collapseIconElement , header , currentBlock , currentParameters , lineCount ) ;
129- this . updateHeaderFileName ( fileNameContainer , currentParameters . headerDisplayText ) ;
136+ collapseIconElement = this . updateHeaderCollapseIcon ( collapseIconElement , header , currentBlock , currentParameters ) ;
137+ this . updateHeaderFileName ( fileNameContainer , currentParameters , currentBlock ) ;
130138 } ;
131139
132140 const documentPath = this . view . file ?. path || 'unknown_document' ;
@@ -222,18 +230,15 @@ export class GroupedCodeBlockRenderChild extends MarkdownRenderChild {
222230 }
223231 } // updateHeaderButtons
224232
225- private updateHeaderCollapseIcon ( collapseIcon : HTMLElement | null , header : HTMLElement , currentBlock : HTMLPreElement , parameters : CBCParameters , lineCount : number ) : HTMLElement | null {
233+ private updateHeaderCollapseIcon ( collapseIcon : HTMLElement | null , header : HTMLElement , currentBlock : HTMLPreElement , parameters : CBCParameters ) : HTMLElement | null {
226234 if ( collapseIcon && collapseIcon . parentElement === header ) {
227235 header . removeChild ( collapseIcon ) ;
228236 }
229237
230- const disableFoldUnlessSpecified = this . plugin . settings . pluginSettings . header . disableFoldUnlessSpecified ;
231- const specificHeader = isSpecificHeader ( parameters , this . plugin . settings , true , lineCount , "reading" ) ;
232- const { foldByDefault } = determineDefaultFoldState ( parameters , this . plugin . settings , lineCount , specificHeader , "reading" ) ;
233- const isCollapseEnabled = ! disableFoldUnlessSpecified || foldByDefault ;
234238 let newCollapseIcon : HTMLElement | null = null ;
235239
236- if ( isCollapseEnabled ) {
240+ if ( ! isFoldDisabled ( parameters , this . plugin . settings ) ) {
241+ header . classList . remove ( `noCollapseIcon` ) ; // the previous tab may have had no collapse icon
237242 const isFullyCollapsed = currentBlock . classList . contains ( 'codeblock-customizer-codeblock-collapsed' ) ;
238243 const isSemiCollapsed = currentBlock . classList . contains ( 'codeblock-customizer-codeblock-semi-collapsed' ) ;
239244 const collapseStyle = this . plugin . settings . pluginSettings . header . collapseIconStyle ;
@@ -255,22 +260,24 @@ export class GroupedCodeBlockRenderChild extends MarkdownRenderChild {
255260 return newCollapseIcon ;
256261 } // updateHeaderCollapseIcon
257262
258- private updateHeaderFileName ( fileNameElement : HTMLElement , headerDisplayText : string ) {
263+ private updateHeaderFileName ( fileNameElement : HTMLElement , parameters : CBCParameters , currentBlock : HTMLPreElement ) {
264+ const isCollapsed = currentBlock . classList . contains ( 'codeblock-customizer-codeblock-collapsed' ) || currentBlock . classList . contains ( 'codeblock-customizer-codeblock-semi-collapsed' ) ;
259265 fileNameElement . empty ( ) ;
260- fileNameElement . setText ( headerDisplayText ) ;
266+ fileNameElement . setText ( ! parameters . hasTitle && isCollapsed ? ( this . plugin . settings . pluginSettings . header . collapsedCodeText || DEFAULT_COLLAPSE_TEXT ) : parameters . headerDisplayText ) ;
261267 } // updateHeaderFileName
262268
263269 private addHeaderClickHandler ( headerContainer : HTMLElement , tabsContainer : HTMLElement , buttonsContainer : HTMLElement , group : HTMLPreElement [ ] ) {
264- const headerClickHandler = ( event : MouseEvent ) => {
270+ const headerClickHandler = ( event : Event ) => {
265271 if ( ! tabsContainer . contains ( event . target as Node ) && ! buttonsContainer . contains ( event . target as Node ) && ! ( event . target as HTMLElement ) . closest ( 'a' ) ) {
266272 const activeBlock = group . find ( block => ! block . classList . contains ( 'codeblock-customizer-grouped-block-hidden' ) ) ;
267273 if ( ! activeBlock )
268274 return ;
269275
270- this . foldCodeBlcok ( activeBlock , headerContainer ) ;
276+ this . foldCodeBlcok ( activeBlock , headerContainer , event . type === UNCOLLAPSE_EVENT ) ;
271277 }
272278 } ;
273279 headerContainer . addEventListener ( 'click' , headerClickHandler , { signal : this . getListenerSignal ( ) } ) ;
280+ headerContainer . addEventListener ( UNCOLLAPSE_EVENT , headerClickHandler , { signal : this . getListenerSignal ( ) } ) ;
274281 } // addHeaderClickHandler
275282
276283 private addHeaderHoverEffect ( headerContainer : HTMLElement , groupedBlocks : HTMLPreElement [ ] , buttonsContainer : HTMLElement ) {
@@ -573,7 +580,7 @@ export class GroupedCodeBlockRenderChild extends MarkdownRenderChild {
573580 tabsContainer . addEventListener ( 'click' , tabClickHandler , { signal : this . getListenerSignal ( ) } ) ;
574581 } // addTabClickHandler
575582
576- private foldCodeBlcok ( activeBlock : HTMLPreElement , header : HTMLElement ) {
583+ private foldCodeBlcok ( activeBlock : HTMLPreElement , header : HTMLElement , fromUncollapseButton = false ) {
577584 const lines = Array . from ( activeBlock . querySelectorAll ( 'code > div' ) ) ;
578585 const visibleLines = lines . filter ( child => ! child . classList . contains ( 'codeblock-customizer-cmdoutput-line' ) && ! child . classList . contains ( 'hidden-line-hidden' ) ) ;
579586 const parameters = this . getParametersFromElement ( activeBlock ) ;
@@ -582,7 +589,7 @@ export class GroupedCodeBlockRenderChild extends MarkdownRenderChild {
582589 const foldSettings = this . plugin . settings . pluginSettings . codeblock . folding ;
583590
584591 const currentCollapseIcon = header . querySelector < HTMLElement > ( '.codeblock-customizer-header-collapse' ) ;
585- if ( ! currentCollapseIcon )
592+ if ( ! currentCollapseIcon && ! fromUncollapseButton )
586593 return ;
587594
588595 const isCollapsed = activeBlock . classList . contains ( 'codeblock-customizer-codeblock-collapsed' ) ;
@@ -613,15 +620,19 @@ export class GroupedCodeBlockRenderChild extends MarkdownRenderChild {
613620 }
614621 }
615622
623+ const fileNameElement = header . querySelector < HTMLElement > ( '.codeblock-customizer-header-text' ) ;
624+ if ( fileNameElement ) {
625+ this . updateHeaderFileName ( fileNameElement , parameters , activeBlock ) ;
626+ }
627+
616628 const sourcePath = activeBlock . getAttribute ( 'sourcepath' ) ;
617629 const charPosStr = activeBlock . dataset . charPos ;
618630 const remember = foldSettings . scope === FoldingScope . All || ( foldSettings . scope === FoldingScope . NoFoldSpecified && ! parameters . fold && ! parameters . unfold ) ;
619631
620632 if ( remember && sourcePath && charPosStr ) {
621633 const charPos = parseInt ( charPosStr , 10 ) ;
622634 if ( ! isNaN ( charPos ) ) {
623- // the initial fold state of grouped blocks is set by CodeBlockRenderer, which doesn't treat them as tabbed group members so isMemberOfTabbedGroup must be false
624- this . plugin . setFoldState ( sourcePath , charPos , newState , 'reading' , parameters , lines . length , false ) ;
635+ this . plugin . setFoldState ( sourcePath , charPos , newState , 'reading' , parameters , lines . length , true ) ;
625636 }
626637 }
627638 } // foldCodeBlcok
0 commit comments