This repository was archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.js
executable file
·280 lines (264 loc) · 11 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See http://opensource.org/licenses/MIT for more information.
* This information must remain intact.
*/
(function(global, $){
var codiad = global.codiad,
scripts = document.getElementsByTagName('script'),
path = scripts[scripts.length-1].src.split('?')[0],
curpath = path.split('/').slice(0, -1).join('/')+'/',
self;
$(function() {
codiad.MarkdownPreview.init();
});
codiad.MarkdownPreview = {
path : curpath,
callback: this.showResult,
default : "",
defObj : {},
file : "",
init: function() {
var _this = this;
self = this;
//Context callbacks
amplify.subscribe("context-menu.onShow", function(obj){
var ext = _this.getExtension(obj.path);
if (ext == "md" || ext == "markdown") {
$('#context-menu').append('<hr class="file-only markdown">');
if (codiad.project.isAbsPath($('#file-manager a[data-type="root"]').attr('data-path'))) {
$('#context-menu').append('<a class="file-only markdown" onclick="codiad.MarkdownPreview.showPreview($(\'#context-menu\').attr(\'data-path\'), true);"><span class="icon-eye"></span>Preview</a>');
}
$('#context-menu').append('<a class="file-only markdown" onclick="codiad.MarkdownPreview.generate($(\'#context-menu\').attr(\'data-path\'));"><span class="icon-code"></span>Generate html</a>');
}
});
amplify.subscribe("context-menu.onHide", function(){
$('.markdown').remove();
});
//Register preview callbacks
amplify.subscribe("helper.onPreview", function(path){
var ext = _this.getExtension(path);
if (ext == "md" || ext == "markdown") {
_this.showPreview(path, false);
return false;
}
});
amplify.subscribe('helper.onStartLivePreview', function(path){
var ext = _this.getExtension(path);
if (ext == "md" || ext == "markdown") {
_this.livePreview();
//Weired workaround
setTimeout(function(){
_this.livePreview();
},100);
return false;
}
});
amplify.subscribe('helper.onChangeLivePreview', function(event){
var path = codiad.active.getPath();
var ext = _this.getExtension(path);
if (ext == "md" || ext == "markdown") {
_this.livePreview();
return false;
}
});
amplify.subscribe('helper.onLivePreviewInit', function(){
codiad.LivePreviewHelper.registerExtension(["md", "markdown"]);
});
//Load helper and libs
if (typeof(codiad.PreviewHelper) == 'undefined') {
$.getScript(this.path+"previewHelper.js");
}
if (typeof(codiad.LivePreviewHelper) == 'undefined') {
$.getScript(this.path+"LivePreviewHelper.js", function(){
codiad.LivePreviewHelper.init(_this.path);
});
}
$.getScript(this.path+"markdown.js");
//Load template
$.get(this.path+'livePreviewTemplate.html', function(template){
_this.livePreviewTemplate = template;
});
},
//////////////////////////////////////////////////////////
//
// Show dialog to choose paser method
//
// Parameter:
//
// path - {String} - File path
// isAbsolutePath - {bool} - Has project an absolute path
//
//////////////////////////////////////////////////////////
showPreview: function(path, isAbsolutePath) {
var ext = this.getExtension(path);
if (ext == "md" || ext == "markdown") {
this.file = path;
if (this.default !== "") {
this.parse(this.default, this.showResult, false);
} else {
this.callback = this.showResult;
codiad.modal.load(400, this.path+"dialog.php?chooseMethod&absolutePath=" + isAbsolutePath.toString());
}
} else {
codiad.filemanager.openInBrowser(path);
}
},
//////////////////////////////////////////////////////////
//
// Parse markdown file
//
// Parameter:
//
// method - {String} - Method to parse file
// callback - {Function} - Callback function
// checkDefault - (Optional) - {Boolean} - Check if it should be sets adefault
//
//////////////////////////////////////////////////////////
parse: function(method, callback, checkDefault) {
var _this = this;
if (typeof(callback) != 'function') {
callback = this.callback;
}
if (typeof(checkDefault) == 'undefined') {
if ($('#setDefault').attr("checked") == "checked") {
this.default = method;
}
}
$.get(this.path+"controller.php?action=getContent&path="+this.file, function(content){
if (method == "github") {
if (checkDefault !== false || typeof(checkDefault) == 'undefined') {
if ($('#gfm').attr("checked") == "checked") {
_this.defObj = {
"text": content,
"mode": "gfm",
"context": $('#gfm_context').val()
};
} else {
_this.defObj = {
"text": content
};
}
} else {
_this.defObj.text = content;
}
$.post("https://api.github.com/markdown", JSON.stringify(_this.defObj),function(text){
try {
var result = $.parseJSON(text);
codiad.message.warning(result.message);
_this.parse("js", callback);
} catch (e) {
callback(text);
}
}).fail(function(){
codiad.message.error("Github is unreachable!");
_this.parse("js", callback);
});
} else if (method == "js") {
var text = markdown.toHTML(content, 'Maruku');
callback(text);
} else {
codiad.filemanager.openInBrowser(_this.file);
}
codiad.modal.unload();
});
},
//////////////////////////////////////////////////////////
//
// Show result in new window
//
// Parameter:
//
// content - {String} - Parsed content of the file
//
//////////////////////////////////////////////////////////
showResult: function(content) {
$.post(self.path+"controller.php?action=savePreview&file="+self.file, {"content": content}, function(){
window.open(self.path+"preview.html",'_newtab');
});
},
//////////////////////////////////////////////////////////
//
// Generate html of markdown
//
// Parameter:
//
// path - {String} - Path of file
//
//////////////////////////////////////////////////////////
generate: function(path) {
var ext = this.getExtension(path);
if (ext == "md" || ext == "markdown") {
this.file = path;
if (this.default !== "") {
this.parse(this.default, this.saveResult, false);
} else {
this.callback = this.saveResult;
var isAbsolutePath = codiad.project.isAbsPath($('#file-manager a[data-type="root"]').attr('data-path'));
codiad.modal.load(400, this.path+"dialog.php?chooseMethod&absolutePath =" + isAbsolutePath.toString());
}
} else {
codiad.filemanager.openInBrowser(path);
}
},
//////////////////////////////////////////////////////////
//
// Save parsed result
//
// Parameter:
//
// content - {String} - Parsed content of the file
//
//////////////////////////////////////////////////////////
saveResult: function(content) {
$.post(self.path + "controller.php?action=saveContent&path=" + self.file, {"content": content}, function(result){
result = JSON.parse(result);
codiad.message[result.status](result.message);
if (result.status == "success") {
codiad.filemanager.rescan(self.getDirname(self.file));
}
});
},
//////////////////////////////////////////////////////////
//
// Handle live preview
//
//////////////////////////////////////////////////////////
livePreview: function() {
var content = codiad.editor.getContent();
content = markdown.toHTML(content, 'Maruku');
content = this.livePreviewTemplate
.replace('__content_', content)
.replace('__title__', codiad.active.getPath())
.replace('__PATH__', this.path);
codiad.LivePreviewHelper.updateContent(content);
},
//////////////////////////////////////////////////////////
//
// Get extension of file
//
// Parameter:
//
// path - {String} - File path
//
//////////////////////////////////////////////////////////
getExtension: function(path) {
return path.substring(path.lastIndexOf(".")+1);
},
//////////////////////////////////////////////////////////
//
// Get dirname of file
//
// from php.js <phpjs.org>, licensed under the MIT licenses.
//
// Parameter:
//
// path - {String} - File path
//
//////////////////////////////////////////////////////////
getDirname: function(path) {
return path.replace(/\\/g, '/').replace(/\/[^\/]*\/?$/, '');
}
};
})(this, jQuery);