Skip to content

Commit

Permalink
fix bug 18 and update mainfest (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
shabegom authored May 2, 2021
1 parent bc4632a commit 8cf8259
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 48 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Run commands and open links by clicking on ✨ Buttons ✨

---

**last updated:** May 1, 2021
**last updated:** May 2, 2021
- You can add a block-id below a button block. The button block-id can be used to inherit arguments from a button or to remove multiple buttons
- `remove` can now be an array of button block-ids to remove (it can still be true to remove the clicked button)
- `replace` now takes an array like [startLine,endLine] to define the start and end line to be replaced.
Expand Down Expand Up @@ -253,6 +253,10 @@ replace [7,25]

## Releases

### 0.3.2
- Fix the uncaught Type Error if there are no button block-ids in the vault
- Fix the position error if not on insiders build

### 0.3.0
- You can add a block-id below a button block. The button block-id can be used to inherit arguments from a button or to remove multiple buttons
- `remove` can now be an array of button block-ids to remove (it can still be true to remove the clicked button)
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "buttons",
"name": "Buttons",
"description": "Create Buttons in your Obsidian notes to run commands, open links, and insert templates",
"version": "0.3.1",
"version": "0.3.2",
"author": "shabegom",
"authorUrl": "https://shbgm.ca",
"isDesktopOnly": false,
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
"@babel/runtime": "7.13.10",
"math-expression-evaluator": "^1.3.7",
"obsidian": "^0.12.0",
"tslib": "2.1.0"
"remark-parse": "^9.0.0",
"tslib": "2.1.0",
"unified": "^9.2.1",
"unist-util-visit": "^3.0.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "17.1.0",
"@rollup/plugin-json": "4.1.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "11.2.0",
"@rollup/plugin-replace": "2.4.1",
"@rollup/plugin-typescript": "8.2.0",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typescript from "@rollup/plugin-typescript";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";

const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
Expand All @@ -18,5 +19,5 @@ export default {
banner,
},
external: ["obsidian"],
plugins: [typescript(), nodeResolve({ browser: true }), commonjs()],
plugins: [typescript(), nodeResolve({ browser: true }), commonjs(), json()],
};
89 changes: 48 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin, EventRef } from "obsidian";
import { Plugin, EventRef, MarkdownView } from "obsidian";
import { createArgumentObject, insertButton } from "./utils";
import {
initializeButtonStore,
Expand All @@ -14,6 +14,7 @@ import {
link,
command,
} from "./buttonTypes";
import { getButtonPosition } from "./parser";

// extend the obsidian module with some additional typings

Expand All @@ -37,46 +38,52 @@ export default class ButtonsPlugin extends Plugin {
"button",
async (source, el, ctx) => {
// create an object out of the arguments
const position = ctx.getSectionInfo(el);
let args = createArgumentObject(source);
const storeArgs = await getButtonFromStore(this.app, args);
args = storeArgs ? storeArgs : args;
// handle button clicks
const clickHandler = async () => {
// handle command buttons
if (args.replace) {
replace(this.app, args);
}
if (args.type === "command") {
command(this.app, args);
}
// handle link buttons
if (args.type === "link") {
link(args);
}
// handle template buttons
if (args.type && args.type.includes("template")) {
template(this.app, args, position);
}
if (args.type === "calculate") {
calculate(this.app, args, position);
}
// handle removing the button
if (args.remove) {
remove(this.app, args, position);
}
};
//create the button element
const button = el.createEl("button", {
text: args.name,
cls: args.class
? `${args.class} ${args.color}`
: `button-default ${args.color ? args.color : ""}`,
});
args.id ? button.setAttribute("id", args.id) : "";
button.on("click", "button", () => {
clickHandler();
});
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
const content = await this.app.vault.cachedRead(activeView.file);
let args = createArgumentObject(source);
const position = ctx.getSectionInfo
? ctx.getSectionInfo(el)
: getButtonPosition(content, args);
const storeArgs = await getButtonFromStore(this.app, args);
args = storeArgs ? storeArgs : args;
// handle button clicks
const clickHandler = async () => {
// handle command buttons
if (args.replace) {
replace(this.app, args);
}
if (args.type === "command") {
command(this.app, args);
}
// handle link buttons
if (args.type === "link") {
link(args);
}
// handle template buttons
if (args.type && args.type.includes("template")) {
template(this.app, args, position);
}
if (args.type === "calculate") {
calculate(this.app, args, position);
}
// handle removing the button
if (args.remove) {
remove(this.app, args, position);
}
};
//create the button element
const button = el.createEl("button", {
text: args.name,
cls: args.class
? `${args.class} ${args.color}`
: `button-default ${args.color ? args.color : ""}`,
});
args.id ? button.setAttribute("id", args.id) : "";
button.on("click", "button", () => {
clickHandler();
});
}
}
);
}
Expand Down
34 changes: 34 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import parse from "remark-parse";
import unified from "unified";
import { visit } from "unist-util-visit";
import { Node } from "unist";
import { Arguments } from "./types";

const parser = unified().use(parse);

interface ButtonNode extends Node {
lang: string;
value: string;
}

interface Position {
lineStart: number;
lineEnd: number;
}

export const getButtonPosition = (
content: string,
args: Arguments
): Position => {
const tree = parser.parse(content);
const position = { lineStart: 0, lineEnd: 0 };
visit(tree, "code", (node: ButtonNode) => {
if (node.lang === "button") {
if (args && node.value.includes(args.name)) {
position.lineStart = node.position.start.line - 1;
position.lineEnd = node.position.end.line - 1;
}
}
});
return position;
};
Loading

0 comments on commit 8cf8259

Please sign in to comment.