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

feat/3279 default block text #3283

Merged
merged 8 commits into from
Sep 5, 2024
14 changes: 14 additions & 0 deletions src/block/text/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ import { createBlock, createBlocksFromInnerBlocksTemplate } from '@wordpress/blo
* Internal dependencies
*/
import { TEMPLATE as ICON_LABEL_TEMPLATE } from '../icon-label/edit'
import { settings } from 'stackable'

const transforms = {
from: [
// When pasting, ensure that the default text block setting is followed
{
bfintal marked this conversation as resolved.
Show resolved Hide resolved
type: 'raw',
isMatch: node =>
node.nodeName === 'P' &&
settings.stackable_enable_text_default_block,
transform: node => {
return createBlock( 'stackable/text', {
text: node.textContent.trim(),
} )
},
priority: 11,
},
{
type: 'block',
isMultiBlock: true,
Expand Down
13 changes: 13 additions & 0 deletions src/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ public function register_settings() {
'default' => true,
)
);

register_setting(
'stackable_editor_settings',
'stackable_enable_text_default_block',
array(
'type' => 'boolean',
'description' => __( 'If this is enabled, the default block when adding a new block will be the Stackable Text block.', STACKABLE_I18N ),
'sanitize_callback' => 'sanitize_text_field',
'show_in_rest' => true,
'default' => false,
)
);
}

public function sanitize_array_setting( $input ) {
Expand All @@ -174,6 +186,7 @@ public function add_settings( $settings ) {
$settings['stackable_auto_collapse_panels'] = get_option( 'stackable_auto_collapse_panels' );
$settings['stackable_enable_block_linking'] = get_option( 'stackable_enable_block_linking' );
$settings['stackable_enable_carousel_lazy_loading'] = get_option( 'stackable_enable_carousel_lazy_loading' );
$settings['stackable_enable_text_default_block'] = get_option( 'stackable_enable_text_default_block' );
return $settings;
}

Expand Down
4 changes: 4 additions & 0 deletions src/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ContentAlign } from './content-align'
import { EditorDom } from './get-editor-dom'
import { ClientTree } from './get-client-id-tree'
import { StackableThemeFonts } from './get-theme-fonts'
import { TextDefaultBlock } from './text-default-block'

/**
* WordPress dependencies
Expand Down Expand Up @@ -42,4 +43,7 @@ fetchSettings().then( response => {
if ( response.stackable_enable_block_linking ) {
registerPlugin( 'stackable-block-linking', { render: BlockLinking } )
}
if ( response.stackable_enable_text_default_block ) {
registerPlugin( 'stackable-text-default-block', { render: TextDefaultBlock } )
}
} )
14 changes: 14 additions & 0 deletions src/plugins/text-default-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { setDefaultBlockName, getDefaultBlockName } from '@wordpress/blocks'
import { useEffect } from '@wordpress/element'

export const TextDefaultBlock = () => {
// Set the default block to stackable/text
useEffect( () => {
if ( getDefaultBlockName() === 'stackable/text' ) {
return null
}
setDefaultBlockName( 'stackable/text' )
}, [] )

return null
}
15 changes: 15 additions & 0 deletions src/welcome/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ const EditorSettings = () => {
'stackable_auto_collapse_panels',
'stackable_enable_block_linking',
'stackable_enable_carousel_lazy_loading',
'stackable_enable_text_default_block',
] ) )
} )
} )
Expand Down Expand Up @@ -413,6 +414,20 @@ const EditorSettings = () => {
} }
help={ __( 'Disable this if you encounter layout or spacing issues when using images inside carousel-type blocks because of image lazy loading.', i18n ) }
/>
<AdminToggleSetting
label={ __( 'Stackable Text as Default Block', i18n ) }
value={ settings.stackable_enable_text_default_block }
onChange={ value => {
setIsBusy( true )
const model = new models.Settings( { stackable_enable_text_default_block: value } ) // eslint-disable-line camelcase
model.save().then( () => setIsBusy( false ) )
setSettings( {
...settings,
stackable_enable_text_default_block: value, // eslint-disable-line camelcase
} )
} }
help={ __( 'If enabled, Stackable Text blocks will be added by default instead of the native Paragraph Block.', i18n ) }
/>
{ isBusy &&
<div className="s-absolute-spinner">
<Spinner />
Expand Down
Loading