Skip to content

Commit b09d603

Browse files
committed
Re-render reading view on separator and semi-fold setting changes, unfold editor blocks on line limit change, per-block expand observers
1 parent 16606b0 commit b09d603

5 files changed

Lines changed: 74 additions & 74 deletions

File tree

‎main.js‎

Lines changed: 50 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/ReadingView/CodeBlockRenderer.ts‎

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
2020
plugin: CodeBlockCustomizerPlugin;
2121
context: MarkdownPostProcessorContext;
2222
observer: MutationObserver | null = null;
23-
expandObserver: ResizeObserver | null = null;
24-
expandInsertedListener: (() => void) | null = null;
23+
expandCleanups: Map<HTMLElement, () => void> = new Map();
2524
allPreElements: HTMLElement[] | null = null;
2625
codeBlockSectionInfo: MarkdownSectionInformation | null = null;
2726

@@ -40,15 +39,8 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
4039
this.observer.disconnect();
4140
}
4241

43-
if (this.expandObserver) {
44-
this.expandObserver.disconnect();
45-
this.expandObserver = null;
46-
}
47-
48-
if (this.expandInsertedListener) {
49-
this.expandInsertedListener();
50-
this.expandInsertedListener = null;
51-
}
42+
this.expandCleanups.forEach(cleanup => cleanup());
43+
this.expandCleanups.clear();
5244

5345
if (this.allPreElements) {
5446
for (const preElement of this.allPreElements) {
@@ -531,11 +523,11 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
531523
if (!previewView) {
532524
// reading view renders sections before attaching them, so wait until the block is in the DOM
533525
if (!preParent.isConnected) {
534-
this.expandInsertedListener?.();
535-
this.expandInsertedListener = preParent.onNodeInserted(() => {
536-
this.expandInsertedListener = null;
526+
this.expandCleanups.get(preElement)?.();
527+
this.expandCleanups.set(preElement, preParent.onNodeInserted(() => {
528+
this.expandCleanups.delete(preElement);
537529
this.setupExpandObserver(preElement);
538-
}, true);
530+
}, true));
539531
}
540532
return;
541533
}
@@ -561,12 +553,10 @@ export class CodeBlockRenderer extends MarkdownRenderChild {
561553
preParent.style.setProperty('--cbc-expand-margin', `${expandLeft - (preParentRect.left - currentMargin)}px`);
562554
};
563555

564-
if (this.expandObserver) {
565-
this.expandObserver.disconnect();
566-
}
567-
568-
this.expandObserver = new ResizeObserver(update);
569-
this.expandObserver.observe(previewView);
556+
this.expandCleanups.get(preElement)?.();
557+
const expandObserver = new ResizeObserver(update);
558+
expandObserver.observe(previewView);
559+
this.expandCleanups.set(preElement, () => expandObserver.disconnect());
570560

571561
update();
572562
}// setupExpandObserver

‎src/SettingsTab/Behavior.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DropdownComponent, Notice, Platform, Setting, TextComponent, ToggleComponent } from "obsidian";
1+
import { debounce, DropdownComponent, Notice, Platform, Setting, TextComponent, ToggleComponent } from "obsidian";
22

33
import CodeBlockCustomizerPlugin from "src/main";
44
import { createDetailsGroup, SettingsPage, SettingsPageData } from "./Common";
@@ -200,6 +200,7 @@ export class BehaviorSettings {
200200
const number = parseInt(value);
201201
this.plugin.settings.pluginSettings.semiFold.visibleLines = number;
202202
await this.plugin.saveSettings(true);
203+
this.plugin.renderReadingViews();
203204
})
204205
})
205206
.addToggle(toggle => semiFoldShowButton = toggle
@@ -227,6 +228,10 @@ export class BehaviorSettings {
227228
semiFoldEffect.settingEl.toggleClass('codeblock-customizer-setting-hidden', !this.plugin.settings.pluginSettings.semiFold.enableSemiFold);
228229

229230
let longCodeblockLinesInput: TextComponent;
231+
// wait until typing stops
232+
const saveLongCodeBlockLines = debounce(() => {
233+
void this.plugin.saveSettings(true).then(() => this.plugin.renderReadingViews());
234+
}, 750, true);
230235
const autoFold = new Setting(foldDetails)
231236
.setName('Auto semi-fold long code blocks')
232237
.setDesc('If enabled, code blocks longer than a specified number of lines will be semi-folded when a note is opened.')
@@ -237,6 +242,7 @@ export class BehaviorSettings {
237242
longCodeblockLinesInput.setDisabled(!value);
238243
longCodeblockLinesInput.inputEl.classList.toggle('is-disabled', !value);
239244
await this.plugin.saveSettings(true);
245+
this.plugin.renderReadingViews();
240246
})
241247
)
242248
.addText(text => {
@@ -251,7 +257,7 @@ export class BehaviorSettings {
251257
const lines = parseInt(value);
252258
if (!isNaN(lines)) {
253259
this.plugin.settings.pluginSettings.semiFold.longCodeBlockLines = lines;
254-
await this.plugin.saveSettings();
260+
saveLongCodeBlockLines();
255261
}
256262
});
257263
});

‎src/SettingsTab/Highlighting.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export class HighlightingSettings {
128128
.onChange(async (value) => {
129129
this.plugin.settings.pluginSettings.textHighlight.lineSeparator = value.charAt(0);
130130
await this.plugin.saveSettings();
131+
this.plugin.renderReadingViewsDebounced();
131132
})
132133
);
133134

@@ -140,6 +141,7 @@ export class HighlightingSettings {
140141
.onChange(async (value) => {
141142
this.plugin.settings.pluginSettings.textHighlight.textSeparator = value.charAt(0);
142143
await this.plugin.saveSettings();
144+
this.plugin.renderReadingViewsDebounced();
143145
})
144146
);
145147

‎src/main.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin, MarkdownView, WorkspaceLeaf, TAbstractFile, TFile, getLinkpath, Vault, Notice, Editor } from "obsidian";
1+
import { Plugin, MarkdownView, WorkspaceLeaf, TAbstractFile, TFile, getLinkpath, Vault, Notice, Editor, debounce } from "obsidian";
22

33
import { ChangeSet, Extension, StateField } from "@codemirror/state";
44
import { EditorView, DecorationSet } from "@codemirror/view";
@@ -84,6 +84,7 @@ export default class CodeBlockCustomizerPlugin extends Plugin {
8484
modifiedBlocks: Map<string, string> = new Map();
8585
executeCodeObservers: WeakMap<HTMLElement, MutationObserver> = new WeakMap();
8686
executeCodeLanguages: string[] = [];
87+
renderReadingViewsDebounced = debounce(() => this.renderReadingViews(), 750, true);
8788

8889
async onload() {
8990
for (const doc of getTargetDocuments(this.app)) {
@@ -311,6 +312,7 @@ export default class CodeBlockCustomizerPlugin extends Plugin {
311312

312313
if (this.debounceTimer)
313314
window.clearTimeout(this.debounceTimer);
315+
this.renderReadingViewsDebounced.cancel();
314316

315317
void this.savePermanentData();
316318
}// onunload

0 commit comments

Comments
 (0)