-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathactorviewer.js
191 lines (166 loc) · 5.79 KB
/
actorviewer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import SilentFilePicker from "./customFilepickers/foundryFilePicker.js";
let filePath = window.location.href.replace("/game", "");
Hooks.once("init", () => {
game.settings.register("externalactor", "systemSite", {
scope: "client",
type: String,
default: "https://ardittristan.github.io/VTTExternalActorSite/",
config: false,
});
game.settings.register("externalactor", "compatMode", {
scope: "world",
type: Boolean,
default: false,
config: true,
name: "Enable performance mode.",
hint:
"If this module causes performance loss on startup. You can disable certain features with this option. Keep in mind that this'll mean that some data doesn't get exported.",
});
});
Hooks.once("setup", async () => {
await FilePicker.createDirectory("data", "actorAPI", {}).catch(() => {});
const hookNotExecuted = Hooks.call("actorViewerGenerate");
if (hookNotExecuted) {
console.warn("ActorViewer | No settings found for this system.");
let actors = {};
game.actors.forEach((actor) => {
const compatMode = game.settings.get("externalactor", "compatMode");
let items = [];
if (!compatMode) {
if (game.user.isGM) {
actor.setFlag("externalactor", "classLabels", actor.itemTypes.class.map((c) => c.name).join(", "));
}
actor.items.forEach((item) => {
if (game.user.isGM) {
item.setFlag("externalactor", "labels", item.labels);
}
items.push(item.data);
});
}
actors[actor.id] = JSON.parse(JSON.stringify(actor.data));
if (!compatMode) {
actors[actor.id].items = JSON.parse(JSON.stringify(items));
}
});
// create json files
ActorViewer.createActorsFile(actors);
ActorViewer.createWorldsFile();
// set application button url
game.settings.set("externalactor", "systemSite", "https://ardittristan.github.io/VTTExternalActorSite/");
}
});
Hooks.on("renderActorSheet", (sheet, html) => {
jQuery('<a class="character-id"><i class="fas fa-link"></i>Get id</a>').insertAfter(html.find(".window-title"));
html.find(".character-id").on("click", () => {
if (filePath.includes("https://")) {
new CopyPopupApplication(filePath + sheet.actor.id).render(true);
} else {
new CopyPopupApplication(`${window.location.href.replace("/game", "")}/actorAPI/${game.world.data.name}-actors.json${sheet.actor.id}`).render(true);
}
});
});
class CopyPopupApplication extends Application {
constructor(url, options = {}) {
super(options);
this.url = url;
}
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
id: "copyPopup",
title: game.i18n.localize("actorViewer.actorUrl"),
template: "modules/externalactor/templates/copyPopup.html",
classes: ["copy-url-window"],
resizable: false,
});
}
getData() {
return {
url: this.url,
};
}
/**
* @param {JQuery} html
*/
activateListeners(html) {
super.activateListeners(html);
html.find(".close").on("click", () => {
this.close();
});
html.find(".sendToApp").on("click", () => {
Object.assign(document.createElement("a"), { target: "_blank", href: game.settings.get("externalactor", "systemSite") + "?" + this.url }).click();
});
html.find(".copyButton").on("click", () => {
copyToClipboard(this.url);
});
}
}
/**
* @param {String} fileName
* @param {String} worldName
* @param {String} content
*/
async function createJsonFile(fileName, content) {
const file = new File([content], fileName, { type: "application/json", lastModified: Date.now() });
let response = await upload("data", "actorAPI", file, {});
filePath = response.path;
console.log('ActorViewer |', response);
}
function copyToClipboard(text) {
const listener = function (ev) {
ev.preventDefault();
ev.clipboardData.setData("text/plain", text);
};
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
ui.notifications.info(game.i18n.localize("actorViewer.copied"));
}
/**
* @param {Actor[]} actors
*/
function createActorsFile(actors) {
createJsonFile(`${game.world.data.name}-actors.json`, JSON.stringify(actors));
}
/**
* Create or update the worlds.json file
*/
function createWorldsFile() {
let worlds = [];
const world = {'name': game.world.data.name, 'title': game.world.data.title, 'system': game.world.data.system};
console.debug('ActorViewer |', 'Checking for existing worlds.json');
fetch(`${window.location.href.replace("/game", "")}/actorAPI/worlds.json`)
.then((response) => response.json())
.then((data) => {
console.debug('ActorViewer |', 'Existing worlds.json data', data);
worlds = data;
if (!worlds.some(w => w.name === game.world.data.name)) {
worlds.push(world);
console.debug('ActorViewer |', 'Writing data to worlds.json', worlds);
createJsonFile('worlds.json', JSON.stringify(worlds));
}
})
.catch(() => {
console.debug('ActorViewer |', 'Creating worlds.json');
worlds.push(world);
console.debug('ActorViewer |', 'Writing data to existing worlds.json', worlds);
createJsonFile('worlds.json', JSON.stringify(worlds));
});
}
/**
* @type {FilePicker.upload}
*
* @returns {Promise}
*/
async function upload(source, path, file, options) {
if (typeof ForgeVTT_FilePicker !== "undefined") {
const SilentForgeFilePicker = (await import("./customFilepickers/forgeFilePicker.js")).default;
return await SilentForgeFilePicker.upload(source, path, file, options);
} else {
return await SilentFilePicker.upload(source, path, file, options);
}
}
globalThis.ActorViewer = {
createActorsFile: createActorsFile,
createWorldsFile: createWorldsFile,
copyToClipboard: copyToClipboard,
};