diff --git a/rollup.config.js b/rollup.config.js index 4f6107e..53613f1 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,9 +1,9 @@ import typescript from '@rollup/plugin-typescript'; -import {nodeResolve} from '@rollup/plugin-node-resolve'; +import { nodeResolve } from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; export default { - input: 'main.ts', + input: 'src/main.ts', output: { dir: '.', sourcemap: 'inline', @@ -13,7 +13,7 @@ export default { external: ['obsidian'], plugins: [ typescript(), - nodeResolve({browser: true}), + nodeResolve({ browser: true }), commonjs(), ] }; \ No newline at end of file diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..b88d9ca --- /dev/null +++ b/src/constants.ts @@ -0,0 +1 @@ +export const VIEW_TYPE_STATS_TRACKER = "stats-tracker"; \ No newline at end of file diff --git a/main.ts b/src/main.ts similarity index 73% rename from main.ts rename to src/main.ts index 6f046e4..ce01d00 100644 --- a/main.ts +++ b/src/main.ts @@ -1,4 +1,6 @@ -import { TFile, Plugin, MarkdownView, debounce, Debouncer } from 'obsidian'; +import { TFile, Plugin, MarkdownView, debounce, Debouncer, WorkspaceLeaf } from 'obsidian'; +import { VIEW_TYPE_STATS_TRACKER } from './constants'; +import StatsTrackerView from './view'; interface WordCount { initial: number; @@ -22,6 +24,8 @@ export default class DailyStats extends Plugin { today: string; debouncedUpdate: Debouncer<[contents: string, filepath: string]>; + private view: StatsTrackerView; + async onload() { await this.loadSettings(); @@ -37,6 +41,24 @@ export default class DailyStats extends Plugin { this.updateWordCount(contents, filepath); }, 400, false); + this.registerView( + VIEW_TYPE_STATS_TRACKER, + (leaf: WorkspaceLeaf) => (this.view = new StatsTrackerView(leaf)) + ); + + this.addCommand({ + id: "show-daily-stats-tracker-view", + name: "Open tracker view", + checkCallback: (checking: boolean) => { + if (checking) { + return ( + this.app.workspace.getLeavesOfType(VIEW_TYPE_STATS_TRACKER).length === 0 + ); + } + this.initLeaf(); + }, + }); + this.registerEvent( this.app.workspace.on("quit", this.onunload.bind(this)) ); @@ -55,6 +77,23 @@ export default class DailyStats extends Plugin { this.updateDate(); this.saveSettings(); }, 1000)); + + if (this.app.workspace.layoutReady) { + this.initLeaf(); + } else { + this.registerEvent( + this.app.workspace.on("layout-ready", this.initLeaf.bind(this)) + ); + } + } + + initLeaf(): void { + if (this.app.workspace.getLeavesOfType(VIEW_TYPE_STATS_TRACKER).length) { + return; + } + this.app.workspace.getRightLeaf(false).setViewState({ + type: VIEW_TYPE_STATS_TRACKER, + }); } async onunload() { diff --git a/src/view.ts b/src/view.ts new file mode 100644 index 0000000..f752c72 --- /dev/null +++ b/src/view.ts @@ -0,0 +1,26 @@ +import { ItemView, WorkspaceLeaf } from "obsidian"; +import { VIEW_TYPE_STATS_TRACKER } from "./constants"; + +export default class StatsTrackerView extends ItemView { + + constructor(leaf: WorkspaceLeaf) { + super(leaf); + } + + getDisplayText() { + return "Daily Stats"; + } + + getIcon() { + return "calendar-with-checkmark"; + } + + getViewType() { + return VIEW_TYPE_STATS_TRACKER; + } + + async onOpen() { + var modal_content = '
Inner content
'; + (this as any).contentEl.innerHTML = modal_content; + } +} \ No newline at end of file