-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
90 changed files
with
4,349 additions
and
1,361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,73 @@ | ||
import { Asset } from "@datasworn/core/dist/Datasworn"; | ||
import { html, render } from "lit-html"; | ||
|
||
import { CampaignDataContext } from "campaigns/context"; | ||
import IronVaultPlugin from "index"; | ||
import { MarkdownRenderChild } from "obsidian"; | ||
import { FileBasedCampaignWatch } from "sidebar/sidebar-block"; | ||
import renderAssetCard from "./asset-card"; | ||
|
||
export default function registerAssetBlock(plugin: IronVaultPlugin) { | ||
plugin.registerMarkdownCodeBlockProcessor( | ||
"iron-vault-asset", | ||
async (source, el: AssetBlockContainerEl, _ctx) => { | ||
// We can't render blocks until datastore is ready. | ||
await plugin.datastore.waitForReady; | ||
if (!el.assetRenderer) { | ||
const asset = AssetBlockRenderer.getAsset(plugin, source); | ||
if (!asset) { | ||
render(html`<p>No such asset: ${source}</p>`, el); | ||
return; | ||
} | ||
el.assetRenderer = new AssetBlockRenderer(el, plugin, asset); | ||
} | ||
await el.assetRenderer.render(); | ||
(source, el: HTMLElement, ctx) => { | ||
ctx.addChild(new AssetBlockRenderer(el, plugin, source, ctx.sourcePath)); | ||
}, | ||
); | ||
} | ||
|
||
interface AssetBlockContainerEl extends HTMLElement { | ||
assetRenderer?: AssetBlockRenderer; | ||
} | ||
|
||
class AssetBlockRenderer { | ||
contentEl: HTMLElement; | ||
plugin: IronVaultPlugin; | ||
asset: Asset; | ||
class AssetBlockRenderer extends MarkdownRenderChild { | ||
campaignSource: FileBasedCampaignWatch; | ||
|
||
constructor(contentEl: HTMLElement, plugin: IronVaultPlugin, asset: Asset) { | ||
this.contentEl = contentEl; | ||
this.plugin = plugin; | ||
this.asset = asset; | ||
constructor( | ||
contentEl: HTMLElement, | ||
readonly plugin: IronVaultPlugin, | ||
readonly source: string, | ||
sourcePath: string, | ||
) { | ||
super(contentEl); | ||
this.campaignSource = this.addChild( | ||
new FileBasedCampaignWatch( | ||
plugin.app.vault, | ||
plugin.campaignManager, | ||
sourcePath, | ||
).onUpdate(() => this.render()), | ||
); | ||
} | ||
|
||
static getAsset(plugin: IronVaultPlugin, source: string) { | ||
const trimmed = source.trim().toLowerCase(); | ||
getAsset(dataContext: CampaignDataContext) { | ||
const trimmed = this.source.trim().toLowerCase(); | ||
return ( | ||
plugin.datastore.assets.get(trimmed) || | ||
[...plugin.datastore.assets.values()].find( | ||
dataContext.assets.get(trimmed) || | ||
[...dataContext.assets.values()].find( | ||
(a) => a.name.toLowerCase() === trimmed, | ||
) | ||
); | ||
} | ||
|
||
async render() { | ||
const dataContext = this.campaignSource.campaignContext; | ||
if (!dataContext) { | ||
render( | ||
html`<article class="error"> | ||
Asset block may only be used within a campaign folder. | ||
</article>`, | ||
this.containerEl, | ||
); | ||
return; | ||
} | ||
const asset = this.getAsset(dataContext); | ||
if (!asset) { | ||
render(html`<p>No such asset: ${this.source}</p>`, this.containerEl); | ||
return; | ||
} | ||
render( | ||
renderAssetCard(this.plugin, { | ||
id: this.asset._id, | ||
renderAssetCard(this.plugin, dataContext, { | ||
id: asset._id, | ||
abilities: [true, false, false], | ||
options: {}, | ||
controls: {}, | ||
}), | ||
this.contentEl, | ||
this.containerEl, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.