foundryvtt-reve-de-dragon/module/apps/rdd-text-roll-editor.js

78 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2025-01-12 20:00:13 +01:00
import "./xregexp-all.js";
import { SystemCompendiums } from "../settings/system-compendiums.js";
import { ACTOR_TYPES } from "../item.js";
import { TextRollAlchimie } from "./textroll/text-roll-alchimie.js";
import { TextRollCaracCompetence } from "./textroll/text-roll-carac-competence.js";
import { TextRollFormula } from "./textroll/text-roll-formula.js";
import { TextRollManager } from "./textroll/text-roll-formatter.js";
const TEXT_ROLL_MANAGERS = [
new TextRollAlchimie(),
new TextRollCaracCompetence(),
new TextRollFormula()]
2025-01-12 20:00:13 +01:00
export class RdDTextEditor {
2025-01-12 22:45:07 +01:00
static registerChatCallbacks(html) {
html.on("click", '.roll-text', async event => await RdDTextEditor.rollText(event))
}
2025-01-12 20:00:13 +01:00
static async enrichHTML(text, object, options = {showlink:true}) {
2025-01-12 20:00:13 +01:00
const context = {
text,
object,
2025-01-12 22:45:07 +01:00
options,
2025-01-12 20:00:13 +01:00
competences: await SystemCompendiums.getCompetences(ACTOR_TYPES.personnage),
}
for (let manager of TEXT_ROLL_MANAGERS) {
context.code = manager.code
context.template = manager.template
context.text = await manager.onReplaceRoll(context);
}
return await TextEditor.enrichHTML(context.text, {
relativeTo: object,
secrets: object?.isOwner,
async: true
})
}
static async _applyReplaceAll(manager, context) {
context.code = manager.code
context.template = manager.template
context.text = await manager.onReplaceRoll(context);
return context.text
}
static getEventElement(event) {
return $(event.currentTarget)?.parents(".roll-text-link");
}
static async rollText(event, actor) {
const code = TextRollManager.getNode(event)?.data('code')
const manager = TEXT_ROLL_MANAGERS.find(it => it.code == code)
if (manager) {
await manager.onRollText(event, actor)
}
}
static async chatRollText(event) {
const node = TextRollManager.getNode(event);
if (node) {
const code = node.data('code')
const param = node.data('json')
const manager = TEXT_ROLL_MANAGERS.find(it => it.code == code)
2025-01-12 22:45:07 +01:00
const text = await TextRollManager.createRollText(
{
code,
template: manager.template,
options: { showLink: false }
},
param)
2025-01-12 20:00:13 +01:00
ChatMessage.create({
content: text
})
}
}
}