Skip to content

Commit

Permalink
Potential fix for conversion not working
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanGiesberts committed Apr 6, 2024
1 parent 7d20be1 commit 5d22187
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
30 changes: 23 additions & 7 deletions main.ts
@@ -1,4 +1,4 @@
import { Plugin, TFile, WorkspaceWindow } from 'obsidian';
import { EditorPosition, MarkdownFileInfo, Plugin, TFile, WorkspaceWindow } from 'obsidian';

export default class TableCheckboxesPlugin extends Plugin {
async onload() {
Expand All @@ -21,12 +21,15 @@ export default class TableCheckboxesPlugin extends Plugin {
location.ch += 1; // Increase char by one because Obsidian autocompletes checkboxes now
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.editor.getDoc().getValue());
view.editor.replaceSelection(`<input type="checkbox" unchecked id="${checkboxId}">`); // Replace selection with unchecked HTML checkbox
return this.handleCheckboxReplacement(view, rowValue, location, false);
} // else we add the ] manually and check again, just in case for other locales

location.ch -= 1; // Reduce by 1 because we previously added it.
const rowChars = rowValue.split(""); // In this case rowValue isn't up to date with the input event, we need to add ] manually.
rowChars.splice(location.ch, 0, evt.data); // Luckily we know exactly where ] needs to go
const newRowValue = rowChars.join("");
if (this.isMDCheckboxInTable(newRowValue)) {
this.handleCheckboxReplacement(view, newRowValue, location, true);
}
}
});
Expand All @@ -48,6 +51,19 @@ export default class TableCheckboxesPlugin extends Plugin {
});
}

private handleCheckboxReplacement (view: MarkdownFileInfo, rowValue: string, location: EditorPosition, manuallyAdded: boolean) {
if (!view.editor) { return; }
const checkBox = this.getCheckboxLength(rowValue);
const start = {...location}; // Shallow copy
start.ch -= checkBox.length; // Subtract the length from the location of ']'
if (manuallyAdded) {
start.ch += 1;
}
view.editor.setSelection(start, location); // Select '-[]'
const checkboxId = this.generateUniqueCheckboxId(view.editor.getDoc().getValue());
view.editor.replaceSelection(`<input type="checkbox" unchecked id="${checkboxId}">`); // Replace selection with unchecked HTML checkbox
}

private generateUniqueCheckboxId(page: string): string {
let id = crypto.randomUUID().slice(-6);
while (this.idExistsInFile(id, page)) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
@@ -1,7 +1,7 @@
{
"id": "table-checkboxes",
"name": "Markdown table checkboxes",
"version": "2.0.7",
"version": "2.0.8",
"minAppVersion": "1.5.0",
"description": "Converts markdown checkboxes in tables to HTML, and reflects the state upon (un)checking them.",
"author": "Dylan Giesberts",
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "obsidian-table-checkboxes-plugin",
"version": "2.0.7",
"version": "2.0.8",
"description": "Converts markdown checkboxes inside tables to HTML, and reflects the state upon (un)checking them.",
"main": "main.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Expand Up @@ -11,5 +11,6 @@
"2.0.4": "0.15.0",
"2.0.5": "0.15.0",
"2.0.6": "0.15.0",
"2.0.7": "1.5.0"
"2.0.7": "1.5.0",
"2.0.8": "1.5.0"
}

0 comments on commit 5d22187

Please sign in to comment.