Skip to content

Commit

Permalink
Merge pull request #29 from terryli710/hotfixes/0.2-bugs
Browse files Browse the repository at this point in the history
Hotfixes/0.2 bugs
  • Loading branch information
terryli710 authored Sep 8, 2023
2 parents 6629db3 + dfc8dbc commit 58ac28f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
37 changes: 28 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App, Plugin } from 'obsidian';
import type { PluginManifest, Workspace, WorkspaceLeaf } from 'obsidian';
import type { Editor, PluginManifest, Workspace, WorkspaceLeaf } from 'obsidian';
import type { TaskCardSettings } from './settings';
import { SettingStore, SettingsTab } from './settings';
import { DefaultSettings, SettingStore, SettingsTab } from './settings';
import { logger } from './utils/log';
import AttributeSuggest from './autoSuggestions/EditorSuggestions';
import { Project, ProjectModule } from './taskModule/project';
Expand Down Expand Up @@ -42,16 +42,24 @@ export default class TaskCardPlugin extends Plugin {
this.staticTaskListRenderManager = new StaticTaskListRenderManager(this);
this.cache = new TaskCardCache(this);
}

async loadSettings() {
// Load saved settings from storage
const loadedSettings = await this.loadData();

// Initialize settings with default values
let initialSettings = JSON.parse(JSON.stringify(DefaultSettings));

// Update the initial settings with any saved settings
SettingStore.update((old) => {
return {
...old,
...(loadedSettings || {})
...initialSettings, // Start with default settings
...old, // Override with old settings (if any)
...loadedSettings // Finally, override with loaded settings
};
});
}


async writeSettings(
changeOpts: (settings: TaskCardSettings) => void
Expand Down Expand Up @@ -88,20 +96,31 @@ export default class TaskCardPlugin extends Plugin {

registerCommands() {
this.addCommand({
id: 'task-card-single-line-display-mode',
name: 'Task Card: Single Line Display Mode',
id: 'task-card-preview-display-mode',
name: 'Preview Display Mode',
callback: () => {
this.taskMonitor.changeDisplayModeCommandHandler('single-line');
}
})

this.addCommand({
id: 'task-card-multi-line-display-mode',
name: 'Task Card: Multi Line Display Mode',
id: 'task-card-detailed-display-mode',
name: 'Detailed Display Mode',
callback: () => {
this.taskMonitor.changeDisplayModeCommandHandler('multi-line');
}
})

this.addCommand({
id: 'task-card-add-query',
name: 'Add Query',
editorCallback: (editor: Editor) => {
editor.replaceRange(
`\n\`\`\`${this.settings.parsingSettings.blockLanguage}\n\`\`\``,
editor.getCursor()
)
}
})
}

registerPostProcessors() {
Expand Down
27 changes: 22 additions & 5 deletions src/query/querySyncManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { MarkdownPostProcessorContext } from "obsidian";
import TaskCardPlugin from "..";
import { SettingStore } from "../settings";
import { Project } from "../taskModule/project";
import { PositionedObsidianTask } from "../taskModule/task";
import { logger } from "../utils/log";
import { MultipleAttributeTaskQuery } from "./cache";

export interface TaskQueryOptions {
Expand All @@ -21,16 +23,23 @@ export class QuerySyncManager {
editMode: boolean = true;
options: TaskQueryOptions;
codeBlockMetadata: {
sectionEl: HTMLElement;
ctx: MarkdownPostProcessorContext;
sourcePath: string;
lineStart: number;
lineEnd: number;
}

constructor(plugin: TaskCardPlugin, blockLanguage: string, source: string, codeBlockMetadata: {
sourcePath: string;
lineStart: number;
lineEnd: number;
}) {
constructor(plugin: TaskCardPlugin,
blockLanguage: string,
source: string,
codeBlockMetadata: {
sectionEl: HTMLElement;
ctx: MarkdownPostProcessorContext;
sourcePath: string;
lineStart: number;
lineEnd: number;
}) {
this.plugin = plugin;
this.blockLanguage = blockLanguage;
this.source = source;
Expand Down Expand Up @@ -167,6 +176,7 @@ export class QuerySyncManager {
this.editMode = editMode;
const filledQuery = this.setDefaultQueryValues(taskQuery);
const newQuery = this.formatCodeBlock(this.queryFormatter(filledQuery));
this.updateCodeBlockMetadata();
this.plugin.fileOperator.updateFile(
this.codeBlockMetadata.sourcePath,
newQuery,
Expand All @@ -175,6 +185,13 @@ export class QuerySyncManager {
);
}

updateCodeBlockMetadata() {
const mdSectionInfo = this.codeBlockMetadata.ctx.getSectionInfo(this.codeBlockMetadata.sectionEl);
this.codeBlockMetadata.sourcePath = this.codeBlockMetadata.ctx.sourcePath;
this.codeBlockMetadata.lineStart = mdSectionInfo.lineStart;
this.codeBlockMetadata.lineEnd = mdSectionInfo.lineEnd;
}

toEditMode() {
this.updateTaskQueryToFile(this.taskQuery, true);
}
Expand Down
1 change: 0 additions & 1 deletion src/renderer/fileOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export class FileOperator {
const fileLines = await this.getFileLines(filePath);
const newFileLines: string[] = [...fileLines];
newFileLines.splice(lineStart, lineEnd - lineStart, newContent);
logger.debug(`update file: start line content: ${fileLines[lineStart]}, end line content: ${fileLines[lineEnd - 1]}`);
await this.app.vault.modify(file as TFile, newFileLines.join('\n'));
}

Expand Down
4 changes: 4 additions & 0 deletions src/renderer/queryAndTaskListSvelteAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class QueryAndTaskListSvelteAdapter {
svelteComponent: SvelteComponent
codeBlockEl: HTMLElement
codeBlockMetadata: {
sectionEl: HTMLElement
ctx: MarkdownPostProcessorContext
sourcePath: string
lineStart: number
lineEnd: number
Expand All @@ -28,6 +30,8 @@ export class QueryAndTaskListSvelteAdapter {
this.codeBlockEl = el
const mdSectionInfo: MarkdownSectionInformation = ctx.getSectionInfo(el);
this.codeBlockMetadata = {
sectionEl: el,
ctx: ctx,
sourcePath: ctx.sourcePath,
lineStart: mdSectionInfo.lineStart,
lineEnd: mdSectionInfo.lineEnd
Expand Down
21 changes: 11 additions & 10 deletions src/ui/QueryEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
let filePath = query.filePathQuery;
let dueDateEnabled = false;
let filePathEnabled = false;
// let dueDateEnabled = false;
// let filePathEnabled = false;
let lowerPaths = paths.map((path) => path.toLowerCase());
function toggleFilter(filterType) {
if (filterType === 'dueDate') {
dueDateEnabled = !dueDateEnabled;
} else if (filterType === 'filePath') {
filePathEnabled = !filePathEnabled;
}
}
// function toggleFilter(filterType) {
// if (filterType === 'dueDate') {
// dueDateEnabled = !dueDateEnabled;
// } else if (filterType === 'filePath') {
// filePathEnabled = !filePathEnabled;
// }
// }
// Function to save the query
function saveQuery() {
Expand Down Expand Up @@ -108,7 +108,8 @@
// Validate the parsed time
if (!time || !isValidDate(time)) {
logger.error(`Invalid date string: ${value}`);
// logger.error(`Invalid date string: ${value}`);
return;
}
// Assign the parsed time to the appropriate variable
Expand Down

0 comments on commit 58ac28f

Please sign in to comment.