Add and apply prettierrc

This commit is contained in:
Adam Goldsmith 2022-01-03 13:10:48 -05:00
parent 4ce42c929b
commit ce9b34b62f
3 changed files with 182 additions and 64 deletions

1
.prettierrc.json Normal file
View File

@ -0,0 +1 @@
{}

View File

@ -5,21 +5,20 @@
* in Tabletop Simulator * in Tabletop Simulator
*/ */
useLibrary('project'); useLibrary("project");
useLibrary('imageutils'); useLibrary("imageutils");
useLibrary('threads'); useLibrary("threads");
useLibrary('uilayout'); useLibrary("uilayout");
useLibrary('uicontrols'); useLibrary("uicontrols");
importClass(arkham.project.CopiesList); importClass(arkham.project.CopiesList);
const TTSJson = require('./TTSJson.js'); const TTSJson = require("./TTSJson.js");
const TTS_CARDS_PER_IMAGE = 69; const TTS_CARDS_PER_IMAGE = 69;
const TTS_MAX_ROWS = 7; const TTS_MAX_ROWS = 7;
const getName = () => "TTSDeck";
const getName = () => 'TTSDeck'; const getDescription = () => "Generates a TTS deck image and JSON file";
const getDescription = () => 'Generates a TTS deck image and JSON file';
const getVersion = () => 1.0; const getVersion = () => 1.0;
const getPluginType = () => arkham.plugins.Plugin.INJECTED; const getPluginType = () => arkham.plugins.Plugin.INJECTED;
@ -33,7 +32,7 @@ testProjectScript();
// TODO: allow setting a default copy count // TODO: allow setting a default copy count
// Hack to override the default return value of 1 // Hack to override the default return value of 1
function copyCount(copies_list, name) { function copyCount(copies_list, name) {
const entries = copies_list.getListEntries().map(x => String(x)); const entries = copies_list.getListEntries().map((x) => String(x));
if (entries.indexOf(String(name)) == -1) { if (entries.indexOf(String(name)) == -1) {
return 1; return 1;
} else { } else {
@ -42,9 +41,11 @@ function copyCount(copies_list, name) {
} }
function getImageFile(parent, format, page_num) { function getImageFile(parent, format, page_num) {
return new File(parent.file, parent.getName() + '_' + page_num + '.' + format); return new File(
}; parent.file,
parent.getName() + "_" + page_num + "." + format
);
}
function Card(member) { function Card(member) {
this.member = member; this.member = member;
@ -53,7 +54,10 @@ function Card(member) {
this.makeImageUncached = function makeImageUncached(resolution, back) { this.makeImageUncached = function makeImageUncached(resolution, back) {
println("Generating image for card ", this.member); println("Generating image for card ", this.member);
const sheets = this.component.createDefaultSheets(); const sheets = this.component.createDefaultSheets();
const card_image = sheets[back ? 1 : 0].paint(arkham.sheet.RenderTarget.EXPORT, resolution); const card_image = sheets[back ? 1 : 0].paint(
arkham.sheet.RenderTarget.EXPORT,
resolution
);
return card_image; return card_image;
}; };
@ -61,10 +65,16 @@ function Card(member) {
// export front face, or retrive it from a cached file // export front face, or retrive it from a cached file
// TODO: handle two-sided cards // TODO: handle two-sided cards
this.makeImage = function makeImage(format, resolution) { this.makeImage = function makeImage(format, resolution) {
const cache_dir = new File(this.member.parent.file, '.ttsdeck_cache'); const cache_dir = new File(this.member.parent.file, ".ttsdeck_cache");
const cached_file = new File(cache_dir, this.member.file.name + '.' + format); const cached_file = new File(
cache_dir,
this.member.file.name + "." + format
);
if (cached_file.exists() && cached_file.lastModified() > this.member.file.lastModified()) { if (
cached_file.exists() &&
cached_file.lastModified() > this.member.file.lastModified()
) {
println("Got cached image for card", this.member); println("Got cached image for card", this.member);
return ImageUtils.read(cached_file); return ImageUtils.read(cached_file);
} else { } else {
@ -78,8 +88,14 @@ function Card(member) {
}; };
} }
function TTSDeckPage(
function TTSDeckPage(busy_props, image_format, image_resolution, page_num, page_cards, copies_list) { busy_props,
image_format,
image_resolution,
page_num,
page_cards,
copies_list
) {
this.rows = Math.min(Math.ceil(Math.sqrt(page_cards.length)), TTS_MAX_ROWS); this.rows = Math.min(Math.ceil(Math.sqrt(page_cards.length)), TTS_MAX_ROWS);
this.columns = Math.ceil(page_cards.length / this.rows); this.columns = Math.ceil(page_cards.length / this.rows);
this.deck_image = null; this.deck_image = null;
@ -87,7 +103,11 @@ function TTSDeckPage(busy_props, image_format, image_resolution, page_num, page_
this.card_jsons = []; this.card_jsons = [];
for (let row = 0; row < this.rows; row++) { for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.columns && row * this.columns + col < page_cards.length; col++) { for (
let col = 0;
col < this.columns && row * this.columns + col < page_cards.length;
col++
) {
if (busy_props.cancelled) return; if (busy_props.cancelled) return;
let index = row * this.columns + col; let index = row * this.columns + col;
let card = page_cards[index]; let card = page_cards[index];
@ -98,45 +118,90 @@ function TTSDeckPage(busy_props, image_format, image_resolution, page_num, page_
let copies = copyCount(copies_list, card.baseName); let copies = copyCount(copies_list, card.baseName);
for (let ii = 0; ii < copies; ii++) { for (let ii = 0; ii < copies; ii++) {
this.card_jsons.push(TTSJson.makeCardJSON(page_num * 100 + index, card.component.getName())); this.card_jsons.push(
TTSJson.makeCardJSON(
page_num * 100 + index,
card.component.getName()
)
);
} }
let card_image = card.makeImage(image_format, image_resolution); let card_image = card.makeImage(image_format, image_resolution);
if (!this.deck_image) { if (!this.deck_image) {
this.deck_image = ImageUtils.create( this.deck_image = ImageUtils.create(
card_image.width * this.columns, card_image.height * this.rows, false); card_image.width * this.columns,
card_image.height * this.rows,
false
);
deck_graphics = this.deck_image.createGraphics(); deck_graphics = this.deck_image.createGraphics();
} }
deck_graphics.drawImage(card_image, col * card_image.width, row * card_image.height, null); deck_graphics.drawImage(
card_image,
col * card_image.width,
row * card_image.height,
null
);
} catch (ex) { } catch (ex) {
Thread.invokeLater(() => alert('Error while processing ' + card + ': ' + ex, true)); Thread.invokeLater(() =>
alert("Error while processing " + card + ": " + ex, true)
);
} }
} }
println("End of Row ", row); println("End of Row ", row);
} }
this.face_url = String(getImageFile(page_cards[0].member.parent, image_format, page_num).toPath().toUri()); this.face_url = String(
this.back_url = String(getImageFile(page_cards[0].member.parent, image_format, "back").toPath().toUri()); getImageFile(page_cards[0].member.parent, image_format, page_num)
.toPath()
.toUri()
);
this.back_url = String(
getImageFile(page_cards[0].member.parent, image_format, "back")
.toPath()
.toUri()
);
} }
function makeTTSDeck(busy_props, image_format, image_resolution, cards, copies_list) { function makeTTSDeck(
busy_props,
image_format,
image_resolution,
cards,
copies_list
) {
const pages = []; const pages = [];
busy_props.title = "Processing Cards"; busy_props.title = "Processing Cards";
busy_props.maximumProgress = cards.length; busy_props.maximumProgress = cards.length;
for (let page_num = 0; page_num * TTS_CARDS_PER_IMAGE < cards.length; page_num++) { for (
let page_cards = cards.slice(page_num * TTS_CARDS_PER_IMAGE, (page_num + 1) * TTS_CARDS_PER_IMAGE); let page_num = 0;
page_num * TTS_CARDS_PER_IMAGE < cards.length;
page_num++
) {
let page_cards = cards.slice(
page_num * TTS_CARDS_PER_IMAGE,
(page_num + 1) * TTS_CARDS_PER_IMAGE
);
printf("Making page %d, with %d cards:\n", page_num + 1, page_cards.length); printf("Making page %d, with %d cards:\n", page_num + 1, page_cards.length);
pages.push(new TTSDeckPage(busy_props, image_format, image_resolution, page_num + 1, page_cards, copies_list)); pages.push(
new TTSDeckPage(
busy_props,
image_format,
image_resolution,
page_num + 1,
page_cards,
copies_list
)
);
if (busy_props.cancelled) return [,]; if (busy_props.cancelled) return [,];
} }
const deck_json = TTSJson.makeDeckJSON(pages); const deck_json = TTSJson.makeDeckJSON(pages);
return [deck_json, pages.map(page => page.deck_image)]; return [deck_json, pages.map((page) => page.deck_image)];
} }
function settingsDialog(deck_task) { function settingsDialog(deck_task) {
@ -144,17 +209,23 @@ function settingsDialog(deck_task) {
const image_format_field = comboBox([ const image_format_field = comboBox([
ImageUtils.FORMAT_JPEG, ImageUtils.FORMAT_JPEG,
ImageUtils.FORMAT_PNG ImageUtils.FORMAT_PNG,
]); ]);
image_format_field.setSelectedItem(task_settings.get("tts_image_format", "jpg")); image_format_field.setSelectedItem(
const resolution_field = textField(task_settings.get("tts_image_resolution", "200"), 15); task_settings.get("tts_image_format", "jpg")
);
const resolution_field = textField(
task_settings.get("tts_image_resolution", "200"),
15
);
const clear_cache_button = button("Clear Cache", undefined, function (e) { const clear_cache_button = button("Clear Cache", undefined, function (e) {
const cache_dir = new File(deck_task.file, '.ttsdeck_cache'); const cache_dir = new File(deck_task.file, ".ttsdeck_cache");
cache_dir.listFiles().forEach((file) => file.delete()); cache_dir.listFiles().forEach((file) => file.delete());
}); });
const panel = new Grid(); const panel = new Grid();
// prettier-ignore
panel.place( panel.place(
"Image Format", "", "Image Format", "",
image_format_field, "grow,span", image_format_field, "grow,span",
@ -162,28 +233,33 @@ function settingsDialog(deck_task) {
resolution_field, "grow,span", resolution_field, "grow,span",
clear_cache_button, "grow,span" clear_cache_button, "grow,span"
); );
const close_button = panel.createDialog('TTS Export').showDialog(); const close_button = panel.createDialog("TTS Export").showDialog();
return [close_button, image_format_field.getSelectedItem(), Number(resolution_field.text)]; return [
close_button,
image_format_field.getSelectedItem(),
Number(resolution_field.text),
];
} }
function run() { function run() {
const ttsDeckAction = JavaAdapter(TaskAction, { const ttsDeckAction = JavaAdapter(TaskAction, {
getLabel: () => 'Generate TTS Deck', getLabel: () => "Generate TTS Deck",
getActionName: () => 'ttsdeck', getActionName: () => "ttsdeck",
// Applies to Deck Tasks // Applies to Deck Tasks
appliesTo: function appliesTo(project, task, member) { appliesTo: function appliesTo(project, task, member) {
if (member != null || task == null) { if (member != null || task == null) {
return false;
}
const type = task.settings.get(Task.KEY_TYPE);
if (NewTaskType.DECK_TYPE.equals(type)) {
return true;
}
return false; return false;
}
const type = task.settings.get(Task.KEY_TYPE);
if (NewTaskType.DECK_TYPE.equals(type)) {
return true;
}
return false;
}, },
perform: function perform(project, task, member) { perform: function perform(project, task, member) {
let deck_task = ProjectUtilities.simplify(project, task, member); let deck_task = ProjectUtilities.simplify(project, task, member);
const [close_button, image_format, image_resolution] = settingsDialog(deck_task); const [close_button, image_format, image_resolution] =
settingsDialog(deck_task);
// User canceled the dialog or closed it without pressing ok // User canceled the dialog or closed it without pressing ok
if (close_button != 1) { if (close_button != 1) {
@ -198,28 +274,43 @@ function run() {
Eons.setWaitCursor(true); Eons.setWaitCursor(true);
try { try {
Thread.busyWindow( Thread.busyWindow(
(busy_props) => this.performImpl(busy_props, image_format, image_resolution, deck_task), (busy_props) =>
'Setting up...', this.performImpl(
true); busy_props,
image_format,
image_resolution,
deck_task
),
"Setting up...",
true
);
} catch (ex) { } catch (ex) {
Error.handleUncaught(ex); Error.handleUncaught(ex);
} finally { } finally {
Eons.setWaitCursor(false); Eons.setWaitCursor(false);
} }
}, },
performImpl: function performImpl(busy_props, image_format, image_resolution, member) { performImpl: function performImpl(
busy_props,
image_format,
image_resolution,
member
) {
let copies_list; let copies_list;
try { try {
copies_list = new CopiesList(member); copies_list = new CopiesList(member);
} catch (ex) { } catch (ex) {
copies_list = new CopiesList(); copies_list = new CopiesList();
alert("unable to read copies list, using card count of 2 for all files", true); alert(
"unable to read copies list, using card count of 2 for all files",
true
);
} }
const children = member.getChildren(); const children = member.getChildren();
const cards = children const cards = children
.map(child => { .map((child) => {
if (ProjectUtilities.matchExtension(child, 'eon')) { if (ProjectUtilities.matchExtension(child, "eon")) {
let card = new Card(child); let card = new Card(child);
if (card.component.isDeckLayoutSupported()) { if (card.component.isDeckLayoutSupported()) {
return card; return card;
@ -227,32 +318,58 @@ function run() {
} }
return undefined; return undefined;
}) })
.filter(card => card !== undefined); .filter((card) => card !== undefined);
const [deck_json, deck_images] = makeTTSDeck(busy_props, image_format, image_resolution, cards, copies_list); const [deck_json, deck_images] = makeTTSDeck(
busy_props,
image_format,
image_resolution,
cards,
copies_list
);
if (busy_props.cancelled) return; if (busy_props.cancelled) return;
const saved_object = TTSJson.makeSavedObjectJSON([deck_json], member.getName()); const saved_object = TTSJson.makeSavedObjectJSON(
[deck_json],
member.getName()
);
busy_props.status = ""; busy_props.status = "";
busy_props.maximumProgress = -1; busy_props.maximumProgress = -1;
busy_props.title = "Writing JSON"; busy_props.title = "Writing JSON";
const json_file = new File(member.file, member.getName() + '.json'); const json_file = new File(member.file, member.getName() + ".json");
ProjectUtilities.writeTextFile(json_file, JSON.stringify(saved_object, null, 4)); ProjectUtilities.writeTextFile(
json_file,
JSON.stringify(saved_object, null, 4)
);
busy_props.title = "Writing Images"; busy_props.title = "Writing Images";
busy_props.maximumProgress = deck_images.length; busy_props.maximumProgress = deck_images.length;
deck_images.forEach((deck_image, index) => { deck_images.forEach((deck_image, index) => {
busy_props.currentProgress = index; busy_props.currentProgress = index;
const image_file = getImageFile(member, image_format, index + 1); const image_file = getImageFile(member, image_format, index + 1);
ImageUtils.write(deck_image, image_file, image_format, -1, false, image_resolution); ImageUtils.write(
deck_image,
image_file,
image_format,
-1,
false,
image_resolution
);
}); });
let back_image = cards[0].makeImageUncached(image_resolution, true); let back_image = cards[0].makeImageUncached(image_resolution, true);
const back_image_file = getImageFile(member, image_format, "back"); const back_image_file = getImageFile(member, image_format, "back");
ImageUtils.write(back_image, back_image_file, image_format, -1, false, image_resolution); ImageUtils.write(
back_image,
back_image_file,
image_format,
-1,
false,
image_resolution
);
member.synchronize(); member.synchronize();
} },
}); });
ActionRegistry.register(ttsDeckAction, Actions.PRIORITY_IMPORT_EXPORT); ActionRegistry.register(ttsDeckAction, Actions.PRIORITY_IMPORT_EXPORT);

View File

@ -57,7 +57,7 @@ exports.makeDeckJSON = function makeDeckJSON(pages, nickname, description) {
Locked: false, Locked: false,
SidewaysCard: false, SidewaysCard: false,
DeckIDs: pages DeckIDs: pages
.map(page => page.card_jsons.map(card => card.CardID)) .map((page) => page.card_jsons.map((card) => card.CardID))
.reduce((acc, val) => acc.concat(val), []), .reduce((acc, val) => acc.concat(val), []),
CustomDeck: pages.reduce((acc, page, index) => { CustomDeck: pages.reduce((acc, page, index) => {
acc[String(index + 1)] = { acc[String(index + 1)] = {
@ -70,7 +70,7 @@ exports.makeDeckJSON = function makeDeckJSON(pages, nickname, description) {
return acc; return acc;
}, {}), }, {}),
ContainedObjects: pages ContainedObjects: pages
.map(page => page.card_jsons) .map((page) => page.card_jsons)
.reduce((acc, val) => acc.concat(val), []), .reduce((acc, val) => acc.concat(val), []),
}; };
}; };