Skip to content

Commit 86ae8b1

Browse files
authored
TinyMCE per block: Small tweaks (#244)
1 parent 73fb96b commit 86ae8b1

File tree

9 files changed

+21
-29
lines changed

9 files changed

+21
-29
lines changed

tinymce-per-block/src/blocks/html-block/form.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ export default class HtmlBlockForm extends Component {
2828
render() {
2929
const { api, block, isSelected, first, last, focusConfig } = this.props;
3030
const splitValue = ( left, right ) => {
31-
api.change( {
32-
content: left,
33-
externalChange: ( block.externalChange || 0 ) + 1
34-
} );
31+
api.change( { content: left } );
3532
if ( right ) {
3633
api.appendBlock( {
3734
...block,
@@ -65,7 +62,6 @@ export default class HtmlBlockForm extends Component {
6562
<EditableComponent
6663
ref={ this.bindEditable }
6764
content={ block.content }
68-
externalChange={ block.externalChange }
6965
moveCursorUp={ api.moveCursorUp }
7066
moveCursorDown={ api.moveCursorDown }
7167
splitValue={ splitValue }

tinymce-per-block/src/blocks/html-block/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ registerBlock( 'html', {
4343
const currentBlock = state.blocks[ index ];
4444
const blockToMerge = state.blocks[ index + 1 ];
4545
const newBlock = Object.assign( {}, currentBlock, {
46-
content: currentBlock.content + blockToMerge.content,
47-
externalChange: ( currentBlock.externalChange || 0 ) + 1
46+
content: currentBlock.content + blockToMerge.content
4847
} );
4948
const newBlocks = [
5049
...state.blocks.slice( 0, index ),

tinymce-per-block/src/blocks/inline-text-block/form.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ export default class InlineTextBlockForm extends Component {
1717
const { api, block, setToolbarState, focusConfig } = this.props;
1818

1919
const splitValue = ( left, right ) => {
20-
api.change( {
21-
content: left,
22-
externalChange: ( block.externalChange || 0 ) + 1
23-
} );
20+
api.change( { content: left } );
2421
if ( right ) {
2522
api.appendBlock( {
2623
...block,
@@ -35,7 +32,6 @@ export default class InlineTextBlockForm extends Component {
3532
<EditableComponent
3633
ref={ this.bindEditable }
3734
content={ block.content }
38-
externalChange={ block.externalChange }
3935
moveCursorUp={ api.moveCursorUp }
4036
moveCursorDown={ api.moveCursorDown }
4137
splitValue={ splitValue }

tinymce-per-block/src/blocks/quote-block/form.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ export default class QuoteBlockForm extends Component {
4545
render() {
4646
const { api, block, first, last, isSelected, focusConfig } = this.props;
4747
const splitValue = ( left, right ) => {
48-
api.change( {
49-
cite: left,
50-
citeExternalChange: ( block.citeExternalChange || 0 ) + 1
51-
} );
48+
api.change( { cite: left } );
5249
api.appendBlock( {
5350
blockType: 'text',
5451
content: right
@@ -100,7 +97,6 @@ export default class QuoteBlockForm extends Component {
10097
<EditableComponent
10198
ref={ this.bindContent }
10299
content={ block.content }
103-
externalChange={ block.externalChange }
104100
moveCursorUp={ api.moveCursorUp }
105101
moveCursorDown={ this.moveToCite }
106102
mergeWithPrevious={ api.mergeWithPrevious }
@@ -122,7 +118,6 @@ export default class QuoteBlockForm extends Component {
122118
mergeWithPrevious={ this.moveToContent }
123119
remove={ this.moveToContent }
124120
content={ block.cite }
125-
externalChange={ block.citeExternalChange }
126121
splitValue={ splitValue }
127122
onChange={ ( value ) => api.change( { cite: value } ) }
128123
setToolbarState={ focusInput === 'cite' ? this.setToolbarState : undefined }

tinymce-per-block/src/blocks/text-block/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ registerBlock( 'text', {
4545
const content = div.childNodes.length === 1 && div.firstChild.nodeName === 'P'
4646
? div.firstChild.innerHTML
4747
: block.content;
48-
const rawContent = `<p style="text-align: ${ block.align };">${ content }</p>`;
48+
const alignStyle = block.align && block.align !== 'no-align'
49+
? ` style="text-align: ${ block.align };"`
50+
: '';
51+
const rawContent = `<p${ alignStyle }>${ content }</p>`;
4952

5053
return {
5154
blockType: 'text',

tinymce-per-block/src/external/wp-blocks/editable/index.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ export default class EditableComponent extends Component {
5050

5151
updateContent() {
5252
// This could not be called on each content change, it used to change the cursor position
53-
const bookmark = this.editor.selection.getBookmark( 2, true );
53+
let bookmark;
54+
if ( this.props.focusConfig ) {
55+
bookmark = this.editor.selection.getBookmark( 2, true );
56+
}
5457
this.editor.setContent( this.props.content );
55-
this.editor.selection.moveToBookmark( bookmark );
58+
if ( this.props.focusConfig ) {
59+
this.editor.selection.moveToBookmark( bookmark );
60+
}
5661
}
5762

5863
executeCommand = ( ...args ) => {
@@ -70,9 +75,7 @@ export default class EditableComponent extends Component {
7075
this.focus();
7176
}
7277

73-
// We should be able to compare content instead
74-
// But I came up with externalChange due the moving cursor bugs when spitting/merging content
75-
if ( this.props.externalChange !== prevProps.externalChange ) {
78+
if ( this.props.content !== prevProps.content ) {
7679
this.updateContent();
7780
}
7881
}
@@ -143,6 +146,7 @@ export default class EditableComponent extends Component {
143146
const after = getHtml( childNodes.slice( splitIndex ) );
144147
const hasAfter = !! childNodes.slice( splitIndex )
145148
.reduce( ( memo, node ) => memo + node.textContent, '' );
149+
this.editor.setContent( this.props.content );
146150
if ( before ) {
147151
this.props.splitValue( before, hasAfter ? after : '' );
148152
}
@@ -202,7 +206,7 @@ export default class EditableComponent extends Component {
202206
this.editor = editor;
203207

204208
editor.on( 'init', this.onInit );
205-
editor.on( 'change focusout undo redo', this.onChange );
209+
editor.on( 'focusout undo redo', this.onChange );
206210
editor.on( 'keydown', this.onKeyDown );
207211
editor.on( 'paste', this.onPaste );
208212
editor.on( 'nodechange', this.syncToolbar );

tinymce-per-block/src/renderers/block/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* External dependencies
33
*/
44
import { createElement, Component } from 'wp-elements';
5-
import { map, debounce } from 'lodash';
5+
import { map, debounce, isEqual } from 'lodash';
66
import { findDOMNode } from 'react-dom';
77
import classNames from 'classnames';
88
import { getBlock } from 'wp-blocks';

tinymce-per-block/src/renderers/block/updater.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const blockLevelUpdater = ( state, command ) => {
2222
},
2323

2424
append: ( { block: commandBlock } ) => {
25-
const createdBlock = commandBlock ? commandBlock : { blockType: 'text', content: ' ' };
25+
const createdBlock = commandBlock ? commandBlock : { blockType: 'text', content: '' };
2626
const appenedBlockId = uniqueId();
2727
const newBlocks = [
2828
...state.blocks.slice( 0, currentIndex + 1 ),
@@ -92,7 +92,7 @@ const blockLevelUpdater = ( state, command ) => {
9292
moveCursorDown: () => {
9393
const nextBlock = state.blocks[ currentIndex + 1 ];
9494
return mergeStates( {
95-
focus: nextBlock ? { uid: nextBlock.uid, config: { end: true } } : state.focus,
95+
focus: nextBlock ? { uid: nextBlock.uid, config: { start: true } } : state.focus,
9696
selected: null
9797
} );
9898
},

tinymce-per-block/src/utils/state.js

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const mergeInlineTextBlocks = ( state, index ) => {
2020
const blockToMerge = state.blocks[ index + 1 ];
2121
const newBlock = Object.assign( {}, currentBlock, {
2222
content: getLeaves( currentBlock.content ) + getLeaves( blockToMerge.content ),
23-
externalChange: ( currentBlock.externalChange || 0 ) + 1
2423
} );
2524
const newBlocks = [
2625
...state.blocks.slice( 0, index ),

0 commit comments

Comments
 (0)