Skip to content

Commit

Permalink
Add properties to String instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed May 4, 2023
1 parent fc88c6e commit 310d80f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useSelectObject } from './use-select-object';
import { useInputAndSelection } from './use-input-and-selection';
import { useSelectionChangeCompat } from './use-selection-change-compat';
import { useDelete } from './use-delete';
import { RichTextString } from '../value';
import { createRichTextString } from '../value';

export function useRichText( {
value = '',
Expand Down Expand Up @@ -88,7 +88,7 @@ export function useRichText( {
multilineTag === 'li' ? [ 'ul', 'ol' ] : undefined,
preserveWhiteSpace,
} )
: { ...value };
: value.value;
if ( disableFormats ) {
record.current.formats = Array( value.length );
record.current.replacements = Array( value.length );
Expand Down Expand Up @@ -144,7 +144,7 @@ export function useRichText( {
if ( disableFormats ) {
_value.current = newRecord.text;
} else {
_value.current = new RichTextString( {
_value.current = createRichTextString( {
value: __unstableBeforeSerialize
? {
...newRecord,
Expand Down
3 changes: 1 addition & 2 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 { RichTextString } from './value';

/** @typedef {import('./types').RichTextValue} RichTextValue */

Expand Down Expand Up @@ -165,7 +164,7 @@ export function create( {
};
}

if ( html instanceof RichTextString ) {
if ( html instanceof String ) {
return html;
}

Expand Down
23 changes: 9 additions & 14 deletions packages/rich-text/src/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
* Internal dependencies
*/
import { toHTMLString } from './to-html-string';
export class RichTextString extends String {
constructor( { value, ...settings } ) {
super( toHTMLString( { 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 ],
} );
}
export function createRichTextString( args ) {
const string = new String( toHTMLString( args ) );

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

return string;
}

0 comments on commit 310d80f

Please sign in to comment.