Advertisement
_Jackky

jackky-tinymce.js

Apr 22nd, 2021 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ( function( $, wp, window, rwmb ) {
  2.     'use strict';
  3.  
  4.     /**
  5.      * Transform textarea into wysiwyg editor.
  6.      */
  7.     function transform() {
  8.         var $this = $( this ),
  9.             $wrapper = $this.closest( '.wp-editor-wrap' ),
  10.             id = $this.attr( 'id' ),
  11.             isInBlock = $this.closest( '.wp-block, .components-panel' ).length > 0;
  12.  
  13.         // Update the ID attribute if the editor is in a new block.
  14.         if ( isInBlock ) {
  15.             id = id + '_' + rwmb.uniqid();
  16.             $this.attr( 'id', id );
  17.         }
  18.  
  19.         // Get current editor mode before updating the DOM.
  20.         var mode = $wrapper.hasClass( 'tmce-active' ) ? 'tmce' : 'html';
  21.  
  22.         // Update the DOM
  23.         $this.show();
  24.         updateDom( $wrapper, id );
  25.         // Get id of the original editor to get its tinyMCE and quick tags settings
  26.         var originalId      = getOriginalId( this ),
  27.             settings        = getEditorSettings( originalId ),
  28.             customSettings  = $('.jackky-tinymce-id[data-id="' + originalId + '"]').data('settings')
  29.  
  30.         // TinyMCE
  31.         if ( window.tinymce ) {
  32.             settings.tinymce.selector = '#' + id;
  33.             settings.tinymce.setup = function( editor )
  34.             {
  35.  
  36.                 editor.on( 'keyup change', function() {
  37.                     editor.save(); // Required for live validation.
  38.                     $this.trigger( 'change' );
  39.                 } );
  40.                
  41.             }
  42.  
  43.             // Set editor mode after initializing.
  44.             settings.tinymce.init_instance_callback = function() {
  45.                 switchEditors.go( id, mode );
  46.             }
  47.  
  48.             tinymce.init( $.extend( settings.tinymce, customSettings.tinymce ) );
  49.         }
  50.  
  51.         // Quick tags
  52.         if ( window.quicktags ) {
  53.             settings.quicktags.id = id;
  54.             quicktags( $.extend( settings.quicktags, customSettings.quicktags ) );
  55.             QTags._buttonsInit();
  56.         }
  57.     }
  58.  
  59.     function getEditorSettings( id ) {
  60.         var settings = getDefaultEditorSettings();
  61.  
  62.         if ( id && tinyMCEPreInit.mceInit.hasOwnProperty( id ) ) {
  63.             settings.tinymce = tinyMCEPreInit.mceInit[ id ];
  64.         }
  65.         if ( id && window.quicktags && tinyMCEPreInit.qtInit.hasOwnProperty( id ) ) {
  66.             settings.quicktags = tinyMCEPreInit.qtInit[ id ];
  67.         }
  68.  
  69.         return settings;
  70.     }
  71.  
  72.     function getDefaultEditorSettings() {
  73.         var settings = wp.editor.getDefaultSettings();
  74.  
  75.         settings.tinymce.toolbar1 = 'formatselect,bold,italic,bullist,numlist,blockquote,alignleft,aligncenter,alignright,link,unlink,wp_more,spellchecker,fullscreen,wp_adv';
  76.         settings.tinymce.toolbar2 = 'strikethrough,hr,forecolor,pastetext,removeformat,charmap,outdent,indent,undo,redo,wp_help';
  77.  
  78.         settings.quicktags.buttons = 'strong,em,link,block,del,ins,img,ul,ol,li,code,more,close';
  79.  
  80.         return settings;
  81.     }
  82.  
  83.     /**
  84.      * Get original ID of the textarea
  85.      * The ID will be used to reference to tinyMCE and quick tags settings
  86.      * @param el Current cloned textarea
  87.      */
  88.     function getOriginalId( el ) {
  89.         return el.closest( '.rwmb-input' ).querySelector( '.jackky-tinymce-id' ).dataset.id;
  90.     }
  91.  
  92.     /**
  93.      * Update id, class, [data-] attributes, ... of the cloned editor.
  94.      * @param $wrapper Editor wrapper element
  95.      * @param id       Editor ID
  96.      */
  97.     function updateDom( $wrapper, id ) {
  98.         // Wrapper div and media buttons
  99.         $wrapper.attr( 'id', 'wp-' + id + '-wrap' )
  100.             .find( '.mce-container' ).remove().end() // Remove rendered tinyMCE editor
  101.             .find( '.wp-editor-tools' ).attr( 'id', 'wp-' + id + '-editor-tools' )
  102.             .find( '.wp-media-buttons' ).attr( 'id', 'wp-' + id + '-media-buttons' )
  103.             .find( 'button' ).data( 'editor', id ).attr( 'data-editor', id );
  104.  
  105.         // Set default active mode.
  106.         $wrapper.removeClass( 'html-active tmce-active' );
  107.         $wrapper.addClass( window.tinymce ? 'tmce-active' : 'html-active' );
  108.  
  109.         // Editor tabs
  110.         $wrapper.find( '.switch-tmce' )
  111.             .attr( 'id', id + 'tmce' )
  112.             .data( 'wp-editor-id', id ).attr( 'data-wp-editor-id', id ).end()
  113.             .find( '.switch-html' )
  114.             .attr( 'id', id + 'html' )
  115.             .data( 'wp-editor-id', id ).attr( 'data-wp-editor-id', id );
  116.  
  117.         // Quick tags
  118.         $wrapper.find( '.wp-editor-container' ).attr( 'id', 'wp-' + id + '-editor-container' )
  119.             .find( '.quicktags-toolbar' ).attr( 'id', 'qt_' + id + '_toolbar' ).html( '' );
  120.     }
  121.  
  122.     function init( e ) {
  123.         $( e.target ).find( '.jackky-tinymce' ).each( transform );
  124.     }
  125.  
  126.     /**
  127.      * Add required attribute for validation.
  128.      *
  129.      * this = textarea element.
  130.      */
  131.     function addRequiredAttribute() {
  132.         if ( this.classList.contains( 'jackky-tinymce-required' ) ) {
  133.             this.setAttribute( 'required', true );
  134.         }
  135.     }
  136.  
  137.     /**
  138.      * Setup events for the classic editor to make live validation work.
  139.      *
  140.      * When change:
  141.      * - Save content to textarea for live validation.
  142.      * - Trigger change event for compatibility.
  143.      *
  144.      * this = textarea element.
  145.      */
  146.     function setupEvents() {
  147.         if ( ! window.tinymce ) {
  148.             return;
  149.         }
  150.         var editor = tinymce.get( this.id );
  151.         if ( ! editor ) {
  152.             return;
  153.         }
  154.         var $this = $( this );
  155.         editor.on( 'keyup change', function() {
  156.             editor.save(); // Required for live validation.
  157.             $this.trigger( 'change' );
  158.         } );
  159.     }
  160.  
  161.     $( function() {
  162.         var $editors = $( '.jackky-tinymce' );
  163.         $editors.each( addRequiredAttribute );
  164.         $editors.each( setupEvents );
  165.  
  166.         // Force re-render editors in Gutenberg. Use setTimeOut to run after all other code. Bug occurs in WP 5.6.
  167.         if ( rwmb.isGutenberg ) {
  168.             setTimeout( function() {
  169.                 $editors.each( transform );
  170.             }, 0 );
  171.         }
  172.     } );
  173.    
  174.    
  175.     rwmb.$document
  176.         .on( 'mb_blocks_edit', init )
  177.         .on( 'mb_init_editors', init )
  178.         .on( 'clone', '.jackky-tinymce', function() {
  179.             /*
  180.              * Transform a textarea to an editor is a heavy task.
  181.              * Moving it to the end of task queue with setTimeout makes cloning faster.
  182.              */
  183.             setTimeout( transform.bind( this ), 0 );
  184.         } );
  185. } )( jQuery, wp, window, rwmb );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement