Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
aad05a6
[squashed] RichText structure:
ellatrix Sep 14, 2018
6de0653
Test deprecated node matcher
ellatrix Sep 14, 2018
0f64289
Fix equality check for deprecated node matcher value
ellatrix Sep 14, 2018
be8ddbd
Deprecate wp.blocks.node and wp.blocks.children
ellatrix Sep 14, 2018
6491282
Value manipulation: ensure passed value is not mutated
ellatrix Sep 17, 2018
d8cd31b
Fix initial selection RichText field
ellatrix Sep 17, 2018
b417549
Represent object format in text string
ellatrix Sep 17, 2018
89c9287
Reduce this.editor dependency
ellatrix Sep 17, 2018
d4295bb
Adjust e2e test expected output
ellatrix Sep 19, 2018
5bb7b12
More docs, rename things
ellatrix Sep 20, 2018
ec01348
Make mobile tests pass
ellatrix Sep 20, 2018
112f42b
Fix richTextValue dependency in test plugin
ellatrix Sep 20, 2018
3a879fd
Remove web logic from native format toolbar
ellatrix Sep 20, 2018
29cf891
Mobile: drop custom formatters as well
ellatrix Sep 20, 2018
aa82875
Mobile: absort formatting toolbar into RichText to reuse formatting m…
ellatrix Sep 20, 2018
209fd38
RN entrypoints for escape-html, rich-text-value packages
hypest Sep 20, 2018
817de47
Rewrite multiline splitting
ellatrix Sep 20, 2018
ce6d906
Rebase: correct new tests
ellatrix Sep 21, 2018
18c41bc
Bump deprecation version and add to deprecated.md
ellatrix Sep 21, 2018
e432f8b
Adjust wrong isEmpty check in cover image block
ellatrix Sep 21, 2018
fb33b33
Update package.json
gziolo Sep 24, 2018
ed4a9ec
Prepare packages for the intial release
gziolo Sep 24, 2018
3274ab5
Fix package-lock.json file for the new packages
gziolo Sep 24, 2018
228264b
Fix typo in pattern transform
ellatrix Sep 25, 2018
cd518dd
Rebase: fix new failing test #9454
ellatrix Sep 25, 2018
be16be0
Abstract tree creation
ellatrix Sep 25, 2018
0607230
Restore children and node sources
ellatrix Sep 25, 2018
2def469
Clean up
ellatrix Sep 25, 2018
c9369ff
Fix isEmptyLine
ellatrix Sep 25, 2018
5cdc4ee
Adjust merge functions
ellatrix Sep 25, 2018
3e4c01a
Clean up
ellatrix Sep 25, 2018
48b8963
Polish: make create() easier to read
ellatrix Sep 26, 2018
d8c35e9
Inline pasted text should be parsed as HTML
ellatrix Sep 26, 2018
e2a9fa2
Fix create() call after 9a754dc
ellatrix Sep 26, 2018
6e6e910
children and node sources should work without default set
ellatrix Sep 26, 2018
13ee0e3
Cast default values of children and node sources as array
ellatrix Sep 26, 2018
445ab90
Fix verse transform
ellatrix Sep 26, 2018
6596b4f
List merging: don't merge empty line
ellatrix Sep 26, 2018
e000668
Use line separator to separate multiline values, instead of double li…
ellatrix Sep 26, 2018
6855216
Change deprecation removal version
ellatrix Sep 26, 2018
a3b7745
Update quote to use line separators
ellatrix Sep 26, 2018
9bc2dd4
Guard against blocks passing null value to RichText
ellatrix Sep 27, 2018
b25545d
Ensure formats with different reference are reduced to different node…
ellatrix Sep 28, 2018
94352e7
rich-text-value => rich-text, better deprecation messages, cast strin…
ellatrix Oct 1, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reduce this.editor dependency
  • Loading branch information
ellatrix committed Oct 1, 2018
commit 89c9287b6e65614de55de88deccaafede80b72b9
38 changes: 23 additions & 15 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
createValue,
getTextContent,
join,
insert,
} from '@wordpress/rich-text-structure';
import deprecated from '@wordpress/deprecated';

