From 310d80fff6982538cd55ce143f5ce9aaee771439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Thu, 4 May 2023 09:56:02 +0200 Subject: [PATCH] Add properties to String instance --- packages/rich-text/src/component/index.js | 6 +++--- packages/rich-text/src/create.js | 3 +-- packages/rich-text/src/value.js | 23 +++++++++-------------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/rich-text/src/component/index.js b/packages/rich-text/src/component/index.js index 08196333afd377..fe3d887b3ba66d 100644 --- a/packages/rich-text/src/component/index.js +++ b/packages/rich-text/src/component/index.js @@ -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 = '', @@ -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 ); @@ -144,7 +144,7 @@ export function useRichText( { if ( disableFormats ) { _value.current = newRecord.text; } else { - _value.current = new RichTextString( { + _value.current = createRichTextString( { value: __unstableBeforeSerialize ? { ...newRecord, diff --git a/packages/rich-text/src/create.js b/packages/rich-text/src/create.js index fed30ad07f7a49..c4f082eb289f7f 100644 --- a/packages/rich-text/src/create.js +++ b/packages/rich-text/src/create.js @@ -14,7 +14,6 @@ import { OBJECT_REPLACEMENT_CHARACTER, ZWNBSP, } from './special-characters'; -import { RichTextString } from './value'; /** @typedef {import('./types').RichTextValue} RichTextValue */ @@ -165,7 +164,7 @@ export function create( { }; } - if ( html instanceof RichTextString ) { + if ( html instanceof String ) { return html; } diff --git a/packages/rich-text/src/value.js b/packages/rich-text/src/value.js index 5817b2d6d6a882..5beb9046612b32 100644 --- a/packages/rich-text/src/value.js +++ b/packages/rich-text/src/value.js @@ -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; }