-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
199 lines (180 loc) · 6.82 KB
/
index.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
$(function() {
var curpost;
var postList = [];
var postsDialog;
var syncPosts = function() {
// sync the posts
Post.sync(postList, function(posts) {
postList = posts;
Storage.savePosts(postList);
if (postsDialog)
postsDialog.setPostEntities(postList);
});
};
var initEditor = function() {
editor = editormd("editormd", {
path : "editor.md/lib/", // Autoload modules mode, codemirror, marked... dependents libs path
width : "100%",
height : 100,
emoji : true,
tex : true,
lang : langs[langName],
toolbarIcons : function() {
// Or return editormd.toolbarModes[name]; // full, simple, mini
// Using "||" set icons align right.
return [
"undo", "redo", "|",
"bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|",
"h1", "h2", "h3", "h4", "|",
"list-ul", "list-ol", "hr", "|",
"link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", "emoji", "html-entities", "pagebreak", "|",
"search", "|",
"||",
"login", "preview", "new", "postentities", "meta", "commit", "help", "info"];
},
onload : function() {
//Fix Bug: editor not extend when empty
editor.setMarkdown(" ");
//force fullscreen (just the content)
this.fullscreen();
//Load local posts
Storage.restorePost(Post.new(), function(post) {
curpost = post;
editor.setMarkdown(post.content);
});
Storage.restorePosts(postList, function(posts) {
postList = posts;
postList.unshift({ loading: true});
if (postsDialog)
postsDialog.setPostEntities(postList);
});
Github.onUserFetched(function(error, user_info) {
if (!error) {
//this.toolbar.find(".fa[name=login]").html(user_info.login);
this.settings.toolbarIconTexts.login = user_info.login;
this.settings.lang.toolbar.login = user_info.login;
this.setToolbar();
syncPosts();
} else {
this.settings.toolbarIconTexts.login = this.lang.toolbar.login;
this.settings.lang.toolbar.login = this.lang.toolbar.login;
this.setToolbar();
}
}.bind(this));
//login at startup
Github.autoLogin();
},
onfullscreenExit : function() {
//force full screen
this.fullscreen();
},
onchange : function() {
curpost.content = editor.getMarkdown();
Storage.savePost(curpost);
var images = this.previewContainer.find("img");
images = Array.prototype.slice.call(images);
images.forEach(function(img) {
//filter the converted url
if (img.src.indexOf("blob:") === 0 || img.src.indexOf("chrome-extension:") === 0) {
return;
}
Resource.load(img.src, function(url) {
img.src = url;
});
});
},
toolbarIconsClass : {
new : "fa-file",
postentities : "fa-bars",
meta : "fa-comment",
commit: "fa-pencil-square-o"
},
toolbarIconTexts : {
//customer icon and text
login : "<i class=\"fa fa-spinner fa-spin\"></i>"
},
toolbarHandlers : {
new : function(cm, icon, cursor, selection) {
this.confirmDialog(this.lang.dialog.warn.title, this.lang.dialog.warn.content, function(ok) {
if (ok) {
curpost = Post.new();
editor.setMarkdown(curpost.content);
}
});
},
login : function(cm, icon, cursor, selection) {
Github.login();
this.settings.toolbarIconTexts.login = "<i class=\"fa fa-spinner fa-spin\"></i>";
this.settings.lang.toolbar.login = this.lang.toolbar.login + "...";
this.setToolbar();
},
commit : function(cm, icon, cursor, selecton) {
this.confirmDialog(this.lang.dialog.commit.title, this.lang.dialog.commit.content, function(ok) {
if (ok) {
var dialog = this.confirmDialog(this.lang.dialog.commit.title, this.lang.dialog.commit.title + '...');
dialog.loading(true);
Post.update(curpost, function(err, msg) {
if (err) {
dialog.setTitle(this.lang.dialog.error.title);
dialog.setContent(this.lang.dialog.error.content[msg]);
}
else {
dialog.setTitle(this.lang.dialog.succ.title);
dialog.setContent(this.lang.dialog.succ.content[msg]);
Storage.savePost(curpost);
//update post list
if (msg == "created") {
postList.unshift($.extend({}, curpost));
}
else if (msg == "updated") {
for (var i = 0; i < postList.length; i++) {
if (postList[i].sha == curpost.sha) {
postList[i] = $.extend({}, curpost);
}
}
}
if (postsDialog)
postsDialog.setPostEntities(postList);
}
dialog.loading(false);
}.bind(this));
}
}.bind(this));
},
postentities : function(cm, icon, cursor, selection) {
postsDialog = editor.postEntitiesDialog([], true, function(ok, selected) {
if (ok) {
editor.confirmDialog(editor.lang.dialog.warn.title, editor.lang.dialog.warn.content, function(ok) {
if (ok) {
curpost = $.extend({}, postList[selected]);
editor.setMarkdown(curpost.content);
}
});
}
});
postsDialog.setPostEntities(postList);
//dialog.loading(true);
},
meta : function(cm, icon, cursor, selection) {
this.metaDataDialog(curpost.meta, function(ok, meta) {
if (ok) {
curpost.meta = meta;
Storage.savePost(curpost);
}
});
}
}
});
$(window).resize(function() {
console.log("window resize");
editor.fullscreenExit();
});
};
if (langName == 'en') {
editormd.loadScript("editor.md/languages/en", initEditor);
} else if (langName == 'zh_TW') {
editormd.loadScript("editor.md/languages/zh-tw", initEditor);
} else {
initEditor();
}
});