Skip to content

Commit

Permalink
Merge pull request #5 from liamcain/fix/overwriting-existing-tasks
Browse files Browse the repository at this point in the history
Don't override today's notes
  • Loading branch information
liamcain authored Mar 16, 2021
2 parents 409a030 + e623f98 commit 44a9e9e
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 140 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Liam Cain

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"id": "things-logbook",
"name": "Things Logbook",
"description": "Sync your Things.app Logbook with Daily Notes",
"version": "0.1.6",
"version": "0.1.7",
"author": "Liam Cain",
"authorUrl": "https://github.com/liamcain/",
"isDesktopOnly": false,
"isDesktopOnly": true,
"minAppVersion": "0.10.8"
}
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
{
"name": "obsidian-things-logbook-plugin",
"version": "0.1.6",
"version": "0.1.7",
"description": "Sync Things.app Logbook with Obsidian",
"author": "liamcain",
"main": "main.js",
"license": "MIT",
"scripts": {
"lint": "eslint . --ext .ts",
"build": "npm run lint && rollup -c",
"build:nolint": "rollup -c",
"test": "jest",
"test:watch": "yarn test -- --watch"
},
"dependencies": {
"obsidian": "obsidianmd/obsidian-api#master",
"obsidian-daily-notes-interface": "0.5.1",
"obsidian-daily-notes-interface": "0.7.7",
"papaparse": "5.3.0",
"tslib": "2.0.3"
"tslib": "2.1.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "17.0.0",
"@rollup/plugin-commonjs": "17.1.0",
"@rollup/plugin-json": "4.1.0",
"@rollup/plugin-node-resolve": "11.0.0",
"@rollup/plugin-replace": "2.3.3",
"@rollup/plugin-typescript": "8.1.0",
"@rollup/plugin-node-resolve": "11.2.0",
"@rollup/plugin-replace": "2.4.1",
"@rollup/plugin-typescript": "8.2.0",
"@types/jest": "26.0.20",
"@types/moment": "2.13.0",
"@types/node": "14.14.21",
"@types/node": "14.14.34",
"@types/papaparse": "5.2.5",
"@typescript-eslint/eslint-plugin": "4.13.0",
"@typescript-eslint/parser": "4.13.0",
"eslint": "7.18.0",
"@typescript-eslint/eslint-plugin": "4.17.0",
"@typescript-eslint/parser": "4.17.0",
"eslint": "7.22.0",
"jest": "26.6.3",
"moment": "2.29.1",
"rollup": "2.37.1",
"typescript": "4.1.3"
"rollup": "2.41.2",
"typescript": "4.2.3"
},
"jest": {
"moduleNameMapper": {
Expand Down
12 changes: 12 additions & 0 deletions src/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Editor } from "codemirror";
import { App, MarkdownView, TFile } from "obsidian";

export function getEditorForFile(app: App, file: TFile): Editor | null {
let editor = null;
app.workspace.iterateAllLeaves((leaf) => {
if (leaf.view instanceof MarkdownView && leaf.view.file === file) {
editor = leaf.view.sourceMode.cmEditor;
}
});
return editor;
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
getTasksFromThingsLogbook,
ITask,
} from "./things";
import { groupBy, isMacOS, updateSection } from "./utils";
import { groupBy, isMacOS, updateSection } from "./textUtils";

declare global {
interface Window {
Expand Down Expand Up @@ -144,6 +144,7 @@ export default class ThingsLogbookPlugin extends Plugin {
}

await updateSection(
this.app,
dailyNote,
"## Logbook",
logbookRenderer.render(tasks)
Expand Down
2 changes: 1 addition & 1 deletion src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App } from "obsidian";
import { ISettings } from "./settings";
import { ISubTask, ITask } from "./things";
import { getHeadingLevel, getTab, groupBy, toHeading } from "./utils";
import { getHeadingLevel, getTab, groupBy, toHeading } from "./textUtils";

export class LogbookRenderer {
private app: App;
Expand Down
12 changes: 5 additions & 7 deletions src/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ async function handleSqliteQuery(
const stdOut: Buffer[] = [];
const stdErr: Buffer[] = [];

const spawned = spawn("sqlite3", [
dbPath,
"-header",
"-csv",
"-readonly",
query,
]);
const spawned = spawn(
"sqlite3",
[dbPath, "-header", "-csv", "-readonly", query],
{ detached: true }
);

spawned.stdout.on("data", (buffer: Buffer) => {
stdOut.push(buffer);
Expand Down
25 changes: 20 additions & 5 deletions src/utils.ts → src/textUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as os from "os";
import type { TFile } from "obsidian";
import type { App, TFile } from "obsidian";
import { getEditorForFile } from "./fileUtils";

export function getHeadingLevel(line = ""): number | null {
const heading = line.match(/^(#{1,6})\s\b/);
Expand Down Expand Up @@ -31,10 +31,11 @@ export function groupBy<T>(
}

export function isMacOS(): boolean {
return os.platform() === "darwin";
return navigator.appVersion.indexOf("Mac") !== -1;
}

export async function updateSection(
app: App,
file: TFile,
heading: string,
sectionContents: string
Expand All @@ -53,15 +54,28 @@ export async function updateSection(
logbookSectionLineNum = i;
} else if (logbookSectionLineNum !== -1) {
const currLevel = getHeadingLevel(fileLines[i]);
if (currLevel && currLevel < headingLevel) {
if (currLevel && currLevel <= headingLevel) {
nextSectionLineNum = i;
break;
}
}
}

// Section already exists, just replace
const editor = getEditorForFile(app, file);
if (editor) {
const from = { line: logbookSectionLineNum, ch: 0 };
const to =
nextSectionLineNum !== -1
? { line: nextSectionLineNum - 1, ch: 0 }
: { line: fileLines.length, ch: 0 };

editor.replaceRange(`${sectionContents}\n`, from, to);
return;
}

// Editor is not open, modify the file on disk...
if (logbookSectionLineNum !== -1) {
// Section already exists, just replace
const prefix = fileLines.slice(0, logbookSectionLineNum);
const suffix =
nextSectionLineNum !== -1 ? fileLines.slice(nextSectionLineNum) : [];
Expand All @@ -71,6 +85,7 @@ export async function updateSection(
[...prefix, sectionContents, ...suffix].join("\n")
);
} else {
// Section does not exist, append to end of file.
return vault.modify(file, [...fileLines, "", sectionContents].join("\n"));
}
}
2 changes: 1 addition & 1 deletion src/things.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export async function getTasksFromThingsLogbook(
): Promise<ITaskRecord[]> {
const taskRecords: ITaskRecord[] = [];
let isSyncCompleted = false;
let stopTime = latestSyncTime;
let stopTime = window.moment.unix(latestSyncTime).startOf("day").unix();

try {
while (!isSyncCompleted) {
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "es5",
"target": "esnext",
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"importHelpers": true,
"lib": ["dom", "es5", "scripthost", "es2015"]
"lib": ["dom", "esnext"]
},
"include": ["**/*.ts"]
}
Loading

0 comments on commit 44a9e9e

Please sign in to comment.