Skip to content

Commit

Permalink
feat(#24): codeblock counter in status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Jul 5, 2024
1 parent 1d09331 commit a6a7c53
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "api-request",
"name": "APIRequest",
"version": "1.3.8",
"version": "1.3.9",
"minAppVersion": "0.15.0",
"description": "Fetch data from APIs or other sources. Responses in JSON, MD or HTML directly in your notes.",
"author": "rooyca",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "api-request",
"version": "1.3.8",
"version": "1.3.9",
"description": "Fetch data from APIs or other sources. Responses in JSON, MD or HTML directly in your notes.",
"main": "main.js",
"scripts": {
Expand Down
23 changes: 22 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// CLEAN UP THIS MESS
// ---------------------------------------------

import { App, Editor, MarkdownView, Modal, Plugin, Notice, requestUrl } from 'obsidian';
import { App, Editor, MarkdownView, Modal, Plugin, Notice, requestUrl, setIcon, debounce } from 'obsidian';
import { readFrontmatter, parseFrontmatter } from 'src/functions/frontmatterUtils';
import { MarkdownParser } from 'src/functions/mdparse';
import { saveToID, addBtnCopy, replaceOrder, nestedValue, toDocument } from 'src/functions/general';
Expand Down Expand Up @@ -79,6 +79,26 @@ export default class MainAPIR extends Plugin {
console.log('loading APIR');
await this.loadSettings();

async function updateStatusBar() {
const statusbar = document.getElementsByClassName("status-bar-item plugin-api-request");
while (statusbar[0]) {
statusbar[0].parentNode?.removeChild(statusbar[0]);
}

// count the number of code-blocks
const markdownContent = this.app.workspace.getActiveViewOfType(MarkdownView)!.getViewData();
const codeBlocks = markdownContent.match(/```req/g)?.length || 0;
if (codeBlocks > 0) {
const item = this.addStatusBarItem();

const statusText = this.settings.countBlocksText.replace("%d", codeBlocks.toString());
item.createEl("span", { text: statusText });
}
}

this.registerEvent(this.app.workspace.on('file-open', debounce(updateStatusBar.bind(this), 300)));
this.registerEvent(this.app.workspace.on('editor-change', updateStatusBar.bind(this)));

this.addCommand({
id: 'show-response-in-modal',
name: 'Show response in Modal',
Expand Down Expand Up @@ -406,6 +426,7 @@ export default class MainAPIR extends Plugin {
});
}
}));
return;
}

const trimAndProcessKey = (key: string) => {
Expand Down
2 changes: 2 additions & 0 deletions src/settings/settingsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface LoadAPIRSettings {
Key: string;
Value: string;
KeyValueCodeblocks: object[];
countBlocksText: string;
}

export const DEFAULT_SETTINGS: LoadAPIRSettings = {
Expand All @@ -24,4 +25,5 @@ export const DEFAULT_SETTINGS: LoadAPIRSettings = {
Key: '',
Value: '',
KeyValueCodeblocks: [],
countBlocksText: 'Count blocks: %d',
}
11 changes: 11 additions & 0 deletions src/settings/settingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ export default class APRSettings extends PluginSettingTab {
this.plugin.settings.DisabledReq = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Status-bar text')
.setDesc("Text to display in the status bar when there are code blocks (use %d to show the number of blocks)")
.addText(text => text
.setPlaceholder('Count blocks: %d')
.setValue(this.plugin.settings.countBlocksText)
.onChange(async (value) => {
if (!value.includes("%d")) value = "🗲 %d";
this.plugin.settings.countBlocksText = value;
await this.plugin.saveSettings();
}));

const codeblock = containerEl.createEl('details');
codeblock.createEl('summary', { text: 'Click to Add Key/Value ↴', cls: 'summary-text' });
Expand Down

0 comments on commit a6a7c53

Please sign in to comment.