-
Notifications
You must be signed in to change notification settings - Fork 85
Add per-block Fediverse visibility toggle #3055
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
a51015a
31d58b9
856c7f8
77aa289
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| Add per-block Fediverse visibility toggle to control which blocks are shared to Mastodon and other Fediverse platforms. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "$schema": "https://schemas.wp.org/trunk/block.json", | ||
| "apiVersion": 3, | ||
| "name": "activitypub/block-visibility", | ||
| "title": "ActivityPub Block Visibility", | ||
| "category": "widgets", | ||
| "description": "Adds Fediverse visibility controls to all blocks.", | ||
| "icon": "screenoptions", | ||
| "textdomain": "activitypub", | ||
| "editorScript": "file:./plugin.js" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { addFilter } from '@wordpress/hooks'; | ||
| import { createHigherOrderComponent } from '@wordpress/compose'; | ||
| import { InspectorControls } from '@wordpress/block-editor'; | ||
| import { PanelBody, ToggleControl } from '@wordpress/components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
|
|
||
| /** | ||
| * Adds a "Fediverse" panel to the block inspector for all blocks, | ||
| * allowing users to control whether a block is included in federated content. | ||
| */ | ||
| const withFediverseVisibility = createHigherOrderComponent( ( BlockEdit ) => { | ||
| return ( props ) => { | ||
| const { attributes, setAttributes, isSelected } = props; | ||
|
|
||
| // Get the current fediverse visibility state (default: true). | ||
| const metadata = attributes?.metadata || {}; | ||
| const blockVisibility = metadata?.blockVisibility || {}; | ||
| const isFediverseVisible = blockVisibility?.fediverse !== false; | ||
|
|
||
| /** | ||
| * Update the fediverse visibility in block metadata. | ||
| * | ||
| * @param {boolean} value Whether the block should be visible on the Fediverse. | ||
| */ | ||
| const onChange = ( value ) => { | ||
| const updatedBlockVisibility = { ...blockVisibility }; | ||
|
|
||
| if ( value ) { | ||
| // Remove the fediverse key when true (default state). | ||
| delete updatedBlockVisibility.fediverse; | ||
| } else { | ||
| updatedBlockVisibility.fediverse = false; | ||
| } | ||
|
|
||
| // Clean up: if blockVisibility is empty, remove it from metadata. | ||
| const updatedMetadata = { ...metadata }; | ||
| if ( Object.keys( updatedBlockVisibility ).length === 0 ) { | ||
| delete updatedMetadata.blockVisibility; | ||
| } else { | ||
| updatedMetadata.blockVisibility = updatedBlockVisibility; | ||
| } | ||
|
|
||
| // Clean up: if metadata is empty, remove it entirely. | ||
| if ( Object.keys( updatedMetadata ).length === 0 ) { | ||
| setAttributes( { metadata: undefined } ); | ||
| } else { | ||
| setAttributes( { metadata: updatedMetadata } ); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <BlockEdit { ...props } /> | ||
| { isSelected && ( | ||
| <InspectorControls> | ||
| <PanelBody title={ __( 'Fediverse ⁂', 'activitypub' ) } initialOpen={ false }> | ||
| <ToggleControl | ||
| __nextHasNoMarginBottom | ||
| label={ __( 'Share to the Fediverse', 'activitypub' ) } | ||
| help={ | ||
| isFediverseVisible | ||
| ? __( | ||
| 'This block will be shared to Mastodon and other Fediverse platforms.', | ||
| 'activitypub' | ||
| ) | ||
| : __( 'This block will only be visible on your site.', 'activitypub' ) | ||
| } | ||
| checked={ isFediverseVisible } | ||
| onChange={ onChange } | ||
| /> | ||
| </PanelBody> | ||
| </InspectorControls> | ||
| ) } | ||
| </> | ||
| ); | ||
| }; | ||
| }, 'withFediverseVisibility' ); | ||
|
|
||
| addFilter( 'editor.BlockEdit', 'activitypub/block-visibility', withFediverseVisibility ); |
Uh oh!
There was an error while loading. Please reload this page.