changed modify to process

This commit is contained in:
anareaty 2025-02-21 19:23:26 +04:00
parent ccbbf3c9e7
commit 23a2526fc4
2 changed files with 87 additions and 78 deletions

37
main.ts
View file

@ -54,38 +54,41 @@ export default class HTMLCheckboxPlugin extends Plugin {
}
if (file instanceof TFile) {
let content = await this.app.vault.cachedRead(file)
await this.app.vault.process(file, (content) => {
let id = target.id
let checkboxRE = new RegExp("<input[^>]*?id=\"" + id + "\"[^>]*?>")
if (file.extension == "canvas") {
if (file && file.extension == "canvas") {
checkboxRE = new RegExp("<input[^>]*?id=\\\\\"" + id + "\\\\\"[^>]*?>")
}
let checkboxMatch = content.match(checkboxRE)
let newContent = content
if (checkboxMatch) {
let checkboxString = checkboxMatch[0]
let dataTaskMatch = checkboxString.match(/data-task="."/)
let newCheckboxString = checkboxString
if ((target as HTMLInputElement).checked) {
if (!(target as HTMLInputElement).checked) {
newCheckboxString = newCheckboxString.replace(">", " checked>")
} else {
newCheckboxString = newCheckboxString.replace(" checked>", ">").replace(" >", ">")
if (dataTaskMatch) {
newCheckboxString = newCheckboxString.replace(dataTaskMatch[0], "")
}
}
let newContent = content.replace(checkboxString, newCheckboxString)
await this.app.vault.modify(file, newContent)
newContent = content.replace(checkboxString, newCheckboxString)
}
return newContent
})
let inCanvas = target.closest(".canvas-node-content")
if (file.extension != "canvas" && inCanvas) {
let leaf = this.app.workspace.getMostRecentLeaf()
if (leaf instanceof WorkspaceLeaf) {
@ -93,7 +96,10 @@ export default class HTMLCheckboxPlugin extends Plugin {
await leaf.rebuildView()
}
}
}
}
}
});
@ -224,11 +230,12 @@ export default class HTMLCheckboxPlugin extends Plugin {
}
if (file instanceof TFile) {
let content = await this.app.vault.cachedRead(file)
await this.app.vault.process(file, (content) => {
let id = target.id
let checkboxRE = new RegExp("<input[^>]*?id=\"" + id + "\"[^>]*?>")
if (file.extension == "canvas") {
if (file && file.extension == "canvas") {
checkboxRE = new RegExp("<input[^>]*?id=\\\\\"" + id + "\\\\\"[^>]*?>")
}
@ -237,7 +244,7 @@ export default class HTMLCheckboxPlugin extends Plugin {
let statusRe = new RegExp("data-task=\"" + status + "\"")
let dataTaskMatch = checkboxString.match(/data-task="."/)
if (file.extension == "canvas") {
if (file && file.extension == "canvas") {
dataTaskMatch = checkboxString.match(/data-task=\\".\\"/)
}
@ -249,7 +256,7 @@ export default class HTMLCheckboxPlugin extends Plugin {
let statusMatch = checkboxString.match(statusRe)
let dataTaskString = "data-task=\"" + status + "\""
if (file.extension == "canvas") {
if (file && file.extension == "canvas") {
dataTaskString = 'data-task=\\\"' + status + '\\\"'
}
@ -273,7 +280,9 @@ export default class HTMLCheckboxPlugin extends Plugin {
}
let newContent = content.replace(checkboxString, newCheckboxString)
await this.app.vault.modify(file, newContent)
return newContent
})
}
}
}

View file

@ -1,8 +1,8 @@
{
"id": "html-checkboxes",
"name": "HTML checkboxes",
"version": "1.0.4",
"minAppVersion": "0.15.0",
"version": "1.0.5",
"minAppVersion": "1.1.0",
"description": "Allows to quickly add HTML checkboxes to your notes and makes them clickable.",
"author": "Anareaty",
"authorUrl": "https://github.com/anareaty",