Skip to content

Commit

Permalink
feat: adds text block type to layouts (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Jan 13, 2022
1 parent 860aeac commit 137566a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 51 deletions.
8 changes: 8 additions & 0 deletions src/settings/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ function blockGenerator(type: string): StatblockItem {
calculate: true
};
}
case "text": {
return {
type: "text",
id: nanoid(),
properties: [],
text: null
};
}
}
}

Expand Down
26 changes: 23 additions & 3 deletions src/settings/ui/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import type {
StatblockItem,
PropertyItem,
TableItem,
TraitsItem
TraitsItem,
TextItem
} from "src/data/constants";
import type StatBlockPlugin from "src/main";
import TableHeaders from "./TableHeaders.svelte";
Expand Down Expand Up @@ -96,7 +97,7 @@ export class BlockModal extends Modal {
);
});
}
if (this.block.type == "traits") {
if (this.block.type == "traits" || this.block.type == "text") {
new Setting(this.contentEl)
.setName("Section Heading")
.setDesc(
Expand Down Expand Up @@ -161,6 +162,25 @@ export class BlockModal extends Modal {
} */
}
if (!this.advanced) return;
if (this.block.type == "text") {
new Setting(el)
.setHeading()
.setName("Text to Show")
.setDesc(
createFragment((e) => {
e.createSpan({ text: "The block will " });
e.createEl("strong", { text: "always" });
e.createSpan({
text: " display the text entered here."
});
})
);
new TextAreaComponent(el)
.setValue(this.block.text)
.onChange((v) => {
(this.block as TextItem).text = v;
});
}
if (this.block.type == "property") {
new Setting(el)
.setHeading()
Expand Down Expand Up @@ -294,7 +314,7 @@ export class BlockModal extends Modal {
this.display();
});
});

this.buildProperties(this.contentEl.createDiv());
this.buildConditions(this.contentEl.createDiv());
this.buildDice(this.contentEl.createDiv());
Expand Down
107 changes: 59 additions & 48 deletions src/view/ui/Content.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import SectionHeading from "./SectionHeading.svelte";
import Subheading from "./Subheading.svelte";
import Table from "./Table.svelte";
import Text from "./Text.svelte";
import {
onMount,
createEventDispatcher,
Expand Down Expand Up @@ -65,6 +66,12 @@
}
targets.push(target);
switch (item.type) {
case "group": {
for (const nested of item.nested ?? []) {
targets.push(...getElementForStatblockItem(nested, target));
}
break;
}
case "heading": {
const heading = new Heading({
target,
Expand All @@ -78,6 +85,28 @@
heading.$on("export", (e) => dispatch("export", e.detail));
break;
}
case "inline": {
const inline = createDiv("statblock-item-inline");
for (const nested of item.nested ?? []) {
getElementForStatblockItem(
nested,
inline.createDiv("statblock-inline-item")
);
}
targets.push(inline);
break;
}
case "image": {
new Image({
target,
props: {
monster,
item
},
context
});
break;
}
case "property": {
new PropertyLine({
target,
Expand All @@ -100,37 +129,6 @@
});
break;
}
case "traits": {
const blocks: Trait[] = monster[item.properties[0]] as Trait[];
if (!Array.isArray(blocks) || !blocks.length) return [];
if (item.heading) {
new SectionHeading({
target,
props: {
header: item.heading
},
context
});
}
try {
for (const block of blocks) {
const prop = createDiv("statblock-item-container");
new PropertyBlock({
target: prop,
props: {
name: block.name,
desc: block.desc
},
context
});
targets.push(prop);
}
} catch (e) {
return [];
}
break;
}
case "spells": {
const blocks: Trait[] = monster[item.properties[0]] as Trait[];
if (!Array.isArray(blocks) || !blocks.length) return;
Expand Down Expand Up @@ -166,31 +164,44 @@
});
break;
}
case "image": {
new Image({
case "text": {
new Text({
target,
props: {
monster,
item
},
context
}
});
break;
}
case "inline": {
const inline = createDiv("statblock-item-inline");
for (const nested of item.nested ?? []) {
getElementForStatblockItem(
nested,
inline.createDiv("statblock-inline-item")
);
case "traits": {
const blocks: Trait[] = monster[item.properties[0]] as Trait[];
if (!Array.isArray(blocks) || !blocks.length) return [];
if (item.heading) {
new SectionHeading({
target,
props: {
header: item.heading
},
context
});
}
targets.push(inline);
break;
}
case "group": {
for (const nested of item.nested ?? []) {
targets.push(...getElementForStatblockItem(nested, target));
try {
for (const block of blocks) {
const prop = createDiv("statblock-item-container");
new PropertyBlock({
target: prop,
props: {
name: block.name,
desc: block.desc
},
context
});
targets.push(prop);
}
} catch (e) {
return [];
}
break;
}
Expand Down
30 changes: 30 additions & 0 deletions src/view/ui/Text.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script lang="ts">
import type { Monster } from "@types";
import type { TextItem } from "src/data/constants";
import { stringify } from "../utils";
import DiceHolder from "./DiceHolder.svelte";
import SectionHeading from "./SectionHeading.svelte";
export let monster: Monster;
export let item: TextItem;
let property =
item.text && item.text.length
? item.text
: stringify(monster[item.properties[0]]);
if (!item.conditioned && !`${property}`.length) {
property = item.fallback ?? "-";
}
</script>

{#if !item.conditioned || (item.conditioned && `${property}`.length)}
{#if item.heading}
<SectionHeading header={item.heading} />
{/if}
<div class="line">
<DiceHolder {property} />
</div>
{/if}

<style></style>

0 comments on commit 137566a

Please sign in to comment.