Skip to content

Commit 567599a

Browse files
committed
Revert React tree
1 parent 931615c commit 567599a

File tree

7 files changed

+93
-219
lines changed

7 files changed

+93
-219
lines changed

packages/rich-text/src/component/content.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/rich-text/src/component/editable.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

packages/rich-text/src/component/index.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import deprecated from '@wordpress/deprecated';
2323
* Internal dependencies
2424
*/
2525
import FormatEdit from './format-edit';
26-
import Editable from './editable';
2726
import { create } from '../create';
2827
import { apply } from '../to-dom';
2928
import { toHTMLString } from '../to-html-string';
@@ -57,6 +56,24 @@ const INSERTION_INPUT_TYPES_TO_IGNORE = new Set( [
5756
'insertLink',
5857
] );
5958

59+
// In HTML, leading and trailing spaces are not visible, and multiple spaces
60+
// elsewhere are visually reduced to one space. This rule prevents spaces from
61+
// collapsing so all space is visible in the editor and can be removed. It also
62+
// prevents some browsers from inserting non-breaking spaces at the end of a
63+
// line to prevent the space from visually disappearing. Sometimes these non
64+
// breaking spaces can linger in the editor causing unwanted non breaking spaces
65+
// in between words. If also prevent Firefox from inserting a trailing `br` node
66+
// to visualise any trailing space, causing the element to be saved.
67+
//
68+
// > Authors are encouraged to set the 'white-space' property on editing hosts
69+
// > and on markup that was originally created through these editing mechanisms
70+
// > to the value 'pre-wrap'. Default HTML whitespace handling is not well
71+
// > suited to WYSIWYG editing, and line wrapping will not work correctly in
72+
// > some corner cases if 'white-space' is left at its default value.
73+
// >
74+
// > https://html.spec.whatwg.org/multipage/interaction.html#best-practices-for-in-page-editors
75+
const whiteSpace = 'pre-wrap';
76+
6077
/**
6178
* Global stylesheet.
6279
*/
@@ -143,6 +160,8 @@ class RichText extends Component {
143160
console.warn( 'RichText cannot be used with an inline container. Please use a different tagName.' );
144161
}
145162
}
163+
164+
this.applyRecord( this.record, { domOnly: true } );
146165
}
147166

148167
createRecord() {
@@ -814,9 +833,11 @@ class RichText extends Component {
814833
__unstableIsSelected: isSelected,
815834
} = this.props;
816835

836+
// Check if tag name changed.
837+
let shouldReapply = tagName !== prevProps.tagName;
838+
817839
// Check if the content changed.
818-
let shouldReapply = (
819-
tagName === prevProps.tagName &&
840+
shouldReapply = shouldReapply || (
820841
value !== prevProps.value &&
821842
value !== this.value
822843
);
@@ -935,25 +956,21 @@ class RichText extends Component {
935956

936957
Editable( props ) {
937958
const {
938-
tagName,
939-
style,
959+
tagName: TagName = 'div',
960+
style = {},
940961
className,
941962
placeholder,
942963
forwardedRef,
943-
__unstableMultilineTag: multilineTag,
944964
} = this.props;
945965
const ariaProps = pickBy( this.props, ( value, key ) =>
946966
startsWith( key, 'aria-' ) );
947967

948968
return (
949-
<Editable
969+
<TagName
950970
{ ...props }
951971
{ ...ariaProps }
952972
ref={ forwardedRef }
953-
tagName={ tagName }
954-
style={ style }
955-
value={ this.record }
956-
placeholder={ placeholder }
973+
style={ { ...style, whiteSpace } }
957974
className={ classnames( 'rich-text', className ) }
958975
onPaste={ this.onPaste }
959976
onInput={ this.onInput }
@@ -973,8 +990,11 @@ class RichText extends Component {
973990
onKeyUp={ this.onSelectionChange }
974991
onMouseUp={ this.onSelectionChange }
975992
onTouchEnd={ this.onSelectionChange }
976-
multilineTag={ multilineTag }
977-
prepareEditableTree={ createPrepareEditableTree( this.props, 'format_prepare_functions' ) }
993+
role="textbox"
994+
aria-multiline
995+
aria-label={ placeholder }
996+
contentEditable
997+
suppressContentEditableWarning
978998
/>
979999
);
9801000
}

packages/rich-text/src/to-element.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

packages/rich-text/src/to-html-string.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
* Internal dependencies
1313
*/
1414

15-
import { toObjectTree } from './to-object-tree';
15+
import { toTree } from './to-tree';
1616

1717
/**
1818
* Create an HTML string from a Rich Text value. If a `multilineTag` is
@@ -25,10 +25,67 @@ import { toObjectTree } from './to-object-tree';
2525
* @return {string} HTML string.
2626
*/
2727
export function toHTMLString( { value, multilineTag } ) {
28-
const tree = toObjectTree( { value, multilineTag } );
28+
const tree = toTree( {
29+
value,
30+
multilineTag,
31+
createEmpty,
32+
append,
33+
getLastChild,
34+
getParent,
35+
isText,
36+
getText,
37+
remove,
38+
appendText,
39+
} );
40+
2941
return createChildrenHTML( tree.children );
3042
}
3143

44+
function createEmpty() {
45+
return {};
46+
}
47+
48+
function getLastChild( { children } ) {
49+
return children && children[ children.length - 1 ];
50+
}
51+
52+
function append( parent, object ) {
53+
if ( typeof object === 'string' ) {
54+
object = { text: object };
55+
}
56+
57+
object.parent = parent;
58+
parent.children = parent.children || [];
59+
parent.children.push( object );
60+
return object;
61+
}
62+
63+
function appendText( object, text ) {
64+
object.text += text;
65+
}
66+
67+
function getParent( { parent } ) {
68+
return parent;
69+
}
70+
71+
function isText( { text } ) {
72+
return typeof text === 'string';
73+
}
74+
75+
function getText( { text } ) {
76+
return text;
77+
}
78+
79+
function remove( object ) {
80+
const index = object.parent.children.indexOf( object );
81+
82+
if ( index !== -1 ) {
83+
object.parent.children.splice( index, 1 );
84+
}
85+
86+
return object;
87+
}
88+
3289
function createElementHTML( { type, attributes, object, children } ) {
3390
let attributeString = '';
3491

packages/rich-text/src/to-object-tree.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

packages/rich-text/src/to-tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export function toTree( {
258258
// Necessary to prevent the placeholder from catching
259259
// selection. The placeholder is also not editable after
260260
// all.
261-
contentEditable: 'false',
261+
contenteditable: 'false',
262262
},
263263
} );
264264
}

0 commit comments

Comments
 (0)