Skip to content

Commit

Permalink
Ensure default link text of (Edit) is translatable #5
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Sep 9, 2021
1 parent 5f1b080 commit 26c7796
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 39 deletions.
5 changes: 2 additions & 3 deletions block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
"type": "string"
},
"label": {
"type": "string",
"default": "(Edit)"
"type": "string"
}
},
"supports": {
"supports": {
"html": false,
"color": {
"gradients": true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sb-post-edit-block",
"version": "0.3.0",
"version": "0.3.1",
"description": "Post edit block to allow direct editing of the post",
"author": "bobbingwide",
"license": "GPL-2.0-or-later",
Expand Down
13 changes: 7 additions & 6 deletions sb-post-edit-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: Post edit block to allow direct editing of the post
* Requires at least: 5.7
* Requires PHP: 7.3
* Version: 0.3.0
* Version: 0.3.1
* Author: bobbingwide
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand All @@ -30,10 +30,7 @@ function oik_sb_sb_post_edit_block_block_init() {
* from the locale specific .json file in the languages folder
*/
$ok = wp_set_script_translations( 'oik-sb-sb-post-edit-block-editor-script', 'sb-post-edit-block' , __DIR__ .'/languages' );
//echo "?$ok?";

//add_filter( 'load_script_textdomain_relative_path', 'oik_load_script_textdomain_relative_path', 10, 2);

}

function oik_sb_sb_post_edit_block_loaded() {
Expand All @@ -42,7 +39,7 @@ function oik_sb_sb_post_edit_block_loaded() {

function oik_sb_sb_post_edit_block_load_plugin_textdomain( $domain="sb-post-edit-block" ) {
$languages_dir = "$domain/languages";
bw_trace2( $languages_dir, "languages dir" );
//bw_trace2( $languages_dir, "languages dir" );
$loaded = load_plugin_textdomain( $domain, false, $languages_dir );
return $loaded;
}
Expand All @@ -63,8 +60,12 @@ function oik_sb_sb_post_edit_block_dynamic_block( $attributes ) {
$html = '<span></span>';
return $html;
}
$text = empty( $attributes['label']) ? __( '(Edit)', 'sb-post-edit-block' ) : $attributes['label'];

/** Use the default value for the link text if the label attribute is not set or just blank.
*/
$text = empty( $attributes['label']) ? '' : $attributes['label'];
$text = trim( $text );
$text = empty( $text ) ? __( '(Edit)', 'sb-post-edit-block' ) : $text;

$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";

Expand Down
37 changes: 19 additions & 18 deletions src/post-edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ export default function Edit ( { attributes, className, isSelected, setAttribute
};
const { textAlign, label } = attributes;

let displayLabel = label;

/** Force a default value if the label is empty or all blank */
if ( label === undefined || label.trim() === '') {
setAttributes( { label: undefined } );
displayLabel = __( '(Edit)', 'sb-post-edit-block');
}

const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );


return (
<>
<BlockControls group="block">
Expand All @@ -58,24 +65,18 @@ export default function Edit ( { attributes, className, isSelected, setAttribute
</BlockControls>
<InspectorControls>
<PanelBody>
<PanelRow>
<TextControl
label={ __("Link text", 'sb-post-edit-block' ) }
value={ label }
onChange={ onChangeLabel }
/>
</PanelRow>
<PanelRow>
<TextControl
label={ __("Link text", 'sb-post-edit-block' ) }
value={ displayLabel }
onChange={ onChangeLabel }
/>
</PanelRow>
</PanelBody>

</InspectorControls>



<div { ...blockProps }><a href="#">{ label }</a>

</div>
</>


<div { ...blockProps }>
<a href="#">{ displayLabel }</a>
</div>
</>
);
}
19 changes: 8 additions & 11 deletions src/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
import { registerBlockType } from '@wordpress/blocks';

import { __ } from '@wordpress/i18n';

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* All files containing `style` keyword are bundled together. The code used
Expand All @@ -18,26 +20,21 @@ import './style.scss';
* Internal dependencies
*/
import Edit from './edit';
import save from './save';

import metadata from '../../block.json';

/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
*/
registerBlockType( 'oik-sb/sb-post-edit-block', {
example: {
attributes: {
label: __( "(Edit)", 'sb-post-edit-block')
}
},
/**
* @see ./edit.js
*/
edit: Edit,
attributes: metadata.attributes,
supports: metadata.supports

/**
* @see ./save.js
save,
*/
edit: Edit
} );

0 comments on commit 26c7796

Please sign in to comment.