UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 3.28 kB
{ "version": 3, "sources": ["../../src/gallery/use-get-new-images.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { 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": ";AAGA,SAAS,SAAS,gBAAgB;AAYnB,SAAR,gBAAkC,QAAQ,WAAY;AAC5D,QAAM,CAAE,eAAe,gBAAiB,IAAI,SAAU,CAAC,CAAE;AAEzD,SAAO,QAAS,MAAM,aAAa,GAAG,CAAE,QAAQ,SAAU,CAAE;AAE5D,WAAS,eAAe;AACvB,QAAI,gBAAgB;AAGpB,UAAM,mBAAmB,cAAc;AAAA,MAAQ,CAAE,eAChD,OAAO,KAAM,CAAE,QAAS;AACvB,eAAO,WAAW,aAAa,IAAI;AAAA,MACpC,CAAE;AAAA,IACH;AAEA,QAAK,iBAAiB,SAAS,cAAc,QAAS;AACrD,sBAAgB;AAAA,IACjB;AAIA,WAAO,QAAS,CAAE,UAAW;AAC5B,UACC,MAAM,oBACN,CAAE,iBAAiB;AAAA,QAClB,CAAE,iBAAkB,aAAa,OAAO,MAAM;AAAA,MAC/C,GACC;AACD,wBAAgB;AAChB,yBAAiB,KAAM,KAAM;AAAA,MAC9B;AAAA,IACD,CAAE;AAIF,UAAM,YAAY,OAAO;AAAA,MACxB,CAAE,UACD,CAAE,iBAAiB;AAAA,QAClB,CAAE,iBACD,MAAM,YACN,aAAa,aAAa,MAAM;AAAA,MAClC,KACA,WAAW,KAAM,CAAE,QAAS,IAAI,OAAO,MAAM,EAAG,KAChD,CAAE,MAAM;AAAA,IACV;AAEA,QAAK,iBAAiB,WAAW,SAAS,GAAI;AAC7C,uBAAkB,CAAE,GAAG,kBAAkB,GAAG,SAAU,CAAE;AAAA,IACzD;AAEA,WAAO,UAAU,SAAS,IAAI,YAAY;AAAA,EAC3C;AACD;", "names": [] }