scratch – Diff between revs 58 and 125

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