generated from obsidian-community/obsidian-preact-template
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* basic inline buttons * inline button prepend and replace * new calculate * swap arg * templater button * started on button creator modal * button maker 5000 * complete button maker * update readme * img
- Loading branch information
Showing
16 changed files
with
1,614 additions
and
298 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { App, Notice, MarkdownView } from "obsidian"; | ||
import { Arguments } from "./types"; | ||
import { | ||
calculate, | ||
remove, | ||
replace, | ||
template, | ||
link, | ||
command, | ||
swap, | ||
templater, | ||
} from "./buttonTypes"; | ||
import { getButtonPosition, getInlineButtonPosition } from "./parser"; | ||
|
||
export const createButton = ( | ||
app: App, | ||
el: HTMLElement, | ||
args: Arguments, | ||
inline: boolean, | ||
id?: string | ||
): HTMLElement => { | ||
//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(app, args, inline, id); | ||
}); | ||
return button; | ||
}; | ||
|
||
const clickHandler = async ( | ||
app: App, | ||
args: Arguments, | ||
inline: boolean, | ||
id: string | ||
) => { | ||
const activeView = app.workspace.getActiveViewOfType(MarkdownView); | ||
let content = await app.vault.read(activeView.file); | ||
let position = inline | ||
? await getInlineButtonPosition(app, id) | ||
: 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.replace) { | ||
replace(app, args); | ||
} | ||
if (args.type === "command") { | ||
command(app, args); | ||
} | ||
// handle link buttons | ||
if (args.type === "link") { | ||
link(args); | ||
} | ||
// handle template buttons | ||
if (args.type && args.type.includes("template")) { | ||
setTimeout(async () => { | ||
content = await app.vault.read(activeView.file); | ||
position = inline | ||
? await getInlineButtonPosition(app, id) | ||
: getButtonPosition(content, args); | ||
template(app, args, position); | ||
}, 50); | ||
} | ||
if (args.type === "calculate") { | ||
calculate(app, args, position); | ||
} | ||
// handle removing the button | ||
if (args.remove) { | ||
setTimeout(async () => { | ||
content = await app.vault.read(activeView.file); | ||
position = inline | ||
? await getInlineButtonPosition(app, id) | ||
: getButtonPosition(content, args); | ||
remove(app, args, position); | ||
}, 75); | ||
} | ||
if (args.swap) { | ||
if (!inline) { | ||
new Notice("swap args only work in inline buttons for now", 2000); | ||
} else { | ||
swap(app, args.swap, id, inline, activeView.file); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.