File modules/editor/trumbowyg/plugins/preformatted/trumbowyg.preformatted.js
Last commit: Tue Aug 28 00:33:27 2018 +0200 Jan Dankert Editoren für Markdown (SimpleMDE) und HTML (Trumbowyg) installiert.
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 <pre>' 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 tr: { 35 preformatted: 'Kod örneği <pre>' 36 } 37 }, 38 // jshint camelcase:true 39 40 plugins: { 41 preformatted: { 42 init: function (trumbowyg) { 43 var btnDef = { 44 fn: function () { 45 trumbowyg.saveRange(); 46 var text = trumbowyg.getRangeText(); 47 if (text.replace(/\s/g, '') !== '') { 48 try { 49 var curtag = getSelectionParentElement().tagName.toLowerCase(); 50 if (curtag === 'code' || curtag === 'pre') { 51 return unwrapCode(); 52 } 53 else { 54 trumbowyg.execCmd('insertHTML', '<pre><code>' + strip(text) + '</code></pre>'); 55 } 56 } catch (e) { 57 } 58 } 59 }, 60 tag: 'pre' 61 }; 62 63 trumbowyg.addBtnDef('preformatted', btnDef); 64 } 65 } 66 } 67 }); 68 69 /* 70 * GetSelectionParentElement 71 */ 72 function getSelectionParentElement() { 73 var parentEl = null, 74 selection; 75 76 if (window.getSelection) { 77 selection = window.getSelection(); 78 if (selection.rangeCount) { 79 parentEl = selection.getRangeAt(0).commonAncestorContainer; 80 if (parentEl.nodeType !== 1) { 81 parentEl = parentEl.parentNode; 82 } 83 } 84 } else if ((selection = document.selection) && selection.type !== 'Control') { 85 parentEl = selection.createRange().parentElement(); 86 } 87 88 return parentEl; 89 } 90 91 /* 92 * Strip 93 * returns a text without HTML tags 94 */ 95 function strip(html) { 96 var tmp = document.createElement('DIV'); 97 tmp.innerHTML = html; 98 return tmp.textContent || tmp.innerText || ''; 99 } 100 101 /* 102 * UnwrapCode 103 * ADD/FIX: to improve, works but can be better 104 * "paranoic" solution 105 */ 106 function unwrapCode() { 107 var container = null; 108 109 if (document.selection) { //for IE 110 container = document.selection.createRange().parentElement(); 111 } else { 112 var select = window.getSelection(); 113 if (select.rangeCount > 0) { 114 container = select.getRangeAt(0).startContainer.parentNode; 115 } 116 } 117 118 //'paranoic' unwrap 119 var ispre = $(container).contents().closest('pre').length; 120 var iscode = $(container).contents().closest('code').length; 121 122 if (ispre && iscode) { 123 $(container).contents().unwrap('code').unwrap('pre'); 124 } else if (ispre) { 125 $(container).contents().unwrap('pre'); 126 } else if (iscode) { 127 $(container).contents().unwrap('code'); 128 } 129 } 130 })(jQuery);
Downloadmodules/editor/trumbowyg/plugins/preformatted/trumbowyg.preformatted.js
History Tue, 28 Aug 2018 00:33:27 +0200 Jan Dankert Editoren für Markdown (SimpleMDE) und HTML (Trumbowyg) installiert.