Plugin Directory

Changeset 2435466 for tinymce-advanced


Ignore:
Timestamp:
12/09/2020 01:24:16 PM (5 years ago)
Author:
azaozz
Message:

Updae for WP 5.6.

Location:
tinymce-advanced/branches/dev/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • tinymce-advanced/branches/dev/src/block-editor/classic-paragraph/edit.js

    r2389434 r2435466  
    44 */
    55
     6
    67const { wp } = window;
    7 const { Component, createElement } = wp.element;
    8 const { BACKSPACE, DELETE, F10 } = wp.keycodes;
     8const { debounce } = window._;
     9const settings = window.tadvBlockRegister;
     10
     11const { BlockControls, useBlockProps } = wp.blockEditor;
     12const { ToolbarGroup } = wp.components;
     13const { Component, createElement, Fragment, useEffect, useRef } = wp.element;
     14const { BACKSPACE, DELETE, F10, isKeyboardEvent } = wp.keycodes;
    915
    1016// TODO
    1117// const { BlockControls, BlockAlignmentToolbar } = wp.blockEditor;
    12 
    13 const settings = window.tadvBlockRegister;
    14 
    1518
    1619function isTmceEmpty( editor ) {
     
    3033}
    3134
    32 export default class ClassicEdit extends Component {
    33     constructor( props ) {
    34         super( props );
    35         this.initialize = this.initialize.bind( this );
    36         this.onSetup = this.onSetup.bind( this );
    37         this.focus = this.focus.bind( this );
    38     }
    39 
    40     componentDidMount() {
     35export default function TadvEdit( {
     36    clientId,
     37    attributes: { content },
     38    setAttributes,
     39    onReplace,
     40} ) {
     41    const didMount = useRef( false );
     42
     43    useEffect( () => {
     44        if ( ! didMount.current ) {
     45            return;
     46        }
     47
     48        const editor = window.tinymce.get( `editor-${ clientId }` );
     49        let currentContent;
     50
     51        if ( editor ) {
     52            currentContent = editor.getContent();
     53        }
     54
     55        if ( currentContent !== content ) {
     56            editor.setContent( content || '' );
     57        }
     58    }, [ content ] );
     59
     60    useEffect( () => {
    4161        const { baseURL, suffix } = window.wpEditorL10n.tinymce;
     62
     63        didMount.current = true;
    4264
    4365        window.tinymce.EditorManager.overrideDefaults( {
     
    4668        } );
    4769
     70        function onSetup( editor ) {
     71            let bookmark;
     72
     73            if ( content ) {
     74                editor.on( 'loadContent', () => editor.setContent( content ) );
     75            }
     76
     77            editor.on( 'blur', () => {
     78                bookmark = editor.selection.getBookmark( 2, true );
     79                // There is an issue with Chrome and the editor.focus call in core at https://core.trac.wordpress.org/browser/trunk/src/js/_enqueues/lib/link.js#L451.
     80                // This causes a scroll to the top of editor content on return from some content updating dialogs so tracking
     81                // scroll position until this is fixed in core.
     82                const scrollContainer = document.querySelector(
     83                    '.interface-interface-skeleton__content'
     84                );
     85                const scrollPosition = scrollContainer.scrollTop;
     86
     87                setAttributes( {
     88                    content: editor.getContent(),
     89                } );
     90
     91                editor.once( 'focus', () => {
     92                    if ( bookmark ) {
     93                        editor.selection.moveToBookmark( bookmark );
     94                        if ( scrollContainer.scrollTop !== scrollPosition ) {
     95                            scrollContainer.scrollTop = scrollPosition;
     96                        }
     97                    }
     98                } );
     99
     100                return false;
     101            } );
     102
     103            editor.on( 'mousedown touchstart', () => {
     104                bookmark = null;
     105            } );
     106
     107            const debouncedOnChange = debounce( () => {
     108                const value = editor.getContent();
     109
     110                if ( value !== editor._lastChange ) {
     111                    editor._lastChange = value;
     112                    setAttributes( {
     113                        content: value,
     114                    } );
     115                }
     116            }, 250 );
     117            editor.on( 'Paste Change input Undo Redo', debouncedOnChange );
     118
     119            // We need to cancel the debounce call because when we remove
     120            // the editor (onUnmount) this callback is executed in
     121            // another tick. This results in setting the content to empty.
     122            editor.on( 'remove', debouncedOnChange.cancel );
     123
     124            editor.on( 'keydown', ( event ) => {
     125                if ( isKeyboardEvent.primary( event, 'z' ) ) {
     126                    // Prevent the gutenberg undo kicking in so TinyMCE undo stack works as expected
     127                    event.stopPropagation();
     128                }
     129
     130                if (
     131                    ( event.keyCode === BACKSPACE ||
     132                        event.keyCode === DELETE ) &&
     133                    isTmceEmpty( editor )
     134                ) {
     135                    // delete the block
     136                    onReplace( [] );
     137                    event.preventDefault();
     138                    event.stopImmediatePropagation();
     139                }
     140
     141                const { altKey } = event;
     142                /*
     143                 * Prevent Mousetrap from kicking in: TinyMCE already uses its own
     144                 * `alt+f10` shortcut to focus its toolbar.
     145                 */
     146                if ( altKey && event.keyCode === F10 ) {
     147                    event.stopPropagation();
     148                }
     149            } );
     150
     151            editor.on( 'init', () => {
     152                const rootNode = editor.getBody();
     153
     154                // Create the toolbar by refocussing the editor.
     155                if ( rootNode.ownerDocument.activeElement === rootNode ) {
     156                    rootNode.blur();
     157                    editor.focus();
     158                }
     159            } );
     160        }
     161
     162        function initialize() {
     163            const { settings } = window.wpEditorL10n.tinymce;
     164            wp.oldEditor.initialize( `editor-${ clientId }`, {
     165                tinymce: {
     166                    ...settings,
     167                    inline: true,
     168                    content_css: false,
     169                    fixed_toolbar_container: `#toolbar-${ clientId }`,
     170                    setup: onSetup,
     171                },
     172            } );
     173        }
     174
     175        function onReadyStateChange() {
     176            if ( document.readyState === 'complete' ) {
     177                initialize();
     178            }
     179        }
     180
    48181        if ( document.readyState === 'complete' ) {
    49             this.initialize();
     182            initialize();
    50183        } else {
    51             window.addEventListener( 'DOMContentLoaded', this.initialize );
    52         }
    53     }
    54 
    55     componentWillUnmount() {
    56         window.addEventListener( 'DOMContentLoaded', this.initialize );
    57         wp.oldEditor.remove( `editor-${ this.props.clientId }` );
    58     }
    59 
    60     componentDidUpdate( prevProps ) {
    61         const { clientId, isSelected, attributes: { content } } = this.props;
     184            document.addEventListener( 'readystatechange', onReadyStateChange );
     185        }
     186
     187        return () => {
     188            document.removeEventListener(
     189                'readystatechange',
     190                onReadyStateChange
     191            );
     192            wp.oldEditor.remove( `editor-${ clientId }` );
     193        };
     194    }, [] );
     195
     196    function focus() {
    62197        const editor = window.tinymce.get( `editor-${ clientId }` );
    63 
    64         if ( prevProps.attributes.content !== content && this.content !== content ) {
    65             editor.setContent( content || '' );
    66         }
    67 
    68         if ( ! isSelected && editor.initialized ) {
    69             editor.fire( 'blur', { wpBlockDidUpdate: true } );
    70         }
    71     }
    72 
    73     initialize() {
    74         const { clientId, setAttributes } = this.props;
    75         const { settings } = window.wpEditorL10n.tinymce;
    76         wp.oldEditor.initialize( `editor-${ clientId }`, {
    77             tinymce: {
    78                 ...settings,
    79                 inline: true,
    80                 content_css: false,
    81                 fixed_toolbar_container: `#toolbar-${ clientId }`,
    82                 setup: this.onSetup,
    83             },
    84         } );
    85 
    86         setAttributes({
    87             id: clientId,
    88         });
    89     }
    90 
    91     onSetup( editor ) {
    92         const { attributes: { content }, setAttributes } = this.props;
    93         let bookmark;
    94 
    95         this.editor = editor;
    96 
    97         if ( content ) {
    98             editor.on( 'loadContent', () => editor.setContent( content ) );
    99         }
    100 
    101         editor.on( 'blur', ( event ) => {
    102             if ( event.wpBlockDidUpdate ) {
    103                 return;
    104             }
    105 
    106             bookmark = editor.selection.getBookmark( 2, true );
    107             this.content = editor.getContent();
    108             const { isSelected } = this.props;
    109 
    110             setAttributes( {
    111                 content: this.content,
    112             } );
    113 
    114             editor.once( 'focus', () => {
    115                 if ( bookmark ) {
    116                     editor.selection.moveToBookmark( bookmark );
    117                 }
    118             } );
    119 
    120             if ( isSelected ) {
    121                 return false;
    122             }
    123         } );
    124 
    125         editor.on( 'mousedown touchstart', () => {
    126             bookmark = null;
    127         } );
    128 
    129         editor.on( 'keydown', ( event ) => {
    130             if ( ( event.keyCode === BACKSPACE || event.keyCode === DELETE ) && isTmceEmpty( editor ) ) {
    131                 // delete the block
    132                 this.props.onReplace( [] );
    133                 event.preventDefault();
    134                 event.stopImmediatePropagation();
    135             }
    136 
    137             const { altKey } = event;
    138             /*
    139              * Prevent Mousetrap from kicking in: TinyMCE already uses its own
    140              * `alt+f10` shortcut to focus its toolbar.
    141              */
    142             if ( altKey && event.keyCode === F10 ) {
    143                 event.stopPropagation();
    144             }
    145         } );
    146 
    147         editor.on( 'init', () => {
    148             const rootNode = this.editor.getBody();
    149 
    150             // Create the toolbar by refocussing the editor.
    151             if ( document.activeElement === rootNode ) {
    152                 rootNode.blur();
    153                 this.editor.focus();
    154             }
    155         } );
    156     }
    157 
    158     focus() {
    159         if ( this.editor ) {
    160             this.editor.focus();
    161         }
    162     }
    163 
    164     onToolbarKeyDown( event ) {
     198        if ( editor ) {
     199            editor.focus();
     200        }
     201    }
     202
     203    function onToolbarKeyDown( event ) {
     204        // Prevent WritingFlow from kicking in and allow arrows navigation on the toolbar.
    165205        event.stopPropagation();
     206        // Prevent Mousetrap from moving focus to the top toolbar when pressing `alt+f10` on this block toolbar.
    166207        event.nativeEvent.stopImmediatePropagation();
    167208    }
    168209
    169     render() {
    170         const { clientId, name, attributes, setAttributes } = this.props;
    171 
    172         // TODO
    173         //const width = attributes.width || null;
    174 
    175         return [
    176             createElement( 'div', {
    177                 key: "toolbar",
     210    return createElement(
     211        'div',
     212        useBlockProps(),
     213        createElement(
     214            'div',
     215            {
     216                key: 'toolbar',
    178217                id: `toolbar-${ clientId }`,
    179                 className: "block-library-classic__toolbar tma-classic-paragraph__toolbar",
    180                 onClick: this.focus,
     218                className: 'block-library-classic__toolbar tma-classic-paragraph__toolbar',
     219                onClick: focus,
    181220                'data-placeholder': settings.classicParagraphTitle,
    182                 onKeyDown: this.onToolbarKeyDown,
    183             } ),
    184             createElement( 'div', {
    185                 key: "editor",
     221                onKeyDown: onToolbarKeyDown,
     222            }
     223        ),
     224        createElement(
     225            'div',
     226            {
     227                key: 'editor',
    186228                id: `editor-${ clientId }`,
    187                 className: "wp-block-freeform block-library-rich-text__tinymce tma-classic-paragraph",
    188             } ),
    189 
    190             /* TODO
    191             createElement( BlockControls,
    192                 null,
    193                 createElement( BlockAlignmentToolbar, {
    194                     value: width,
    195                     onChange: ( nextWidth ) => setAttributes( { width: nextWidth } ),
    196                     controls: [ 'center', 'wide', 'full' ],
    197                 } )
    198             ),
    199             */
    200         ];
    201     }
     229                className: 'wp-block-freeform block-library-rich-text__tinymce tma-classic-paragraph',
     230            }
     231        )
     232    );
     233
     234    /* TODO: Add alignment toolbar with "wide" and "full" buttons?
     235        const width = attributes.width || null;
     236
     237        createElement( BlockControls,
     238            null,
     239            createElement( BlockAlignmentToolbar, {
     240                value: width,
     241                onChange: ( nextWidth ) => setAttributes( { width: nextWidth } ),
     242                controls: [ 'center', 'wide', 'full' ],
     243            } )
     244        )
     245    */
    202246}
  • tinymce-advanced/branches/dev/src/block-editor/classic-paragraph/index.js

    r2389434 r2435466  
    4848                        label: __( 'Convert to Blocks' ),
    4949                        onClick: convertToBlocks,
    50                         small: null,
    51                         role: 'menuitem',
    5250                    }
    5351                );
  • tinymce-advanced/branches/dev/src/block-editor/tma-block-editor.css

    r2389434 r2435466  
    3535
    3636.block-library-classic__toolbar .mce-menubar i.mce-caret {
    37     margin-top: .35em;
     37    margin-top: .4em;
    3838}
    3939
  • tinymce-advanced/branches/dev/src/readme.txt

    r2389448 r2435466  
    22Contributors: automattic, azaozz
    33Tags: block editor, classic editor, editor, Gutenberg, formatting, tinymce, write
    4 Requires at least: 5.5
    5 Tested up to: 5.5
    6 Stable tag: 5.5.0
     4Requires at least: 5.6
     5Tested up to: 5.6
     6Stable tag: 5.6.0
    77Requires PHP: 5.6
    88License: GPLv2
     
    4747
    4848== Changelog ==
     49
     50= 5.6.0 =
     51* Updated for WordPress 5.6.
    4952
    5053= 5.5.1 =
Note: See TracChangeset for help on using the changeset viewer.