Skip to content

Commit

Permalink
Post pr merge fixes (#205)
Browse files Browse the repository at this point in the history
* fixed errors after merging all the prs

* moved to new templater processor

* plugin working post pr merge

* fix: apped to append

* move templater button processing to the new templater method

* update README

---------

Co-authored-by: Sam Morrison <shbgm@littletop.local>
  • Loading branch information
shabegom and Sam Morrison authored Feb 1, 2024
1 parent 0efb381 commit 80d12ee
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 2,740 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,31 @@ Run commands and open links by clicking on ✨ Buttons ✨

---

**last updated:** Sept 28, 2022
**last updated:** February 1st, 2024

Holy shit, look at all these awesome people that helped improve Buttons!

- Enhancement: Moved to more reliable templater processor ([shabegom])
- Bugfix: buttons now render in Live Preview mode when Obsidian starts ([Lx])
- Bugfix: improve reliability of `templater` option ([Lx])
- Enhancement: improve speed of `remove` option ([Lx])
- Feature: Add default folder and prompt for name settings ([unxok])
- Features: Button type copy for "copy text to clipboard", and custom color for button background and tex ([rafa-carmo])
- Feature: adds hidden attribute to buttons ([Liimurr])
- Enhancement: Create folder for new note if it doesn't exist ([SabriDW])
- Bugfix: fix template search with folders having "/" prefixed ([Balake])
- Feature: Open new tab when creating new file ([0snug0])
- Update Readme: new note from template ([antulik])


[Lx]: https://github.com/Lx
[unxok]: https://github.com/unxok
[rafa-carmo]: https://github.com/rafa-carmo
[Liimurr]: https://github.com/Liimurr
[SabriDW]: https://github.com/SabriDW
[Balake]: https://github.com/Balake
[0snug0]: https://github.com/0snug0
[antulik]: https://github.com/antulik

---

Expand Down Expand Up @@ -307,7 +331,7 @@ Note: swap count is reset if you close the note.

## Releases

### Next version (not released yet)
### 0.5.0


- Bugfix: buttons now render in Live Preview mode when Obsidian starts ([Lx])
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.4.19",
"version": "0.5.0",
"author": "shabegom",
"authorUrl": "https://shbgm.ca",
"isDesktopOnly": false,
Expand Down
56 changes: 24 additions & 32 deletions src/button.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { App, Notice, MarkdownView } from "obsidian";
import { App, MarkdownView, Notice } from "obsidian";
import { Arguments } from "./types";
import {
calculate,
remove,
replace,
template,
link,
command,
copy,
copyText,
link,
remove,
replace,
swap,
template,
templater,
text,
copyText
} from "./buttonTypes";
import { getButtonPosition, getInlineButtonPosition } from "./parser";

Expand Down Expand Up @@ -39,16 +39,16 @@ export const createButton = ({
const button = el.createEl("button", {
cls: [
args.class
? `${args.class} ${args.color}`
: `button-default ${args.color ? args.color : ""}`,
inline ? "button-inline" : ""
]
? `${args.class} ${args.color}`
: `button-default ${args.color ? args.color : ""}`,
inline ? "button-inline" : "",
],
});
if(args.customcolor) {

if (args.customcolor) {
button.style.backgroundColor = args.customcolor;
}
if(args.customtextcolor) {
if (args.customtextcolor) {
button.style.color = args.customtextcolor;
}
button.innerHTML = args.name;
Expand All @@ -65,28 +65,20 @@ const clickHandler = async (
app: App,
args: Arguments,
inline: boolean,
id: string
id: string,
) => {
const activeView = app.workspace.getActiveViewOfType(MarkdownView);
if (args.type === "command") {
command(app, args);
}
// handle link buttons
if (args.type === "link") {
link(args);
}
let content = await app.vault.read(activeView.file);
const buttonStart = getButtonPosition(content, args);
let position = inline
? await getInlineButtonPosition(app, id)
: getButtonPosition(content, args);
const buttonStart = getButtonPosition(content,args);
// handle command buttons
if (args.templater) {
args = await templater(app, position);
if (inline) {
new Notice("templater args don't work with inline buttons yet", 2000);
}
}
// if (args.templater) {
// args = await templater(app, position);
// if (inline) {
// new Notice("templater args don't work with inline buttons yet", 2000);
// }
// }
if (args.replace) {
replace(app, args);
}
Expand All @@ -102,8 +94,8 @@ const clickHandler = async (
link(args);
}
// handle copy text buttons
if(args.type === 'copy') {
copyText(args)
if (args.type === "copy") {
copyText(args);
}
// handle template buttons
if (args.type && args.type.includes("template")) {
Expand All @@ -127,7 +119,7 @@ const clickHandler = async (
if (!inline) {
new Notice("swap args only work in inline buttons for now", 2000);
} else {
await swap(app, args.swap, id, inline, activeView.file);
await swap(app, args.swap, id, inline, activeView.file, buttonStart);
}
}
// handle removing the button
Expand Down
24 changes: 9 additions & 15 deletions src/buttonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import {
getInlineButtonPosition,
findNumber,
} from "./parser";
import { handleValueArray, getNewArgs, runTemplater } from "./utils";
import { handleValueArray, getNewArgs } from "./utils";
import {
getButtonSwapById,
setButtonSwapById,
getButtonById,
} from "./buttonStore";
import { processTemplate } from "./templater"

export const calculate = async (
app: App,
Expand Down Expand Up @@ -102,10 +103,6 @@ export const template = async (
const folders: string[] = [
templatesEnabled &&
app.internalPlugins.plugins.templates.instance.options.folder?.toLowerCase(),
templaterPluginEnabled &&
app.plugins?.plugins[
"templater-obsidian"
]?.settings.template_folder?.toLowerCase(),
templaterPluginEnabled &&
app.plugins?.plugins[
"templater-obsidian"
Expand All @@ -124,23 +121,20 @@ export const template = async (
return found;
})[0];
if (file) {
const content = await app.vault.read(file);
const content = await processTemplate(file)
// prepend template above the button
if (args.type.includes("prepend")) {
await prependContent(app, content, position.lineStart);
await runTemplater(app);
}
// append template below the button
if (args.type.includes("append")) {
await appendContent(app, content, position.lineEnd);
await runTemplater(app);
}
if (args.type.includes("note")) {
createNote(app, content, args.type, file, args.templater args.folder, args.prompt);
createNote(app, content, args.type, args.folder, args.prompt, file, args.templater);
}
if (args.type.includes("line")) {
await addContentAtLine(app, content, args.type);
await runTemplater(app);
}
} else {
new Notice(
Expand All @@ -166,7 +160,7 @@ export const copy = ({ action }: Arguments): void => {
navigator.clipboard.writeText(action);
}

export const command = (app: App, { action }: Arguments): void => {
export const command = (app: App, args: Arguments, buttonStart): void => {

const allCommands = app.commands.listCommands();
const action = args.action;
Expand All @@ -191,7 +185,8 @@ export const swap = async (
swap: string,
id: string,
inline: boolean,
file: TFile
file: TFile,
buttonStart
): Promise<void> => {
handleValueArray(swap, async (argArray) => {
const swap = await getButtonSwapById(app, id);
Expand All @@ -211,7 +206,7 @@ export const swap = async (
await replace(app, args);
}
if (args.type === "command") {
command(app, args);
command(app, args, buttonStart);
}
// handle link buttons
if (args.type === "link") {
Expand Down Expand Up @@ -258,8 +253,7 @@ export const templater = async (
if (activeView) {
await activeView.save();
const file = activeView.file;
const content = await app.vault.cachedRead(file);
await runTemplater(app);
const content = await processTemplate(file);
const { args } = await getNewArgs(app, position);
const cachedData: string[] = [];
const cacheChange = app.vault.on("modify", (file) => {
Expand Down
Loading

0 comments on commit 80d12ee

Please sign in to comment.