Skip to content

Commit

Permalink
add TextItem to layout typings
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Jan 13, 2022
1 parent 1db68ab commit e0fec78
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 46 deletions.
69 changes: 51 additions & 18 deletions src/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,36 @@ export function nanoid() {
});
}

export type StatblockItemType =
| "traits"
| "heading"
| "subheading"
| "property"
| "table"
| "saves"
| "spells"
| "inline"
| "group"
| "image";
export const StatblockItemTypes = [
"traits",
"heading",
"subheading",
"property",
"table",
"saves",
"spells",
"inline",
"group",
"image",
"text"
] as const;

export const TypeNames: Array<[typeof StatblockItemTypes[number], string]> = [
["group", "Group"],
["heading", "Heading"],
["image", "Image"],
["inline", "Inline Group"],
["property", "Property Line"],
["saves", "Saves"],
["spells", "Spells"],
["subheading", "Subheading"],
["table", "Table"],
["text", "Text"],
["traits", "Traits"]
];

export type StatblockItemType = typeof StatblockItemTypes[number];

export interface DiceProps {
callback?: string;
property?: keyof Monster;
Expand All @@ -42,6 +61,7 @@ interface CommonProps {
diceProperty?: keyof Monster;
diceText?: string;
diceCallback?: string;
markdown?: boolean;
}

type GroupProps = {
Expand Down Expand Up @@ -73,6 +93,7 @@ type SubHeadingProps = {
type TableProps = {
type: "table";
headers: string[];
calculate: boolean;
};
type TraitsProps = {
type: "traits";
Expand All @@ -82,6 +103,11 @@ type ImageProps = {
type: "image";
heading?: string;
};
type TextProps = {
type: "text";
heading?: string;
text: string;
};

export type GroupItem = CommonProps & GroupProps;
export type HeadingItem = CommonProps & HeadingProps;
Expand All @@ -93,6 +119,8 @@ export type SpellsItem = CommonProps & SpellsProps;
export type SubHeadingItem = CommonProps & SubHeadingProps;
export type TableItem = CommonProps & TableProps;
export type ImageItem = CommonProps & ImageProps;
export type TextItem = CommonProps & TextProps;

export type StatblockItem =
| GroupItem
| HeadingItem
Expand All @@ -103,18 +131,22 @@ export type StatblockItem =
| SpellsItem
| SubHeadingItem
| TableItem
| ImageItem;
| ImageItem
| TextItem;

export interface StatblockRecord {
traits: TraitsItem;
export interface StatblockItemMap
extends Record<typeof StatblockItemTypes[number], StatblockItem> {
group: GroupItem;
heading: HeadingItem;
subheading: SubHeadingItem;
inline: InlineItem;
property: PropertyItem;
table: TableItem;
saves: SavesItem;
traits: TraitsItem;
spells: SpellsItem;
inline: InlineItem;
group: GroupItem;
subheading: SubHeadingItem;
table: TableItem;
image: ImageItem;
text: TextItem;
}

export interface Layout {
Expand Down Expand Up @@ -196,6 +228,7 @@ export const Statblock5e: StatblockItem[] = [
id: nanoid(),
properties: ["stats"],
headers: ["Str", "Dex", "Con", "Wis", "Int", "Cha"],
calculate: true,
hasRule: true,
conditioned: true
},
Expand Down
37 changes: 9 additions & 28 deletions src/settings/add.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageItem, nanoid } from "src/data/constants";
import { ImageItem, nanoid, TextItem, TypeNames } from "src/data/constants";
import type {
StatblockItemType,
GroupItem,
Expand All @@ -10,22 +10,15 @@ import type {
TraitsItem,
SpellsItem,
TableItem,
SubHeadingItem
SubHeadingItem,
StatblockItemMap
} from "src/data/constants";
import type StatBlockPlugin from "src/main";
import { Menu } from "obsidian";

function blockGenerator(type: "group"): GroupItem;
function blockGenerator(type: "heading"): HeadingItem;
function blockGenerator(type: "inline"): InlineItem;
function blockGenerator(type: "property"): PropertyItem;
function blockGenerator(type: "saves"): SavesItem;
function blockGenerator(type: "traits"): TraitsItem;
function blockGenerator(type: "spells"): SpellsItem;
function blockGenerator(type: "subheading"): SubHeadingItem;
function blockGenerator(type: "table"): TableItem;
function blockGenerator(type: "image"): ImageItem;
function blockGenerator(type: StatblockItemType): StatblockItem;
function blockGenerator<T extends keyof StatblockItemMap>(
type: T
): StatblockItemMap[T];
function blockGenerator(type: string): StatblockItem {
switch (type) {
case "inline":
Expand Down Expand Up @@ -91,32 +84,20 @@ function blockGenerator(type: string): StatblockItem {
type: "table",
id: nanoid(),
properties: [],
headers: []
headers: [],
calculate: true
};
}
}
}

const types: Array<[StatblockItemType, string]> = [
["group", "Group"],
["heading", "Heading"],
["image", "Image"],
["inline", "Inline Group"],
["property", "Property Line"],
["saves", "Saves"],
["spells", "Spells"],
["subheading", "Subheading"],
["table", "Table"],
["traits", "Traits"]
];

export const generate = async (
plugin: StatBlockPlugin,
evt: MouseEvent
): Promise<StatblockItem | void> => {
return new Promise((resolve, reject) => {
const addMenu = new Menu(plugin.app).setNoIcon();
types.forEach((type) => {
TypeNames.forEach((type) => {
addMenu.addItem((item) => {
item.setTitle(type[1]).onClick(() => {
const gen = blockGenerator(type[0]);
Expand Down

0 comments on commit e0fec78

Please sign in to comment.