Something went wrong. Try again.
Automerge backed Markdown editor for the Atmosphere
Something went wrong. Try again.
3.2 kB · 78 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import { Plugin } from '../../plugin/plugin'import type { App } from '../../plugin/app'import type { PluginManifest } from '../../plugin/types'import { isTypesRecordsEnabled, subscribeTypesRecords } from '../../types-records/feature-setting'import { TypesBrowserView, TYPES_BROWSER_VIEW_TYPE } from './types-browser-view'import { TypeEditorView, TYPE_EDITOR_VIEW_TYPE } from './type-editor-view'import { classifyTypeSchemaPath } from '../../types-records/type-schema'import { BaseView, BASE_VIEW_TYPE } from '../../bases/base-view'import { BaseSurface } from '../../bases/base-surface'import { createRoot } from 'react-dom/client'import { createElement } from 'react'import { MarkdownRenderChild } from '../../plugin/markdown-render-child'import { openBaseDocument } from '../../bases/open-base'const TYPES_BROWSER_MANIFEST: PluginManifest = { id: 'reams-types-browser', name: 'Types', author: 'Reams', version: '1.0.0', minAppVersion: '0.0.0', description: 'Browse typed roots and blocks across the vault',}
export class TypesBrowserPlugin extends Plugin { constructor(app: App) { super(app, TYPES_BROWSER_MANIFEST) }
onload(): void { this.registerView(TYPES_BROWSER_VIEW_TYPE, leaf => new TypesBrowserView(leaf, this.app)) this.registerView(TYPE_EDITOR_VIEW_TYPE, leaf => new TypeEditorView(leaf)) this.registerView(BASE_VIEW_TYPE, leaf => new BaseView(leaf)) this.register(this.app.workspace.registerFileViewResolver({ id: `${this.manifest.id}:base`, matches: file => file.extension.toLowerCase() === 'base' ? BASE_VIEW_TYPE : null, })) this.registerMarkdownCodeBlockProcessor('base', (source, el, ctx) => { const target = document.createElement('div') el.appendChild(target) const root = createRoot(target) root.render(createElement(BaseSurface, { app: this.app, source })) ctx.addChild(new class extends MarkdownRenderChild { onunload() { root.unmount() } }(target)) }) this.register(this.app.workspace.registerFileViewResolver({ id: `${this.manifest.id}:type-editor`, matches: file => { if (!isTypesRecordsEnabled()) return null return classifyTypeSchemaPath(file.path) ? TYPE_EDITOR_VIEW_TYPE : null }, })) this.addCommand({ id: 'open-base', name: 'Open Base', callback: () => { void openBaseDocument(this.app).catch(error => { console.error('Failed to open Base:', error) }) }, }) const syncLeaf = () => { if (!isTypesRecordsEnabled()) { for (const leaf of this.app.workspace.getLeavesOfType(TYPES_BROWSER_VIEW_TYPE)) leaf.detach() for (const leaf of this.app.workspace.getLeavesOfType(TYPE_EDITOR_VIEW_TYPE)) leaf.detach() return } if (!this.app.workspace.layoutReady) return if (this.app.workspace.getLeavesOfType(TYPES_BROWSER_VIEW_TYPE).length > 0) return const leaf = this.app.workspace.getLeftLeaf(true) leaf.openView(TYPES_BROWSER_VIEW_TYPE).catch(error => { console.error('Failed to open Types view:', error) }) }
this.register(subscribeTypesRecords(syncLeaf)) this.app.workspace.onLayoutReady(syncLeaf) }}