Skip to content

Commit

Permalink
Minor technical improvements; adapted plugin for pop-out windows
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanGiesberts committed Oct 24, 2023
1 parent c6226e1 commit 9a5857a
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 1,597 deletions.
43 changes: 24 additions & 19 deletions main.ts
@@ -1,48 +1,53 @@
import { MarkdownView, Plugin } from 'obsidian';
import { Plugin, TFile } from 'obsidian';

export default class TableCheckboxesPlugin extends Plugin {
async onload() {

this.registerDomEvent(document, 'keyup', (evt: KeyboardEvent): void => {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (evt.key == "]" && view) {
this.registerDomEvent(activeWindow, "keyup", (evt: KeyboardEvent): void => {
if (evt.key == "]") {
const view = this.app.workspace.activeEditor;
if (!view || !view.editor) {
return;
}
const location = view.editor.getCursor("anchor");
const rowValue = view.editor.getLine(location.line);
if (this.isMDCheckboxInTable(rowValue)) {
const checkBox = this.getCheckboxLength(rowValue);
const start = {...location}; // Shallow copy
start.ch -= checkBox.length; // Subtract the length from the location of ']'
view.editor.setSelection(start, location); // Select '-[]'
const checkboxId = this.generateUniqueCheckboxId(view);
const checkboxId = this.generateUniqueCheckboxId(view.editor.getDoc().getValue());
view.editor.replaceSelection(`<input type="checkbox" unchecked id="${checkboxId}">`); // Replace selection with unchecked HTML checkbox
}
}
});

this.registerDomEvent(document, "change", (evt: InputEvent): void => {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
this.registerDomEvent(activeWindow, "change", (evt: InputEvent): void => {
// Check for data-task attribute to ignore markdown checkboxes
if (evt.target instanceof HTMLInputElement && view && evt.target.id && evt.target.hasAttribute("data-task") === false) {
const checkbox = evt.target;
if (checkbox.getAttribute("type") === "checkbox") {
const page = view.getViewData();
const id = evt.target.id;
this.toggleCheckbox(page, view, checkbox.checked, id);
const changeEl = evt.target as Element;
if (changeEl.instanceOf(HTMLInputElement) && changeEl.id && changeEl.hasAttribute("data-task") === false) {
const view = this.app.workspace.activeEditor;
if (!view || !view.editor || !view.file) {
return;
}
if (changeEl.getAttribute("type") === "checkbox") {
const page = view.editor.getDoc().getValue();
const id = changeEl.id;
this.toggleCheckbox(page, view.file, changeEl.checked, id);
}
}
})
}

private generateUniqueCheckboxId(view: MarkdownView): string {
private generateUniqueCheckboxId(page: string): string {
let id = crypto.randomUUID().slice(-6);
while (this.idExistsInFile(id, view)) {
while (this.idExistsInFile(id, page)) {
id = crypto.randomUUID();
}
return id;
}

private idExistsInFile(id: string, view: MarkdownView): boolean {
const page = view.getViewData();
private idExistsInFile(id: string, page: string): boolean {
const idIndex = page.search(id);
return idIndex !== -1;
}
Expand All @@ -63,8 +68,8 @@ export default class TableCheckboxesPlugin extends Plugin {
return checkboxMatch![0];
}

private toggleCheckbox(page: string, view: MarkdownView, isChecked: boolean, checkboxId: string): void {
private toggleCheckbox(page: string, file: TFile, isChecked: boolean, checkboxId: string): void {
page = page.replace(new RegExp(`<input type="checkbox" (un)?checked id="${checkboxId}">`), `<input type="checkbox" ${isChecked ? "" : "un"}checked id="${checkboxId}">`);
this.app.vault.modify(view.file, page);
this.app.vault.modify(file, page);
}
}
2 changes: 1 addition & 1 deletion manifest.json
@@ -1,7 +1,7 @@
{
"id": "table-checkboxes",
"name": "Markdown table checkboxes",
"version": "2.0.2",
"version": "2.0.3",
"minAppVersion": "0.15.0",
"description": "Converts markdown checkboxes in tables to HTML, and reflects the state upon (un)checking them.",
"author": "Dylan Giesberts",
Expand Down

0 comments on commit 9a5857a

Please sign in to comment.