From 794c90e7d1244ba1c90b0f3d75f39123e6737087 Mon Sep 17 00:00:00 2001 From: iseulde Date: Thu, 6 Dec 2018 22:27:02 +0100 Subject: [PATCH] Add logic to change list type --- lib/client-assets.php | 2 - lib/packages-dependencies.php | 2 +- .../editor/src/components/rich-text/index.js | 15 +---- .../src/components/rich-text/list-edit.js | 67 +++++++++++++------ .../src/components/rich-text/tinymce.js | 5 -- packages/rich-text/src/change-list-type.js | 53 +++++++++++++++ packages/rich-text/src/index.js | 1 + 7 files changed, 102 insertions(+), 43 deletions(-) create mode 100644 packages/rich-text/src/change-list-type.js diff --git a/lib/client-assets.php b/lib/client-assets.php index 6fa221bf4c5d8c..64754889998aab 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -87,8 +87,6 @@ function register_tinymce_scripts() { gutenberg_override_script( 'wp-tinymce-root', includes_url( 'js/tinymce/' ) . "tinymce{$mce_suffix}.js", array(), $tinymce_version ); gutenberg_override_script( 'wp-tinymce', includes_url( 'js/tinymce/' ) . "plugins/compat3x/plugin{$suffix}.js", array( 'wp-tinymce-root' ), $tinymce_version ); } - - gutenberg_override_script( 'wp-tinymce-lists', includes_url( 'js/tinymce/' ) . "plugins/lists/plugin{$suffix}.js", array( 'wp-tinymce' ), $tinymce_version ); } } diff --git a/lib/packages-dependencies.php b/lib/packages-dependencies.php index 34c70851ff727c..193405c932ad52 100644 --- a/lib/packages-dependencies.php +++ b/lib/packages-dependencies.php @@ -133,7 +133,7 @@ 'wp-editor' => array( 'jquery', 'lodash', - 'wp-tinymce-lists', + 'wp-tinymce', 'wp-a11y', 'wp-api-fetch', 'wp-blob', diff --git a/packages/editor/src/components/rich-text/index.js b/packages/editor/src/components/rich-text/index.js index fe10c1dc8fdbb6..6ebb7f49b16def 100644 --- a/packages/editor/src/components/rich-text/index.js +++ b/packages/editor/src/components/rich-text/index.js @@ -91,7 +91,6 @@ export class RichText extends Component { this.onSplit = this.props.unstableOnSplit; } - this.onSetup = this.onSetup.bind( this ); this.onFocus = this.onFocus.bind( this ); this.onBlur = this.onBlur.bind( this ); this.onChange = this.onChange.bind( this ); @@ -133,15 +132,6 @@ export class RichText extends Component { this.editableRef = node; } - /** - * Sets a reference to the TinyMCE editor instance. - * - * @param {Editor} editor The editor instance as passed by TinyMCE. - */ - onSetup( editor ) { - this.editor = editor; - } - setFocusedElement() { if ( this.props.setFocusedElement ) { this.props.setFocusedElement( this.props.instanceId ); @@ -831,12 +821,10 @@ export class RichText extends Component {
- { isSelected && this.editor && this.multilineTag === 'li' && ( + { isSelected && this.multilineTag === 'li' && ( this.onChange( this.createRecord() ) } value={ record } onChange={ this.onChange } /> @@ -861,7 +849,6 @@ export class RichText extends Component { ( @@ -82,26 +109,24 @@ export const ListEdit = ( { { icon: 'editor-ul', title: __( 'Convert to unordered list' ), - isActive: isActiveListType( editor, 'ul', tagName ), + isActive: isActiveListType( 'ul', tagName ), onClick() { - if ( isListRootSelected( editor ) ) { + onChange( changeListType( value, { type: 'ul' } ) ); + + if ( isListRootSelected() ) { onTagNameChange( 'ul' ); - } else { - editor.execCommand( 'InsertUnorderedList' ); - onSyncDOM(); } }, }, { icon: 'editor-ol', title: __( 'Convert to ordered list' ), - isActive: isActiveListType( editor, 'ol', tagName ), + isActive: isActiveListType( 'ol', tagName ), onClick() { - if ( isListRootSelected( editor ) ) { + onChange( changeListType( value, { type: 'ol' } ) ); + + if ( isListRootSelected() ) { onTagNameChange( 'ol' ); - } else { - editor.execCommand( 'InsertOrderedList' ); - onSyncDOM(); } }, }, diff --git a/packages/editor/src/components/rich-text/tinymce.js b/packages/editor/src/components/rich-text/tinymce.js index a29117bdce24d8..529fa15c1a54ce 100644 --- a/packages/editor/src/components/rich-text/tinymce.js +++ b/packages/editor/src/components/rich-text/tinymce.js @@ -200,16 +200,11 @@ export default class TinyMCE extends Component { lists_indent_on_tab: false, }; - if ( multilineTag === 'li' ) { - settings.plugins.push( 'lists' ); - } - tinymce.init( { ...settings, target: this.editorNode, setup: ( editor ) => { this.editor = editor; - this.props.onSetup( editor ); // TinyMCE resets the element content on initialization, even // when it's already identical to what exists currently. This diff --git a/packages/rich-text/src/change-list-type.js b/packages/rich-text/src/change-list-type.js new file mode 100644 index 00000000000000..fd1dede3c617ce --- /dev/null +++ b/packages/rich-text/src/change-list-type.js @@ -0,0 +1,53 @@ +/** + * Internal dependencies + */ + +import { LINE_SEPARATOR } from './special-characters'; +import { normaliseFormats } from './normalise-formats'; + +function getLineIndex( { start, text }, startIndex = start ) { + let index = startIndex; + + while ( index-- ) { + if ( text[ index ] === LINE_SEPARATOR ) { + return 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 length = text.length; + const newFormats = formats.slice( 0 ); + const startCount = startLineFormats.length - 1; + const endCount = endLineFormats.length - 1; + + for ( let index = startLineIndex || 0; index < length; index++ ) { + if ( text[ index ] !== LINE_SEPARATOR ) { + continue; + } + + if ( ( newFormats[ index ] || [] ).length <= startCount ) { + break; + } + + if ( ! newFormats[ index ] ) { + continue; + } + + newFormats[ index ] = newFormats[ index ].map( ( format, i ) => { + return i < startCount || i > endCount ? format : newFormat; + } ); + } + + return normaliseFormats( { + text, + formats: newFormats, + start, + end, + } ); +} diff --git a/packages/rich-text/src/index.js b/packages/rich-text/src/index.js index 712c1a609b3441..86f7cd1b29ae1d 100644 --- a/packages/rich-text/src/index.js +++ b/packages/rich-text/src/index.js @@ -27,3 +27,4 @@ export { LINE_SEPARATOR } from './special-characters'; export { unregisterFormatType } from './unregister-format-type'; export { indentListItems } from './indent-list-items'; export { outdentListItems } from './outdent-list-items'; +export { changeListType } from './change-list-type';