Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 25, 2019
1 parent febe43b commit 360e6dc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 38 deletions.
43 changes: 19 additions & 24 deletions packages/editor/src/components/rich-text/list-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ import {
import { RichTextShortcut } from './shortcut';
import BlockFormatControls from '../block-format-controls';

function isListRootSelected() {
/**
* Gets the selected list node, which is the closest list node to the start of
* the selection.
*
* @return {?Element} The selected list node, or undefined if none is selected.
*/
function getSelectedListNode() {
const selection = window.getSelection();

if ( selection.rangeCount === 0 ) {
return true;
return;
}

let { startContainer } = selection.getRangeAt( 0 );
Expand All @@ -34,38 +40,27 @@ function isListRootSelected() {
const rootNode = startContainer.closest( '*[contenteditable]' );

if ( ! rootNode || ! rootNode.contains( startContainer ) ) {
return true;
return;
}

return startContainer.closest( 'ol,ul' ) === rootNode;
return startContainer.closest( 'ol,ul' );
}

function isActiveListType( tagName, rootTagName ) {
const selection = window.getSelection();

if ( selection.rangeCount === 0 ) {
return tagName === rootTagName;
}

let { startContainer } = selection.getRangeAt( 0 );
function isListRootSelected() {
const listNode = getSelectedListNode();

if ( startContainer.nodeType === window.Node.TEXT_NODE ) {
startContainer = startContainer.parentNode;
}
// Consider the root list selected if nothing is selected.
return ! listNode || listNode.contentEditable === 'true';
}

const rootNode = startContainer.closest( '*[contenteditable]' );
function isActiveListType( tagName, rootTagName ) {
const listNode = getSelectedListNode();

if ( ! rootNode || ! rootNode.contains( startContainer ) ) {
if ( ! listNode ) {
return tagName === rootTagName;
}

const list = startContainer.closest( 'ol,ul' );

if ( ! list ) {
return;
}

return list.nodeName.toLowerCase() === tagName;
return listNode.nodeName.toLowerCase() === tagName;
}

export const ListEdit = ( {
Expand Down
11 changes: 4 additions & 7 deletions packages/rich-text/src/change-list-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ import { getParentLineIndex } from './get-parent-line-index';
*/
export function changeListType( value, newFormat ) {
const { text, formats, start, end } = value;
const startLineIndex = getLineIndex( value, start );
const endLineIndex = getLineIndex( value, end );
const startLineFormats = formats[ startLineIndex ] || [];
const endLineFormats = formats[ endLineIndex ] || [];
const startIndex = getParentLineIndex( value, startLineIndex, startLineFormats.length );
const length = text.length;
const startLineFormats = formats[ getLineIndex( value, start ) ] || [];
const endLineFormats = formats[ getLineIndex( value, end ) ] || [];
const startIndex = getParentLineIndex( value, start );
const newFormats = formats.slice( 0 );
const startCount = startLineFormats.length - 1;
const endCount = endLineFormats.length - 1;

let changed;

for ( let index = startIndex + 1 || 0; index < length; index++ ) {
for ( let index = startIndex + 1 || 0; index < text.length; index++ ) {
if ( text[ index ] !== LINE_SEPARATOR ) {
continue;
}
Expand Down
10 changes: 7 additions & 3 deletions packages/rich-text/src/get-parent-line-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import { LINE_SEPARATOR } from './special-characters';
* @return {Array} The parent list line index.
*/
export function getParentLineIndex( { text, formats }, startIndex ) {
const startFormats = formats[ startIndex ] || [];

let index = startIndex;
let startFormats;

while ( index-- ) {
if ( text[ index ] !== LINE_SEPARATOR ) {
Expand All @@ -26,7 +25,12 @@ export function getParentLineIndex( { text, formats }, startIndex ) {

const formatsAtIndex = formats[ index ] || [];

if ( formatsAtIndex.length === startFormats - 1 ) {
if ( ! startFormats ) {
startFormats = formatsAtIndex;
continue;
}

if ( formatsAtIndex.length === startFormats.length - 1 ) {
return index;
}
}
Expand Down
6 changes: 2 additions & 4 deletions packages/rich-text/src/indent-list-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ export function indentListItems( value, rootFormat ) {

const { text, formats, start, end } = value;
const formatsAtLineIndex = formats[ lineIndex ] || [];
const previousLineIndex = getLineIndex( value, lineIndex );
const formatsAtPreviousLineIndex = formats[ previousLineIndex ] || [];
const targetFormats = formats[ getLineIndex( value, lineIndex ) ] || [];

// The the indentation of the current line is greater than previous line,
// then the line cannot be furter indented.
if ( formatsAtLineIndex.length > formatsAtPreviousLineIndex.length ) {
if ( formatsAtLineIndex.length > targetFormats.length ) {
return value;
}

const newFormats = formats.slice();
const targetFormats = formats[ getLineIndex( value, lineIndex ) ] || [];

for ( let index = lineIndex; index < end; index++ ) {
if ( text[ index ] !== LINE_SEPARATOR ) {
Expand Down

0 comments on commit 360e6dc

Please sign in to comment.