forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magike_editor.js
325 lines (285 loc) · 8.8 KB
/
magike_editor.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/********************************************
Magike Project
copyright (c) Magike Group
This software must be only used in Magike Systeam.
http://www.magike.net
modify by Hanny: http://www.imhan.com
********************************************/
var start = 0;
var end = 0;
var hasPrepare = false;
var magikeTextareaScrolltop = 0;
var range = null;
var magikeTextarea;
var editorDraftChange = false;
var magikeToolbar;
var magikeEditor;
var insertImageToEditor;
var insertLinkToEditor;
function addButtonCtx(button_ctx)
{
$(button_ctx).appendTo(magikeToolbar);
}
function addButton(label ,left_tag, right_tag, accesskey)
{
addButtonCtx("<button type=\"button\" accesskey=\"" + accesskey + "\" onclick=\"editorAdd('" + left_tag + "','" + right_tag + "');\">" + label + "</button>");
}
//创建工具栏区
function createToolbar()
{
$("<p class=\"toolbarspace\"></p>").prependTo(magikeTextarea.parentNode);
$("<div id=\"tttt\" class=\"toolbar\"></div>").prependTo(magikeTextarea.parentNode);
magikeToolbar = magikeTextarea.parentNode.firstChild;
}
function createDefaultButtons()
{
addButton('<strong>B</strong>', '<strong>', '</strong>', 'b');
addButton('<em>I</em>', '<em>', '</em>', 'i');
addButton('<del>del</del>', '<del>', '</del>', 'd');
addButton('quote', '<blockquote>', '</blockquote>', 'q');
addButton('code', '<code>', '</code>', 'c');
addButtonCtx("<button type=\"button\" accesskey=\"a\" style=\"color:#0000EE;text-decoration: underline;\" onclick=\"editorInsertLink('插入一个链接','资源地址','标题','目标','插入','取消','请输入一个链接URL。');\">link</button>");
addButtonCtx("<button type=\"button\" accesskey=\"t\" onclick=\"editorInsertImage('插入一张图片','图片地址','标题','对齐','插入','取消','请输入一个图片URL');\">img</button>");
addButton('more', '<!--more-->', '', 'm');
}
/*
insmode 说明
BIT:
0:是否安装附件管理器
0: 未安装附件管理器
1: 已安装附件管理器
2~1:图片插入的方式
0: 默认的图片插入方式
1: 仅插入图片
2: 插入图片+链接
3: 保留(默认的插入方式)
*/
function initEditorInterface(elid, insmode)
{
/*
//已经安装附件管理器插件
Typecho.insertFileToEditor = function (title, url, is_img) {
//get_attach_id(url);
var lst = $("#file-list").children("li");
var index = 0;
while (index < lst.length) {
if (lst[index].dataset['url'] == url) {
break;
}
index++;
}
if (index >= 8) {
return;
}
//Typecho.insertFileToEditor(t.text(), p.data('url'), p.data('image'));
var textarea = $('#text'), sel = textarea.getSelection(),
html = '<attach>' + lst[index].dataset['cid'] + '</attach>';
offset = (sel ? sel.start : 0) + html.length;
textarea.replaceSelection(html);
textarea.setSelection(offset, offset);
};
*/
Typecho.insertFileToEditor = function (title, url, is_img) {
var textarea = $('#text'), sel = textarea.getSelection();
var html = '';
if (is_img) {
//插入图片
picture_mode = (insmode >> 1) & 0x3;
if (picture_mode == 1) {
//仅插入图片
html = '<img src="' + url + '" alt="' + title + '" />';
} else if (picture_mode == 2) {
//插入图片链接
html = '<a href="' + url + '" title="点击查看原图" target="_blank"><img src="' + url + '" alt="' + title + '" /></a>';
}
} else {
attach_mode = insmode & 0x1;
if (attach_mode) {
//已经安装附件管理器插件
var lst = $("#file-list").children("li");
var index = 0;
while (index < lst.length) {
if (lst[index].children[1].text == title) {
break;
}
index++;
}
if (index >= 8) {
return;
}
html = '<attach>' + lst[index].children[0].value + '</attach>';
} else {
html = '<a href="' + url + '">' + title + '</a>';
}
}
var offset = (sel ? sel.start : 0) + html.length;
textarea.replaceSelection(html);
textarea.setSelection(offset, offset);
};
}
function initEditor(elid, insmode)
{
magikeTextarea = document.getElementById(elid);
initEditorInterface('#'+elid, insmode);
createToolbar();
createDefaultButtons();
}
function editPrepare()
{
magikeTextareaScrolltop = magikeTextarea.scrollTop;
if(typeof(magikeTextarea.selectionStart) == "number")
{
magikeTextarea.focus();
start = magikeTextarea.selectionStart;
end = magikeTextarea.selectionEnd;
}
else if(document.selection)
{
magikeTextarea.focus();
range = document.selection.createRange();
}
hasPrepare = true;
}
function editorAdd(flg1,flg2)
{
if(!hasPrepare)
{
editPrepare();
}
editorDraftChange = true;
if(typeof(magikeTextarea.selectionStart) == "number")
{
pre = magikeTextarea.value.substr(0, start);
post = magikeTextarea.value.substr(end);
center = magikeTextarea.value.substr(start,end-start);
magikeTextarea.value = pre + flg1 +center+ flg2+ post;
magikeTextarea.setSelectionRange(start+flg1.length,start+flg1.length);
}
else if(document.selection)
{
if(range.text.length > 0)
{
range.text = flg1 + range.text + flg2;
}
else
{
range.text = flg1 + flg2;
}
}
setTimeout('magikeTextarea.scrollTop = magikeTextareaScrolltop',0);
magikeTextarea.focus();
hasPrepare = false;
return true;
}
var editorInsertLinkError;
function editorInsertLink(popupTitle,urlWord,titleWord,openType,okText,cancelText,errorWord)
{
editPrepare();
div = $(document.createElement("div"));
editorInsertLinkError = errorWord;
p = $(document.createElement("p"));
span = $(document.createElement("span"));
span.text(urlWord);
input = $(document.createElement("input"));
input.addClass("text");
input.attr("type","text");
input.attr("name","url");
input.attr("value","http://");
p.append(span);
p.append(input);
div.append(p);
p = $(document.createElement("p"));
span = $(document.createElement("span"));
span.text(titleWord);
input = $(document.createElement("input"));
input.addClass("text");
input.attr("type","text");
input.attr("name","title");
p.append(span);
p.append(input);
div.append(p);
p = $(document.createElement("p"));
span = $(document.createElement("span"));
span.text(openType);
select = magikeCreateSelect({none:"",_blank:"_blank"});
select.attr("name","link");
p.append(span);
p.append(select);
div.append(p);
magikeUI.createPopup({title: popupTitle,center: true,width: 400,height: 175,text:div,ok:okText,cancel:cancelText,handle:editorInsertLinkHandle});
}
function editorInsertLinkHandle()
{
var url = $("input[name=url]",$((this.parentNode).parentNode)).val();
var ititle = $("input[name=title]",$((this.parentNode).parentNode)).val();
var link = $("select[name=link]",$((this.parentNode).parentNode)).val();
if(url && url != "http://")
{
editorAdd('<a href="' + url + '"' + (ititle ? ' title="' + ititle + '"' : '') + (link ? ' target="' + link + '"' : '') + '>','</a>');
$(((this.parentNode).parentNode).parentNode).remove();
}
else
{
alert(editorInsertLinkError);
}
}
var editorInsertImageError;
function editorInsertImage(popupTitle,urlWord,titleWord,alignType,okText,cancelText,errorWord)
{
editPrepare();
div = $(document.createElement("div"));
editorInsertImageError = errorWord;
p = $(document.createElement("p"));
span = $(document.createElement("span"));
span.text(urlWord);
input = $(document.createElement("input"));
input.addClass("text");
input.attr("type","text");
input.attr("name","img_url");
input.attr("value","http://");
p.append(span);
p.append(input);
div.append(p);
p = $(document.createElement("p"));
span = $(document.createElement("span"));
span.text(titleWord);
input = $(document.createElement("input"));
input.addClass("text");
input.attr("type","text");
input.attr("name","img_title");
p.append(span);
p.append(input);
div.append(p);
p = $(document.createElement("p"));
span = $(document.createElement("span"));
span.text(alignType);
select = magikeCreateSelect({无:"",左:"left",中:"center",右:"right"});
select.attr("name","img_align");
p.append(span);
p.append(select);
div.append(p);
magikeUI.createPopup({title: popupTitle,center: true,width: 400,height: 175,text:div,ok:okText,cancel:cancelText,handle:editorInsertImageHandle});
}
var editorInsertImageIsImage = true;
function editorInsertImageHandle()
{
var url = $("input[name=img_url]",$((this.parentNode).parentNode)).val();
var ititle = $("input[name=img_title]",$((this.parentNode).parentNode)).val();
var align = $("select[name=img_align]",$((this.parentNode).parentNode)).val();
if(url && url != "http://")
{
if(editorInsertImageIsImage)
{
editorAdd('<img src="' + url + '"' + (ititle ? ' alt="' + ititle + '"' : '') + (align ? ' align="' + align + '"' : '') + '/>','');
}
else
{
editorAdd('<a href="' + url + '"' + (ititle ? ' title="' + ititle + '"' : '') + '/>','</a>');
}
$(((this.parentNode).parentNode).parentNode).remove();
}
else
{
alert(editorInsertImageError);
}
}