blocks
5 years ago
tinymce
5 years ago
script-insert.js
5 years ago
script-tools.js
5 years ago
script.js
5 years ago
script-tools.js
81 lines
| 1 | function sc_show_insert(shortcode=false, block_editor_id=false){ |
| 2 | var $ = jQuery; |
| 3 | var popup = '<div id="sci_wrap"><div id="sci_bg"></div><div id="sci_popup"><header><span id="sci_title"></span><span id="sci_close" title="Close"><span class="dashicons dashicons-no"></span></span></header><iframe></iframe></div></div>'; |
| 4 | |
| 5 | if(typeof window.SC_INSERT_VARS === 'undefined'){ |
| 6 | console.log('Cannot load shortcode insert window as the script is not loaded properly'); |
| 7 | } |
| 8 | |
| 9 | window.SC_INSERT_VARS.block_editor = block_editor_id; |
| 10 | |
| 11 | if($('#sci_wrap').length != 0 && !window.SC_INSERT_VARS.popup_opened){ |
| 12 | $('#sci_wrap').show(); |
| 13 | sc_notify_insert(shortcode); |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | $('body').append(popup); |
| 18 | |
| 19 | $('#sci_title').text(window.SC_INSERT_VARS.popup_title); |
| 20 | $('#sci_popup > iframe').attr('src', window.SC_INSERT_VARS.insert_page); |
| 21 | |
| 22 | $('#sci_close').on('click', function(){ |
| 23 | sc_close_insert(); |
| 24 | }); |
| 25 | |
| 26 | window.SC_INSERT_VARS.popup_opened = true; |
| 27 | window.SC_INSERT_VARS.iframe = $('#sci_popup > iframe'); |
| 28 | |
| 29 | window.SC_INSERT_VARS.iframe.load(function(){ |
| 30 | sc_notify_insert(shortcode); |
| 31 | }); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | function sc_close_insert(){ |
| 36 | jQuery('#sci_wrap').hide(); |
| 37 | window.SC_INSERT_VARS.popup_opened = false; |
| 38 | window.SC_INSERT_VARS.block_editor = false; |
| 39 | } |
| 40 | |
| 41 | function sc_notify_insert(shortcode){ |
| 42 | |
| 43 | if(shortcode === false){ |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | var $iframe = window.SC_INSERT_VARS.iframe; |
| 48 | var content_window = $iframe[0].contentWindow; |
| 49 | |
| 50 | content_window.postMessage(shortcode); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | function sc_block_editor_content(content){ |
| 55 | var block_id = window.SC_INSERT_VARS.block_editor; |
| 56 | |
| 57 | if(block_id !== false){ |
| 58 | var sc_box = document.getElementById('shortcoder_box_' + block_id); |
| 59 | var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value").set; |
| 60 | nativeInputValueSetter.call(sc_box, content); |
| 61 | |
| 62 | var the_event = new Event('input', {bubbles: true}); |
| 63 | sc_box.dispatchEvent(the_event); |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | function sc_qt_show_insert(){ |
| 72 | sc_show_insert(); |
| 73 | } |
| 74 | |
| 75 | if(window.addEventListener){ |
| 76 | window.addEventListener('load', function(){ |
| 77 | if( typeof QTags === 'function' ){ |
| 78 | QTags.addButton( 'QT_sc_insert', 'Shortcoder', sc_qt_show_insert ); |
| 79 | } |
| 80 | }); |
| 81 | } |