Skip to content

Commit

Permalink
extend String
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed May 4, 2023
1 parent 2bbe676 commit fc88c6e
Showing 1 changed file with 2 additions and 42 deletions.
44 changes: 2 additions & 42 deletions packages/rich-text/src/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Internal dependencies
*/
import { toHTMLString } from './to-html-string';
export class RichTextString {
export class RichTextString extends String {
constructor( { value, ...settings } ) {
super( toHTMLString( { value, ...settings } ) );
for ( const key in value ) {
Object.defineProperty( this, key, {
value: value[ key ],
Expand All @@ -17,45 +18,4 @@ export class RichTextString {
} );
}
}

toString() {
if ( ! this.cache ) {
Object.defineProperty( this, 'cache', {
value: toHTMLString( {
value: { ...this },
multilineTag: this.multilineTag,
preserveWhiteSpace: this.preserveWhiteSpace,
} ),
} );
}

return this.cache;
}
}

Object.getOwnPropertyNames( String.prototype )
.filter(
( prop ) =>
typeof String.prototype[ prop ] === 'function' &&
prop !== 'constructor' &&
prop !== 'toString'
)
.forEach( ( method ) => {
Object.defineProperty( RichTextString.prototype, method, {
value() {
return this.toString()[ method ]( ...arguments );
},
} );
} );

Object.defineProperty( RichTextString.prototype, 'length', {
get() {
return this.toString().length;
},
} );

Object.defineProperty( RichTextString.prototype, 'toJSON', {
value() {
return this.toString();
},
} );

0 comments on commit fc88c6e

Please sign in to comment.