@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 7 kB
Source Map (JSON)
{"version":3,"file":"hooks.cjs","names":["addQueryArgs","removeQueryArgs","stripHTML","useEntityBlockEditor","useSelect","coreStore","useMemo"],"sources":["../../src/table-of-contents/hooks.js"],"sourcesContent":["import { useEntityBlockEditor, store as coreStore } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\nimport { __unstableStripHTML as stripHTML } from '@wordpress/dom';\nimport { useMemo } from '@wordpress/element';\nimport { addQueryArgs, removeQueryArgs } from '@wordpress/url';\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Flattens a block tree in document order, the same order the server walks\n * the post content in.\n *\n * @param {Object[]} blocks Blocks to flatten.\n *\n * @return {Object[]} Flat list of blocks.\n */\nfunction flattenBlocks( blocks ) {\n\treturn blocks.flatMap( ( block ) => [\n\t\tblock,\n\t\t...flattenBlocks( block.innerBlocks ?? [] ),\n\t] );\n}\n\nfunction getLatestHeadings(\n\tblocks,\n\tclientId,\n\tpermalink,\n\t{ onlyIncludeCurrentPage, maxLevel }\n) {\n\tconst allBlocks = flattenBlocks( blocks );\n\tconst isPaginated = allBlocks.some(\n\t\t( block ) => block.name === 'core/nextpage'\n\t);\n\n\t// If onlyIncludeCurrentPage is true, calculate the page (of a paginated post) this block is part of, so we know which headings to include; otherwise, skip the calculation.\n\t// A Table of Contents placed outside the post content, in a template,\n\t// is not part of the post, so it uses the first page.\n\tlet tocPage = 1;\n\n\tif ( isPaginated && onlyIncludeCurrentPage ) {\n\t\tconst tocIndex = allBlocks.findIndex(\n\t\t\t( block ) => block.clientId === clientId\n\t\t);\n\n\t\tfor ( const block of allBlocks.slice( 0, Math.max( tocIndex, 0 ) ) ) {\n\t\t\tif ( block.name === 'core/nextpage' ) {\n\t\t\t\ttocPage++;\n\t\t\t}\n\t\t}\n\t}\n\n\tconst latestHeadings = [];\n\n\t/** The page (of a paginated post) a heading will be part of. */\n\tlet headingPage = 1;\n\tlet headingPageLink = null;\n\n\tif ( typeof permalink === 'string' ) {\n\t\theadingPageLink = isPaginated\n\t\t\t? addQueryArgs( permalink, { page: headingPage } )\n\t\t\t: permalink;\n\t}\n\n\tfor ( const block of allBlocks ) {\n\t\tif ( block.name === 'core/nextpage' ) {\n\t\t\theadingPage++;\n\n\t\t\t// If we're only including headings from the current page (of\n\t\t\t// a paginated post), then exit the loop if we've reached the\n\t\t\t// pages after the one with the Table of Contents block.\n\t\t\tif ( onlyIncludeCurrentPage && headingPage > tocPage ) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tif ( typeof permalink === 'string' ) {\n\t\t\t\theadingPageLink = addQueryArgs(\n\t\t\t\t\tremoveQueryArgs( permalink, [ 'page' ] ),\n\t\t\t\t\t{ page: headingPage }\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\t// If we're including all headings or we've reached headings on\n\t\t// the same page as the Table of Contents block, add them to the\n\t\t// list.\n\t\telse if ( ! onlyIncludeCurrentPage || headingPage === tocPage ) {\n\t\t\tif ( block.name === 'core/heading' ) {\n\t\t\t\tconst headingAttributes = block.attributes;\n\n\t\t\t\t// Skip headings that are deeper than maxLevel\n\t\t\t\tif ( maxLevel && headingAttributes.level > maxLevel ) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst canBeLinked =\n\t\t\t\t\ttypeof headingPageLink === 'string' &&\n\t\t\t\t\ttypeof headingAttributes.anchor === 'string' &&\n\t\t\t\t\theadingAttributes.anchor !== '';\n\n\t\t\t\tlatestHeadings.push( {\n\t\t\t\t\t// Convert line breaks to spaces, and get rid of HTML tags in the headings.\n\t\t\t\t\tcontent: stripHTML(\n\t\t\t\t\t\theadingAttributes.content.replace(\n\t\t\t\t\t\t\t/(<br *\\/?>)+/g,\n\t\t\t\t\t\t\t' '\n\t\t\t\t\t\t)\n\t\t\t\t\t),\n\t\t\t\t\tlevel: headingAttributes.level,\n\t\t\t\t\tlink: canBeLinked\n\t\t\t\t\t\t? `${ headingPageLink }#${ headingAttributes.anchor }`\n\t\t\t\t\t\t: null,\n\t\t\t\t} );\n\t\t\t}\n\t\t}\n\t}\n\n\treturn latestHeadings;\n}\n\n/**\n * Returns the headings of the post given by the block context, including\n * unsaved edits.\n *\n * @param {Object} options\n * @param {string} options.clientId Table of Contents block client ID.\n * @param {number} options.postId Post ID from the block context.\n * @param {string} options.postType Post type from the block context.\n * @param {boolean} options.onlyIncludeCurrentPage Whether to only include headings from the current page.\n * @param {number} options.maxLevel Maximum heading level to include.\n *\n * @return {Object[]} Heading data.\n */\nexport function useHeadings( {\n\tclientId,\n\tpostId,\n\tpostType,\n\tonlyIncludeCurrentPage,\n\tmaxLevel,\n} ) {\n\tconst [ blocks ] = useEntityBlockEditor( 'postType', postType, {\n\t\tid: postId,\n\t} );\n\tconst permalink = useSelect(\n\t\t( select ) =>\n\t\t\tpostId\n\t\t\t\t? select( coreStore ).getEditedEntityRecord(\n\t\t\t\t\t\t'postType',\n\t\t\t\t\t\tpostType,\n\t\t\t\t\t\tpostId\n\t\t\t\t\t)?.link\n\t\t\t\t: undefined,\n\t\t[ postType, postId ]\n\t);\n\n\treturn useMemo( () => {\n\t\tif ( ! postId || ! blocks ) {\n\t\t\treturn EMPTY_ARRAY;\n\t\t}\n\n\t\treturn getLatestHeadings( blocks, clientId, permalink, {\n\t\t\tonlyIncludeCurrentPage,\n\t\t\tmaxLevel,\n\t\t} );\n\t}, [\n\t\tblocks,\n\t\tclientId,\n\t\tpermalink,\n\t\tpostId,\n\t\tonlyIncludeCurrentPage,\n\t\tmaxLevel,\n\t] );\n}\n"],"mappings":";;;;;;;;;;;AAMA,MAAM,cAAc,CAAC;;;;;;;;;AAUrB,SAAS,cAAe,QAAS;CAChC,OAAO,OAAO,SAAW,UAAW,CACnC,OACA,GAAG,cAAe,MAAM,eAAe,CAAC,CAAE,CAC3C,CAAE;AACH;AAEA,SAAS,kBACR,QACA,UACA,WACA,EAAE,wBAAwB,YACzB;CACD,MAAM,YAAY,cAAe,MAAO;CACx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