Skip to content

Commit

Permalink
feat: added new option to use Stackable text block as the default tex…
Browse files Browse the repository at this point in the history
…t block (#3283)

* feat: create an editor setting for enabling default block as stackable text

* feat: set default block to stackable text when the setting is enabled

* fix: create dedicated plugin for setting default block

* Updated option description

* fix: use raw transform to prevent pasting multiline text of having core/paragraph in between

* fix: make sure setting of default block only run once

* chore: add comments

* fix: load initial enable_text_default_block setting

---------

Co-authored-by: Benjamin Intal <[email protected]>
  • Loading branch information
Arukuen and bfintal authored Sep 5, 2024
1 parent a8af5f4 commit c1994bb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
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
{
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

0 comments on commit c1994bb

Please sign in to comment.