Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c53df83
ToC block: use static markup and only support core Heading and Page B…
ZebulanStanphill Jun 27, 2021
afadefa
Remove unused index prop from NestedHeadingData.
ZebulanStanphill Jun 27, 2021
2796c70
Add unit test fixtures.
ZebulanStanphill Oct 15, 2021
f564667
Update test.
ZebulanStanphill Nov 2, 2021
4b7bbc6
Use some TypeScript.
ZebulanStanphill Nov 11, 2021
0ff623d
Disable permalink support when core/editor store is unavailable.
ZebulanStanphill Mar 2, 2022
8ba556a
Add Zebulan as code owner for Table of Contents.
ZebulanStanphill Mar 2, 2022
dc2730a
Switch from ul to ol for list element.
ZebulanStanphill Mar 14, 2022
8f7db2d
Optimize re-renders caused by shallow comparisons of useSelect return…
ZebulanStanphill Mar 16, 2022
4282578
Optimize calculating which page the block is on.
ZebulanStanphill Mar 21, 2022
3080087
Enable Heading anchor auto-generation when a Table of Contents is in …
ZebulanStanphill Mar 22, 2022
803eddc
Add List -> Table of Contents block transform.
ZebulanStanphill Mar 30, 2022
6a724bf
Prevent links from being clicked in the editor since they don't work …
ZebulanStanphill Mar 30, 2022
1eb8d22
Strip HTML tags from listed headings.
ZebulanStanphill Mar 31, 2022
e7e866a
Prefer <Disabled /> over pointer-events rule
mcsf Mar 31, 2022
6221796
Make comment formatting consistent.
ZebulanStanphill Mar 31, 2022
38bb612
Simplify TypeScript config.
ZebulanStanphill Mar 31, 2022
a8a1cbd
Simplify condition using optional chaining in utils.ts.
ZebulanStanphill Mar 31, 2022
6d9c653
Fix page index calculation breaking when ToC or Page Break is nested.
ZebulanStanphill Mar 31, 2022
21adec5
Make comment about editor store dependency consistent with other plac…
ZebulanStanphill Apr 1, 2022
fa6cc7d
Simplify logic thanks to changes made in #39985.
ZebulanStanphill Apr 4, 2022
2ed8a4a
Optimize logic when onlyIncludeCurrentPage is false.
ZebulanStanphill Apr 8, 2022
c439c5b
Revise JSDoc for permalink const.
ZebulanStanphill Apr 8, 2022
425109f
Update inspector panel title as per #40275.
ZebulanStanphill Apr 14, 2022
327338f
Make block experimental.
ZebulanStanphill Apr 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify logic thanks to changes made in #39985.
  • Loading branch information
ZebulanStanphill committed May 5, 2022
commit fa6cc7da4674be480ca9a831a0bb76c25b93b627
64 changes: 21 additions & 43 deletions packages/block-library/src/table-of-contents/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
*/
import icon from './icon';
import TableOfContentsList from './list';
import { comparePathAToB, linearToNestedHeadingList } from './utils';
import { linearToNestedHeadingList } from './utils';

/** @typedef {import('./utils').HeadingData} HeadingData */

Expand Down Expand Up @@ -85,11 +85,8 @@ export default function TableOfContentsEdit( {
( select ) => {
const {
getBlockAttributes,
getBlockIndex,
getBlockName,
getBlockOrder,
getBlockParents,
getClientIdsOfDescendants,
getClientIdsWithDescendants,
__experimentalGetGlobalBlocksByName: getGlobalBlocksByName,
} = select( blockEditorStore );

Expand All @@ -105,49 +102,30 @@ export default function TableOfContentsEdit( {

const isPaginated = pageBreakClientIds.length !== 0;

/**
* Get the relative indices of the block from top to bottom nesting
* level.
*
* @param {string} blockClientId
*
* @return {number[]} The path of indices to the block.
*/
function getBlockPath( blockClientId ) {
const indices = getBlockParents(
blockClientId
).map( ( ancestorId ) => getBlockIndex( ancestorId ) );
indices.push( getBlockIndex( blockClientId ) );
return indices;
}

// We can't use just getBlockIndex because it only returns the index relative to sibling blocks, so we have to get all the indices from top to bottom.
const tocPath = getBlockPath( clientId );
// Get the client ids of all blocks in the editor.
const allBlockClientIds = getClientIdsWithDescendants();

// Calculate the page (of a paginated post) this block is part of.
// Note that pageBreakClientIds may not be in the order they appear on
// the page, so we have to iterate over all of them.
let tocPage = 1;
for ( const pageBreakClientId of pageBreakClientIds ) {
if (
comparePathAToB(
tocPath,
getBlockPath( pageBreakClientId )
) < 0
) {
tocPage++;
}
}

// Get the top-level block client ids, and add them and the client ids
// of their children to an ordered list. We don't use
// getClientIdsWithDescendants since it returns ids in the wrong order.
const allBlockClientIds = [];
for ( const blockClientId of getBlockOrder() ) {
allBlockClientIds.push(
if ( isPaginated ) {
// We can't use getBlockIndex because it only returns the index
// relative to sibling blocks.
const tocIndex = allBlockClientIds.indexOf( clientId );

for ( const [
blockIndex,
blockClientId,
...getClientIdsOfDescendants( [ blockClientId ] )
);
] of allBlockClientIds.entries() ) {
// If we've reached blocks after the Table of Contents, we've
// finished calculating which page the block is on.
if ( blockIndex >= tocIndex ) {
break;
}
if ( getBlockName( blockClientId ) === 'core/nextpage' ) {
tocPage++;
}
}
}

const _latestHeadings = [];
Expand Down
30 changes: 0 additions & 30 deletions packages/block-library/src/table-of-contents/utils.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
/**
* Determines if the first path of indices leads to an earlier spot
* than the second path.
*
* @param pathA
* @param pathB
* @return Negative: A is before B; positive: A is after B; zero: the paths are identical.
*/
export function comparePathAToB( pathA: number[], pathB: number[] ): number {
let a: number | undefined = 0;
let b: number | undefined = 0;

// To avoid modifying the arrays passed into the function.
const clonedPathA = [ ...pathA ];
const clonedPathB = [ ...pathB ];

do {
a = clonedPathA.shift();
b = clonedPathB.shift();
} while ( a === b && a !== undefined && b !== undefined );

// Defaulting to -1 ensures that if a path terminates before the other, it
// is considered as leading to an earlier global index. This ensures that
// parent blocks are considered as coming before their first child.
// Technically, this isn't needed for the Table of Contents use-case, since
// neither it nor Page Break blocks support children, but it's good to play
// it safe in case this code gets reused elsewhere.
return ( a ?? -1 ) - ( b ?? -1 );
}

export interface HeadingData {
/** The plain text content of the heading. */
content: string;
Expand Down