Ajouter de "jet de dés" dans les descriptions,

On peut maintenant ajouter des liens dans les descriptions
(acteurs, items) et autres champs similaires.
This commit is contained in:
2025-01-11 00:44:59 +01:00
parent 785bd4b9ce
commit b87f406093
15 changed files with 169 additions and 68 deletions

View File

@ -0,0 +1,54 @@
import "./xregexp-all.js";
import { RdDCarac } from "../rdd-carac.js";
import { SystemCompendiums } from "../settings/system-compendiums.js";
import { RdDItemCompetence } from "../item-competence.js";
import { ACTOR_TYPES } from "../item.js";
const XREGEXP_ROLL = XRegExp("@roll\\[(?<carac>[A-Za-zÀ-ÖØ-öø-ÿ\\s\\-]+)(\\/(?<competence>[A-Za-zÀ-ÖØ-öø-ÿ\\s\\-]+))?(/(?<diff>[\\+\\-]?\\d+))?\\]", 'giu')
export class RdDTextEditor {
static async enrichHTML(text) {
const rddTextEditor = new RdDTextEditor(text)
const replacedRolls = await rddTextEditor.replaceRolls()
return await TextEditor.enrichHTML(replacedRolls, { async: true })
}
constructor(text) {
this.original = text
}
async replaceRolls() {
if (!this.updated) {
this.updated = this.original
await XRegExp.forEach(this.original, XREGEXP_ROLL, async (rollMatch, i) => await this._replaceOneRoll(rollMatch))
}
return this.updated
}
async _replaceOneRoll(rollMatch) {
const carac = RdDCarac.caracDetails(rollMatch.carac);
const competence = rollMatch.competence ? RdDItemCompetence.findCompetence(await SystemCompendiums.getCompetences(ACTOR_TYPES.personnage),
rollMatch.competence) : undefined
if (carac) {
const replacement = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/apps/link-text-roll.hbs`, {
carac: carac,
competence: competence?.name,
diff: rollMatch.diff
});
this.updated = this.updated.replace(rollMatch[0], replacement);
}
}
static async rollText(event, actor) {
const caracCode = event.currentTarget.attributes['data-carac-code'].value;
const competence = event.currentTarget.attributes['data-competence']?.value;
const diff = event.currentTarget.attributes['data-diff']?.value
const path = RdDCarac.caracDetails(caracCode)?.path
const actors = actor ? [actor] : canvas.tokens.controlled.map(it => it.actor).filter(it => it)
actors.filter(it => foundry.utils.getProperty(it, path) != undefined)
.forEach(it => it.doRollCaracCompetence(caracCode, competence, diff))
}
}

View File

@ -2002,7 +2002,7 @@ XRegExp.exec = function (str, regex, pos, sticky) {
*/
XRegExp.forEach = function (str, regex, callback) {
XRegExp.forEach = async function (str, regex, callback) {
var pos = 0;
var i = -1;
var match;
@ -2014,7 +2014,7 @@ XRegExp.forEach = function (str, regex, callback) {
// at least. Actually, because of the way `XRegExp.exec` caches globalized versions of
// regexes, mutating the regex will not have any effect on the iteration or matched strings,
// which is a nice side effect that brings extra safety.
callback(match, ++i, str, regex);
await callback(match, ++i, str, regex);
pos = match.index + (match[0].length || 1);
}
};