Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lang and dir attributes to text-formatting tools #49985

Merged
merged 34 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
10afc01
WIP - lang attribute
aristath Apr 20, 2023
26f970c
applying attributes
aristath Apr 20, 2023
c5d1438
Porting of JB plugin.
afercia Apr 20, 2023
8cb75d3
lang & dir are defined in the upper scope
aristath Apr 21, 2023
88a5d12
add translation icon
aristath Apr 21, 2023
c29c917
rename to language
aristath Apr 21, 2023
01400b5
migrate to useAnchor
aristath Apr 21, 2023
b757424
Use the title
aristath Apr 21, 2023
c1d2a8a
rename function to Edit
aristath Apr 21, 2023
6ed7ca1
Strings tweaks
aristath Apr 21, 2023
1c7d7fb
Fix styles
aristath Apr 21, 2023
1535c47
a11y tweak - add aria-describedby to the paragraph
aristath Apr 21, 2023
538e32d
Use "help" instead of a paragraph
aristath Apr 21, 2023
81695fc
Remove aria-describedby
aristath Apr 24, 2023
21be348
Add missing role="menuitemcheckbox"
aristath Apr 24, 2023
10bd019
Update packages/format-library/src/language/index.js
aristath Apr 25, 2023
86899ba
Update packages/format-library/src/language/index.js
aristath Apr 25, 2023
da3064e
move the language format definition before the component
aristath Apr 25, 2023
480e20a
change anchorRef to popoverAnchor
aristath Apr 25, 2023
9ba8b38
Revert "move the language format definition before the component"
aristath Apr 25, 2023
f895ae4
Move language above Edit
aristath May 2, 2023
5394618
change class to has-language
aristath May 2, 2023
310a6e0
Use a form
aristath May 2, 2023
449745d
change classnames for the form and the popover
aristath May 2, 2023
4580d7a
Update packages/format-library/src/language/index.js
aristath May 3, 2023
5c8dbcc
Update packages/format-library/src/language/index.js
aristath May 3, 2023
78389be
CS fix
aristath May 3, 2023
e8a06c5
Add event.preventDefault()
aristath May 3, 2023
68598d4
Update icon
aristath May 3, 2023
998eeca
Use a dropdown & automate RTL
aristath May 5, 2023
bf1d31c
Revert "Use a dropdown & automate RTL"
aristath May 5, 2023
0aeef93
rename icon from "translation" to "language"
aristath May 5, 2023
6a8db2c
right-align the button
aristath May 11, 2023
b56bf76
remove 0-spacing, it's not necessary
aristath May 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/format-library/src/default-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { subscript } from './subscript';
import { superscript } from './superscript';
import { keyboard } from './keyboard';
import { unknown } from './unknown';
import { language } from './language';

export default [
bold,
Expand All @@ -27,4 +28,5 @@ export default [
superscript,
keyboard,
unknown,
language,
];
124 changes: 124 additions & 0 deletions packages/format-library/src/language/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* WordPress dependencies
*/
import { RichTextToolbarButton } from '@wordpress/block-editor';
import {
TextControl,
SelectControl,
Button,
Popover,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { applyFormat, removeFormat, useAnchor } from '@wordpress/rich-text';
import { language as languageIcon } from '@wordpress/icons';

const name = 'core/language';
const title = __( 'Language' );

export const language = {
name,
tagName: 'span',
className: 'has-language',
edit: Edit,
title,
};

function Edit( props ) {
const { contentRef, isActive, onChange, value } = props;
const popoverAnchor = useAnchor( {
editableContentElement: contentRef.current,
settings: language,
} );

const [ lang, setLang ] = useState( '' );
const [ dir, setDir ] = useState( 'ltr' );

const [ isPopoverVisible, setIsPopoverVisible ] = useState( false );
const togglePopover = () => {
setIsPopoverVisible( ( state ) => ! state );
setLang( '' );
setDir( 'ltr' );
};

return (
<>
<RichTextToolbarButton
aristath marked this conversation as resolved.
Show resolved Hide resolved
icon={ languageIcon }
label={ title }
title={ title }
onClick={ () => {
if ( isActive ) {
onChange( removeFormat( value, name ) );
} else {
togglePopover();
}
} }
isActive={ isActive }
role="menuitemcheckbox"
/>

{ isPopoverVisible && (
<Popover
className="block-editor-format-toolbar__language-popover"
anchor={ popoverAnchor }
placement="bottom"
aristath marked this conversation as resolved.
Show resolved Hide resolved
onClose={ togglePopover }
>
<form
className="block-editor-format-toolbar__language-container-content"
onSubmit={ ( event ) => {
onChange(
applyFormat( value, {
type: name,
attributes: {
lang,
dir,
},
} )
);
togglePopover();
event.preventDefault();
} }
>
<TextControl
label={ title }
value={ lang }
onChange={ ( val ) => setLang( val ) }
help={ __(
'A valid language attribute, like "en" or "fr".'
) }
/>
<SelectControl
label={ __( 'Text direction' ) }
value={ dir }
options={ [
{
label: __( 'Left to right' ),
value: 'ltr',
},
{
label: __( 'Right to left' ),
value: 'rtl',
},
] }
onChange={ ( val ) => setDir( val ) }
/>
<HStack alignment="right">
<Button
variant="primary"
type="submit"
text={ __( 'Apply' ) }
/>
</HStack>
</form>
</Popover>
) }
</>
);
}
6 changes: 6 additions & 0 deletions packages/format-library/src/language/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.block-editor-format-toolbar__language-popover {
.components-popover__content {
width: auto;
padding: 1rem;
}
}
1 change: 1 addition & 0 deletions packages/format-library/src/style.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import "./image/style.scss";
@import "./link/style.scss";
@import "./text-color/style.scss";
@import "./language/style.scss";
1 change: 1 addition & 0 deletions packages/icons/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export { default as justifyStretch } from './library/justify-stretch';
export { default as key } from './library/key';
export { default as keyboardClose } from './library/keyboard-close';
export { default as keyboardReturn } from './library/keyboard-return';
export { default as language } from './library/language';
export { default as layout } from './library/layout';
export { default as levelUp } from './library/level-up';
export { default as lifesaver } from './library/lifesaver';
Expand Down
12 changes: 12 additions & 0 deletions packages/icons/src/library/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* WordPress dependencies
*/
import { SVG, Path } from '@wordpress/primitives';

const language = (
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<Path d="M17.5 10h-1.7l-3.7 10.5h1.7l.9-2.6h3.9l.9 2.6h1.7L17.5 10zm-2.2 6.3 1.4-4 1.4 4h-2.8zm-4.8-3.8c1.6-1.8 2.9-3.6 3.7-5.7H16V5.2h-5.8V3H8.8v2.2H3v1.5h9.6c-.7 1.6-1.8 3.1-3.1 4.6C8.6 10.2 7.8 9 7.2 8H5.6c.6 1.4 1.7 2.9 2.9 4.4l-2.4 2.4c-.3.4-.7.8-1.1 1.2l1 1 1.2-1.2c.8-.8 1.6-1.5 2.3-2.3.8.9 1.7 1.7 2.5 2.5l.6-1.5c-.7-.6-1.4-1.3-2.1-2z" />
</SVG>
);

export default language;