From bd83d8f85b74dfb27cca52306beaf896a4882bf9 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Tue, 5 Mar 2019 11:44:08 +0000 Subject: [PATCH 01/25] Make sure multiline property is filtered out of props on save. --- .../src/components/rich-text/index.native.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index a4761ca33f1214..2176f3e660ebe9 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -584,11 +584,22 @@ const RichTextContainer = compose( [ } ), ] )( RichText ); -RichTextContainer.Content = ( { value, format, tagName: Tag, ...props } ) => { +RichTextContainer.Content = ( { value, format, tagName: Tag, multiline, ...props } ) => { let content; + let html = value; + let MultilineTag; + + if ( multiline === true || multiline === 'p' || multiline === 'li' ) { + MultilineTag = multiline === true ? 'p' : multiline; + } + + if ( ! html && MultilineTag ) { + html = `<${ MultilineTag }>`; + } + switch ( format ) { case 'string': - content = { value }; + content = { html }; break; } From 8ed9cc085ec2c7ec85df103ddbe76c2312dfb2c9 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Tue, 5 Mar 2019 16:31:49 +0000 Subject: [PATCH 02/25] Send block edit parameters using the context. --- .../src/components/rich-text/index.native.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index 2176f3e660ebe9..23798a803a5fe8 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -31,6 +31,7 @@ import { isURL } from '@wordpress/url'; */ import FormatEdit from './format-edit'; import FormatToolbar from './format-toolbar'; +import { withBlockEditContext } from '../block-edit/context'; import styles from './style.scss'; @@ -582,6 +583,28 @@ const RichTextContainer = compose( [ formatTypes: getFormatTypes(), }; } ), + withBlockEditContext( ( context, ownProps ) => { + // When explicitly set as not selected, do nothing. + if ( ownProps.isSelected === false ) { + return { + clientId: context.clientId, + }; + } + // When explicitly set as selected, use the value stored in the context instead. + if ( ownProps.isSelected === true ) { + return { + isSelected: context.isSelected, + clientId: context.clientId, + }; + } + + // Ensures that only one RichText component can be focused. + return { + clientId: context.clientId, + isSelected: context.isSelected, + onFocus: context.onFocus, + }; + } ), ] )( RichText ); RichTextContainer.Content = ( { value, format, tagName: Tag, multiline, ...props } ) => { From e0d7cf69a9a32d76f85bed0fb1abe16b4badaa08 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Tue, 5 Mar 2019 16:44:42 +0000 Subject: [PATCH 03/25] Add multiline variables to allow proper parsing and saving of properties. --- .../src/components/rich-text/index.native.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index 23798a803a5fe8..504d36853fe2a8 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -71,8 +71,17 @@ const gutenbergFormatNamesToAztec = { }; export class RichText extends Component { - constructor() { + constructor( { multiline } ) { super( ...arguments ); + + if ( multiline === true || multiline === 'p' || multiline === 'li' ) { + this.multilineTag = multiline === true ? 'p' : multiline; + } + + if ( this.multilineTag === 'li' ) { + this.multilineWrapperTags = [ 'ul', 'ol' ]; + } + this.isIOS = Platform.OS === 'ios'; this.onChange = this.onChange.bind( this ); this.onEnter = this.onEnter.bind( this ); From eb33f84478f4425d7183ccea1aa94c46350fe02d Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Tue, 5 Mar 2019 22:14:56 +0000 Subject: [PATCH 04/25] Add list edit toolbar options --- .../src/components/rich-text/index.native.js | 10 +++ .../components/rich-text/list-edit.native.js | 77 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 packages/editor/src/components/rich-text/list-edit.native.js diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index 504d36853fe2a8..8a13b4e4fdcddf 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -32,6 +32,7 @@ import { isURL } from '@wordpress/url'; import FormatEdit from './format-edit'; import FormatToolbar from './format-toolbar'; import { withBlockEditContext } from '../block-edit/context'; +import { ListEdit } from './list-edit'; import styles from './style.scss'; @@ -511,6 +512,7 @@ export class RichText extends Component { style, formattingControls, isSelected, + onTagNameChange, } = this.props; const record = this.getRecord(); @@ -532,6 +534,14 @@ export class RichText extends Component { return ( + { isSelected && this.multilineTag === 'li' && ( + + ) } { isSelected && ( diff --git a/packages/editor/src/components/rich-text/list-edit.native.js b/packages/editor/src/components/rich-text/list-edit.native.js new file mode 100644 index 00000000000000..74b0ce08bbda96 --- /dev/null +++ b/packages/editor/src/components/rich-text/list-edit.native.js @@ -0,0 +1,77 @@ +/** + * WordPress dependencies + */ + +import { Toolbar } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { + changeListType, +} from '@wordpress/rich-text'; + +/** + * Internal dependencies + */ + +import BlockFormatControls from '../block-format-controls'; + +/** + * Whether or not the root list is selected. + * + * @return {boolean} True if the root list or nothing is selected, false if an + * inner list is selected. + */ +function isListRootSelected() { + // Consider the root list selected if nothing is selected. + return true; +} + +/** + * Wether or not the selected list has the given tag name. + * + * @param {string} tagName The tag name the list should have. + * @param {string} rootTagName The current root tag name, to compare with in + * case nothing is selected. + * + * @return {boolean} [description] + */ +function isActiveListType( tagName, rootTagName ) { + return tagName === rootTagName; +} + +export const ListEdit = ( { + onTagNameChange, + tagName, + value, + onChange, +} ) => ( + + + +); From c64dd5ad366c72d48fa60623e15764c97aedb3bd Mon Sep 17 00:00:00 2001 From: SergioEstevao Date: Mon, 25 Mar 2019 17:15:41 +0000 Subject: [PATCH 05/25] Add multiline property. --- packages/block-editor/src/components/rich-text/index.native.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index 8a13b4e4fdcddf..60916e8d0cde33 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -75,8 +75,10 @@ export class RichText extends Component { constructor( { multiline } ) { super( ...arguments ); + this.isMultiline = false; if ( multiline === true || multiline === 'p' || multiline === 'li' ) { this.multilineTag = multiline === true ? 'p' : multiline; + this.isMultiline = true; } if ( this.multilineTag === 'li' ) { @@ -581,6 +583,7 @@ export class RichText extends Component { fontWeight={ this.props.fontWeight } fontStyle={ this.props.fontStyle } disableEditingMenu={ this.props.disableEditingMenu } + isMultiline={ this.isMultiline } /> { isSelected && } From f0a1e21116b51bf5d115c59fe34ed8ba4991a709 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Mon, 4 Mar 2019 16:44:35 +0000 Subject: [PATCH 06/25] Add list block to mobile gb. --- packages/block-library/src/index.native.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/block-library/src/index.native.js b/packages/block-library/src/index.native.js index a1bd0b0d2f4f8c..5090d1f18bb3a4 100644 --- a/packages/block-library/src/index.native.js +++ b/packages/block-library/src/index.native.js @@ -15,6 +15,7 @@ import * as more from './more'; import * as paragraph from './paragraph'; import * as image from './image'; import * as nextpage from './nextpage'; +import * as list from './list'; export const registerCoreBlocks = () => { [ @@ -24,6 +25,7 @@ export const registerCoreBlocks = () => { more, image, nextpage, + list, ].forEach( ( { name, settings } ) => { registerBlockType( name, settings ); } ); From 73aaa4f0321d7d474c85d84180fbce46f28e5c14 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Wed, 20 Mar 2019 13:57:25 +0000 Subject: [PATCH 07/25] Move list-edit.native.js to new location. --- .../src/components/rich-text/list-edit.native.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) rename packages/{editor => block-editor}/src/components/rich-text/list-edit.native.js (83%) diff --git a/packages/editor/src/components/rich-text/list-edit.native.js b/packages/block-editor/src/components/rich-text/list-edit.native.js similarity index 83% rename from packages/editor/src/components/rich-text/list-edit.native.js rename to packages/block-editor/src/components/rich-text/list-edit.native.js index 74b0ce08bbda96..bd3c9982d37579 100644 --- a/packages/editor/src/components/rich-text/list-edit.native.js +++ b/packages/block-editor/src/components/rich-text/list-edit.native.js @@ -5,6 +5,8 @@ import { Toolbar } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { + indentListItems, + outdentListItems, changeListType, } from '@wordpress/rich-text'; @@ -71,6 +73,20 @@ export const ListEdit = ( { } }, }, + { + icon: 'editor-outdent', + title: __( 'Outdent list item' ), + onClick: () => { + onChange( outdentListItems( value ) ); + }, + }, + { + icon: 'editor-indent', + title: __( 'Indent list item' ), + onClick: () => { + onChange( indentListItems( value, { type: tagName } ) ); + }, + }, ].filter( Boolean ) } /> From b6938aaa6331e8e5a8ff4a81f87006a7d8c33868 Mon Sep 17 00:00:00 2001 From: SergioEstevao Date: Tue, 26 Mar 2019 13:36:21 +0000 Subject: [PATCH 08/25] Make block edit send down the onFocus property. --- packages/block-editor/src/components/block-edit/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/block-edit/index.js b/packages/block-editor/src/components/block-edit/index.js index c4c78f906aef91..582e3a9f3ed17c 100644 --- a/packages/block-editor/src/components/block-edit/index.js +++ b/packages/block-editor/src/components/block-edit/index.js @@ -31,12 +31,13 @@ class BlockEdit extends Component { } static getDerivedStateFromProps( props ) { - const { clientId, name, isSelected } = props; + const { clientId, name, isSelected, onFocus } = props; return { name, isSelected, clientId, + onFocus, }; } From 74f77cf4c6564de3bf5469f809974f138f8803c2 Mon Sep 17 00:00:00 2001 From: SergioEstevao Date: Tue, 26 Mar 2019 16:28:35 +0000 Subject: [PATCH 09/25] Handle case where unstableSplit is passed has prop. --- .../src/components/rich-text/index.native.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index 60916e8d0cde33..1003134c59f770 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -85,6 +85,12 @@ export class RichText extends Component { this.multilineWrapperTags = [ 'ul', 'ol' ]; } + if ( this.props.onSplit ) { + this.onSplit = this.props.onSplit; + } else if ( this.props.unstableOnSplit ) { + this.onSplit = this.props.unstableOnSplit; + } + this.isIOS = Platform.OS === 'ios'; this.onChange = this.onChange.bind( this ); this.onEnter = this.onEnter.bind( this ); @@ -132,10 +138,8 @@ export class RichText extends Component { * handler. * */ - splitContent( currentRecord, blocks = [], isPasted = false ) { - const { onSplit } = this.props; - - if ( ! onSplit ) { + splitContent( currentRecord, blocks = [], isPasted = false ) { + if ( ! this.onSplit ) { return; } @@ -174,7 +178,7 @@ export class RichText extends Component { // always update when provided with new content. this.lastEventCount = undefined; - onSplit( before, after, ...blocks ); + this.onSplit( before, after, ...blocks ); } valueToFormat( value ) { @@ -268,7 +272,7 @@ export class RichText extends Component { // eslint-disable-next-line no-unused-vars onEnter( event ) { this.lastEventCount = event.nativeEvent.eventCount; - if ( ! this.props.onSplit ) { + if ( ! this.onSplit ) { // TODO: insert the \n char instead? return; } From 79f408cf5ecf66e20617e8385c74e3892f603327 Mon Sep 17 00:00:00 2001 From: SergioEstevao Date: Wed, 27 Mar 2019 15:42:01 +0000 Subject: [PATCH 10/25] Pass multiline tags to serialiser. --- .../block-editor/src/components/rich-text/index.native.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index 1003134c59f770..8c72cc4bca8561 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -86,7 +86,7 @@ export class RichText extends Component { } if ( this.props.onSplit ) { - this.onSplit = this.props.onSplit; + this.onSplit = this.props.onSplit; } else if ( this.props.unstableOnSplit ) { this.onSplit = this.props.unstableOnSplit; } @@ -138,7 +138,7 @@ export class RichText extends Component { * handler. * */ - splitContent( currentRecord, blocks = [], isPasted = false ) { + splitContent( currentRecord, blocks = [], isPasted = false ) { if ( ! this.onSplit ) { return; } @@ -439,7 +439,8 @@ export class RichText extends Component { ...create( { html: innerContent, range: null, - multilineTag: false, + multilineTag: this.multilineTag, + multilineWrapperTags: this.multilineWrapperTags, } ), }; From 1f12a9fad6ab0b11a57b3ce28dfa63d5d1c495d5 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Thu, 28 Mar 2019 23:33:56 +0200 Subject: [PATCH 11/25] Use the format-lib for handling "Enter" in lists --- .../src/components/rich-text/index.native.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index 8c72cc4bca8561..e47a57bb29afa1 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -19,6 +19,8 @@ import { split, toHTMLString, insert, + insertLineSeparator, + isEmptyLine, isCollapsed, } from '@wordpress/rich-text'; import { decodeEntities } from '@wordpress/html-entities'; @@ -282,7 +284,22 @@ export class RichText extends Component { currentContent: unescapeSpaces( event.nativeEvent.text ), } ); - this.splitContent( currentRecord ); + if ( this.multilineTag ) { + if ( event.shiftKey ) { + const insertedLineBreak = { needsSelectionUpdate: true, ...insertLineBreak( currentRecord ) }; + this.onFormatChange( insertedLineBreak ); + } else if ( this.onSplit && isEmptyLine( currentRecord ) ) { + this.onSplit( ...split( currentRecord ).map( this.valueToFormat ) ); + } else { + const insertedLineSeparator = { needsSelectionUpdate: true, ...insertLineSeparator( currentRecord ) }; + this.onFormatChange( insertedLineSeparator ); + } + } else if ( event.shiftKey || ! this.onSplit ) { + const insertedLineBreak = { needsSelectionUpdate: true, ...insertLineBreak( currentRecord ) }; + this.onFormatChange( insertedLineBreak ); + } else { + this.splitContent( currentRecord ); + } } // eslint-disable-next-line no-unused-vars From ca338c28a3a1a2fee1cbea38c1b9eb60481ac148 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Fri, 29 Mar 2019 19:34:50 +0200 Subject: [PATCH 12/25] Force selection reset on split --- packages/block-editor/src/components/rich-text/index.native.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index e47a57bb29afa1..73cac2d480a237 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -289,6 +289,9 @@ export class RichText extends Component { const insertedLineBreak = { needsSelectionUpdate: true, ...insertLineBreak( currentRecord ) }; this.onFormatChange( insertedLineBreak ); } else if ( this.onSplit && isEmptyLine( currentRecord ) ) { + this.setState( { + needsSelectionUpdate: false, + } ); this.onSplit( ...split( currentRecord ).map( this.valueToFormat ) ); } else { const insertedLineSeparator = { needsSelectionUpdate: true, ...insertLineSeparator( currentRecord ) }; From 076936f4f7d891a2cd892fa6f8cac9b62ea0bdd7 Mon Sep 17 00:00:00 2001 From: SergioEstevao Date: Fri, 29 Mar 2019 18:09:59 +0000 Subject: [PATCH 13/25] Add multiline wrapper tags to formatToValue. --- packages/block-editor/src/components/rich-text/index.native.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index e47a57bb29afa1..ea920353370a90 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -20,6 +20,7 @@ import { toHTMLString, insert, insertLineSeparator, + insertLineBreak, isEmptyLine, isCollapsed, } from '@wordpress/rich-text'; @@ -470,6 +471,7 @@ export class RichText extends Component { return create( { html: children.toHTML( value ), multilineTag: this.multilineTag, + multilineWrapperTags: this.multilineWrapperTags, } ); } @@ -477,6 +479,7 @@ export class RichText extends Component { return create( { html: value, multilineTag: this.multilineTag, + multilineWrapperTags: this.multilineWrapperTags, } ); } From 73fbd38fcc602dbef6492bd1e8fc5bb0cbf081e6 Mon Sep 17 00:00:00 2001 From: SergioEstevao Date: Fri, 29 Mar 2019 18:56:36 +0000 Subject: [PATCH 14/25] Remove unnecessary code. --- .../block-editor/src/components/rich-text/index.native.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index 5d26c3fe5bda48..20df2c95acccfa 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -275,10 +275,6 @@ export class RichText extends Component { // eslint-disable-next-line no-unused-vars onEnter( event ) { this.lastEventCount = event.nativeEvent.eventCount; - if ( ! this.onSplit ) { - // TODO: insert the \n char instead? - return; - } const currentRecord = this.createRecord( { ...event.nativeEvent, From e07498ea44582120d87a3bda9a6556a438df4343 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Fri, 29 Mar 2019 21:56:05 +0200 Subject: [PATCH 15/25] Force rich-text text update on list type change --- .../src/components/rich-text/index.native.js | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index 20df2c95acccfa..be445efc6377dc 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -100,6 +100,7 @@ export class RichText extends Component { this.onBackspace = this.onBackspace.bind( this ); this.onPaste = this.onPaste.bind( this ); this.onContentSizeChange = this.onContentSizeChange.bind( this ); + this.onFormatChangeForceChild = this.onFormatChangeForceChild.bind( this ); this.onFormatChange = this.onFormatChange.bind( this ); // This prevents a bug in Aztec which triggers onSelectionChange twice on format change this.onSelectionChange = this.onSelectionChange.bind( this ); @@ -202,7 +203,11 @@ export class RichText extends Component { } ).map( ( name ) => gutenbergFormatNamesToAztec[ name ] ).filter( Boolean ); } - onFormatChange( record ) { + onFormatChangeForceChild( record ) { + this.onFormatChange( record, true ); + } + + onFormatChange( record, doUpdateChild ) { let newContent; // valueToFormat might throw when converting the record to a tree structure // let's ignore the event for now and force a render update so we're still in sync @@ -224,9 +229,13 @@ export class RichText extends Component { needsSelectionUpdate: record.needsSelectionUpdate, } ); } else { - // make sure the component rerenders without refreshing the text on gutenberg - // (this can trigger other events that might update the active formats on aztec) - this.lastEventCount = 0; + if ( doUpdateChild ) { + this.lastEventCount = undefined; + } else { + // make sure the component rerenders without refreshing the text on gutenberg + // (this can trigger other events that might update the active formats on aztec) + this.lastEventCount = 0; + } this.forceUpdate(); } } @@ -284,7 +293,7 @@ export class RichText extends Component { if ( this.multilineTag ) { if ( event.shiftKey ) { const insertedLineBreak = { needsSelectionUpdate: true, ...insertLineBreak( currentRecord ) }; - this.onFormatChange( insertedLineBreak ); + this.onFormatChangeForceChild( insertedLineBreak ); } else if ( this.onSplit && isEmptyLine( currentRecord ) ) { this.setState( { needsSelectionUpdate: false, @@ -292,11 +301,11 @@ export class RichText extends Component { this.onSplit( ...split( currentRecord ).map( this.valueToFormat ) ); } else { const insertedLineSeparator = { needsSelectionUpdate: true, ...insertLineSeparator( currentRecord ) }; - this.onFormatChange( insertedLineSeparator ); + this.onFormatChangeForceChild( insertedLineSeparator ); } } else if ( event.shiftKey || ! this.onSplit ) { const insertedLineBreak = { needsSelectionUpdate: true, ...insertLineBreak( currentRecord ) }; - this.onFormatChange( insertedLineBreak ); + this.onFormatChangeForceChild( insertedLineBreak ); } else { this.splitContent( currentRecord ); } @@ -565,7 +574,7 @@ export class RichText extends Component { onTagNameChange={ onTagNameChange } tagName={ tagName } value={ record } - onChange={ this.onFormatChange } + onChange={ this.onFormatChangeForceChild } /> ) } { isSelected && ( From 58eae26e42791e1e100f7cb9a0b0d47f6d2e0b8e Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Thu, 4 Apr 2019 09:50:31 +0100 Subject: [PATCH 16/25] Disable indent and outdent. --- .../src/components/rich-text/list-edit.native.js | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/list-edit.native.js b/packages/block-editor/src/components/rich-text/list-edit.native.js index bd3c9982d37579..74b0ce08bbda96 100644 --- a/packages/block-editor/src/components/rich-text/list-edit.native.js +++ b/packages/block-editor/src/components/rich-text/list-edit.native.js @@ -5,8 +5,6 @@ import { Toolbar } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { - indentListItems, - outdentListItems, changeListType, } from '@wordpress/rich-text'; @@ -73,20 +71,6 @@ export const ListEdit = ( { } }, }, - { - icon: 'editor-outdent', - title: __( 'Outdent list item' ), - onClick: () => { - onChange( outdentListItems( value ) ); - }, - }, - { - icon: 'editor-indent', - title: __( 'Indent list item' ), - onClick: () => { - onChange( indentListItems( value, { type: tagName } ) ); - }, - }, ].filter( Boolean ) } /> From 08318cad772fb8ebe727a7ab6971aa2940b8d151 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Thu, 4 Apr 2019 18:02:47 +0300 Subject: [PATCH 17/25] Enable toggling list type of nested lists --- .../components/rich-text/list-edit.native.js | 10 +++++----- .../rich-text/src/get-start-nesting-level.js | 19 +++++++++++++++++++ packages/rich-text/src/index.js | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 packages/rich-text/src/get-start-nesting-level.js diff --git a/packages/block-editor/src/components/rich-text/list-edit.native.js b/packages/block-editor/src/components/rich-text/list-edit.native.js index 74b0ce08bbda96..2aa79daf308f5b 100644 --- a/packages/block-editor/src/components/rich-text/list-edit.native.js +++ b/packages/block-editor/src/components/rich-text/list-edit.native.js @@ -6,6 +6,7 @@ import { Toolbar } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { changeListType, + getStartNestingLevel, } from '@wordpress/rich-text'; /** @@ -20,9 +21,8 @@ import BlockFormatControls from '../block-format-controls'; * @return {boolean} True if the root list or nothing is selected, false if an * inner list is selected. */ -function isListRootSelected() { - // Consider the root list selected if nothing is selected. - return true; +function isListRootSelected( value ) { + return getStartNestingLevel( value ) < 1; } /** @@ -54,7 +54,7 @@ export const ListEdit = ( { onClick() { onChange( changeListType( value, { type: 'ul' } ) ); - if ( isListRootSelected() ) { + if ( isListRootSelected( value ) ) { onTagNameChange( 'ul' ); } }, @@ -66,7 +66,7 @@ export const ListEdit = ( { onClick() { onChange( changeListType( value, { type: 'ol' } ) ); - if ( isListRootSelected() ) { + if ( isListRootSelected( value ) ) { onTagNameChange( 'ol' ); } }, diff --git a/packages/rich-text/src/get-start-nesting-level.js b/packages/rich-text/src/get-start-nesting-level.js new file mode 100644 index 00000000000000..5cba5f5cee451c --- /dev/null +++ b/packages/rich-text/src/get-start-nesting-level.js @@ -0,0 +1,19 @@ +/** + * Internal dependencies + */ + +import { getLineIndex } from './get-line-index'; + +/** + * Returns the nesting level of the list at the selection start position. + * + * @param {Object} value The rich-text value + * + * @return {number} The nesting level, starting from 0. + */ +export function getStartNestingLevel( value ) { + const { text, replacements, start, end } = value; + const startingLineIndex = getLineIndex( value, start ); + const startLineFormats = replacements[ startingLineIndex ] || []; + return startLineFormats.length; +} diff --git a/packages/rich-text/src/index.js b/packages/rich-text/src/index.js index 63da636257619d..6347aa4985ee66 100644 --- a/packages/rich-text/src/index.js +++ b/packages/rich-text/src/index.js @@ -12,6 +12,7 @@ export { getActiveObject } from './get-active-object'; export { getSelectionEnd } from './get-selection-end'; export { getSelectionStart } from './get-selection-start'; export { getTextContent } from './get-text-content'; +export { getStartNestingLevel } from './get-start-nesting-level'; export { isCollapsed } from './is-collapsed'; export { isEmpty, isEmptyLine } from './is-empty'; export { join } from './join'; From 9dcd23b226fd53d818ffcd97ba673997b7537409 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Thu, 4 Apr 2019 18:45:28 +0300 Subject: [PATCH 18/25] Update list type toolbar button on native mobile --- ...get-start-nesting-level.js => get-start-list-format.js} | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) rename packages/rich-text/src/{get-start-nesting-level.js => get-start-list-format.js} (64%) diff --git a/packages/rich-text/src/get-start-nesting-level.js b/packages/rich-text/src/get-start-list-format.js similarity index 64% rename from packages/rich-text/src/get-start-nesting-level.js rename to packages/rich-text/src/get-start-list-format.js index 5cba5f5cee451c..17f7402126cbbb 100644 --- a/packages/rich-text/src/get-start-nesting-level.js +++ b/packages/rich-text/src/get-start-list-format.js @@ -9,11 +9,12 @@ import { getLineIndex } from './get-line-index'; * * @param {Object} value The rich-text value * - * @return {number} The nesting level, starting from 0. + * @return {Object} Object with { nestingLevel, listFormat }. */ -export function getStartNestingLevel( value ) { +export function getStartListFormat( value ) { const { text, replacements, start, end } = value; const startingLineIndex = getLineIndex( value, start ); const startLineFormats = replacements[ startingLineIndex ] || []; - return startLineFormats.length; + const listFormat = startLineFormats.slice( -1 ); + return { nestingLevel: startLineFormats.length, listFormat }; } From fd62f01d679442c200cd7675b7251aa57073875c Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Thu, 4 Apr 2019 19:04:33 +0300 Subject: [PATCH 19/25] Include diff missed by previous commit --- .../components/rich-text/list-edit.native.js | 18 ++++++++++++------ .../rich-text/src/get-start-list-format.js | 4 ++-- packages/rich-text/src/index.js | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/list-edit.native.js b/packages/block-editor/src/components/rich-text/list-edit.native.js index 2aa79daf308f5b..d96cda385b0803 100644 --- a/packages/block-editor/src/components/rich-text/list-edit.native.js +++ b/packages/block-editor/src/components/rich-text/list-edit.native.js @@ -6,7 +6,7 @@ import { Toolbar } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { changeListType, - getStartNestingLevel, + getStartListFormat, } from '@wordpress/rich-text'; /** @@ -22,7 +22,7 @@ import BlockFormatControls from '../block-format-controls'; * inner list is selected. */ function isListRootSelected( value ) { - return getStartNestingLevel( value ) < 1; + return getStartListFormat( value ).nestingLevel < 1; } /** @@ -34,8 +34,14 @@ function isListRootSelected( value ) { * * @return {boolean} [description] */ -function isActiveListType( tagName, rootTagName ) { - return tagName === rootTagName; +function isActiveListType( tagName, rootTagName, value ) { + const listFormat = getStartListFormat( value ); + + if ( ! listFormat || ! listFormat.type ) { + return tagName === rootTagName; + } + + return listFormat.type.toLowerCase() === tagName; } export const ListEdit = ( { @@ -50,7 +56,7 @@ export const ListEdit = ( { onTagNameChange && { icon: 'editor-ul', title: __( 'Convert to unordered list' ), - isActive: isActiveListType( 'ul', tagName ), + isActive: isActiveListType( 'ul', tagName, value ), onClick() { onChange( changeListType( value, { type: 'ul' } ) ); @@ -62,7 +68,7 @@ export const ListEdit = ( { onTagNameChange && { icon: 'editor-ol', title: __( 'Convert to ordered list' ), - isActive: isActiveListType( 'ol', tagName ), + isActive: isActiveListType( 'ol', tagName, value ), onClick() { onChange( changeListType( value, { type: 'ol' } ) ); diff --git a/packages/rich-text/src/get-start-list-format.js b/packages/rich-text/src/get-start-list-format.js index 17f7402126cbbb..c8e7169cf54abd 100644 --- a/packages/rich-text/src/get-start-list-format.js +++ b/packages/rich-text/src/get-start-list-format.js @@ -15,6 +15,6 @@ export function getStartListFormat( value ) { const { text, replacements, start, end } = value; const startingLineIndex = getLineIndex( value, start ); const startLineFormats = replacements[ startingLineIndex ] || []; - const listFormat = startLineFormats.slice( -1 ); - return { nestingLevel: startLineFormats.length, listFormat }; + const [ listFormat ] = startLineFormats.slice( -1 ); + return { nestingLevel: startLineFormats.length, ...listFormat }; } diff --git a/packages/rich-text/src/index.js b/packages/rich-text/src/index.js index 6347aa4985ee66..8eea3577e2d998 100644 --- a/packages/rich-text/src/index.js +++ b/packages/rich-text/src/index.js @@ -12,7 +12,7 @@ export { getActiveObject } from './get-active-object'; export { getSelectionEnd } from './get-selection-end'; export { getSelectionStart } from './get-selection-start'; export { getTextContent } from './get-text-content'; -export { getStartNestingLevel } from './get-start-nesting-level'; +export { getStartListFormat } from './get-start-list-format'; export { isCollapsed } from './is-collapsed'; export { isEmpty, isEmptyLine } from './is-empty'; export { join } from './join'; From 66e971dd2bdfc34d86d6eb8b7e94cc2daa3a2939 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Fri, 5 Apr 2019 10:42:42 +0300 Subject: [PATCH 20/25] Rename to denote that it's about lines --- .../src/components/rich-text/list-edit.native.js | 6 +++--- .../{get-start-list-format.js => get-line-list-format.js} | 2 +- packages/rich-text/src/index.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename packages/rich-text/src/{get-start-list-format.js => get-line-list-format.js} (92%) diff --git a/packages/block-editor/src/components/rich-text/list-edit.native.js b/packages/block-editor/src/components/rich-text/list-edit.native.js index d96cda385b0803..431f74616bd5b4 100644 --- a/packages/block-editor/src/components/rich-text/list-edit.native.js +++ b/packages/block-editor/src/components/rich-text/list-edit.native.js @@ -6,7 +6,7 @@ import { Toolbar } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { changeListType, - getStartListFormat, + getLineListFormat, } from '@wordpress/rich-text'; /** @@ -22,7 +22,7 @@ import BlockFormatControls from '../block-format-controls'; * inner list is selected. */ function isListRootSelected( value ) { - return getStartListFormat( value ).nestingLevel < 1; + return getLineListFormat( value ).nestingLevel < 1; } /** @@ -35,7 +35,7 @@ function isListRootSelected( value ) { * @return {boolean} [description] */ function isActiveListType( tagName, rootTagName, value ) { - const listFormat = getStartListFormat( value ); + const listFormat = getLineListFormat( value ); if ( ! listFormat || ! listFormat.type ) { return tagName === rootTagName; diff --git a/packages/rich-text/src/get-start-list-format.js b/packages/rich-text/src/get-line-list-format.js similarity index 92% rename from packages/rich-text/src/get-start-list-format.js rename to packages/rich-text/src/get-line-list-format.js index c8e7169cf54abd..6cfd61fb201209 100644 --- a/packages/rich-text/src/get-start-list-format.js +++ b/packages/rich-text/src/get-line-list-format.js @@ -11,7 +11,7 @@ import { getLineIndex } from './get-line-index'; * * @return {Object} Object with { nestingLevel, listFormat }. */ -export function getStartListFormat( value ) { +export function getLineListFormat( value ) { const { text, replacements, start, end } = value; const startingLineIndex = getLineIndex( value, start ); const startLineFormats = replacements[ startingLineIndex ] || []; diff --git a/packages/rich-text/src/index.js b/packages/rich-text/src/index.js index 8eea3577e2d998..817697a429287b 100644 --- a/packages/rich-text/src/index.js +++ b/packages/rich-text/src/index.js @@ -12,7 +12,7 @@ export { getActiveObject } from './get-active-object'; export { getSelectionEnd } from './get-selection-end'; export { getSelectionStart } from './get-selection-start'; export { getTextContent } from './get-text-content'; -export { getStartListFormat } from './get-start-list-format'; +export { getLineListFormat } from './get-line-list-format'; export { isCollapsed } from './is-collapsed'; export { isEmpty, isEmptyLine } from './is-empty'; export { join } from './join'; From da0e1a7aa07c11971bf61800833f28ef468bb0a6 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Fri, 5 Apr 2019 11:09:18 +0300 Subject: [PATCH 21/25] Split into separate functions and mark unstable --- .../components/rich-text/list-edit.native.js | 7 ++++--- .../rich-text/src/get-line-list-format.js | 8 ++++---- .../rich-text/src/get-line-nesting-level.js | 19 +++++++++++++++++++ packages/rich-text/src/index.js | 3 ++- 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 packages/rich-text/src/get-line-nesting-level.js diff --git a/packages/block-editor/src/components/rich-text/list-edit.native.js b/packages/block-editor/src/components/rich-text/list-edit.native.js index 431f74616bd5b4..0834a263203696 100644 --- a/packages/block-editor/src/components/rich-text/list-edit.native.js +++ b/packages/block-editor/src/components/rich-text/list-edit.native.js @@ -6,7 +6,8 @@ import { Toolbar } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { changeListType, - getLineListFormat, + __unstableGetLineNestingLevel, + __unstableGetLineListFormat, } from '@wordpress/rich-text'; /** @@ -22,7 +23,7 @@ import BlockFormatControls from '../block-format-controls'; * inner list is selected. */ function isListRootSelected( value ) { - return getLineListFormat( value ).nestingLevel < 1; + return __unstableGetLineNestingLevel( value ) < 1; } /** @@ -35,7 +36,7 @@ function isListRootSelected( value ) { * @return {boolean} [description] */ function isActiveListType( tagName, rootTagName, value ) { - const listFormat = getLineListFormat( value ); + const listFormat = __unstableGetLineListFormat( value ); if ( ! listFormat || ! listFormat.type ) { return tagName === rootTagName; diff --git a/packages/rich-text/src/get-line-list-format.js b/packages/rich-text/src/get-line-list-format.js index 6cfd61fb201209..f8c5fb478660dc 100644 --- a/packages/rich-text/src/get-line-list-format.js +++ b/packages/rich-text/src/get-line-list-format.js @@ -5,16 +5,16 @@ import { getLineIndex } from './get-line-index'; /** - * Returns the nesting level of the list at the selection start position. + * Returns the list format of the line at the selection start position. * * @param {Object} value The rich-text value * - * @return {Object} Object with { nestingLevel, listFormat }. + * @return {Object} Object with listFormat. */ export function getLineListFormat( value ) { - const { text, replacements, start, end } = value; + const { replacements, start } = value; const startingLineIndex = getLineIndex( value, start ); const startLineFormats = replacements[ startingLineIndex ] || []; const [ listFormat ] = startLineFormats.slice( -1 ); - return { nestingLevel: startLineFormats.length, ...listFormat }; + return listFormat; } diff --git a/packages/rich-text/src/get-line-nesting-level.js b/packages/rich-text/src/get-line-nesting-level.js new file mode 100644 index 00000000000000..e844abb5d677cc --- /dev/null +++ b/packages/rich-text/src/get-line-nesting-level.js @@ -0,0 +1,19 @@ +/** + * Internal dependencies + */ + +import { getLineIndex } from './get-line-index'; + +/** + * Returns the nesting level of the list at the selection start position. + * + * @param {Object} value The rich-text value + * + * @return {number} The list nesting level, starting from 0. + */ +export function getLineNestingLevel( value ) { + const { replacements, start } = value; + const startingLineIndex = getLineIndex( value, start ); + const startLineFormats = replacements[ startingLineIndex ] || []; + return startLineFormats.length; +} diff --git a/packages/rich-text/src/index.js b/packages/rich-text/src/index.js index 817697a429287b..310eb776b80813 100644 --- a/packages/rich-text/src/index.js +++ b/packages/rich-text/src/index.js @@ -12,7 +12,8 @@ export { getActiveObject } from './get-active-object'; export { getSelectionEnd } from './get-selection-end'; export { getSelectionStart } from './get-selection-start'; export { getTextContent } from './get-text-content'; -export { getLineListFormat } from './get-line-list-format'; +export { getLineNestingLevel as __unstableGetLineNestingLevel } from './get-line-nesting-level'; +export { getLineListFormat as __unstableGetLineListFormat } from './get-line-list-format'; export { isCollapsed } from './is-collapsed'; export { isEmpty, isEmptyLine } from './is-empty'; export { join } from './join'; From eb009e8f34086ebf4e34225ccf545704b7c5cbe4 Mon Sep 17 00:00:00 2001 From: Tugdual de Kerviler Date: Fri, 5 Apr 2019 11:37:28 +0200 Subject: [PATCH 22/25] Add missing JSDoc param --- .../block-editor/src/components/rich-text/list-edit.native.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/block-editor/src/components/rich-text/list-edit.native.js b/packages/block-editor/src/components/rich-text/list-edit.native.js index 0834a263203696..8af8d4216981ed 100644 --- a/packages/block-editor/src/components/rich-text/list-edit.native.js +++ b/packages/block-editor/src/components/rich-text/list-edit.native.js @@ -19,6 +19,8 @@ import BlockFormatControls from '../block-format-controls'; /** * Whether or not the root list is selected. * + * @param {Object} value The internal rich-text value. + * * @return {boolean} True if the root list or nothing is selected, false if an * inner list is selected. */ @@ -32,6 +34,7 @@ function isListRootSelected( value ) { * @param {string} tagName The tag name the list should have. * @param {string} rootTagName The current root tag name, to compare with in * case nothing is selected. + * @param {Object} value The internal rich-text value. * * @return {boolean} [description] */ From cc5666ee3b83cabb11fc147c1c812644b77962b1 Mon Sep 17 00:00:00 2001 From: Tugdual de Kerviler Date: Fri, 5 Apr 2019 11:50:52 +0200 Subject: [PATCH 23/25] Update snapshot for BlockControls --- .../components/block-controls/test/__snapshots__/index.js.snap | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-editor/src/components/block-controls/test/__snapshots__/index.js.snap b/packages/block-editor/src/components/block-controls/test/__snapshots__/index.js.snap index 681c33a42d9976..ba44cc9445f656 100644 --- a/packages/block-editor/src/components/block-controls/test/__snapshots__/index.js.snap +++ b/packages/block-editor/src/components/block-controls/test/__snapshots__/index.js.snap @@ -8,6 +8,7 @@ exports[`BlockControls should render a dynamic toolbar of controls 1`] = ` "focusedElement": null, "isSelected": true, "name": undefined, + "onFocus": undefined, "setFocusedElement": [Function], } } From 4451cb30ffe2fb86b2b0ff5206c4e021b4ab9a96 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Fri, 5 Apr 2019 13:14:28 +0300 Subject: [PATCH 24/25] Move isActiveListType, isListRootSelected to rich-text package --- .../components/rich-text/list-edit.native.js | 44 +++---------------- ...ist-format.js => get-line-list-formats.js} | 7 ++- .../rich-text/src/get-line-nesting-level.js | 19 -------- packages/rich-text/src/index.js | 4 +- packages/rich-text/src/is-active-list-type.js | 27 ++++++++++++ .../rich-text/src/is-list-root-selected.js | 18 ++++++++ 6 files changed, 56 insertions(+), 63 deletions(-) rename packages/rich-text/src/{get-line-list-format.js => get-line-list-formats.js} (70%) delete mode 100644 packages/rich-text/src/get-line-nesting-level.js create mode 100644 packages/rich-text/src/is-active-list-type.js create mode 100644 packages/rich-text/src/is-list-root-selected.js diff --git a/packages/block-editor/src/components/rich-text/list-edit.native.js b/packages/block-editor/src/components/rich-text/list-edit.native.js index 8af8d4216981ed..27b3d56ac9fdf7 100644 --- a/packages/block-editor/src/components/rich-text/list-edit.native.js +++ b/packages/block-editor/src/components/rich-text/list-edit.native.js @@ -6,8 +6,8 @@ import { Toolbar } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { changeListType, - __unstableGetLineNestingLevel, - __unstableGetLineListFormat, + __unstableIsListRootSelected, + __unstableIsActiveListType, } from '@wordpress/rich-text'; /** @@ -16,38 +16,6 @@ import { import BlockFormatControls from '../block-format-controls'; -/** - * Whether or not the root list is selected. - * - * @param {Object} value The internal rich-text value. - * - * @return {boolean} True if the root list or nothing is selected, false if an - * inner list is selected. - */ -function isListRootSelected( value ) { - return __unstableGetLineNestingLevel( value ) < 1; -} - -/** - * Wether or not the selected list has the given tag name. - * - * @param {string} tagName The tag name the list should have. - * @param {string} rootTagName The current root tag name, to compare with in - * case nothing is selected. - * @param {Object} value The internal rich-text value. - * - * @return {boolean} [description] - */ -function isActiveListType( tagName, rootTagName, value ) { - const listFormat = __unstableGetLineListFormat( value ); - - if ( ! listFormat || ! listFormat.type ) { - return tagName === rootTagName; - } - - return listFormat.type.toLowerCase() === tagName; -} - export const ListEdit = ( { onTagNameChange, tagName, @@ -60,11 +28,11 @@ export const ListEdit = ( { onTagNameChange && { icon: 'editor-ul', title: __( 'Convert to unordered list' ), - isActive: isActiveListType( 'ul', tagName, value ), + isActive: __unstableIsActiveListType( 'ul', tagName, value ), onClick() { onChange( changeListType( value, { type: 'ul' } ) ); - if ( isListRootSelected( value ) ) { + if ( __unstableIsListRootSelected( value ) ) { onTagNameChange( 'ul' ); } }, @@ -72,11 +40,11 @@ export const ListEdit = ( { onTagNameChange && { icon: 'editor-ol', title: __( 'Convert to ordered list' ), - isActive: isActiveListType( 'ol', tagName, value ), + isActive: __unstableIsActiveListType( 'ol', tagName, value ), onClick() { onChange( changeListType( value, { type: 'ol' } ) ); - if ( isListRootSelected( value ) ) { + if ( __unstableIsListRootSelected( value ) ) { onTagNameChange( 'ol' ); } }, diff --git a/packages/rich-text/src/get-line-list-format.js b/packages/rich-text/src/get-line-list-formats.js similarity index 70% rename from packages/rich-text/src/get-line-list-format.js rename to packages/rich-text/src/get-line-list-formats.js index f8c5fb478660dc..7eff9df12e13ff 100644 --- a/packages/rich-text/src/get-line-list-format.js +++ b/packages/rich-text/src/get-line-list-formats.js @@ -9,12 +9,11 @@ import { getLineIndex } from './get-line-index'; * * @param {Object} value The rich-text value * - * @return {Object} Object with listFormat. + * @return {Array} Array of the list formats on the selected line. */ -export function getLineListFormat( value ) { +export function getLineListFormats( value ) { const { replacements, start } = value; const startingLineIndex = getLineIndex( value, start ); const startLineFormats = replacements[ startingLineIndex ] || []; - const [ listFormat ] = startLineFormats.slice( -1 ); - return listFormat; + return startLineFormats; } diff --git a/packages/rich-text/src/get-line-nesting-level.js b/packages/rich-text/src/get-line-nesting-level.js deleted file mode 100644 index e844abb5d677cc..00000000000000 --- a/packages/rich-text/src/get-line-nesting-level.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Internal dependencies - */ - -import { getLineIndex } from './get-line-index'; - -/** - * Returns the nesting level of the list at the selection start position. - * - * @param {Object} value The rich-text value - * - * @return {number} The list nesting level, starting from 0. - */ -export function getLineNestingLevel( value ) { - const { replacements, start } = value; - const startingLineIndex = getLineIndex( value, start ); - const startLineFormats = replacements[ startingLineIndex ] || []; - return startLineFormats.length; -} diff --git a/packages/rich-text/src/index.js b/packages/rich-text/src/index.js index 310eb776b80813..53acd1179f6f67 100644 --- a/packages/rich-text/src/index.js +++ b/packages/rich-text/src/index.js @@ -12,8 +12,8 @@ export { getActiveObject } from './get-active-object'; export { getSelectionEnd } from './get-selection-end'; export { getSelectionStart } from './get-selection-start'; export { getTextContent } from './get-text-content'; -export { getLineNestingLevel as __unstableGetLineNestingLevel } from './get-line-nesting-level'; -export { getLineListFormat as __unstableGetLineListFormat } from './get-line-list-format'; +export { isListRootSelected as __unstableIsListRootSelected } from './is-list-root-selected'; +export { isActiveListType as __unstableIsActiveListType } from './is-active-list-type'; export { isCollapsed } from './is-collapsed'; export { isEmpty, isEmptyLine } from './is-empty'; export { join } from './join'; diff --git a/packages/rich-text/src/is-active-list-type.js b/packages/rich-text/src/is-active-list-type.js new file mode 100644 index 00000000000000..7fe67e881349cb --- /dev/null +++ b/packages/rich-text/src/is-active-list-type.js @@ -0,0 +1,27 @@ +/** + * Internal dependencies + */ + +import { getLineListFormats } from './get-line-list-formats'; + +/** + * Wether or not the selected list has the given tag name. + * + * @param {string} tagName The tag name the list should have. + * @param {string} rootTagName The current root tag name, to compare with in + * case nothing is selected. + * @param {Object} value The internal rich-text value. + * + * @return {boolean} [description] + */ +export function isActiveListType( tagName, rootTagName, value ) { + const startLineFormats = getLineListFormats( value ); + const [ deepestListFormat ] = startLineFormats.slice( -1 ); + + if ( ! deepestListFormat || ! deepestListFormat.type ) { + return tagName === rootTagName; + } + + return deepestListFormat.type.toLowerCase() === tagName; +} + diff --git a/packages/rich-text/src/is-list-root-selected.js b/packages/rich-text/src/is-list-root-selected.js new file mode 100644 index 00000000000000..ba823ebbf60d7c --- /dev/null +++ b/packages/rich-text/src/is-list-root-selected.js @@ -0,0 +1,18 @@ +/** + * Internal dependencies + */ + +import { getLineListFormats } from './get-line-list-formats'; + +/** + * Whether or not the root list is selected. + * + * @param {Object} value The internal rich-text value. + * + * @return {boolean} True if the root list or nothing is selected, false if an + * inner list is selected. + */ +export function isListRootSelected( value ) { + const startLineFormats = getLineListFormats( value ); + return startLineFormats.length < 1; +} From 97ac0fb78a237331b572c55f0d7f100891dcdaaf Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Fri, 5 Apr 2019 13:20:45 +0300 Subject: [PATCH 25/25] Remove excess empty line --- packages/rich-text/src/is-active-list-type.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/rich-text/src/is-active-list-type.js b/packages/rich-text/src/is-active-list-type.js index 7fe67e881349cb..56efa78b3c7e72 100644 --- a/packages/rich-text/src/is-active-list-type.js +++ b/packages/rich-text/src/is-active-list-type.js @@ -24,4 +24,3 @@ export function isActiveListType( tagName, rootTagName, value ) { return deepestListFormat.type.toLowerCase() === tagName; } -