-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RichText: List: Fix indent/outdent (#12667)
* RichText: List: use value to indent/outdent * Add outdent * Support multi outdent * Hold one reference per format * Keep list formats when indenting to new index * Remove unneeded parameter * Rename * Add logic to change list type * Add tests * Add e2e tests * Add some basic docs. Clean up. * Remove duplicate wp-tinymce dependency * Clean up * Add more docs, fix getSelectedListNode * Put duplicate text values in constant
- Loading branch information
1 parent
0df62c5
commit 6dacd18
Showing
17 changed files
with
785 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
import { LINE_SEPARATOR } from './special-characters'; | ||
import { normaliseFormats } from './normalise-formats'; | ||
import { getLineIndex } from './get-line-index'; | ||
import { getParentLineIndex } from './get-parent-line-index'; | ||
|
||
/** | ||
* Changes the list type of the selected indented list, if any. Looks at the | ||
* currently selected list item and takes the parent list, then changes the list | ||
* type of this list. When multiple lines are selected, the parent lists are | ||
* takes and changed. | ||
* | ||
* @param {Object} value Value to change. | ||
* @param {Object} newFormat The new list format object. Choose between | ||
* `{ type: 'ol' }` and `{ type: 'ul' }`. | ||
* | ||
* @return {Object} The changed value. | ||
*/ | ||
export function changeListType( value, newFormat ) { | ||
const { text, formats, start, end } = value; | ||
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 < text.length; index++ ) { | ||
if ( text[ index ] !== LINE_SEPARATOR ) { | ||
continue; | ||
} | ||
|
||
if ( ( newFormats[ index ] || [] ).length <= startCount ) { | ||
break; | ||
} | ||
|
||
if ( ! newFormats[ index ] ) { | ||
continue; | ||
} | ||
|
||
changed = true; | ||
newFormats[ index ] = newFormats[ index ].map( ( format, i ) => { | ||
return i < startCount || i > endCount ? format : newFormat; | ||
} ); | ||
} | ||
|
||
if ( ! changed ) { | ||
return value; | ||
} | ||
|
||
return normaliseFormats( { | ||
text, | ||
formats: newFormats, | ||
start, | ||
end, | ||
} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
import { LINE_SEPARATOR } from './special-characters'; | ||
|
||
/** | ||
* Gets the currently selected line index, or the first line index if the | ||
* selection spans over multiple items. | ||
* | ||
* @param {Object} value Value to get the line index from. | ||
* @param {boolean} startIndex Optional index that should be contained by the | ||
* line. Defaults to the selection start of the | ||
* value. | ||
* | ||
* @return {?boolean} The line index. Undefined if not found. | ||
*/ | ||
export function getLineIndex( { start, text }, startIndex = start ) { | ||
let index = startIndex; | ||
|
||
while ( index-- ) { | ||
if ( text[ index ] === LINE_SEPARATOR ) { | ||
return index; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
import { LINE_SEPARATOR } from './special-characters'; | ||
|
||
/** | ||
* Gets the index of the first parent list. To get the parent list formats, we | ||
* go through every list item until we find one with exactly one format type | ||
* less. | ||
* | ||
* @param {Object} value Value to search. | ||
* @param {number} startIndex Index to start search at. | ||
* | ||
* @return {Array} The parent list line index. | ||
*/ | ||
export function getParentLineIndex( { text, formats }, startIndex ) { | ||
let index = startIndex; | ||
let startFormats; | ||
|
||
while ( index-- ) { | ||
if ( text[ index ] !== LINE_SEPARATOR ) { | ||
continue; | ||
} | ||
|
||
const formatsAtIndex = formats[ index ] || []; | ||
|
||
if ( ! startFormats ) { | ||
startFormats = formatsAtIndex; | ||
continue; | ||
} | ||
|
||
if ( formatsAtIndex.length === startFormats.length - 1 ) { | ||
return index; | ||
} | ||
} | ||
} |
Oops, something went wrong.