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