required fixes

This commit is contained in:
anareaty 2025-02-02 13:18:54 +04:00
parent adfbe61f94
commit 58b7b0659d
2 changed files with 89 additions and 103 deletions

44
main.ts
View file

@ -1,8 +1,8 @@
import { Editor, Plugin, TFile, Menu } from 'obsidian';
import { Editor, Plugin, TFile, Menu, WorkspaceLeaf } from 'obsidian';
export default class MyPlugin extends Plugin {
export default class HTMLCheckboxPlugin extends Plugin {
async onload() {
@ -20,8 +20,6 @@ export default class MyPlugin extends Plugin {
});
}));
this.addCommand({
id: "add-html-checkbox",
name: "Add HTML checkbox",
@ -39,24 +37,23 @@ export default class MyPlugin extends Plugin {
this.registerDomEvent(window, 'click', async (e: MouseEvent) => {
let target = e.target as HTMLElement
if (target.localName == "input" &&
(target as HTMLInputElement).type == "checkbox" &&
target.id
) {
e.preventDefault()
let file = this.app.workspace.getActiveFile() as TFile
let file = this.app.workspace.getActiveFile()
/* Check if checkbox is inside embedded file */
let embedWrapper = target.closest(".internal-embed")
if (embedWrapper) {
let embedSrc = embedWrapper.getAttribute("src") || ""
file = this.app.metadataCache.getFirstLinkpathDest(embedSrc, "") as TFile
file = this.app.metadataCache.getFirstLinkpathDest(embedSrc, "")
}
if (file instanceof TFile) {
let content = await this.app.vault.cachedRead(file)
let id = target.id
@ -90,11 +87,13 @@ export default class MyPlugin extends Plugin {
let inCanvas = target.closest(".canvas-node-content")
if (file.extension != "canvas" && inCanvas) {
let leaf = this.app.workspace.getMostRecentLeaf()
if (leaf instanceof WorkspaceLeaf) {
//@ts-ignore
await this.app.workspace.getMostRecentLeaf().rebuildView()
await leaf.rebuildView()
}
}
}
}
}
});
@ -160,9 +159,9 @@ export default class MyPlugin extends Plugin {
);
menu.showAtMouseEvent(e as MouseEvent);
console.log(menu)
//@ts-ignore
menu.dom.classList.add("checkbox-menu");
}
}
})
@ -205,18 +204,18 @@ export default class MyPlugin extends Plugin {
async setStatus(status: string, target: HTMLElement) {
let file = this.app.workspace.getActiveFile() as TFile
let file = this.app.workspace.getActiveFile()
/* Check if checkbox is inside embedded file */
let embedWrapper = target.closest(".internal-embed")
if (embedWrapper) {
let embedSrc = embedWrapper.getAttribute("src") || ""
file = this.app.metadataCache.getFirstLinkpathDest(embedSrc, "") as TFile
file = this.app.metadataCache.getFirstLinkpathDest(embedSrc, "")
}
if (file instanceof TFile) {
let content = await this.app.vault.cachedRead(file)
let id = target.id
let checkboxRE = new RegExp("<input[^>]*?id=\"" + id + "\"[^>]*?>")
@ -224,29 +223,21 @@ export default class MyPlugin extends Plugin {
checkboxRE = new RegExp("<input[^>]*?id=\\\\\"" + id + "\\\\\"[^>]*?>")
}
let checkboxString = content.match(checkboxRE)![0]
let newCheckboxString = checkboxString
let statusRe = new RegExp("data-task=\"" + status + "\"")
let dataTaskMatch = checkboxString.match(/data-task="."/)
if (file.extension == "canvas") {
dataTaskMatch = checkboxString.match(/data-task=\\".\\"/)
}
let dataTaskMatchFirst = ""
if (dataTaskMatch) {
dataTaskMatchFirst = dataTaskMatch[0]
}
let statusMatch = checkboxString.match(statusRe)
let dataTaskString = "data-task=\"" + status + "\""
if (file.extension == "canvas") {
@ -275,12 +266,7 @@ export default class MyPlugin extends Plugin {
let newContent = content.replace(checkboxString, newCheckboxString)
await this.app.vault.modify(file, newContent)
}
}
}

View file

@ -1,7 +1,7 @@
{
"id": "html-checkboxes",
"name": "HTML checkboxes",
"version": "1.0.0",
"version": "1.0.1",
"minAppVersion": "0.15.0",
"description": "Allows to quickly add HTML checkboxes to your notes and makes them clickable.",
"author": "Anareaty",