Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
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 tagsexport 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 Greenwichfunction 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: enimage: ""imageDescription: tkstatus: draftpublication: 2077-03-15description: "tk"slug: tk-tkbodyType: obsidiansubtitle: tk`; } else if (data.tag === 'personal') { front = `date: ${today()}`; } const tags = [data.tag]; if (extraTag) tags.push(extraTag) return `---${front}tags: [${tags.join(', ')}]---# ${title}`;}