Skip to content

Commit 712c9c3

Browse files
refactor: enhance BudgetCodeWriter integration in BudgetCodeBlock by passing app instance and removing redundant constructor parameters
1 parent 0f6e3b8 commit 712c9c3

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/codeblocks/BudgetCodeBlock.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class BudgetCodeBlock extends MarkdownRenderChild {
2121
private readonly categories: TableCategories;
2222
private readonly rows: TableRows;
2323
private component: Record<string, unknown>;
24+
private readonly budgetCodeWriter: BudgetCodeWriter;
2425

2526
constructor(
2627
markdownSource: string,
@@ -36,6 +37,7 @@ export class BudgetCodeBlock extends MarkdownRenderChild {
3637

3738
this.categories = categories;
3839
this.rows = rows;
40+
this.budgetCodeWriter = new BudgetCodeWriter(this.app);
3941
}
4042

4143
private createTableStore(): [TableStore, TableStateStore] {
@@ -65,7 +67,7 @@ export class BudgetCodeBlock extends MarkdownRenderChild {
6567

6668
try {
6769
logInfo('The table data is being saved');
68-
await new BudgetCodeWriter().write(newData, sectionInfo, this.app);
70+
await this.budgetCodeWriter.write(newData, sectionInfo);
6971

7072
if (cmScroller) {
7173
cmScroller.scrollTop = scrollPosition.top;

src/codeblocks/BudgetCodeWriter.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class BudgetCodeWriter {
88
private values: TableStoreValues;
99
private isWriting = false;
1010

11-
constructor() {
11+
constructor(protected readonly app: App) {
1212
this.values = {
1313
categories: new Map(),
1414
rows: new Map(),
@@ -109,16 +109,15 @@ export class BudgetCodeWriter {
109109
}
110110

111111
private async updateFileContent(
112-
app: App,
113112
sectionInfo: MarkdownSectionInformation,
114113
formattedCode: string
115114
): Promise<void> {
116-
const activeFile = app.workspace.getActiveFile();
115+
const activeFile = this.app.workspace.getActiveFile();
117116
if (!activeFile) {
118117
throw new Error('Active file not found');
119118
}
120119

121-
const editor = await app.vault.read(activeFile);
120+
const editor = await this.app.vault.read(activeFile);
122121
const lines = editor.split('\n');
123122
const currentSection = lines.slice(sectionInfo.lineStart, sectionInfo.lineEnd + 1);
124123

@@ -133,7 +132,7 @@ export class BudgetCodeWriter {
133132
// Make sure we're only using exactly one closing code block
134133
const newContent = [codeBlockStart, formattedCode.trim(), '```'].join('\n');
135134

136-
const view = app.workspace.getActiveViewOfType(MarkdownView);
135+
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
137136
if (!view) {
138137
throw new Error('Active markdown view not found');
139138
}
@@ -147,8 +146,7 @@ export class BudgetCodeWriter {
147146

148147
public async write(
149148
values: TableStoreValues,
150-
sectionInfo: MarkdownSectionInformation,
151-
app: App
149+
sectionInfo: MarkdownSectionInformation
152150
): Promise<void> {
153151
if (this.isWriting) {
154152
logWarning('Writing is already in progress, skipping');
@@ -164,7 +162,7 @@ export class BudgetCodeWriter {
164162
try {
165163
this.values = values;
166164
const formattedCode = this.formatCode(newCode);
167-
await this.updateFileContent(app, sectionInfo, formattedCode);
165+
await this.updateFileContent(sectionInfo, formattedCode);
168166
} catch (error) {
169167
logError('Error writing to file', error);
170168
} finally {

0 commit comments

Comments
 (0)