1/**
2 * Copyright (c) 2014-2016, CKSource - Frederico Knabben. All rights reserved.
3 * Licensed under the terms of the MIT License (see LICENSE.md).
4 *
5 * The abbr plugin dialog window definition.
6 *
7 * Created out of the CKEditor Plugin SDK:
8 * http://docs.ckeditor.com/#!/guide/plugin_sdk_sample_1
9 */
10
11// Our dialog definition.
12CKEDITOR.dialog.add( 'mswordDialog', function( editor ) {
13    var ed_lang = editor.lang.msword;
14    /*top right  bottom left*/
15//    var olstyle ='list-style: decimal-leading-zero; font-size: 1.1em; margin: 0 0 0 .25em';
16      var olstyle ='font-size: 1.1em;';
17      var oli = '<li style="font-size: 1.1em"><span style="font-size:11pt">';
18
19	return {
20		// Basic properties of the dialog window: title, minimum size.
21		title: 'MS Word',
22		minWidth: 600,
23		minHeight: 350,
24
25		// Dialog window content definition.
26		contents: [
27			{
28				// Definition of the Basic Settings dialog tab (page).
29				id: 'tab-basic',
30				label: 'Text',
31
32				// The tab content.
33				elements: [
34					{
35						type: 'html',
36                        html: '<div contenteditable="true" id="ckgedit_mswin" style="border: 2px solid #ddd; padding:3px; width:600px; height:350px; overflow:auto; cursor:auto;"> </div>',
37                        minWidth: 350,
38		                minHeight: 350,
39					},
40                    {
41                        type: 'hbox',
42                       height: [ '18px;'],
43                       children: [
44                        {
45                          type: 'html',
46                          html: editor.lang.msword.info,
47                        }
48
49                        ]          //hbox children
50                    },            //hbox
51
52				],  //elements
53
54			},  //contents
55            {
56				id: 'Info',
57				label: editor.lang.msword.instructionsLabel,
58
59              	elements: [
60					{
61						type: 'html',
62                        html:  "<div style='padding:40px;overflow:break-word'><ol><li style='font-size:1.2em;'><span style='font-size:11pt'>"+ ed_lang.instructions +'</span></li>'
63                        +'<li style="font-size:1.2em;"><span style="font-size:11pt"> ' + ed_lang.instructions_2 + '</span></li>'
64                        +'<ul style="list-style-position: inside;"><li style="margin: 0 0 0 4px"><span style="font-size:11pt;">'+ed_lang.instructions_3 + '</span></li>'
65                        +'<li style="margin: 0 0 0 4px;"><span style="font-size:11pt">'+ed_lang.instructions_4 + '</span></li>'
66                        + '<li style="margin: 0 0 0 4px"><span style="font-size:11pt">'+ed_lang.instructions_5 + '</span></ul>'
67                         +'<li style="font-size:1.2em;"><span style="font-size:11pt;">' + ed_lang.instructions_6 + '</span></li>'
68                        +'</ol></div>',
69
70
71					},
72               ]
73            }
74		], //contents
75
76       onShow : function()
77       {
78            var dialog = this;
79            geshi_dialog = dialog;
80            selection = editor.getSelection();
81            var selected=editor.getSelectedHtml(true)  ;
82            if(selected) {
83                 var data_id = document.getElementById('ckgedit_mswin');
84                 data_id.innerHTML = selected;
85            }
86                jQuery( "#ckgedit_mswin" ).keydown(function(ev) {
87                      if (ev.which == 13 ) {
88                            ev.stopPropagation();
89                     }
90              });
91       },
92
93
94		onOk: function() {
95			// The context of this function is the dialog object itself.
96			// http://docs.ckeditor.com/#!/api/CKEDITOR.dialog
97           var data_id = document.getElementById('ckgedit_mswin');
98           var inner = data_id.innerHTML;
99            inner = inner.replace(/&lt/gm,'<');
100            inner = inner.replace(/&gt/mg,'>');
101          //if(confirm("exit now")  {
102             //jQuery('.cke_dialog_ui_button_cancel').children().click();
103         // }
104           var  regex = new RegExp('<xml>([^]*)<\/xml>','gm');
105            inner = inner.replace(regex, function(m,n) {
106                return "";
107           });
108
109          if(inner.match(/<img/)) {
110              inner = inner.replace(/<img\s/, '<img alt=\"msword\" ');
111              editor.insertHtml(inner);
112               inner = "";
113          }
114          var  regex = new RegExp('<style>([^]*)<\/style>','gm');
115              inner = inner.replace(regex, function(m,n) {
116               return "";
117           });
118
119          regex = new RegExp('\\<\\!--\\[.*?endif\\]--\\>\\<\\!--\\[if gte.*?\\]\\>([^]*)\\<\\!\\[endif\\]-->','mg');
120           inner = inner.replace(regex,"");
121       //   alert("START\n" +inner +"\nSTOP");
122
123            inner = inner.replace(/&lt/gm,'<');
124            inner = inner.replace(/&gt/mg,'>');
125            inner = inner.replace(/<table\s+class=.*?>/mg, "<table>");
126            inner = inner.replace(/<table.*?>/mg, "<table>");
127            inner = inner.replace(/<tr.*?>/mg, "<tr>");
128            inner = inner.replace(/<td.*?>/mg, "<td>");
129           inner = inner.replace(/style="([^>]+)"/gm,function(m,i){
130                matches = i.match(/level(\d)/);
131                if(matches) {
132                   return  'L_'+matches[1];                   }
133                 return m;
134            });
135
136           inner = inner.replace(/style="[^>]+"/gm,"");
137           inner = inner.replace(/<h(\d).*?><span.*?>/gm,"<h$1>");
138            inner = inner.replace(/(<span\s*>)+/gm,"");
139            inner = inner.replace(/(<\/span>)+/gm,"");
140            inner = inner.replace(/<tbody>([^]+)<\/tbody>/,function(m){
141                m = m.replace(/<\/p>/mg,"");
142                return m.replace(/<p.*?>/mg,"");
143            });
144
145             inner = inner.replace(/<p class="MsoListParagraphCxSpFirst"\s+L_\d>([\s\S]+)<p class="MsoListParagraphCxSpLast"\s+L_\d>.*?\n(.*?)\/p>/gm, function(m,w){
146                        var n = m;
147                        n = n.replace(/&nbsp;/gm,"");
148                         var ar = n.split(/\n/);
149                        var str = "";
150                        var list_type = "ul";
151                          for(j=0;j<ar.length;j++) {
152                                  if(ar[j].match(/SpFirst/)) {
153                                      if(ar[j]. match(/L_\d+>[a-b0-9]/)) {
154                                          list_type = 'ol';
155                                      }
156                                  //    alert(list_type);
157                                     ar[j] = ar[j].replace(/<p.*?>/,"");
158                                      ar[j] = ar[j].replace(/<\/p>/,"");
159
160                                           ar[j] = ar[j].replace(/<p.*?>(\d*|·)/,"");
161
162                                           str += "<" + list_type +">";
163                                  }
164                                  else if (ar[j].match(/SpMiddle/)) {
165                                      ar[j] = ar[j].replace(/<p.*?>/,"");
166                                     ar[j] = ar[j].replace(/<p.*?>/,"");
167                                      ar[j] = ar[j].replace(/<\/p>/,"");
168                                  }
169                                  else if (ar[j].match(/SpLast/)) {
170                                       ar[j] = ar[j].replace(/<p.*?>/,"");
171                                      ar[j] = ar[j].replace(/<\/p>/,"");
172                                       ar[j] = ar[j].replace(/^\s+/,"");
173                                  }
174                                  else {
175                                         ar[j] = ar[j].replace(/<\/p>/,"");
176                                         ar[j] = ar[j].replace(/^\s+/,"");
177                                          ar[j] = ar[j].replace(/\s+$/,"");
178                                           str+='<li>';
179                                           str+=ar[j] + '</li>';
180                                  }
181                          }
182                         str = str.replace(/<li>·*<\/li>/gm,"");
183                         str+="</" + list_type +">";
184                         return str;
185        } );
186          if(inner) {
187              editor.insertHtml(inner);
188          }
189           data_id.innerHTML  ="";
190		}
191	};
192});
193