Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/prepare file #135

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# npm
node_modules
package-lock.json
yarn.lock

# build
main.js
Expand Down
19 changes: 10 additions & 9 deletions src/file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { now } from 'moment';
import { Vault, normalizePath } from 'obsidian';
import { DAY_PLANNER_DEFAULT_CONTENT, DAY_PLANNER_FILENAME } from './constants';
import { DayPlannerMode, DayPlannerSettings, NoteForDateQuery } from './settings';
import { Vault, normalizePath } from 'obsidian';

import MomentDateRegex from './moment-date-regex';
import { DayPlannerSettings, DayPlannerMode, NoteForDateQuery } from './settings';
import { now } from 'moment';

export default class DayPlannerFile {
vault: Vault;
Expand Down Expand Up @@ -35,8 +36,8 @@ export default class DayPlannerFile {
}

async prepareFile() {
try {
if(this.settings.mode === DayPlannerMode.File){
try {
if(this.settings.mode === DayPlannerMode.File){
await this.createFolderIfNotExists(this.settings.customFolder);
await this.createFileIfNotExists(this.todayPlannerFilePath());
}
Expand Down Expand Up @@ -69,20 +70,20 @@ export default class DayPlannerFile {
}

async getFileContents(fileName: string){
this.prepareFile();
await this.prepareFile();
try {
return await this.vault.adapter.read(fileName);
} catch (error) {
console.log(error)
}
}

async updateFile(fileName: string, fileContents: string){
this.prepareFile();
await this.prepareFile();
try {
return await this.vault.adapter.write(normalizePath(fileName), fileContents);
} catch (error) {
console.log(error)
}
}
}
}
19 changes: 10 additions & 9 deletions src/planner-md.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { MarkdownView, Workspace } from 'obsidian';
import { DAY_PLANNER_DEFAULT_CONTENT, MERMAID_REGEX } from './constants';
import { DayPlannerSettings, NoteForDateQuery } from './settings';
import type { MarkdownView, Workspace } from 'obsidian';
import type { PlanItem, PlanSummaryData } from './plan-data';

import type DayPlannerFile from './file';
import PlannerMermaid from './mermaid';
import type Parser from './parser';
import type { PlanItem, PlanSummaryData } from './plan-data';
import PlannerMermaid from './mermaid';
import type Progress from './progress';
import { DayPlannerSettings, NoteForDateQuery} from './settings';

export default class PlannerMarkdown {
workspace: Workspace;
Expand All @@ -16,7 +17,7 @@ export default class PlannerMarkdown {
progress: Progress;
mermaid: PlannerMermaid;
noteForDateQuery: NoteForDateQuery;

constructor(workspace: Workspace, settings: DayPlannerSettings, file: DayPlannerFile, parser: Parser, progress: Progress){
this.workspace = workspace;
this.settings = settings;
Expand All @@ -26,12 +27,12 @@ export default class PlannerMarkdown {
this.mermaid = new PlannerMermaid(this.progress);
this.noteForDateQuery = new NoteForDateQuery();
}

async insertPlanner() {
const filePath = this.file.todayPlannerFilePath();
const fileContents = await (await this.file.getFileContents(filePath)).split('\n');
const view = this.workspace.activeLeaf.view as MarkdownView;
const currentLine = view.sourceMode.cmEditor.getCursor().line;
const currentLine = view.editor.getCursor().line;
const insertResult = [...fileContents.slice(0, currentLine), ...DAY_PLANNER_DEFAULT_CONTENT.split('\n'), ...fileContents.slice(currentLine)];
this.file.updateFile(filePath, insertResult.join('\n'));
}
Expand All @@ -47,7 +48,7 @@ export default class PlannerMarkdown {
console.log(error)
}
}

async updateDayPlannerMarkdown(planSummary: PlanSummaryData) {
if((this.dayPlannerLastEdit + 6000) > new Date().getTime()) {
return;
Expand Down Expand Up @@ -112,4 +113,4 @@ export default class PlannerMarkdown {
this.dayPlannerLastEdit = new Date().getTime();
}
}
}
}