Skip to content

Commit

Permalink
fix issues in #22
Browse files Browse the repository at this point in the history
  • Loading branch information
delashum committed Feb 28, 2021
1 parent a247a5c commit 8086a47
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 65 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
48 changes: 24 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
}
}
1 change: 1 addition & 0 deletions src/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type TodoItem = {
line: number
spacesIndented: number
fileInfo: FileInfo
originalText: string
}

export type TodoGroup = {
Expand Down
35 changes: 19 additions & 16 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -34,7 +33,7 @@ export const parseTodos = async (
.map<Promise<FileInfo>>(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 }
})
)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -152,6 +158,7 @@ const formTodo = (line: string, file: FileInfo, tagMeta: TagMeta, links: LinkCac
subTag: tagMeta?.sub,
spacesIndented,
fileInfo: file,
originalText: rawText,
}
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
8 changes: 3 additions & 5 deletions src/svelte/App.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<script lang="ts">
import type { App } from "obsidian"
import { toggleTodoItem, parseTodos, groupTodos, navToFile, hoverFile } from "src/_utils"
import { toggleTodoItem, parseTodos, groupTodos } from "src/_utils"
import type { GroupByType, LookAndFeel, SortDirection, TodoGroup, TodoItem } from "src/_types"
import CheckCircle from "./CheckCircle.svelte"
import TodoText from "./TextChunk.svelte"
import Loading from "./Loading.svelte"
import type { TodoSettings } from "src/settings"
import Icon from "./Icon.svelte"
import ChecklistGroup from "./ChecklistGroup.svelte"
export let todoTag: string
Expand All @@ -18,7 +15,7 @@
export let _collapsedSections: string[]
export let updateSetting: (updates: Partial<TodoSettings>) => Promise<void>
export let rerenderKey: symbol
const app: App = (window as any).app
export let app: App
let todos: TodoItem[] = []
let todoGroups: TodoGroup[] = []
let firstRun = true
Expand Down Expand Up @@ -63,6 +60,7 @@
{#each todoGroups as group}
<ChecklistGroup
{group}
{app}
{lookAndFeel}
mainTag={todoTag}
isCollapsed={_collapsedSections.includes(group.groupId)}
Expand Down
7 changes: 5 additions & 2 deletions src/svelte/ChecklistGroup.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script lang="ts">
import type { App } from "obsidian"
import type { LookAndFeel, TodoGroup } from "src/_types"
import { navToFile } from "src/_utils"
import ChecklistItem from "./ChecklistItem.svelte"
Expand All @@ -8,10 +10,11 @@
export let mainTag: string
export let isCollapsed: boolean
export let lookAndFeel: LookAndFeel
export let app: App
export let onToggle: (id: string, type: "page" | "tag") => void
function clickTitle(ev: MouseEvent) {
if (group.type === "page") navToFile(group.groupId, ev)
if (group.type === "page") navToFile(app, group.groupId, ev)
}
</script>

Expand All @@ -34,7 +37,7 @@
</div>
{#if !isCollapsed}
{#each group.todos as item}
<ChecklistItem {item} {lookAndFeel} />
<ChecklistItem {item} {lookAndFeel} {app} />
{/each}
{/if}
</div>
Expand Down
8 changes: 3 additions & 5 deletions src/svelte/ChecklistItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
</script>

<div class={`item ${lookAndFeel}`} on:click={(ev) => navToFile(item.filePath, ev)}>
<div class={`item ${lookAndFeel}`} on:click={(ev) => navToFile(app, item.filePath, ev)}>
<div
class="toggle"
on:click={(ev) => {
Expand All @@ -28,7 +26,7 @@
<CheckCircle checked={item.checked} />
</div>
<div class="content">
<TextChunk chunks={item.display} />
<TextChunk chunks={item.display} {app} />
</div>
</div>

Expand Down
5 changes: 2 additions & 3 deletions src/svelte/TextChunk.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import TodoText from "./TextChunk.svelte"
export let chunks: TodoDisplayChunk[]
const app: App = (window as any).app
export let app: App
</script>

{#each chunks as chunk}
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TodoSettings>) => this.plugin.updateSettings(updates),
}
}
Expand All @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 8086a47

Please sign in to comment.