Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document outline: Use links not buttons #10815

Merged
merged 81 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
1ccfe6f
Adjust document outline to use an a tag vs button
Oct 19, 2018
474535f
target href links directly to page anchors, remove onClick handler
Oct 28, 2018
e574784
Merge branch 'master' of github.com:WordPress/gutenberg
Oct 28, 2018
63f4800
Merge branch 'master' into fix/doc-outline
Oct 28, 2018
e62566d
Merge branch 'master' of github.com:WordPress/gutenberg
Oct 30, 2018
ade47e5
Merge branch 'master' into fix/doc-outline
Oct 30, 2018
da9311c
update test snapshot
Oct 30, 2018
8e54535
Merge branch 'master' of github.com:WordPress/gutenberg
Nov 2, 2018
2343fa7
update snapshot
Nov 4, 2018
ef96b90
better titleNode targeting
Nov 4, 2018
3c2a500
update snapshot
Nov 4, 2018
f579dda
update snapshot
Nov 4, 2018
acde4a5
Merge branch 'master' of github.com:WordPress/gutenberg
Nov 7, 2018
20691f3
Merge branch 'master' of github.com:WordPress/gutenberg
Nov 12, 2018
89660bc
Merge branch 'master' of github.com:WordPress/gutenberg
Nov 13, 2018
7aee351
Merge branch 'master' into fix/doc-outline
Nov 18, 2018
e434bae
Close the table of contents panel when a link is clicked
Nov 18, 2018
841bf0f
add deterministic block id to tests
Nov 18, 2018
9082baa
remove redundant screen reader text
Nov 18, 2018
b64b2ed
Adjust map to avoid mutating original object
Nov 20, 2018
e4cc7cf
update snapshot
Nov 20, 2018
55b69f5
Update packages/editor/src/components/document-outline/index.js
tofumatt Nov 20, 2018
46e4e1f
Update packages/editor/src/components/document-outline/test/index.js
tofumatt Nov 20, 2018
0aaaaa0
Update packages/editor/src/components/document-outline/index.js
tofumatt Nov 20, 2018
4f85e47
Merge branch 'master' into fix/doc-outline
Nov 27, 2018
273c7b4
update snapshot
Nov 27, 2018
c69dcf7
Merge branch 'fix/doc-outline' of github.com:WordPress/gutenberg into…
Nov 27, 2018
8b576c2
update snapshots
Nov 27, 2018
275f225
Merge branch 'master' into fix/doc-outline
Dec 1, 2018
eab1548
update snapshot
Dec 3, 2018
6df81db
update snapshots
Dec 4, 2018
4f38edc
fix up e2e tests
Dec 4, 2018
e8aa8d2
Fix snapshots by removing single quotes from outline links
mcsf Dec 9, 2018
33db8e9
Update packages/editor/src/components/document-outline/index.js
mcsf Dec 9, 2018
7d7edf3
Merge branch 'fix/doc-outline' of github.com:WordPress/gutenberg into…
Dec 9, 2018
b85b4c9
change target to href
Dec 9, 2018
f0afad5
rename close -> closeOutline
Dec 10, 2018
06b3cdd
Merge branch 'master' of github.com:WordPress/gutenberg
Dec 10, 2018
a97e293
Merge branch 'master' of github.com:WordPress/gutenberg
Dec 14, 2018
7091d06
Merge branch 'master' into fix/doc-outline
Dec 14, 2018
dd3f8b3
update snapshots after property name changes
Dec 14, 2018
69f94c9
rename close/onClose -> onRequestClose for TOC, on
Dec 16, 2018
7d3f4a9
restore onSelect
Dec 16, 2018
48764d5
complete renaming
Dec 16, 2018
e88aac5
Block links are only valid for the current session - remove hash afte…
Dec 16, 2018
6a6d8d2
update snapshot
Dec 16, 2018
ca3e4be
Merge branch 'master' of github.com:WordPress/gutenberg
Dec 17, 2018
d227dfb
Merge branch 'master' of github.com:WordPress/gutenberg
Dec 19, 2018
cb94e71
Merge branch 'master' of github.com:WordPress/gutenberg
Dec 21, 2018
158bc4a
Merge branch 'master' into fix/doc-outline
Dec 21, 2018
0475978
cleanup; move block id hash removal functionality up to document outl…
Dec 21, 2018
3604126
update snapshot
Dec 21, 2018
729037d
Merge branch 'master' of github.com:WordPress/gutenberg
Jan 4, 2019
7e5be38
Merge branch 'master' into fix/doc-outline
Jan 4, 2019
bada58f
use replaceState vs pushState
Jan 4, 2019
44d6d40
use defer and import at top of file
Jan 4, 2019
d5470e5
removeURLHash as helper
Jan 4, 2019
849949c
Merge branch 'master' of github.com:WordPress/gutenberg
Jan 10, 2019
0a534f6
Merge branch 'master' into fix/doc-outline
Jan 10, 2019
2f1b5e2
Merge branch 'master' of github.com:WordPress/gutenberg
Jan 12, 2019
2a6aacf
Merge branch 'master' into fix/doc-outline
Jan 12, 2019
180cd54
Merge branch 'master' of github.com:WordPress/gutenberg
Jan 18, 2019
7c40fd4
Merge branch 'master' into fix/doc-outline
Jan 18, 2019
cd26b45
Merge branch 'master' into fix/doc-outline
Jan 23, 2019
613bc47
remove passing event in select handler
Jan 23, 2019
99380f2
improve doc block
Jan 23, 2019
800787e
Merge branch 'master' of github.com:WordPress/gutenberg
Jan 25, 2019
506ad72
Merge branch 'master' into fix/doc-outline
Jan 25, 2019
9862909
Merge branch 'master' of github.com:WordPress/gutenberg
Jan 28, 2019
00e3b35
Merge branch 'master' into fix/doc-outline
Jan 28, 2019
ac4d147
Merge branch 'master' into fix/doc-outline
Feb 5, 2019
ca63c58
Skip title in outline when title node not found
Feb 5, 2019
291097c
remove removeURLHash
Feb 5, 2019
da3de52
Merge branch 'master' of github.com:WordPress/gutenberg
Feb 6, 2019
d3443e7
Merge branch 'master' of github.com:WordPress/gutenberg
Feb 10, 2019
0c7d6b5
Merge branch 'master' of github.com:WordPress/gutenberg
Feb 20, 2019
38ca0fe
Merge branch 'master' into fix/doc-outline
Feb 28, 2019
03fc448
Merge branch 'master' of github.com:WordPress/gutenberg
Feb 28, 2019
bdd533f
Merge branch 'master' into fix/doc-outline
Feb 28, 2019
de595d3
Merge branch 'master' of github.com:WordPress/gutenberg
Mar 8, 2019
a04038f
Merge branch 'master' into fix/doc-outline
Mar 8, 2019
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
29 changes: 8 additions & 21 deletions packages/editor/src/components/document-outline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { countBy, flatMap, get } from 'lodash';
*/
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { withSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -61,7 +61,7 @@ const computeOutlineHeadings = ( blocks = [], path = [] ) => {

const isEmptyHeading = ( heading ) => ! heading.attributes.content || heading.attributes.content.length === 0;

export const DocumentOutline = ( { blocks = [], title, onSelect, isTitleSupported } ) => {
export const DocumentOutline = ( { blocks = [], title, close, isTitleSupported } ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this prop was changed. What does a close prop mean in the context of a DocumentOutline component.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is passing through the close functionality so we can close the outline when links are clicked, see https://github.com/WordPress/gutenberg/pull/10815/files#diff-486ea059bfd8ab47600431d73ad9824dR89 - is your question around the naming, eg should this be better named to reflect the functionality - eg called onClick or closeOutline?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the question is specifically: What was wrong with onSelect that it required changing?

There's a bit of conconvention in the naming introduced here, much more imperative "close" than event-driven "onClose" that had existed previously / exists elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ok - thanks for clarifying. Still getting used to the conventions here, I'll change that back to onSelect

const headings = computeOutlineHeadings( blocks );

if ( headings.length < 1 ) {
Expand All @@ -70,17 +70,8 @@ export const DocumentOutline = ( { blocks = [], title, onSelect, isTitleSupporte

let prevHeadingLevel = 1;

// Select the corresponding block in the main editor
// when clicking on a heading item from the list.
const onSelectHeading = ( clientId ) => onSelect( clientId );
const focusTitle = () => {
// Not great but it's the simplest way to focus the title right now.
const titleNode = document.querySelector( '.editor-post-title__input' );
if ( titleNode ) {
titleNode.focus();
}
};

// Not great but it's the simplest way to locate the title right now.
const titleNode = document.querySelector( '.editor-post-title__input' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no selector to get the title? This seems quite brittle.

I wouldn't advocate for this staying, but if it does it needs an E2E test to make sure changing that selector won't regress things.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the only way I could 'discover' the selector though I agree it is brittle. Maybe @aduth has another suggestion how we could do that here.

Otherwise I'll try to add an e2e test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: adding e2e tests for this selector:

Reviewing the e2e testing suite, i see numerous existing instances where we rely on the .editor-post-title__input selector ( see https://github.com/WordPress/gutenberg/search?q=.editor-post-title__input&unscoped_q=.editor-post-title__input) - if this selector didn't exist all these tests would fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm generally fine with this in that it's at least what had existed previously. The two downsides I see with the changes proposed here are:

  • The selector is now executed at the time of rendering, rather than at the time of click, leaving the potential for some drift to occur.
  • It assumes (below) that the node has an id associated with it, which did not exist previously.

These are generally minor, and are "par for the course" in updating the implementation to leverage id as the anchor point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduth @tofumatt are you ok with the current state of this PR? Are there any additional points I can work to address?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I might like to see: The previous implementation guarded against the (real) possibility that the title node could not be found. Here we've removed that protection, which could crash the editor. It could be worth considering to include this in the derived value for hasTitle, which would prevent the rendering of the DocumentOutlineItem if the title node does not exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I might like to see: The previous implementation guarded against the (real) possibility that the title node could not be found.

This was never addressed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 9ca6dea

const hasTitle = isTitleSupported && title;
const countByLevel = countBy( headings, 'level' );
const hasMultipleH1 = countByLevel[ 1 ] > 1;
Expand All @@ -92,7 +83,8 @@ export const DocumentOutline = ( { blocks = [], title, onSelect, isTitleSupporte
<DocumentOutlineItem
level={ __( 'Title' ) }
isValid
onClick={ focusTitle }
close={ close }
target={ '#' + titleNode.id }
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
>
{ title }
</DocumentOutlineItem>
Expand All @@ -115,8 +107,9 @@ export const DocumentOutline = ( { blocks = [], title, onSelect, isTitleSupporte
key={ index }
level={ `H${ item.level }` }
isValid={ isValid }
onClick={ () => onSelectHeading( item.clientId ) }
path={ item.path }
close={ close }
target={ '#block-' + item.clientId }
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
>
{ item.isEmpty ?
emptyHeadingContent :
Expand Down Expand Up @@ -147,11 +140,5 @@ export default compose(
blocks: getBlocks(),
isTitleSupported: get( postType, [ 'supports', 'title' ], false ),
};
} ),
withDispatch( ( dispatch ) => {
const { selectBlock } = dispatch( 'core/editor' );
return {
onSelect: selectBlock,
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
};
} )
)( DocumentOutline );
16 changes: 6 additions & 10 deletions packages/editor/src/components/document-outline/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
Expand All @@ -17,8 +12,9 @@ const TableOfContentsItem = ( {
children,
isValid,
level,
onClick,
path = [],
target,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this named target instead of just href, seems a bit confusing as target has a different meaning for links?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good point - i'll change this to href

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in b85b4c9

close,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same answer as above, should I rename this?

Copy link
Member Author

@adamsilverstein adamsilverstein Dec 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed this prop to closeOutline in f0afad5

} ) => (
<li
className={ classnames(
Expand All @@ -29,9 +25,10 @@ const TableOfContentsItem = ( {
}
) }
>
<button
<a
href={ target }
className="document-outline__button"
onClick={ onClick }
onClick={ close }
>
<span className="document-outline__emdash" aria-hidden="true"></span>
{
Expand All @@ -49,8 +46,7 @@ const TableOfContentsItem = ( {
<span className="document-outline__item-content">
{ children }
</span>
<span className="screen-reader-text">{ __( '(Click to focus this heading)' ) }</span>
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
</button>
</a>
</li>
);

Expand Down
4 changes: 4 additions & 0 deletions packages/editor/src/components/document-outline/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
display: flex;
margin: 4px 0;

a {
text-decoration: none;
}

.document-outline__emdash::before {
color: $light-gray-500;
margin-right: 4px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ exports[`DocumentOutline header blocks present should match snapshot 1`] = `
isValid={true}
key="0"
level="H2"
onClick={[Function]}
path={Array []}
target="#block-7033e374-b102-4ec5-a340-623f9c433e3f"
>
<Component
format="string"
Expand All @@ -22,8 +22,8 @@ exports[`DocumentOutline header blocks present should match snapshot 1`] = `
isValid={true}
key="1"
level="H3"
onClick={[Function]}
path={Array []}
target="#block-9103c389-d5c7-4287-a7ad-e272bc61a68e"
>
<Component
format="string"
Expand All @@ -44,8 +44,8 @@ exports[`DocumentOutline header blocks present should render warnings for multip
isValid={false}
key="0"
level="H1"
onClick={[Function]}
path={Array []}
target="#block-12df094f-a677-4ab2-882d-1821a10307e5"
>
<Component
format="string"
Expand All @@ -65,8 +65,8 @@ exports[`DocumentOutline header blocks present should render warnings for multip
isValid={false}
key="1"
level="H1"
onClick={[Function]}
path={Array []}
target="#block-12df094f-a677-4ab2-882d-1821a10307e5"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your test failures may be related? Need to update snapshot?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated snapshots

>
<Component
format="string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ describe( 'DocumentOutline', () => {
} );

it( 'should not render when no heading blocks provided', () => {
const blocks = [ paragraph ];
const blocks = [ paragraph ].map( ( block, index ) => {
// Set client IDs to a predictable value.
return { ...block, clientId: '_clientId_' + index };
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
} );
const wrapper = shallow( <DocumentOutline blocks={ blocks } /> );

expect( wrapper.html() ).toBe( null );
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/table-of-contents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function TableOfContents( { hasBlocks } ) {
disabled={ ! hasBlocks }
/>
) }
renderContent={ () => <TableOfContentsPanel /> }
renderContent={ ( { onClose } ) => <TableOfContentsPanel close={ onClose } /> }
/>
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/table-of-contents/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { withSelect } from '@wordpress/data';
import WordCount from '../word-count';
import DocumentOutline from '../document-outline';

function TableOfContentsPanel( { headingCount, paragraphCount, numberOfBlocks } ) {
function TableOfContentsPanel( { headingCount, paragraphCount, numberOfBlocks, close } ) {
return (
<Fragment>
<div
Expand Down Expand Up @@ -49,7 +49,7 @@ function TableOfContentsPanel( { headingCount, paragraphCount, numberOfBlocks }
<span className="table-of-contents__title">
{ __( 'Document Outline' ) }
</span>
<DocumentOutline />
<DocumentOutline close={ close } />
</Fragment>
) }
</Fragment>
Expand Down