Skip to content

Commit

Permalink
changes according to Code Review + Added logic for buttons to act acc…
Browse files Browse the repository at this point in the history
…ording to the Min present or not

- Made changes for the previous PR regarding adding buttons to manual kind
- Modified the Button handler methods to respect the min value if present
  • Loading branch information
sukumar1210 authored and zwpaper committed Feb 29, 2024
1 parent a671190 commit a5a5704
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ min: 2024-02-01
# others: number
max: 2024-04-30
# == buttons ==
# == button ==
# Specify whether you wish to show the buttons to +1 or -1 the current value
#
# Requires an id
# Can only be used with kind manual/others or no kind
#
# Possible format: boolean (true or false)
buttons: true
button: true
# == id ==
# Specify the id for a progressbar.
Expand Down Expand Up @@ -146,10 +146,10 @@ will still stay as `I am {unknown}`.

If no name specified, name will be `kind({percentage})` by default.

### Using `id` and `buttons` together
The `buttons` option is supposed to be used with the `id` options where the `id` option can be used anywhere but will have no practical use without the buttons, It will be the same as not having and id.
### Using `id` and `button` together
The `button` option is supposed to be used with the `id` options where the `id` option can be used anywhere but will have no practical use without the buttons, It will be the same as not having and id.

`id` can be used to have multiple progressbar throughout the same document to represent the same quantity. Two progressbar having the same id will be updated together whenever one is updated **using the `buttons`**.
`id` can be used to have multiple progressbar throughout the same document to represent the same quantity. Two progressbar having the same id will be updated together whenever one is updated **using the `button`**.

The progressbars will not be synced automatically without the button's click. The auto syncing between progressbars with same id can be implemented in future releases.

Expand All @@ -162,7 +162,7 @@ These will have the same id, synced together on button clicks, but notice that w
id: test <-- an id assigned as "test"
kind: manual
name: "manual with buttons 1 {max}" <-- Notice the name
buttons: true
button: true
value: 5
max: 10
```
Expand All @@ -175,7 +175,7 @@ These will have the same id, synced together on button clicks, but notice that w
# Notice that progressbar with same id can still be used with different names
name: manual with buttons 2 <-- Notice the name here

buttons: true
button: true
value: 5
max: 10
```
Expand Down
36 changes: 15 additions & 21 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ interface ProgressBarSettings {
const DEFAULT_SETTINGS: ProgressBarSettings = {
setting: 'default'
}

export default class ProgressBar extends Plugin {
// settings: ProgressBarSettings;

async onload() {
// await this.loadSettings();
// This adds a settings tab so the user can configure various aspects of the plugin
// this.addSettingTab(new ProgressBarSettingTab(this.app, this));

this.registerMarkdownCodeBlockProcessor("progressbar", (source, el, ctx) => {
let cfg;
try {
cfg = parseYaml(source);
} catch (e) {
console.log(e);
newError(el, "Cannot parse the YAML Format");
return;
}
Expand All @@ -37,13 +34,13 @@ export default class ProgressBar extends Plugin {
return;
}

if ((cfg.kind && !(cfg.kind==="manual" || cfg.kind=== "other")) && cfg.buttons){
newError(el, "Can only use buttons with kind: manual/other");
if ((cfg.kind && !(cfg.kind==="manual" || cfg.kind=== "other")) && cfg.button){
newError(el, "Can only use button with kind: manual/other");
return;
}

if (cfg.buttons && !cfg.id){
newError(el, "Can not use buttons without id");
if (cfg.button && !cfg.id){
newError(el, "Can not use button without id");
return;
}

Expand Down Expand Up @@ -151,10 +148,10 @@ function newProgressBar(el: HTMLElement, bar: any, val: any) {
max: bar.max,
value: value,
percentage: Math.round(val.value / val.max * 100) + "%",
});1
});
const label = el.createEl("label", { text: message + ": " });

if (bar.buttons) {
if (bar.button) {
const minus=el.createEl("button", { text: "-" });
minus.style.fontSize='larger'
minus.addEventListener("click", () => {
Expand All @@ -167,11 +164,10 @@ function newProgressBar(el: HTMLElement, bar: any, val: any) {
if (bar.width) {
progressbar.style.width = bar.width;
}
if (bar.buttons) {
if (bar.button) {
const plus=el.createEl("button", { text: "+" });
plus.style.fontSize='larger'
plus.addEventListener("click", () => {
console.log("hi");
increment(bar);
})
}
Expand All @@ -182,38 +178,36 @@ function newProgressBar(el: HTMLElement, bar: any, val: any) {
}

function increment(blockTextYAML: any){
if (blockTextYAML.value==blockTextYAML.max) {return;}
if (blockTextYAML.value>=blockTextYAML.max) {return;}
const file = this.app.workspace.getActiveFile();
if (!file) {}
else {
if (file) {
let doneOnce=false;
this.app.vault.process(file, (data: string) => {
const pattern=new RegExp(`\`{3}progressbar[a-zA-Z0-9\\s:{}#"]*id:[\\s]${blockTextYAML.id}[a-zA-Z0-9\\s:{}#"]*\`{3}`, "g")
const pattern=new RegExp(`\`{3}progressbar[a-zA-Z0-9\\s:{}#\\-"]*id:[\\s]${blockTextYAML.id}[a-zA-Z0-9\\s:{}#\\-"]*\`{3}`, "g")
return data.replace(pattern, (source: String)=>{
if (!doneOnce) {
blockTextYAML.value=blockTextYAML.value+1;
doneOnce=true;
}
return source.replace(/value: [0-9]*/g, `value: ${blockTextYAML.value}`)
return source.replace(/value: [0-9\-]*/g, `value: ${blockTextYAML.value}`)
})
})
}
}

function decrement(blockTextYAML: any){
if (blockTextYAML.value==0) {return;}
if (blockTextYAML.value<=(blockTextYAML.min?blockTextYAML.min:0)) {return;}
const file = this.app.workspace.getActiveFile();
if (!file) {}
else {
if (file) {
let doneOnce=false;
this.app.vault.process(file, (data: string) => {
const pattern=new RegExp(`\`{3}progressbar[a-zA-Z0-9\\s:{}#"]*id:[\\s]${blockTextYAML.id}[a-zA-Z0-9\\s:{}#"]*\`{3}`, "g")
const pattern=new RegExp(`\`{3}progressbar[a-zA-Z0-9\\s:{}#\\-"]*id:[\\s]${blockTextYAML.id}[a-zA-Z0-9\\s:{}#\\-"]*\`{3}`, "g")
return data.replace(pattern, (source: String)=>{
if (!doneOnce) {
blockTextYAML.value=blockTextYAML.value-1;
doneOnce=true;
}
return source.replace(/value: [0-9]*/g, `value: ${blockTextYAML.value}`)
return source.replace(/value: [0-9\-]*/g, `value: ${blockTextYAML.value}`)
})
})
}
Expand Down

0 comments on commit a5a5704

Please sign in to comment.