diff --git a/lib/Views/InitiativeTrackerView.ts b/lib/Views/InitiativeTrackerView.ts new file mode 100644 index 0000000..d36dba1 --- /dev/null +++ b/lib/Views/InitiativeTrackerView.ts @@ -0,0 +1,29 @@ +import { ItemView, WorkspaceLeaf } from 'obsidian'; + +export const VIEW_TYPE_EXAMPLE = 'example-view'; + +export class ExampleView extends ItemView { + constructor(leaf: WorkspaceLeaf) { + super(leaf); + } + + getViewType() { + return VIEW_TYPE_EXAMPLE; + } + + getDisplayText() { + return 'Example view'; + } + + async onOpen() { + const container = this.containerEl.children[1]; + container.empty(); + container.createEl('h2', { text: 'Example view' }); + //container.createEl('br'); + container.createEl('h4', { text: 'Example view' }); + } + + async onClose() { + // Nothing to clean up. + } +} \ No newline at end of file diff --git a/lib/main.ts b/lib/main.ts index 79922de..7416c2d 100644 --- a/lib/main.ts +++ b/lib/main.ts @@ -1,5 +1,5 @@ import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; - +import { ExampleView, VIEW_TYPE_EXAMPLE } from './Views/InitiativeTrackerView'; // Remember to rename these classes and interfaces! interface MyPluginSettings { @@ -16,6 +16,16 @@ export default class ForbiddenLandsCharacterSheet extends Plugin { async onload() { await this.loadSettings(); + + this.registerView( + VIEW_TYPE_EXAMPLE, + (leaf) => new ExampleView(leaf) + ); + + this.addRibbonIcon('dice', 'Activate view', () => { + this.activateView(); + }); + // This creates an icon in the left ribbon. const CharacterRibbon = this.addRibbonIcon('book', 'Sample Plugin', (evt: MouseEvent) => { // Called when the user clicks the icon. @@ -87,6 +97,26 @@ export default class ForbiddenLandsCharacterSheet extends Plugin { } + async activateView() { + const { workspace } = this.app; + + let leaf: WorkspaceLeaf | null = null; + const leaves = workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE); + + if (leaves.length > 0) { + // A leaf with our view already exists, use that + leaf = leaves[0]; + } else { + // Our view could not be found in the workspace, create a new leaf + // in the right sidebar for it + leaf = workspace.getRightLeaf(false); + await leaf.setViewState({ type: VIEW_TYPE_EXAMPLE, active: true }); + } + + // "Reveal" the leaf in case it is in a collapsed sidebar + workspace.revealLeaf(leaf); + } + async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); } diff --git a/tsconfig.json b/tsconfig.json index 2d6fbdf..32df59e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,9 +16,10 @@ "ES5", "ES6", "ES7" - ] + ], }, "include": [ - "**/*.ts" + "**/*.ts", + "lib" ] }