-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Post Content Block: Render readonly content as blocks to preserve block supports styles #35863
Closed
andrewserong
wants to merge
9
commits into
trunk
from
try/swap-post-content-read-only-rendering-to-use-live-preview
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cd78f31
Post Content Block: try rendering read only content as blocks to pres…
andrewserong 9a680bd
Prevent post content inner from being tabbable, fix disabled styles, …
andrewserong 4a296d5
Remove tabIndex prop, replace with __experimentalAsButton, move CSS s…
andrewserong 6c03989
Remove tabIndex prop from post-content edit
andrewserong 04caf9b
Add useDisabled hook, pass layout with alignments, use BlockListItems
andrewserong b06b374
Refactor block preview behaviour to a useBlockPreview hook for the po…
andrewserong 2a5a246
Remove useDisabled hook from post content now that it has been moved …
andrewserong 284ded1
Combine props into options param of useBlockPreview
andrewserong 89fba21
Combine layout logic
andrewserong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { includes, debounce } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback, useLayoutEffect, useRef } from '@wordpress/element'; | ||
import { focus } from '@wordpress/dom'; | ||
|
||
/** | ||
* Names of control nodes which qualify for disabled behavior. | ||
* | ||
* See WHATWG HTML Standard: 4.10.18.5: "Enabling and disabling form controls: the disabled attribute". | ||
* | ||
* @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#enabling-and-disabling-form-controls:-the-disabled-attribute | ||
* | ||
* @type {string[]} | ||
*/ | ||
const DISABLED_ELIGIBLE_NODE_NAMES = [ | ||
'BUTTON', | ||
'FIELDSET', | ||
'INPUT', | ||
'OPTGROUP', | ||
'OPTION', | ||
'SELECT', | ||
'TEXTAREA', | ||
]; | ||
|
||
export default function useDisabled() { | ||
ntsekouras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** @type {import('react').RefObject<HTMLDivElement>} */ | ||
const node = useRef( null ); | ||
|
||
const disable = () => { | ||
if ( ! node.current ) { | ||
return; | ||
} | ||
|
||
focus.focusable.find( node.current ).forEach( ( focusable ) => { | ||
if ( | ||
includes( DISABLED_ELIGIBLE_NODE_NAMES, focusable.nodeName ) | ||
) { | ||
focusable.setAttribute( 'disabled', '' ); | ||
} | ||
|
||
if ( focusable.nodeName === 'A' ) { | ||
focusable.setAttribute( 'tabindex', '-1' ); | ||
} | ||
|
||
const tabIndex = focusable.getAttribute( 'tabindex' ); | ||
if ( tabIndex !== null && tabIndex !== '-1' ) { | ||
focusable.removeAttribute( 'tabindex' ); | ||
} | ||
|
||
if ( focusable.hasAttribute( 'contenteditable' ) ) { | ||
focusable.setAttribute( 'contenteditable', 'false' ); | ||
} | ||
} ); | ||
}; | ||
|
||
// Debounce re-disable since disabling process itself will incur | ||
// additional mutations which should be ignored. | ||
const debouncedDisable = useCallback( | ||
debounce( disable, undefined, { leading: true } ), | ||
[] | ||
); | ||
|
||
useLayoutEffect( () => { | ||
disable(); | ||
|
||
/** @type {MutationObserver | undefined} */ | ||
let observer; | ||
if ( node.current ) { | ||
observer = new window.MutationObserver( debouncedDisable ); | ||
observer.observe( node.current, { | ||
childList: true, | ||
attributes: true, | ||
subtree: true, | ||
} ); | ||
} | ||
|
||
return () => { | ||
if ( observer ) { | ||
observer.disconnect(); | ||
} | ||
debouncedDisable.cancel(); | ||
}; | ||
}, [] ); | ||
|
||
return node; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm little hesitant with this change from
content.rendered
tocontent.raw
. I think that this might lead to security issues with private content.. @peterwilsoncc can you share your thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct: previews need to use the rendered version as the raw version may not be available to all users. For example authors don't have raw rights for others' posts, contributors for any published post.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for raising this @ntsekouras and for the feedback @peterwilsoncc! In the use case in this PR, unfortunately I don't think it'll work to use
content.rendered
as we need the raw markup of blocks in order to be able to parse them and have the styles be rendered properly by the preview.As a bit of background, this PR is an alternative / workaround for the issue of rendering block supports styles on the server in order to display styles correctly in the editor. We currently have no way of retrieving the block supports styles from the server via an API request, so the next best option in this PR was to allow the editor to generate the styles from the block markup.
In the case of the Post Content block, in most cases, I imagine that users will see this block in the editor when editing template parts rather than in an isolated post. Because author and contributor roles don't have access to the site editor, in practice, I'm wondering if it isn't too much of an issue if we have the Post Content preview depend on access to
content.raw
. We already only render the blocks ifcontent.raw
is available, otherwise this falls back to rendering an empty preview, so on balance I think it could be more beneficial to prioritise the correct rendering for the admin view of the block, rather than focusing on author/contributor roles being able to view it in the editor.