Expand Down Expand Up @@ -103,6 +104,8 @@ export class RichText extends Component {
this.applyRecord = this.applyRecord.bind( this );
this.isEmpty = this.isEmpty.bind( this );
this.valueToFormat = this.valueToFormat.bind( this );
this.setRef = this.setRef.bind( this );
this.isActive = this.isActive.bind( this );

this.formatToValue = memize( this.formatToValue.bind( this ), { size: 1 } );

Expand All @@ -126,6 +129,14 @@ export class RichText extends Component {
document.removeEventListener( 'selectionchange', this.onSelectionChange );
}

setRef( node ) {
this.editableRef = node;
}

isActive() {
return this.editableRef === document.activeElement;
}

/**
* Retrieves the settings for this block.
*
Expand Down Expand Up @@ -226,17 +237,15 @@ export class RichText extends Component {

createRecord() {
const { multiline } = this.props;
const rootNode = this.editor.getBody();
const range = this.editor.selection.getRng();
const range = window.getSelection().getRangeAt( 0 );

return create( rootNode, range, multiline, richTextStructureSettings );
return create( this.editableRef, range, multiline, richTextStructureSettings );
}

applyRecord( record ) {
const { multiline } = this.props;
const rootNode = this.editor.getBody();

apply( record, rootNode, multiline );
apply( record, this.editableRef, multiline );
}

isEmpty() {
Expand Down Expand Up @@ -351,7 +360,7 @@ export class RichText extends Component {
} );

if ( typeof content === 'string' ) {
this.editor.insertContent( content );
this.onChange( insert( this.getRecord(), content ) );
} else if ( this.props.onSplit ) {
if ( ! content.length ) {
return;
Expand Down Expand Up @@ -407,7 +416,7 @@ export class RichText extends Component {
*/
onSelectionChange() {
// Ensure it's the active element. This is a global event.
if ( document.activeElement !== this.editor.getBody() ) {
if ( ! this.isActive() ) {
return;
}

Expand Down Expand Up @@ -491,7 +500,7 @@ export class RichText extends Component {
// will include a bogus TinyMCE BR node _after_ the caret, so in a
// forward deletion the isHorizontalEdge function will incorrectly
// interpret the presence of the bogus node as not being at the edge.
const isEdge = ( empty || isHorizontalEdge( this.editor.getBody(), isReverse ) );
const isEdge = ( empty || isHorizontalEdge( this.editableRef, isReverse ) );

if ( ! isEdge ) {
return;
Expand Down Expand Up @@ -568,7 +577,6 @@ export class RichText extends Component {
*/
onKeyDown( event ) {
const dom = this.editor.dom;
const rootNode = this.editor.getBody();
const { keyCode } = event;

const isDelete = keyCode === DELETE || keyCode === BACKSPACE;
Expand Down Expand Up @@ -610,7 +618,7 @@ export class RichText extends Component {

const selectedNode = this.editor.selection.getNode();

if ( selectedNode.parentNode !== rootNode ) {
if ( selectedNode.parentNode !== this.editableRef ) {
return;
}

Expand All @@ -620,7 +628,7 @@ export class RichText extends Component {

event.preventDefault();

const childNodes = Array.from( rootNode.childNodes );
const childNodes = Array.from( this.editableRef.childNodes );
const index = dom.nodeIndex( selectedNode );
const beforeNodes = childNodes.slice( 0, index );
const afterNodes = childNodes.slice( index + 1 );
Expand Down Expand Up @@ -674,7 +682,7 @@ export class RichText extends Component {

scrollToRect( rect ) {
const { top: caretTop } = rect;
const container = getScrollContainer( this.editor.getBody() );
const container = getScrollContainer( this.editableRef );

if ( ! container ) {
return;
Expand Down Expand Up @@ -748,7 +756,7 @@ export class RichText extends Component {
}

onNodeChange( { parents } ) {
if ( document.activeElement !== this.editor.getBody() ) {
if ( ! this.isActive() ) {
return;
}

Expand All @@ -774,7 +782,6 @@ export class RichText extends Component {
const { tagName, value } = this.props;

if (
this.editor &&
tagName === prevProps.tagName &&
value !== prevProps.value &&
value !== this.savedContent
Expand All @@ -791,7 +798,7 @@ export class RichText extends Component {
const record = this.formatToValue( value );
const { start, end } = this.state;

if ( this.editor.hasFocus() ) {
if ( this.isActive() ) {
record.start = start;
record.end = end;
}
Expand Down Expand Up @@ -924,6 +931,7 @@ export class RichText extends Component {
onPaste={ this.onPaste }
onInput={ this.onInput }
multilineTag={ MultilineTag }
setRef={ this.setRef }
/>
{ isPlaceholderVisible &&
<Tagname
Expand Down
4 changes: 4 additions & 0 deletions packages/editor/src/components/rich-text/tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export default class TinyMCE extends Component {
bindEditorNode( editorNode ) {
this.editorNode = editorNode;

if ( this.props.setRef ) {
this.props.setRef( editorNode );
}

/**
* A ref function can be used for cleanup because React calls it with
* `null` when unmounting.
Expand Down
10 changes: 9 additions & 1 deletion packages/rich-text-structure/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ function getEmptyRecord() {
*/
export function create( element, range, multilineTag, settings ) {
if ( typeof element === 'string' ) {
element = createElement( element );
if ( element.indexOf( '<' ) !== -1 || /[^\s]+;/.test( element ) ) {
// May be HTML.
element = createElement( element );
} else {
return {
formats: Array( element.length ),
text: element,
};
}
}

if ( ! multilineTag ) {
Expand Down
10 changes: 10 additions & 0 deletions packages/rich-text-structure/src/insert.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* External dependencies
*/

import { create } from './create';

/**
* Inserts the second given record into the first.
* The value in between the start and end indices will be removed.
Expand All @@ -17,6 +23,10 @@ export function insert(
startIndex = start,
endIndex = end
) {
if ( typeof recordToInsert === 'string' ) {
recordToInsert = create( recordToInsert );
}

const index = startIndex + recordToInsert.text.length;

return {
Expand Down
3 changes: 2 additions & 1 deletion packages/rich-text-structure/src/is-collapsed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*
* @param {Object} record The rich text record to check.
*
* @return {boolean} True if the selection is collapsed, false if not.
* @return {?boolean} True if the selection is collapsed, false if not,
* undefined if there is no selection.
*/
export function isCollapsed( { start, end } ) {
if ( start === undefined || end === undefined ) {
Expand Down
19 changes: 11 additions & 8 deletions packages/rich-text-structure/src/join.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Internal dependencies
*/

import { create } from './create';

/**
* Combines the given records into one record, separated by the given record or
* text.
Expand All @@ -9,15 +15,12 @@
*
* @return {Object} A new combined record.
*/
export function join( records, separator ) {
return records.reduce( ( accumlator, { formats, text } ) => {
if ( typeof separator === 'string' ) {
separator = {
formats: Array( separator.length ),
text: separator,
};
}
export function join( records, separator = '' ) {
if ( typeof separator === 'string' ) {
separator = create( separator );
}

return records.reduce( ( accumlator, { formats, text } ) => {
return {
text: accumlator.text + separator.text + text,
formats: accumlator.formats.concat( separator.formats, formats ),
Expand Down