Skip to content

Commit

Permalink
2.0.3
Browse files Browse the repository at this point in the history
- fixed issue with viewing statblock in settings
- big update to dice parsing
- can now pass dice parsing callback to return dice options
- edit block modal is done
  • Loading branch information
valentine195 committed Dec 8, 2021
1 parent af8421e commit f121c57
Show file tree
Hide file tree
Showing 28 changed files with 586 additions and 381 deletions.
12 changes: 0 additions & 12 deletions @types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ export interface Monster {
layout?: string;
}

/* export interface StatblockMonster
extends Omit<
Monster,
"traits" | "actions" | "legendary_actions" | "reactions"
> {
traits: Map<string, Trait>;
actions: Map<string, Trait>;
legendary_actions: Map<string, Trait>;
reactions: Map<string, Trait>;
monster?: string;
} */

export type Spell = string | { [key: string]: string };

export interface Trait {
Expand Down
62 changes: 30 additions & 32 deletions src/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ export type StatblockItemType =
| "spells"
| "inline"
| "group";

export interface DiceProps {
callback?: string;
property?: keyof Monster;
text?: string;
parse?: boolean;
}
interface CommonProps {
type: StatblockItemType;
id: string;
properties: Array<keyof Monster>;
conditioned?: boolean;
fallback?: string;
hasRule?: boolean;
dice?: {
property?: keyof Monster;
parse?: boolean;
};
dice?: boolean;
diceProperty?: keyof Monster;
diceText?: string;
diceCallback?: string;
}

type GroupProps = {
Expand Down Expand Up @@ -150,10 +155,9 @@ export const Statblock5e: StatblockItem[] = [
id: nanoid(),
properties: ["hp"],
display: "Hit Points",
dice: {
property: "hit_dice",
parse: true
},
dice: true,
diceProperty: "hit_dice",
diceCallback: `return [{ text: monster["hit_dice"] }]`,
conditioned: true
},
{
Expand Down Expand Up @@ -197,20 +201,15 @@ export const Statblock5e: StatblockItem[] = [
id: nanoid(),
display: "Saves",
properties: ["saves"],
conditioned: true,
dice: {
parse: true
}
conditioned: true
/* dice: true */
},
{
type: "saves",
id: nanoid(),
display: "Skills",
properties: ["skillsaves"],
conditioned: true,
dice: {
parse: true
}
conditioned: true
},
{
type: "property",
Expand Down Expand Up @@ -296,48 +295,47 @@ return "";`
id: nanoid(),
properties: ["traits"],
conditioned: true,
dice: {
parse: true
}
dice: true,
diceCallback: `return plugin.parseForDice(property);`
},
{
type: "spells",
id: nanoid(),
properties: ["spells"],
conditioned: true,
dice: {
parse: true
}

dice: true,
diceCallback: `return plugin.parseForDice(property);`
},
{
type: "traits",
id: nanoid(),
properties: ["actions"],
heading: "Actions",
conditioned: true,
dice: {
parse: true
}

dice: true,
diceCallback: `return plugin.parseForDice(property);`
},
{
type: "traits",
id: nanoid(),
properties: ["legendary_actions"],
heading: "Legendary Actions",
conditioned: true,
dice: {
parse: true
}

dice: true,
diceCallback: `return plugin.parseForDice(property);`
},
{
type: "traits",
id: nanoid(),
properties: ["reactions"],
heading: "Reactions",
conditioned: true,
dice: {
parse: true
}

dice: true,
diceCallback: `return plugin.parseForDice(property);`
}
];

Expand Down
80 changes: 69 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ import StatblockSettingTab from "./settings/settings";
import "./main.css";
import { sort } from "fast-sort";
declare module "obsidian" {
interface App {
plugins: {
plugins: {
"obsidian-dice-roller": DiceRollerPlugin;
};
};
}
interface Workspace {
on(
name: "dice-roller:rendered-result",
Expand All @@ -43,6 +50,7 @@ export interface StatblockData {
useDice: boolean;
renderDice: boolean;
export: boolean;
showAdvanced: boolean;
version: {
major: number;
minor: number;
Expand All @@ -57,23 +65,14 @@ const DEFAULT_DATA: StatblockData = {
useDice: true,
renderDice: false,
export: true,
showAdvanced: false,
version: {
major: null,
minor: null,
patch: null
}
};

declare module "obsidian" {
interface App {
plugins: {
plugins: {
"obsidian-dice-roller": DiceRollerPlugin;
};
};
}
}

export default class StatBlockPlugin extends Plugin {
settings: StatblockData;
data: Map<string, Monster>;
Expand Down Expand Up @@ -115,7 +114,7 @@ export default class StatBlockPlugin extends Plugin {
);
addIcon(
"statblock-conditioned",
`<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="question-circle" class="svg-inline--fa fa-question-circle fa-w-16" role="img" viewBox="0 0 512 512"><path fill="currentColor" d="M504 256c0 136.997-111.043 248-248 248S8 392.997 8 256C8 119.083 119.043 8 256 8s248 111.083 248 248zM262.655 90c-54.497 0-89.255 22.957-116.549 63.758-3.536 5.286-2.353 12.415 2.715 16.258l34.699 26.31c5.205 3.947 12.621 3.008 16.665-2.122 17.864-22.658 30.113-35.797 57.303-35.797 20.429 0 45.698 13.148 45.698 32.958 0 14.976-12.363 22.667-32.534 33.976C247.128 238.528 216 254.941 216 296v4c0 6.627 5.373 12 12 12h56c6.627 0 12-5.373 12-12v-1.333c0-28.462 83.186-29.647 83.186-106.667 0-58.002-60.165-102-116.531-102zM256 338c-25.365 0-46 20.635-46 46 0 25.364 20.635 46 46 46s46-20.636 46-46c0-25.365-20.635-46-46-46z"/></svg>`
`<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" data-prefix="far" data-icon="question-circle" class="svg-inline--fa fa-question-circle fa-w-16" role="img" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 448c-110.532 0-200-89.431-200-200 0-110.495 89.472-200 200-200 110.491 0 200 89.471 200 200 0 110.53-89.431 200-200 200zm107.244-255.2c0 67.052-72.421 68.084-72.421 92.863V300c0 6.627-5.373 12-12 12h-45.647c-6.627 0-12-5.373-12-12v-8.659c0-35.745 27.1-50.034 47.579-61.516 17.561-9.845 28.324-16.541 28.324-29.579 0-17.246-21.999-28.693-39.784-28.693-23.189 0-33.894 10.977-48.942 29.969-4.057 5.12-11.46 6.071-16.666 2.124l-27.824-21.098c-5.107-3.872-6.251-11.066-2.644-16.363C184.846 131.491 214.94 112 261.794 112c49.071 0 101.45 38.304 101.45 88.8zM298 368c0 23.159-18.841 42-42 42s-42-18.841-42-42 18.841-42 42-42 42 18.841 42 42z"/></svg>`
);
addIcon(
"dice-roller-dice",
Expand Down Expand Up @@ -278,6 +277,65 @@ export default class StatBlockPlugin extends Plugin {
});
}

parseForDice(property: string) {
const roller = (str: string) => {
let text: string;
let original: string;
if (/\w+ [\+\-]\d+/.test(str.trim())) {
let [, save, sign, number] =
str.match(/(\w+ )([\+\-])(\d+)/) ?? [];
let mult = 1;
if (sign === "-") {
mult = -1;
}
if (!isNaN(Number(number))) {
text = `1d20+${mult * Number(number)}`;
original = `${save} ${sign}${number}`;
}
} else if (/[\+\-]\d+ to hit/.test(str.trim())) {
let [, sign, number] = str.match(/([\+\-])(\d+)/) ?? [];

let mult = 1;
if (sign === "-") {
mult = -1;
}
if (!isNaN(Number(number))) {
text = `1d20+${mult * Number(number)}`;
original = str;
}
} else if (/\d+\s\(\d+d\d+(?:\s*[+\-]\s*\d+)?\)/.test(str.trim())) {
let [, base, dice] =
str.match(/(\d+)\s\((\d+d\d+(?:\s*[+\-]\s*\d+)?)\)/) ?? [];
if (!isNaN(Number(base)) && dice) {
text = dice;
}
}
return { text, original };
};

const match = (str: string) => {
return (
/\w+ [\+\-]\d+/.test(str.trim()) ||
/[\+\-]\d+ to hit/.test(str.trim()) ||
/\d+\s\(\d+d\d+(?:\s*[+\-]\s*\d+)?\)/.test(str.trim())
);
};

return property
.split(
/([\+\-]\d+ to hit|\d+\s\(\d+d\d+(?:\s*[+\-]\s*\d+)?\)|\w+ [\+\-]\d+)/
)
.map((v) => (match(v) ? roller(v) : v));
}

get defaultLayout() {
return (
this.settings.layouts?.find(
(layout) => layout.name == this.settings.default
) ?? Layout5e
);
}

async postprocessor(
source: string,
el: HTMLElement,
Expand Down
12 changes: 8 additions & 4 deletions src/settings/StatblockCreator.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import { createEventDispatcher } from "svelte";
import { generate } from "./add";
import AddButton from "./ui/AddButton.svelte";
import Creator from "./ui/Creator.svelte";
export let layout: Layout;
Expand Down Expand Up @@ -90,15 +89,20 @@
});
};
const add = async (e: CustomEvent<MouseEvent>) => {
const block = await generate(plugin, e.detail);
const add = async (e: MouseEvent) => {
const block = await generate(plugin, e);
if (block) layout.blocks = [...layout.blocks, block];
};
const addButton = (node: HTMLDivElement) => {
new ExtraButtonComponent(node)
.setIcon("plus-with-circle")
.setTooltip("Add Block");
};
</script>

<div class="top">
<div class="name" use:name />
<AddButton {plugin} on:add={add} />
<div class="add" use:addButton on:click={(evt) => add(evt)} />
</div>
<div class="creator-container">
{#key layout}
Expand Down
2 changes: 1 addition & 1 deletion src/settings/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function blockGenerator(type: string): StatblockItem {
case "inline":
case "group": {
return {
type: "group",
type: type,
id: nanoid(),
properties: [],
nested: []
Expand Down
3 changes: 2 additions & 1 deletion src/settings/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
.statblock-additional-container
> .additional
> .setting-item {
padding-top: 0rem;
color: var(--text-muted);
}
.statblock-edit-block .statblock-additional-container > :not(.additional) {
Expand All @@ -19,6 +18,8 @@

.statblock-edit-block .setting-item {
border: 0;

padding-top: 0rem;
}

.statblock-edit-block textarea {
Expand Down
11 changes: 11 additions & 0 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,17 @@ export default class StatblockSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
});
});
new Setting(statblockAdditional)
.setName("Show Advanced Options")
.setDesc("Show advanced options when editing layout blocks.")
.addToggle((t) =>
t
.setValue(this.plugin.settings.showAdvanced)
.onChange(async (v) => {
this.plugin.settings.showAdvanced = v;
await this.plugin.saveSettings();
})
);

const layoutContainer =
statblockCreatorContainer.createDiv("additional");
Expand Down
23 changes: 0 additions & 23 deletions src/settings/ui/AddButton.svelte

This file was deleted.

3 changes: 1 addition & 2 deletions src/settings/ui/Block.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<script lang="ts">
import type { StatblockItem } from "src/data/constants";
import PropertyBlock from "./Blocks/PropertyBlock.svelte";
import PropertyBlock from "./PropertyBlock.svelte";
import type StatBlockPlugin from "src/main";
import { ExtraButtonComponent } from "obsidian";
import { createEventDispatcher } from "svelte";
import { BlockModal } from "./block";
import Rule from "src/view/ui/Rule.svelte";
export let block: StatblockItem;
export let plugin: StatBlockPlugin;
Expand Down
Loading

0 comments on commit f121c57

Please sign in to comment.