UNPKG

@wordpress/block-library

Version:
1 lines • 3.11 kB
{"version":3,"file":"use-get-new-images.mjs","names":[],"sources":["../../src/gallery/use-get-new-images.js"],"sourcesContent":["import { useMemo, useState } from '@wordpress/element';\n\n/**\n * Keeps track of images already in the gallery to allow new innerBlocks to be identified. This\n * is required so default gallery attributes can be applied without overwriting any custom\n * attributes applied to existing images.\n *\n * @param {Array} images Basic image block data taken from current gallery innerBlock\n * @param {Array} imageData The related image data for each of the current gallery images.\n *\n * @return {Array} An array of any new images that have been added to the gallery.\n */\nexport default function useGetNewImages( images, imageData ) {\n\tconst [ currentImages, setCurrentImages ] = useState( [] );\n\n\treturn useMemo( () => getNewImages(), [ images, imageData ] );\n\n\tfunction getNewImages() {\n\t\tlet imagesUpdated = false;\n\n\t\t// First lets check if any images have been deleted.\n\t\tconst newCurrentImages = currentImages.filter( ( currentImg ) =>\n\t\t\timages.find( ( img ) => {\n\t\t\t\treturn currentImg.clientId === img.clientId;\n\t\t\t} )\n\t\t);\n\n\t\tif ( newCurrentImages.length < currentImages.length ) {\n\t\t\timagesUpdated = true;\n\t\t}\n\n\t\t// Now lets see if we have any images hydrated from saved content and if so\n\t\t// add them to currentImages state.\n\t\timages.forEach( ( image ) => {\n\t\t\tif (\n\t\t\t\timage.fromSavedContent &&\n\t\t\t\t! newCurrentImages.find(\n\t\t\t\t\t( currentImage ) => currentImage.id === image.id\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\timagesUpdated = true;\n\t\t\t\tnewCurrentImages.push( image );\n\t\t\t}\n\t\t} );\n\n\t\t// Now check for any new images that have been added to InnerBlocks and for which\n\t\t// we have the imageData we need for setting default block attributes.\n\t\tconst newImages = images.filter(\n\t\t\t( image ) =>\n\t\t\t\t! newCurrentImages.find(\n\t\t\t\t\t( currentImage ) =>\n\t\t\t\t\t\timage.clientId &&\n\t\t\t\t\t\tcurrentImage.clientId === image.clientId\n\t\t\t\t) &&\n\t\t\t\timageData?.find( ( img ) => img.id === image.id ) &&\n\t\t\t\t! image.fromSavedContent\n\t\t);\n\n\t\tif ( imagesUpdated || newImages?.length > 0 ) {\n\t\t\tsetCurrentImages( [ ...newCurrentImages, ...newImages ] );\n\t\t}\n\n\t\treturn newImages.length > 0 ? newImages : null;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;AAYA,SAAwB,gBAAiB,QAAQ,WAAY;CAC5D,MAAM,CAAE,eAAe,oBAAqB,SAAU,CAAC,CAAE;CAEzD,OAAO,cAAe,aAAa,GAAG,CAAE,QAAQ,SAAU,CAAE;CAE5D,SAAS,eAAe;EACvB,IAAI,gBAAgB;EAGpB,MAAM,mBAAmB,cAAc,QAAU,eAChD,OAAO,MAAQ,QAAS;GACvB,OAAO,WAAW,aAAa,IAAI;EACpC,CAAE,CACH;EAEA,IAAK,iBAAiB,SAAS,cAAc,QAC5C,gBAAgB;EAKjB,OAAO,SAAW,UAAW;GAC5B,IACC,MAAM,oBACN,CAAE,iBAAiB,MAChB,iBAAkB,aAAa,OAAO,MAAM,EAC/C,GACC;IACD,gBAAgB;IAChB,iBAAiB,KAAM,KAAM;GAC9B;EACD,CAAE;EAIF,MAAM,YAAY,OAAO,QACtB,UACD,CAAE,iBAAiB,MAChB,iBACD,MAAM,YACN,aAAa,aAAa,MAAM,QAClC,KACA,WAAW,MAAQ,QAAS,IAAI,OAAO,MAAM,EAAG,KAChD,CAAE,MAAM,gBACV;EAEA,IAAK,iBAAiB,WAAW,SAAS,GACzC,iBAAkB,CAAE,GAAG,kBAAkB,GAAG,SAAU,CAAE;EAGzD,OAAO,UAAU,SAAS,IAAI,YAAY;CAC3C;AACD"}