foundryvtt-reve-de-dragon/module/actor/export-scriptarium/export-scriptarium.js

79 lines
2.8 KiB
JavaScript
Raw Normal View History

import { LOG_HEAD } from "../../constants.js"
import { ACTOR_TYPES } from "../../item.js"
import { Misc } from "../../misc.js"
import { EXPORT_CSV_SCRIPTARIUM, OptionsAvancees } from "../../settings/options-avancees.js"
import { Mapping } from "./mapping.js"
const IMG_SCRIPTARIUM = '<img class="context-menu-img" src="systems/foundryvtt-reve-de-dragon/styles/img/ui/scriptarium.svg">'
export class ExportScriptarium {
static init() {
ExportScriptarium.INSTANCE = new ExportScriptarium()
}
constructor() {
this.mapping = Mapping.getMapping()
Hooks.on("getActorDirectoryFolderContext", (actorDirectory, menus) => { ExportScriptarium.INSTANCE.onActorDirectoryMenu(actorDirectory, menus) })
Hooks.on("getActorDirectoryEntryContext", (actorDirectory, menus) => { ExportScriptarium.INSTANCE.onActorDirectoryMenu(actorDirectory, menus) })
}
onActorDirectoryMenu(actorDirectory, menus) {
menus.push({
name: 'Export Personnages',
icon: IMG_SCRIPTARIUM,
condition: (target) => game.user.isGM &&
OptionsAvancees.isUsing(EXPORT_CSV_SCRIPTARIUM) &&
this.$getActors(actorDirectory, target).length > 0,
2024-09-25 23:38:25 +02:00
callback: target => this.exportActors(this.$getActors(actorDirectory, target), this.$getTargetName(actorDirectory, target))
})
}
2024-09-25 23:38:25 +02:00
$getTargetName(actorDirectory, target) {
const li = target.closest(".directory-item")
const folderId = li.data("folderId")
const actorId = li.data("documentId")
return actorId
? game.actors.get(actorId).name
: actorDirectory.folders.find(it => it.id == folderId).name
}
$getActors(actorDirectory, target) {
const li = target.closest(".directory-item")
const folderId = li.data("folderId")
const actorId = li.data("documentId")
const actors = actorId
? [game.actors.get(actorId)]
: folderId
? actorDirectory.folders.find(it => it.id == folderId).contents
: []
return actors.filter(it => it.type == ACTOR_TYPES.personnage)
}
2024-09-25 23:38:25 +02:00
exportActors(actors, targetName) {
const eol = '\n\r'
const header = Misc.join(this.getHeaderLine(), ';')
2024-09-25 23:38:25 +02:00
const actorLines = actors.map(actor => Misc.join(this.getActorLine(actor), ';'))
const data = Misc.join([header, ...actorLines], eol)
const filename = `scriptatium-${targetName?.slugify()}.csv`;
saveDataToFile(data, "text/csv", `${filename}`);
}
getHeaderLine() {
return this.mapping.map(it => it.column)
}
getActorLine(actor) {
const context = Mapping.prepareContext(actor)
return this.mapping.map(it => it.getter(actor, context))
.map(it => this.$escapeQuotes(it))
.map(it => it.replaceAll("\n", " ").replaceAll("\r", ""))
}
$escapeQuotes(it) {
it = '' + it
if (it.includes('"') || it.includes(';')) {
return `"${it.replaceAll('"', '\\"')}"`
}
return it
}
}