forked from wwalc/insertpre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
131 lines (123 loc) · 3.77 KB
/
plugin.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
/*
Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
CKEDITOR.plugins.add( 'insertpre',
{
requires: 'dialog',
lang : 'en,pl', // %REMOVE_LINE_CORE%
icons: 'insertpre', // %REMOVE_LINE_CORE%
onLoad : function()
{
if ( CKEDITOR.config.insertpre_class )
{
CKEDITOR.addCss(
'pre.' + CKEDITOR.config.insertpre_class + ' {' +
CKEDITOR.config.insertpre_style +
'}'
);
}
},
init : function( editor )
{
// allowed and required content is the same for this plugin
var required = CKEDITOR.config.insertpre_class ? ( 'pre( ' + CKEDITOR.config.insertpre_class + ' )' ) : 'pre';
editor.addCommand( 'insertpre', new CKEDITOR.dialogCommand( 'insertpre', {
allowedContent : required,
requiredContent : required
} ) );
editor.ui.addButton && editor.ui.addButton( 'InsertPre',
{
label : editor.lang.insertpre.title,
icon : this.path + 'icons/insertpre.png',
command : 'insertpre',
toolbar: 'insert,99'
} );
if ( editor.contextMenu )
{
editor.addMenuGroup( 'code' );
editor.addMenuItem( 'insertpre',
{
label : editor.lang.insertpre.edit,
icon : this.path + 'icons/insertpre.png',
command : 'insertpre',
group : 'code'
});
editor.contextMenu.addListener( function( element )
{
if ( element )
element = element.getAscendant( 'pre', true );
if ( element && !element.isReadOnly() && element.hasClass( editor.config.insertpre_class ) )
return { insertpre : CKEDITOR.TRISTATE_OFF };
return null;
});
}
CKEDITOR.dialog.add( 'insertpre', function( editor )
{
return {
title : editor.lang.insertpre.title,
minWidth : 540,
minHeight : 380,
contents : [
{
id : 'general',
label : editor.lang.insertpre.code,
elements : [
{
type : 'textarea',
id : 'contents',
label : editor.lang.insertpre.code,
cols: 140,
rows: 22,
validate : CKEDITOR.dialog.validate.notEmpty( editor.lang.insertpre.notEmpty ),
required : true,
setup : function( element )
{
var html = element.getHtml();
if ( html )
{
var div = document.createElement( 'div' );
div.innerHTML = html;
this.setValue( div.firstChild.nodeValue );
}
},
commit : function( element )
{
element.setHtml( CKEDITOR.tools.htmlEncode( this.getValue() ) );
}
}
]
}
],
onShow : function()
{
var sel = editor.getSelection(),
element = sel.getStartElement();
if ( element )
element = element.getAscendant( 'pre', true );
if ( !element || element.getName() != 'pre' || !element.hasClass( editor.config.insertpre_class ) )
{
element = editor.document.createElement( 'pre' );
this.insertMode = true;
}
else
this.insertMode = false;
this.pre = element;
this.setupContent( this.pre );
},
onOk : function()
{
if ( editor.config.insertpre_class )
this.pre.setAttribute( 'class', editor.config.insertpre_class );
if ( this.insertMode )
editor.insertElement( this.pre );
this.commitContent( this.pre );
}
};
} );
}
} );
if (typeof(CKEDITOR.config.insertpre_style) == 'undefined')
CKEDITOR.config.insertpre_style = 'background-color:#F8F8F8;border:1px solid #DDD;padding:10px;';
if (typeof(CKEDITOR.config.insertpre_class) == 'undefined')
CKEDITOR.config.insertpre_class = 'prettyprint';