Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Aug 13, 2022
1 parent 5b6508d commit 54e3708
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
40 changes: 32 additions & 8 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useRegistry } from '@wordpress/data';
/**
* Internal dependencies
*/
import { create, RichTextValue } from '../create';
import { create } from '../create';
import { apply } from '../to-dom';
import { toHTMLString } from '../to-html-string';
import { useDefaultStyle } from './use-default-style';
Expand All @@ -19,6 +19,26 @@ import { useSelectObject } from './use-select-object';
import { useIndentListItemOnSpace } from './use-indent-list-item-on-space';
import { useInputAndSelection } from './use-input-and-selection';
import { useDelete } from './use-delete';
function RichTextValue( { value, ...settings } ) {
for ( const key in value ) {
Object.defineProperty( this, key, {
value: value[ key ],
enumerable: true,
} );
}

for ( const key in settings ) {
Object.defineProperty( this, key, { value: settings[ key ] } );
}
}

RichTextValue.prototype.toString = function () {
return toHTMLString( {
value: { ...this },
multilineTag: this.multilineTag,
preserveWhiteSpace: this.preserveWhiteSpace,
} );
};

export function useRichText( {
value = '',
Expand Down Expand Up @@ -87,7 +107,7 @@ export function useRichText( {
multilineTag === 'li' ? [ 'ul', 'ol' ] : undefined,
preserveWhiteSpace,
} )
: value;
: { ...value };
if ( disableFormats ) {
record.current.formats = Array( value.length );
record.current.replacements = Array( value.length );
Expand Down Expand Up @@ -143,12 +163,16 @@ export function useRichText( {
if ( disableFormats ) {
_value.current = newRecord.text;
} else {
_value.current = __unstableBeforeSerialize
? new RichTextValue( {
...newRecord,
formats: __unstableBeforeSerialize( newRecord ),
} )
: newRecord;
_value.current = new RichTextValue( {
value: __unstableBeforeSerialize
? {
...newRecord,
formats: __unstableBeforeSerialize( newRecord ),
}
: newRecord,
multilineTag,
preserveWhiteSpace,
} );
}

const { start, end, formats, text } = newRecord;
Expand Down
21 changes: 4 additions & 17 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
OBJECT_REPLACEMENT_CHARACTER,
ZWNBSP,
} from './special-characters';
import { toHTMLString } from './to-html-string';

/**
* @typedef {Object} RichTextFormat
Expand All @@ -26,18 +25,6 @@ import { toHTMLString } from './to-html-string';
* @typedef {Array<RichTextFormat>} RichTextFormatList
*/

export function RichTextValue( value ) {
for ( const key of Object.getOwnPropertyNames( value ) ) {
this[ key ] = value[ key ];
}

return this;
}

RichTextValue.prototype.toString = function () {
return toHTMLString( { value: { ...this } } );
};

/**
* @typedef {Object} RichTextValue
*
Expand All @@ -49,11 +36,11 @@ RichTextValue.prototype.toString = function () {
*/

function createEmptyValue() {
return new RichTextValue( {
return {
formats: [],
replacements: [],
text: '',
} );
};
}

function toFormat( { type, attributes } ) {
Expand Down Expand Up @@ -187,11 +174,11 @@ export function create( {
preserveWhiteSpace,
} = {} ) {
if ( typeof text === 'string' && text.length > 0 ) {
return new RichTextValue( {
return {
formats: Array( text.length ),
replacements: Array( text.length ),
text,
} );
};
}

if ( typeof html === 'string' && html.length > 0 ) {
Expand Down

0 comments on commit 54e3708

Please sign in to comment.