Skip to content

Commit 82c598d

Browse files
committed
Format library: visuali
se non-breaking spaces
1 parent 95f6422 commit 82c598d

2 files changed

Lines changed: 62 additions & 9 deletions

File tree

  • packages

‎packages/block-editor/src/components/rich-text/content.scss‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ figcaption.block-editor-rich-text__editable [data-rich-text-placeholder]::before
5454
.rich-text [contenteditable="false"]::selection {
5555
background-color: transparent;
5656
}
57+
58+
.is-selected .non-breaking-space {
59+
outline: 1px solid currentColor;
60+
outline-offset: -1px;
61+
opacity: 0.8;
62+
}
Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,73 @@
11
import { __ } from '@wordpress/i18n';
2-
import { insert } from '@wordpress/rich-text';
2+
import { applyFormat, insert, useAnchor } from '@wordpress/rich-text';
33
import { RichTextShortcut } from '@wordpress/block-editor';
4+
import { Popover } from '@wordpress/components';
45

56
const name = 'core/non-breaking-space';
67
const title = __( 'Non breaking space' );
78

9+
function PopoverAnchor( { contentRef } ) {
10+
const popoverAnchor = useAnchor( {
11+
editableContentElement: contentRef.current,
12+
settings: nonBreakingSpace,
13+
} );
14+
15+
return (
16+
<Popover anchor={ popoverAnchor }>
17+
<div style={ { whiteSpace: 'nowrap', padding: '4px' } }>
18+
{ __( 'Non-breaking space' ) }
19+
</div>
20+
</Popover>
21+
);
22+
}
23+
824
export const nonBreakingSpace = {
925
name,
1026
title,
11-
tagName: 'nbsp',
12-
className: null,
13-
edit( { value, onChange } ) {
27+
tagName: 'span',
28+
className: 'non-breaking-space',
29+
edit( { value, onChange, contentRef } ) {
1430
function addNonBreakingSpace() {
1531
onChange( insert( value, '\u00a0' ) );
1632
}
1733

34+
const selectedValue =
35+
value.start && value.end
36+
? value.text.slice( value.start, value.end )
37+
: null;
38+
1839
return (
19-
<RichTextShortcut
20-
type="primaryShift"
21-
character=" "
22-
onUse={ addNonBreakingSpace }
23-
/>
40+
<>
41+
<RichTextShortcut
42+
type="primaryShift"
43+
character=" "
44+
onUse={ addNonBreakingSpace }
45+
/>
46+
{ selectedValue === '\u00a0' && (
47+
<PopoverAnchor contentRef={ contentRef } />
48+
) }
49+
</>
2450
);
2551
},
52+
__experimentalCreatePrepareEditableTree() {
53+
return ( formats, text ) => {
54+
const NBSP = '\u00a0';
55+
let record = { formats, text };
56+
let index = -1;
57+
58+
do {
59+
index = text.indexOf( NBSP, index + 1 );
60+
if ( index !== -1 ) {
61+
record = applyFormat(
62+
record,
63+
{ type: name },
64+
index,
65+
index + 1
66+
);
67+
}
68+
} while ( index !== -1 );
69+
70+
return record.formats;
71+
};
72+
},
2673
};

0 commit comments

Comments
 (0)