blocks
4 years ago
tinymce
4 years ago
script-insert.js
4 years ago
script-settings.js
4 years ago
script-tools.js
4 years ago
script.js
4 years ago
script-tools.js
108 lines
| 1 | function sc_show_insert(shortcode=false, block_editor_id=false, block_inline_insert=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 | window.SC_INSERT_VARS.block_inline_insert = block_inline_insert; |
| 11 | |
| 12 | if($('#sci_wrap').length != 0 && !window.SC_INSERT_VARS.popup_opened){ |
| 13 | $('#sci_wrap').show(); |
| 14 | sc_notify_insert(shortcode); |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | $('body').append(popup); |
| 19 | |
| 20 | $('#sci_title').text(window.SC_INSERT_VARS.popup_title); |
| 21 | $('#sci_popup > iframe').attr('src', window.SC_INSERT_VARS.insert_page); |
| 22 | |
| 23 | $('#sci_close').on('click', function(){ |
| 24 | sc_close_insert(); |
| 25 | }); |
| 26 | |
| 27 | window.SC_INSERT_VARS.popup_opened = true; |
| 28 | window.SC_INSERT_VARS.iframe = $('#sci_popup > iframe'); |
| 29 | |
| 30 | window.SC_INSERT_VARS.iframe.load(function(){ |
| 31 | sc_notify_insert(shortcode); |
| 32 | }); |
| 33 | |
| 34 | } |
| 35 | |
| 36 | function sc_close_insert(){ |
| 37 | jQuery('#sci_wrap').hide(); |
| 38 | window.SC_INSERT_VARS.popup_opened = false; |
| 39 | window.SC_INSERT_VARS.block_editor = false; |
| 40 | } |
| 41 | |
| 42 | function sc_notify_insert(shortcode){ |
| 43 | |
| 44 | if(shortcode === false){ |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | var $iframe = window.SC_INSERT_VARS.iframe; |
| 49 | var content_window = $iframe[0].contentWindow; |
| 50 | |
| 51 | content_window.postMessage(shortcode); |
| 52 | |
| 53 | } |
| 54 | |
| 55 | function sc_block_editor_content(content){ |
| 56 | var block_id = window.SC_INSERT_VARS.block_editor; |
| 57 | |
| 58 | if(block_id !== false){ |
| 59 | var sc_box = document.getElementById('shortcoder-input-' + block_id); |
| 60 | |
| 61 | sc_set_native_value(sc_box, content); |
| 62 | sc_box.dispatchEvent(new Event('input', { bubbles: true })); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | function sc_block_inline_insert(content){ |
| 71 | |
| 72 | if(window.SC_INSERT_VARS.block_inline_insert && window.sc_inline_insert_props){ |
| 73 | var props = window.sc_inline_insert_props.props; |
| 74 | var insert = window.sc_inline_insert_props.insert; |
| 75 | props.onChange( |
| 76 | insert(props.value, content) |
| 77 | ); |
| 78 | window.sc_inline_insert_props = false; |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | |
| 84 | } |
| 85 | |
| 86 | function sc_qt_show_insert(){ |
| 87 | sc_show_insert(); |
| 88 | } |
| 89 | |
| 90 | function sc_set_native_value(element, value) { |
| 91 | var valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set; |
| 92 | var prototype = Object.getPrototypeOf(element); |
| 93 | var prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set; |
| 94 | |
| 95 | if (valueSetter && valueSetter !== prototypeValueSetter) { |
| 96 | prototypeValueSetter.call(element, value); |
| 97 | } else { |
| 98 | valueSetter.call(element, value); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if(window.addEventListener){ |
| 103 | window.addEventListener('load', function(){ |
| 104 | if( typeof QTags === 'function' ){ |
| 105 | QTags.addButton( 'QT_sc_insert', 'Shortcoder', sc_qt_show_insert ); |
| 106 | } |
| 107 | }); |
| 108 | } |