Skip to content

Commit

Permalink
Reuse toHTMLString
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Sep 11, 2023
1 parent 1c09d7c commit 15b4e94
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export function usePasteHandler( props ) {
disableFilters: !! clipboardData.getData( 'rich-text' ),
} );

if ( typeof content === 'string' ) {
if ( typeof content === 'string' || content instanceof String ) {
const valueToInsert = create( { html: content } );
addActiveFormats( valueToInsert, value.activeFormats );
onChange( insert( value, valueToInsert ) );
Expand Down
7 changes: 4 additions & 3 deletions packages/blocks/src/api/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export { attr, prop, text, query } from 'hpq';
/**
* WordPress dependencies
*/
import { __unstableCreateRichTextString, create } from '@wordpress/rich-text';
import { toHTMLString, create } from '@wordpress/rich-text';

/**
* Internal dependencies
Expand Down Expand Up @@ -43,8 +43,9 @@ export function html( selector, multilineTag, preserveWhiteSpace ) {
return value;
}

return __unstableCreateRichTextString( {
// This is faste because we don't need to parse the HTML string.
// Note that this stores a reference to the created rich text value.
return toHTMLString( {
// This is fast because we don't need to parse the HTML string.
value: create( { element: match } ),
multilineTag,
preserveWhiteSpace,
Expand Down
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 @@ -10,6 +10,7 @@ import { useRegistry } from '@wordpress/data';
*/
import { create } from '../create';
import { apply } from '../to-dom';
import { toHTMLString } from '../to-html-string';
import { useDefaultStyle } from './use-default-style';
import { useBoundaryStyle } from './use-boundary-style';
import { useCopyHandler } from './use-copy-handler';
Expand All @@ -18,7 +19,6 @@ 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 { createRichTextString } from '../value';

export function useRichText( {
value = '',
Expand Down Expand Up @@ -135,7 +135,7 @@ export function useRichText( {
if ( disableFormats ) {
_value.current = newRecord.text;
} else {
_value.current = createRichTextString( {
_value.current = toHTMLString( {
value: __unstableBeforeSerialize
? {
...newRecord,
Expand Down Expand Up @@ -164,7 +164,7 @@ export function useRichText( {
function handleChangesUponInit( newRecord ) {
record.current = newRecord;

_value.current = createRichTextString( {
_value.current = toHTMLString( {
value: __unstableBeforeSerialize
? {
...newRecord,
Expand Down
8 changes: 4 additions & 4 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ export function create( {
__unstableIsEditableTree: isEditableTree,
preserveWhiteSpace,
} = {} ) {
if ( html instanceof String ) {
return html.value;
}

if ( typeof text === 'string' && text.length > 0 ) {
return {
formats: Array( text.length ),
Expand All @@ -157,10 +161,6 @@ export function create( {
};
}

if ( html instanceof String ) {
return html.value;
}

if ( typeof html === 'string' && html.length > 0 ) {
// It does not matter which document this is, we're just using it to
// parse.
Expand Down
1 change: 0 additions & 1 deletion packages/rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export { toHTMLString } from './to-html-string';
export { toggleFormat } from './toggle-format';
export { unregisterFormatType } from './unregister-format-type';
export { createElement as __unstableCreateElement } from './create-element';
export { createRichTextString as __unstableCreateRichTextString } from './value';

export { useAnchorRef } from './component/use-anchor-ref';
export { useAnchor } from './component/use-anchor';
Expand Down
11 changes: 10 additions & 1 deletion packages/rich-text/src/to-html-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ export function toHTMLString( { value, preserveWhiteSpace } ) {
appendText,
} );

return createChildrenHTML( tree.children );
const string = new String( createChildrenHTML( tree.children ) );

Object.defineProperty( string, 'value', {
value,
} );
Object.defineProperty( string, 'preserveWhiteSpace', {
value: preserveWhiteSpace,
} );

return string;
}

function createEmpty() {
Expand Down
16 changes: 0 additions & 16 deletions packages/rich-text/src/value.js

This file was deleted.

0 comments on commit 15b4e94

Please sign in to comment.