github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/pubnot/trumbowyg/plugins/preformatted/trumbowyg.preformatted.js (about) 1 /* =========================================================== 2 * trumbowyg.preformatted.js v1.0 3 * Preformatted plugin for Trumbowyg 4 * http://alex-d.github.com/Trumbowyg 5 * =========================================================== 6 * Author : Casella Edoardo (Civile) 7 */ 8 9 10 (function ($) { 11 'use strict'; 12 13 $.extend(true, $.trumbowyg, { 14 langs: { 15 // jshint camelcase:false 16 en: { 17 preformatted: 'Code sample <pre>' 18 }, 19 fr: { 20 preformatted: 'Exemple de code' 21 }, 22 it: { 23 preformatted: 'Codice <pre>' 24 }, 25 zh_cn: { 26 preformatted: '代码示例 <pre>' 27 }, 28 ru: { 29 preformatted: 'Пример кода <pre>' 30 }, 31 ja: { 32 preformatted: 'コードサンプル <pre>' 33 } 34 }, 35 // jshint camelcase:true 36 37 plugins: { 38 preformatted: { 39 init: function (trumbowyg) { 40 var btnDef = { 41 fn: function () { 42 trumbowyg.saveRange(); 43 var text = trumbowyg.getRangeText(); 44 if (text.replace(/\s/g, '') !== '') { 45 try { 46 var curtag = getSelectionParentElement().tagName.toLowerCase(); 47 if (curtag === 'code' || curtag === 'pre') { 48 return unwrapCode(); 49 } 50 else { 51 trumbowyg.execCmd('insertHTML', '<pre><code>' + strip(text) + '</code></pre>'); 52 } 53 } catch (e) { 54 } 55 } 56 }, 57 tag: 'pre' 58 }; 59 60 trumbowyg.addBtnDef('preformatted', btnDef); 61 } 62 } 63 } 64 }); 65 66 /* 67 * GetSelectionParentElement 68 */ 69 function getSelectionParentElement() { 70 var parentEl = null, 71 selection; 72 if (window.getSelection) { 73 selection = window.getSelection(); 74 if (selection.rangeCount) { 75 parentEl = selection.getRangeAt(0).commonAncestorContainer; 76 if (parentEl.nodeType !== 1) { 77 parentEl = parentEl.parentNode; 78 } 79 } 80 } else if ((selection = document.selection) && selection.type !== 'Control') { 81 parentEl = selection.createRange().parentElement(); 82 } 83 return parentEl; 84 } 85 86 /* 87 * Strip 88 * returns a text without HTML tags 89 */ 90 function strip(html) { 91 var tmp = document.createElement('DIV'); 92 tmp.innerHTML = html; 93 return tmp.textContent || tmp.innerText || ''; 94 } 95 96 /* 97 * UnwrapCode 98 * ADD/FIX: to improve, works but can be better 99 * "paranoic" solution 100 */ 101 function unwrapCode() { 102 var container = null; 103 if (document.selection) { //for IE 104 container = document.selection.createRange().parentElement(); 105 } else { 106 var select = window.getSelection(); 107 if (select.rangeCount > 0) { 108 container = select.getRangeAt(0).startContainer.parentNode; 109 } 110 } 111 //'paranoic' unwrap 112 var ispre = $(container).contents().closest('pre').length; 113 var iscode = $(container).contents().closest('code').length; 114 if (ispre && iscode) { 115 $(container).contents().unwrap('code').unwrap('pre'); 116 } else if (ispre) { 117 $(container).contents().unwrap('pre'); 118 } else if (iscode) { 119 $(container).contents().unwrap('code'); 120 } 121 } 122 123 })(jQuery);