Skip to content

Commit

Permalink
Add support for max size in preview
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Aug 9, 2024
1 parent 56bc11b commit e654e7d
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/editor/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ const sizeInputs = /** @type {NodeListOf<HTMLInputElement>} */ (
);
const jsonPreview = document.querySelector('.mask__json-view__preview');

sizeInputs.forEach((inputSize) => {
inputSize.addEventListener('change', handleClickSizeOption);
});

/**
* Returns selected sizes
*/
Expand All @@ -21,21 +17,17 @@ function getFormSizesValues() {
return exportSizes;
}

function handleClickSizeOption() {
const exportSizes = getFormSizesValues()
.filter((size) => !!size)
.map((size) => ({
purpose: 'maskable',
sizes: `${size}x${size}`,
src: `maskable_icon_x${size}.png`,
type: 'image/png',
}));

jsonPreview.textContent = JSON.stringify(exportSizes, null, 2);
}
/**
* Get the suggested file name for the given maskable icon size.
* @param {number | undefined} size Size of the maskable icon, or `undefined` for max size.
*/
const fileName = (size) =>
size != undefined ? `maskable_icon_x${size}.png` : 'maskable_icon.png';

/**
* @param {File | string} value
* @returns {number | undefined} Number for one of the checkboxes in the second row,
* or `undefined` for max size.
*/
function toSize(value) {
if (value instanceof File) {
Expand All @@ -49,6 +41,24 @@ function toSize(value) {
}
}

/**
* Updates Web App Manifest JSON preview based on selected sizes.
* @param {import('./canvas.js').CanvasController} controller
*/
function updateJsonPreview(controller) {
const exportSizes = getFormSizesValues().map((size) => {
const pixelSize = size ?? controller.getSize();
return {
purpose: 'maskable',
sizes: `${pixelSize}x${pixelSize}`,
src: fileName(size),
type: 'image/png',
};
});

jsonPreview.textContent = JSON.stringify(exportSizes, null, 2);
}

/**
* Enables/disables export size checkboxes based on the biggest layer.
* @param {import('./canvas.js').CanvasController} controller
Expand Down Expand Up @@ -78,8 +88,7 @@ async function download(controller) {

const a = document.createElement('a');
a.href = url;
a.download =
size != undefined ? `maskable_icon_x${size}.png` : 'maskable_icon.png';
a.download = fileName(size);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
Expand Down Expand Up @@ -111,11 +120,18 @@ export function setupExportDialog(controller) {
download(controller);
}

function handleChange() {
updateJsonPreview(controller);
}

updateExportSizes(controller);
updateJsonPreview(controller);
sizes.addEventListener('submit', handleSubmit);
sizes.addEventListener('change', handleChange);

return function cleanup() {
sizes.removeEventListener('submit', handleSubmit);
sizes.removeEventListener('change', handleChange);

jsonPreview.textContent = 'Select some size to display the JSON preview';

Expand Down

0 comments on commit e654e7d

Please sign in to comment.