From 5eedea5001e699c0c41e4ff23c65c04884b10418 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Thu, 20 Feb 2025 21:30:26 -0700 Subject: [PATCH] Add foundation for being able to create embedded items without requiring going through the sidebar --- module/Apps/GenericApp.mjs | 6 +++++- module/Apps/utils.mjs | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/module/Apps/GenericApp.mjs b/module/Apps/GenericApp.mjs index 392cb95..781603d 100644 --- a/module/Apps/GenericApp.mjs +++ b/module/Apps/GenericApp.mjs @@ -1,4 +1,4 @@ -import { deleteItemFromElement, editItemFromElement } from "./utils.mjs"; +import { createItemFromElement, deleteItemFromElement, editItemFromElement } from "./utils.mjs"; import { DicePool } from "./DicePool.mjs"; import { RichEditor } from "./RichEditor.mjs"; import { toBoolean } from "../consts.mjs"; @@ -16,6 +16,10 @@ export function GenericAppMixin(HandlebarsApp) { ], actions: { roll: this.#rollDice, + createItem: (_event, target) => { + const parent = this.document; + createItemFromElement(target, { parent }); + }, editItem: (_event, target) => editItemFromElement(target), deleteItem: (_event, target) => deleteItemFromElement(target), openRichEditor: this.#openRichEditor, diff --git a/module/Apps/utils.mjs b/module/Apps/utils.mjs index ff3d84a..1baee18 100644 --- a/module/Apps/utils.mjs +++ b/module/Apps/utils.mjs @@ -3,7 +3,24 @@ This file contains utilities used by Applications in order to be DRYer */ /** - * @param {HTMLElement} target The element that gets + * @param {HTMLElement} target The element to operate on + */ +export async function createItemFromElement(target, { parent } = {}) { + const data = target.dataset; + const types = data.itemTypes?.split(`,`); + const type = data.defaultItemType; + await Item.createDialog( + { type }, + { parent }, + { + types, + folders: [], + }, + ); +}; + +/** + * @param {HTMLElement} target The element to operate on */ export async function editItemFromElement(target) { const itemEl = target.closest(`[data-item-id]`); @@ -15,7 +32,7 @@ export async function editItemFromElement(target) { }; /** - * @param {HTMLElement} target The element that gets + * @param {HTMLElement} target The element to operate on */ export async function deleteItemFromElement(target) { const itemEl = target.closest(`[data-item-id]`);