diff --git a/manifest.json b/manifest.json index 8fa5e59..08abfb9 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-checklist-plugin", "name": "Checklist", - "version": "1.0.11", + "version": "1.0.12", "minAppVersion": "0.9.12", "description": "Combines checklists accross pages into users sidebar", "author": "delashum", diff --git a/package-lock.json b/package-lock.json index 54137d7..3c3e080 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "obsidian-checklist-plugin", - "version": "1.0.2", + "version": "1.0.11", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -84,9 +84,9 @@ "dev": true }, "@types/node": { - "version": "14.14.20", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.20.tgz", - "integrity": "sha512-Y93R97Ouif9JEOWPIUyU+eyIdyRqQR0I8Ez1dzku4hDx34NWh4HbtIc3WNzwB1Y9ULvNGeu5B8h8bVL5cAk4/A==", + "version": "14.14.31", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.31.tgz", + "integrity": "sha512-vFHy/ezP5qI0rFgJ7aQnjDXwAMrG0KqqIH7tQG5PPv3BWBayOPIQNBjVc/P6hhdZfMx51REc6tfDNXHUio893g==", "dev": true }, "@types/pug": { @@ -181,9 +181,9 @@ "dev": true }, "fsevents": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz", - "integrity": "sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==", + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", "dev": true, "optional": true }, @@ -282,7 +282,7 @@ }, "obsidian": { "version": "https://github.com/obsidianmd/obsidian-api/tarball/master", - "integrity": "sha512-oE7FLE1ICQNKWBm9swMJPlO/iSYtCpau0ANbHx2RNfVyb2/jJMkqcUCcyVYNjwSb1bEn+uU5lkgzrPsk9wJNYA==", + "integrity": "sha512-OGZPxkzYJ1Lgjd4f7eaBFr8KqIG8vkwVcOblH5BMSIoMieY1NmXL9/ouVwMjc1NJH7ZN6Zi7Qclz8Y42fd8rwQ==", "dev": true, "requires": { "@types/codemirror": "0.0.98" @@ -332,18 +332,18 @@ } }, "rollup": { - "version": "2.36.1", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.36.1.tgz", - "integrity": "sha512-eAfqho8dyzuVvrGqpR0ITgEdq0zG2QJeWYh+HeuTbpcaXk8vNFc48B7bJa1xYosTCKx0CuW+447oQOW8HgBIZQ==", + "version": "2.40.0", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.40.0.tgz", + "integrity": "sha512-WiOGAPbXoHu+TOz6hyYUxIksOwsY/21TRWoO593jgYt8mvYafYqQl+axaA8y1z2HFazNUUrsMSjahV2A6/2R9A==", "dev": true, "requires": { - "fsevents": "~2.1.2" + "fsevents": "~2.3.1" } }, "rollup-plugin-svelte": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/rollup-plugin-svelte/-/rollup-plugin-svelte-7.0.0.tgz", - "integrity": "sha512-cw4yv/5v1NQV3nPbpOJtikgkB+9mfSJaqKUdq7x5fVQJnwLtcdc2JOszBs5pBY+SemTs5pmJbdEMseEavbUtjQ==", + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-svelte/-/rollup-plugin-svelte-7.1.0.tgz", + "integrity": "sha512-vopCUq3G+25sKjwF5VilIbiY6KCuMNHP1PFvx2Vr3REBNMDllKHFZN2B9jwwC+MqNc3UPKkjXnceLPEjTjXGXg==", "dev": true, "requires": { "require-relative": "^0.8.7", @@ -383,14 +383,14 @@ } }, "svelte": { - "version": "3.31.2", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.31.2.tgz", - "integrity": "sha512-TxZGrXzX2ggFH3BIKY5fmbeMdJuZrMIMDYPMX6R9255bueuYIuVaBQSLUeY2oD7W4IdeqRZiAVGCjDw2POKBRA==" + "version": "3.34.0", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.34.0.tgz", + "integrity": "sha512-xWcaQ/J4Yd5k0UWz+ef6i5RW5WP3hNpluEI2qtTTKlMOXERHpVL509O9lIw7sgEn1JjJgTOS+lnnDj99vQ3YqQ==" }, "svelte-preprocess": { - "version": "4.6.1", - "resolved": "https://registry.npmjs.org/svelte-preprocess/-/svelte-preprocess-4.6.1.tgz", - "integrity": "sha512-s7KdhR2pOsffyOzZIMEb315f6pfgeDnOWN47m6YKFeSEx3NMf/79Znc3vuG/Ai79SL/vsi86WDrjFPLGRfDesg==", + "version": "4.6.9", + "resolved": "https://registry.npmjs.org/svelte-preprocess/-/svelte-preprocess-4.6.9.tgz", + "integrity": "sha512-SROWH0rB0DJ+0Ii264cprmNu/NJyZacs5wFD71ya93Cg/oA2lKHgQm4F6j0EWA4ktFMzeuJJm/eX6fka39hEHA==", "dev": true, "requires": { "@types/pug": "^2.0.4", @@ -406,9 +406,9 @@ "dev": true }, "typescript": { - "version": "4.1.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", - "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==", + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.2.tgz", + "integrity": "sha512-tbb+NVrLfnsJy3M59lsDgrzWIflR4d4TIUjz+heUnHZwdF7YsrMTKoRERiIvI2lvBG95dfpLxB21WZhys1bgaQ==", "dev": true }, "wrappy": { diff --git a/package.json b/package.json index 6b37d96..152756a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-checklist-plugin", - "version": "1.0.10", + "version": "1.0.11", "description": "A plugin for Obsidian.md which consoldiates todo items into a single view.", "main": "main.js", "scripts": { @@ -17,15 +17,15 @@ "@rollup/plugin-node-resolve": "^9.0.0", "@rollup/plugin-typescript": "^6.0.0", "@tsconfig/svelte": "^1.0.10", - "@types/node": "^14.14.2", + "@types/node": "^14.14.31", "obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master", - "rollup": "^2.32.1", - "rollup-plugin-svelte": "^7.0.0", - "svelte-preprocess": "^4.6.1", + "rollup": "^2.40.0", + "rollup-plugin-svelte": "^7.1.0", + "svelte-preprocess": "^4.6.9", "tslib": "^2.0.3", - "typescript": "^4.0.3" + "typescript": "^4.2.2" }, "dependencies": { - "svelte": "^3.31.2" + "svelte": "^3.34.0" } } diff --git a/src/_types.ts b/src/_types.ts index 3a297d0..664e716 100644 --- a/src/_types.ts +++ b/src/_types.ts @@ -38,6 +38,7 @@ export type TodoItem = { line: number spacesIndented: number fileInfo: FileInfo + originalText: string } export type TodoGroup = { diff --git a/src/_utils.ts b/src/_utils.ts index fa131cf..91b929b 100644 --- a/src/_utils.ts +++ b/src/_utils.ts @@ -1,6 +1,5 @@ -import * as os from 'os' +import {App, LinkCache, MetadataCache, TagCache, TFile, Vault} from 'obsidian' -import type { App, LinkCache, MetadataCache, TagCache, TFile, Vault } from "obsidian" import type { TodoItem, TodoGroup, @@ -34,7 +33,7 @@ export const parseTodos = async ( .map>(async (file) => { const fileCache = cache.getFileCache(file) const tagsOnPage = fileCache?.tags?.filter((e) => getTagMeta(e.tag).main === pageLink) ?? [] - const content = await vault.read(file) + const content = await vault.cachedRead(file) return { content, cache: fileCache, validTags: tagsOnPage, file } }) ) @@ -70,15 +69,19 @@ export const groupTodos = (items: TodoItem[], groupBy: GroupByType): TodoGroup[] } export const toggleTodoItem = async (item: TodoItem, app: App) => { - const file = app.vault.getFiles().find((f) => f.path === item.filePath) - const newData = setTodoStatusAtLineTo(item.fileInfo.content, item.line, !item.checked) + const file = getFileFromPath(app.vault, item.filePath) + if (!file) return + const currentFileContents = await app.vault.read(file) + const currentFileLines = getAllLinesFromFile(currentFileContents) + if (!currentFileLines[item.line].includes(item.originalText)) return + const newData = setTodoStatusAtLineTo(currentFileLines, item.line, !item.checked) app.vault.modify(file, newData) + item.checked = !item.checked } -export const navToFile = async (path: string, ev: MouseEvent) => { +export const navToFile = async (app: App, path: string, ev: MouseEvent) => { path = ensureMdExtension(path) - const app: App = (window as any).app - const file = getFileFromPath(path, app) + const file = getFileFromPath(app.vault, path) if (!file) return const leaf = isMetaPressed(ev) ? app.workspace.splitActiveLeaf() : app.workspace.getUnpinnedLeaf() await leaf.openFile(file) @@ -96,13 +99,16 @@ export const hoverFile = (event: MouseEvent, app: App, filePath: string) => { /** private */ +const getFileFromPath = (vault: Vault, path: string) => { + const file = vault.getAbstractFileByPath(path) + if (file instanceof TFile) return file +} + const ensureMdExtension = (path: string) => { if (!/\.md$/.test(path)) return `${path}.md` return path } -const getFileFromPath = (path: string, app: App) => app.vault.getFiles().find((f) => f.path.endsWith(path)) - const isMetaPressed = (e: MouseEvent): boolean => { return isMacOS() ? e.metaKey : e.ctrlKey } @@ -152,6 +158,7 @@ const formTodo = (line: string, file: FileInfo, tagMeta: TagMeta, links: LinkCac subTag: tagMeta?.sub, spacesIndented, fileInfo: file, + originalText: rawText, } } @@ -233,9 +240,7 @@ const getAllMatches = (r: RegExp, string: string, captureIndex = 0) => { return matches } -const setTodoStatusAtLineTo = (fileContents: string, line: number, setTo: boolean) => { - if (!fileContents) return - const fileLines = getAllLinesFromFile(fileContents) +const setTodoStatusAtLineTo = (fileLines: string[], line: number, setTo: boolean) => { fileLines[line] = setLineTo(fileLines[line], setTo) return combineFileLines(fileLines) } @@ -267,6 +272,4 @@ const getFileLabelFromName = (filename: string) => /^(.+)\.md$/.exec(filename)?. const removeTagFromText = (text: string, tag: string) => text.replace(new RegExp(`\\s?\\#${tag}[^\\s]*`, "g"), "").trim() -const isMacOS = () => { - return os.platform() === "darwin" -} +const isMacOS = () => window.navigator.userAgent.includes("Macintosh") diff --git a/src/svelte/App.svelte b/src/svelte/App.svelte index 667d016..db54dbc 100644 --- a/src/svelte/App.svelte +++ b/src/svelte/App.svelte @@ -1,12 +1,9 @@ @@ -34,7 +37,7 @@ {#if !isCollapsed} {#each group.todos as item} - + {/each} {/if} diff --git a/src/svelte/ChecklistItem.svelte b/src/svelte/ChecklistItem.svelte index 824041a..7d9e30d 100644 --- a/src/svelte/ChecklistItem.svelte +++ b/src/svelte/ChecklistItem.svelte @@ -8,16 +8,14 @@ export let item: TodoItem export let lookAndFeel: LookAndFeel - - const app: App = (window as any).app + export let app: App const toggleItem = async (item: TodoItem) => { toggleTodoItem(item, app) - item.checked = !item.checked } -
navToFile(item.filePath, ev)}> +
navToFile(app, item.filePath, ev)}>
{ @@ -28,7 +26,7 @@
- +
diff --git a/src/svelte/TextChunk.svelte b/src/svelte/TextChunk.svelte index 5f91423..de5d544 100644 --- a/src/svelte/TextChunk.svelte +++ b/src/svelte/TextChunk.svelte @@ -5,8 +5,7 @@ import TodoText from "./TextChunk.svelte" export let chunks: TodoDisplayChunk[] - - const app: App = (window as any).app + export let app: App {#each chunks as chunk} @@ -21,7 +20,7 @@ class="link-item" on:click={(ev) => { ev.stopPropagation() - if (chunk.filePath) navToFile(chunk.filePath, ev) + if (chunk.filePath) navToFile(app, chunk.filePath, ev) }} on:mouseenter={(ev) => { if (chunk.filePath) hoverFile(ev, app, chunk.filePath) diff --git a/src/view.ts b/src/view.ts index 2d432a9..09ff676 100644 --- a/src/view.ts +++ b/src/view.ts @@ -38,6 +38,7 @@ export default class TodoListView extends ItemView { lookAndFeel: this.plugin.getSettingValue("lookAndFeel"), rerenderKey: Symbol("[rerender]"), _collapsedSections: this.plugin.getSettingValue("_collapsedSections"), + app: this.app, updateSetting: (updates: Partial) => this.plugin.updateSettings(updates), } } @@ -47,7 +48,12 @@ export default class TodoListView extends ItemView { target: (this as any).contentEl, props: this.getProps(), }) - this.registerEvent(this.app.metadataCache.on("resolve", () => this.rerender())) + this.registerEvent( + this.app.metadataCache.on("resolve", (...args) => { + // TODO: capture incremental updates here + this.rerender() + }) + ) } rerender() { diff --git a/versions.json b/versions.json index fac1fa8..1c5fde9 100644 --- a/versions.json +++ b/versions.json @@ -10,5 +10,6 @@ "1.0.8": "0.9.12", "1.0.9": "0.9.12", "1.0.10": "0.9.12", - "1.0.11": "0.9.12" + "1.0.11": "0.9.12", + "1.0.12": "0.9.12" }