-
Notifications
You must be signed in to change notification settings - Fork 14
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
12 changed files
with
265 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
import { html, render } from "lit-html"; | ||
import { map } from "lit-html/directives/map.js"; | ||
// import { range } from "lit-html/directives/range.js"; | ||
|
||
import IronVaultPlugin from "index"; | ||
import { EventRef, TFile } from "obsidian"; | ||
// import { vaultProcess } from "utils/obsidian"; | ||
import { CharacterContext } from "../character-tracker"; | ||
import { md } from "utils/ui/directives"; | ||
|
||
export default function registerCharacterBlock(plugin: IronVaultPlugin): void { | ||
plugin.registerMarkdownCodeBlockProcessor( | ||
"iron-vault-character", | ||
async (source: string, el: CharacterContainerEl, ctx) => { | ||
// We can't render blocks until datastore is ready. | ||
await plugin.datastore.waitForReady; | ||
if (!el.characterRenderer) { | ||
el.characterRenderer = new CharacterRenderer(el, source, plugin); | ||
} | ||
const file = plugin.app.vault.getFileByPath(ctx.sourcePath); | ||
await el.characterRenderer.render(file); | ||
}, | ||
); | ||
} | ||
|
||
interface CharacterContainerEl extends HTMLElement { | ||
characterRenderer?: CharacterRenderer; | ||
} | ||
|
||
class CharacterRenderer { | ||
contentEl: HTMLElement; | ||
source: string; | ||
plugin: IronVaultPlugin; | ||
fileWatcher?: EventRef; | ||
|
||
constructor(contentEl: HTMLElement, source: string, plugin: IronVaultPlugin) { | ||
this.contentEl = contentEl; | ||
this.source = source; | ||
this.plugin = plugin; | ||
} | ||
|
||
async render(file: TFile | undefined | null) { | ||
if (!file) { | ||
render( | ||
html`<pre>Error rendering character: no file found.</pre>`, | ||
this.contentEl, | ||
); | ||
return; | ||
} | ||
const character = this.plugin.characters.get(file.path); | ||
if (this.fileWatcher) { | ||
this.plugin.app.metadataCache.offref(this.fileWatcher); | ||
} | ||
this.fileWatcher = this.plugin.app.metadataCache.on( | ||
"changed", | ||
(moddedFile) => { | ||
if (moddedFile.path === file.path) { | ||
this.render(moddedFile); | ||
} | ||
}, | ||
); | ||
this.plugin.registerEvent(this.fileWatcher); | ||
if (!character || character.isLeft()) { | ||
render( | ||
// TODO: we should preserve the error? | ||
//html`<pre>Error rendering clock: ${res.error.message}</pre>`, | ||
html`<pre> | ||
Error rendering character: character file is invalid${character | ||
? ": " + character.error.message | ||
: ""}</pre | ||
>`, | ||
this.contentEl, | ||
); | ||
return; | ||
} else if (character.isRight()) { | ||
await this.renderCharacter(character.value, file); | ||
} | ||
} | ||
|
||
async renderCharacter(charCtx: CharacterContext, file: TFile) { | ||
const lens = charCtx.lens; | ||
const raw = charCtx.character; | ||
const tpl = html` | ||
<article class="iron-vault-character"> | ||
<h3 class="name">${md(this.plugin, lens.name.get(raw), file.path)}</h3> | ||
<h5>Stats</h5> | ||
${this.renderStats(charCtx, file)} | ||
<h5>Meters</h5> | ||
${this.renderMeters(charCtx, file)} | ||
<h5>Impacts</h5> | ||
${this.renderImpacts(charCtx, file)} | ||
<h5>Special Tracks</h5> | ||
${this.renderSpecialTracks(charCtx, file)} | ||
<h5>Assets</h5> | ||
${this.renderAssets(charCtx, file)} | ||
</article> | ||
`; | ||
render(tpl, this.contentEl); | ||
} | ||
|
||
renderStats(charCtx: CharacterContext, _file: TFile) { | ||
const lens = charCtx.lens; | ||
const raw = charCtx.character; | ||
return html` | ||
<ul class="stats"> | ||
${map( | ||
Object.entries(lens.stats), | ||
([stat, value]) => html` | ||
<li> | ||
<dl> | ||
<dt data-value=${stat}>${stat}</dt> | ||
<dd data-value=${value.get(raw)}>${value.get(raw)}</dd> | ||
</dl> | ||
</li> | ||
`, | ||
)} | ||
</ul> | ||
`; | ||
} | ||
|
||
renderMeters(charCtx: CharacterContext, _file: TFile) { | ||
const lens = charCtx.lens; | ||
const raw = charCtx.character; | ||
return html` | ||
<ul class="meters"> | ||
${map( | ||
Object.entries(lens.condition_meters), | ||
([meter, value]) => html` | ||
<li> | ||
<dl> | ||
<dt data-value=${meter}>${meter}</dt> | ||
<dd data-value=${value.get(raw)}>${value.get(raw)}</dd> | ||
</dl> | ||
</li> | ||
`, | ||
)} | ||
</ul> | ||
`; | ||
} | ||
|
||
renderImpacts(charCtx: CharacterContext, _file: TFile) { | ||
const lens = charCtx.lens; | ||
const raw = charCtx.character; | ||
return html` | ||
<ul class="impacts"> | ||
${map( | ||
Object.entries(lens.impacts.get(raw)), | ||
([impact, value]) => html` | ||
<li> | ||
<dl> | ||
<dt data-value=${impact}>${impact}</dt> | ||
<dd data-value=${value}>${value}</dd> | ||
</dl> | ||
</li> | ||
`, | ||
)} | ||
</ul> | ||
`; | ||
} | ||
|
||
renderSpecialTracks(charCtx: CharacterContext, _file: TFile) { | ||
const lens = charCtx.lens; | ||
const raw = charCtx.character; | ||
return html` | ||
<ul class="special-tracks"> | ||
${map( | ||
Object.entries(lens.special_tracks), | ||
([track, value]) => html` | ||
<li> | ||
<dl> | ||
<dt data-value=${track}>${track}</dt> | ||
<dd>${JSON.stringify(value.get(raw))}</dd> | ||
</dl> | ||
</li> | ||
`, | ||
)} | ||
</ul> | ||
`; | ||
} | ||
|
||
renderAssets(charCtx: CharacterContext, _file: TFile) { | ||
const lens = charCtx.lens; | ||
const raw = charCtx.character; | ||
return html` | ||
<ul class="assets"> | ||
${map( | ||
Object.values(lens.assets.get(raw)), | ||
(asset) => html` | ||
<li> | ||
<dl> | ||
<dt data-value=${asset.id}>${asset.id}</dt> | ||
<dd>${JSON.stringify(asset)}</dd> | ||
</dl> | ||
</li> | ||
`, | ||
)} | ||
</ul> | ||
`; | ||
} | ||
|
||
// async updateClockProgress( | ||
// file: TFile, | ||
// { steps, progress }: { steps?: number; progress?: number }, | ||
// ) { | ||
// const newProg = await clockUpdater( | ||
// vaultProcess(this.plugin.app, file.path), | ||
// (clockFile) => | ||
// clockFile.updatingClock((clock) => | ||
// progress != null ? clock.withProgress(progress) : clock.tick(steps!), | ||
// ), | ||
// ); | ||
|
||
// await this.renderCharacter(newProg, file); | ||
// } | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.iron-vault-character .assets { | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@import url("assets.css"); | ||
@import url("impacts.css"); | ||
@import url("meters.css"); | ||
@import url("special_tracks.css"); | ||
@import url("assets.css"); | ||
|
||
.iron-vault-character { | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.iron-vault-character .impacts { | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.iron-vault-character .meters { | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.iron-vault-character .special-tracks { | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.iron-vault-character .stats { | ||
} |
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.