From fe4698aedc9087c8fc779dc918f38a4f73f8285f Mon Sep 17 00:00:00 2001 From: Luna D Date: Sat, 8 Jun 2024 11:58:11 -0400 Subject: [PATCH] Convert gallery rearrangement scripts to TypeScript --- assets/js/{galleries.js => galleries.ts} | 21 +++++++++++++-------- assets/types/booru-object.d.ts | 4 ++++ 2 files changed, 17 insertions(+), 8 deletions(-) rename assets/js/{galleries.js => galleries.ts} (52%) diff --git a/assets/js/galleries.js b/assets/js/galleries.ts similarity index 52% rename from assets/js/galleries.js rename to assets/js/galleries.ts index 6cf97865..1556d8a0 100644 --- a/assets/js/galleries.js +++ b/assets/js/galleries.ts @@ -3,6 +3,7 @@ */ import { arraysEqual } from './utils/array'; +import { assertNotNull, assertNotUndefined } from './utils/assert'; import { $, $$ } from './utils/dom'; import { initDraggables } from './utils/draggable'; import { fetchJson } from './utils/requests'; @@ -10,17 +11,18 @@ import { fetchJson } from './utils/requests'; export function setupGalleryEditing() { if (!$('.rearrange-button')) return; - const [ rearrangeEl, saveEl ] = $$('.rearrange-button'); - const sortableEl = $('#sortable'); - const containerEl = $('.js-resizable-media-container'); + const [ rearrangeEl, saveEl ] = $$('.rearrange-button'); + const sortableEl = assertNotNull($('#sortable')); + const containerEl = assertNotNull($('.js-resizable-media-container')); // Copy array - let oldImages = window.booru.galleryImages.slice(); - let newImages = window.booru.galleryImages.slice(); + const galleryImages = assertNotUndefined(window.booru.galleryImages); + let oldImages = galleryImages.slice(); + let newImages = galleryImages.slice(); initDraggables(); - $$('.media-box', containerEl).forEach(i => i.draggable = true); + $$('.media-box', containerEl).forEach(i => i.draggable = true); rearrangeEl.addEventListener('click', () => { sortableEl.classList.add('editing'); @@ -31,12 +33,15 @@ export function setupGalleryEditing() { sortableEl.classList.remove('editing'); containerEl.classList.remove('drag-container'); - newImages = $$('.image-container', containerEl).map(i => parseInt(i.dataset.imageId, 10)); + newImages = $$('.image-container', containerEl) + .map(i => parseInt(assertNotUndefined(i.dataset.imageId), 10)); // If nothing changed, don't bother. if (arraysEqual(newImages, oldImages)) return; - fetchJson('PATCH', saveEl.dataset.reorderPath, { + const reorderPath = assertNotUndefined(saveEl.dataset.reorderPath); + + fetchJson('PATCH', reorderPath, { image_ids: newImages, // copy the array again so that we have the newly updated set diff --git a/assets/types/booru-object.d.ts b/assets/types/booru-object.d.ts index 2a5949c0..4154385c 100644 --- a/assets/types/booru-object.d.ts +++ b/assets/types/booru-object.d.ts @@ -69,6 +69,10 @@ interface BooruObject { * Indicates whether sensitive staff-only info should be hidden or not. */ hideStaffTools: string; + /** + * List of image IDs in the current gallery. + */ + galleryImages?: number[]; } declare global {