quickformat.js
1.9 KB
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
/*******************************************************************************
* KindEditor - WYSIWYG HTML Editor for Internet
* Copyright (C) 2006-2011 kindsoft.net
*
* @author Roddy <luolonghao@gmail.com>
* @site http://www.kindsoft.net/
* @licence http://www.kindsoft.net/license.php
*******************************************************************************/
KindEditor.plugin('quickformat', function(K) {
var self = this, name = 'quickformat',
blockMap = K.toMap('blockquote,center,div,h1,h2,h3,h4,h5,h6,p');
function getFirstChild(knode) {
var child = knode.first();
while (child && child.first()) {
child = child.first();
}
return child;
}
self.clickToolbar(name, function() {
self.focus();
var doc = self.edit.doc,
range = self.cmd.range,
child = K(doc.body).first(), next,
nodeList = [], subList = [],
bookmark = range.createBookmark(true);
while(child) {
next = child.next();
var firstChild = getFirstChild(child);
if (!firstChild || firstChild.name != 'img') {
if (blockMap[child.name]) {
child.html(child.html().replace(/^(\s| | )+/ig, ''));
child.css('text-indent', '2em');
} else {
subList.push(child);
}
if (!next || (blockMap[next.name] || blockMap[child.name] && !blockMap[next.name])) {
if (subList.length > 0) {
nodeList.push(subList);
}
subList = [];
}
}
child = next;
}
K.each(nodeList, function(i, subList) {
var wrapper = K('<p style="text-indent:2em;"></p>', doc);
subList[0].before(wrapper);
K.each(subList, function(i, knode) {
wrapper.append(knode);
});
});
range.moveToBookmark(bookmark);
self.addBookmark();
});
});
/**
--------------------------
abcd<br />
1234<br />
to
<p style="text-indent:2em;">
abcd<br />
1234<br />
</p>
--------------------------
abcd<img>1233
<p>1234</p>
to
<p style="text-indent:2em;">abcd<img>1233</p>
<p style="text-indent:2em;">1234</p>
--------------------------
*/