Skip to content

Commit

Permalink
Move isActiveListType, isListRootSelected to rich-text package
Browse files Browse the repository at this point in the history
  • Loading branch information
hypest committed Apr 5, 2019
1 parent 91f790c commit 4451cb3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 63 deletions.
44 changes: 6 additions & 38 deletions packages/block-editor/src/components/rich-text/list-edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Toolbar } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import {
changeListType,
__unstableGetLineNestingLevel,
__unstableGetLineListFormat,
__unstableIsListRootSelected,
__unstableIsActiveListType,
} from '@wordpress/rich-text';

/**
Expand All @@ -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,
Expand All @@ -60,23 +28,23 @@ 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' );
}
},
},
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' );
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
19 changes: 0 additions & 19 deletions packages/rich-text/src/get-line-nesting-level.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/rich-text/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
27 changes: 27 additions & 0 deletions packages/rich-text/src/is-active-list-type.js
Original file line number Diff line number Diff line change
@@ -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;
}

18 changes: 18 additions & 0 deletions packages/rich-text/src/is-list-root-selected.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 4451cb3

Please sign in to comment.