import { Plugin, ItemView, Modal } from 'obsidian'; import templateTypes from '../templates.json'; const VIEW_NAME = 'ablaze-view'; // XXX see lower for immediate todo, this is for LATER: // - left leaf with content sorted by type, foldable // - metadata editor for my content types // - center view with db views by type (click opens in tab) // - button to convert frontmatter to simple tags export default class Ablaze extends Plugin { async onload () { // The view must be registered on *every* load, not in onUserEnable() — // that only runs the one time the plugin is enabled in settings, so after // any app restart the persisted leaf has no view and Obsidian shows // "Plugin no longer active". this.registerView(VIEW_NAME, leaf => new AblazeView(leaf, this)); this.addRibbonIcon('book-template', 'Open Ablaze', () => this.activateView()); this.addCommand({ id: 'open-view', name: 'Open panel', callback: () => this.activateView(), }); } // Runs once, when the user first enables the plugin; after that the // workspace remembers the leaf across restarts. async onUserEnable () { await this.activateView(); } async activateView () { const { workspace } = this.app; let leaf = workspace.getLeavesOfType(VIEW_NAME)[0]; if (!leaf) { leaf = workspace.getRightLeaf(false); if (!leaf) return; await leaf.setViewState({ type: VIEW_NAME, active: true }); } await workspace.revealLeaf(leaf); } } // XXX // - button click creates modal // - type text of name // - shows if it already exists // - different create name and h1 // - use correct template // - create in new tab class AblazeView extends ItemView { constructor (leaf, plugin) { super(leaf); this.plugin = plugin; } async onOpen () { this.contentEl.empty(); this.contentEl.addClass('ablaze-root'); const container = this.contentEl.createEl('div', { cls: 'ablaze-tmpl-container' }); templateTypes.forEach(data => { const { name, color, icon } = data; const but = container.createEl('button', { text: `${icon} ${name}` }); but.style.color = color; but.onclick = () => new AblazeModal(this.app, data).open(); }); // So that a device can be checked at a glance for which build it is actually // running (Sync propagating main.js to mobile has been a question). this.contentEl.createEl('div', { cls: 'ablaze-version', text: `v${this.plugin.manifest.version}` }); } getViewType () { return VIEW_NAME; } getDisplayText () { return 'Ablaze'; } getIcon () { return 'book-template'; } } export class AblazeModal extends Modal { constructor (app, data) { super(app); this.data = data; } onOpen () { let exists = false, filename = ''; const { contentEl } = this; const h1 = contentEl.createEl('h1', { text: `${this.data.icon} `}); h1.style.color = this.data.color; const input = h1.createEl('input', { cls: 'ablaze-new-name' }); input.placeholder = this.data.name; if (this.data.tag === 'personal') input.value = today(); input.focus(); const actions = contentEl.createEl('div', { cls: 'ablaze-actions' }); const ok = actions.createEl('button', { text: 'Ok' }); contentEl.createEl('span', { text: 'file: ' }); const status = contentEl.createEl('code'); let markdowns = this.app.vault.getMarkdownFiles(); const refresh = () => { filename = `${sanitize(input.value)}.md`; exists = !!markdowns.find(f => f.name === filename); status.textContent = filename; status.style.color = exists ? 'var(--text-error)' : 'var(--text-normal)'; }; const handleDone = async () => { if (exists || !input.value) { markdowns = this.app.vault.getMarkdownFiles(); refresh(); return; } const md = makeNewMD(input.value, this.data); const newMD = await this.app.vault.create(filename, md); this.app.workspace.getLeaf('tab').openFile(newMD); this.close(); }; ok.onclick = handleDone; input.onkeyup = async (ev) => { refresh(); if (ev.key === 'Enter') await handleDone(); }; refresh(); } onClose () { const { contentEl } = this; contentEl.empty(); } } function sanitize (str) { return (str || '').replace(/[:/"]/g, '_'); } // local date, not UTC — toISOString() is a day off in the evening west of Greenwich function today () { const d = new Date(); return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`; } function makeNewMD (title, data) { let front = ''; let extraTag = ''; if (data.tag === 'reference') { extraTag = 'article'; front = `author: [] url: "" `; } else if (data.tag === 'blog') { front = `lang: en image: "" imageDescription: tk status: draft publication: 2077-03-15 description: "tk" slug: tk-tk bodyType: obsidian subtitle: tk `; } else if (data.tag === 'personal') { front = `date: ${today()} `; } const tags = [data.tag]; if (extraTag) tags.push(extraTag) return `--- ${front}tags: [${tags.join(', ')}] --- # ${title} `; }