plugin.js
3.8 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
CKEDITOR.plugins.add( 'textindent', {
icons: 'textindent',
// availableLangs: {'pt-br':1, 'en':1, 'zh-cn': 1},
// lang: 'pt-br, en, zh-cn',
init: function( editor ) {
var indentation = editor.config.indentation;
var indentationKey = editor.config.indentationKey;
if(typeof(indentation) == 'undefined')
// indentation = '50px';
indentation = '2em'; // chenjm: 首行缩进为2个字符
if(typeof(indentationKey) == 'undefined')
indentationKey = 'tab';
if(editor.ui.addButton){
editor.ui.addButton( 'textindent', {
// label: editor.lang.textindent.labelName,
label: editor.config.lanOfCk.textindent.labelName, // chenjm: 多语言处理
command: 'ident-paragraph',
toolbar: 'textindent,30'
});
}
if( indentationKey !== false){
editor.on('key', function(ev) {
if(ev.data.domEvent.$.key.toLowerCase() === indentationKey.toLowerCase().trim() || ev.data.keyCode === indentationKey){
editor.execCommand('ident-paragraph');
ev.cancel();
}
});
}
editor.on( 'selectionChange', function()
{
var style_textindente = new CKEDITOR.style({
element: 'p',
styles: { 'text-indent': indentation },
overrides: [{
element: 'text-indent', attributes: { 'size': '0'}
}]
});
if( style_textindente.checkActive(editor.elementPath(), editor) )
editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_ON);
else
editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_OFF);
});
editor.addCommand("ident-paragraph", {
allowedContent: 'p{text-indent}',
requiredContent: 'p',
exec: function(evt) {
var range = editor.getSelection().getRanges()[0];
var walker = new CKEDITOR.dom.walker( range ),
node;
var state = editor.getCommand('ident-paragraph').state;
while ( ( node = walker.next() ) ) {
if ( node.type == CKEDITOR.NODE_ELEMENT ) {
if(node.getName() === "p"){
editor.fire('saveSnapshot');
if( state == CKEDITOR.TRISTATE_ON){
node.removeStyle("text-indent");
editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_OFF);
}
else{
node.setStyle( "text-indent", indentation );
editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_ON);
}
}
}
}
if(node === null){
node = editor.getSelection().getStartElement().getAscendant('p', true);
editor.fire('saveSnapshot');
if( state == CKEDITOR.TRISTATE_ON){
node.removeStyle("text-indent");
editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_OFF);
}
else{
node.setStyle( "text-indent", indentation );
editor.getCommand('ident-paragraph').setState(CKEDITOR.TRISTATE_ON);
}
}
}
});
}
});