plugin.js
8.41 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
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
/*
* The "markdown" plugin. It's indented to enhance the
* "sourcearea" editing mode, convert to markdown mode and highlight syntax.
* Licensed under the MIT license
* CodeMirror Plugin: http://codemirror.net/ (MIT License)
* Markdown Parser:
* HTML to Markdown Parser:
*/
'use strict';
(function() {
CKEDITOR.plugins.add('markdown', {
// lang: 'en,zh', // %REMOVE_LINE_CORE%
icons: 'markdown',
hidpi: true, // %REMOVE_LINE_CORE%
init: function(editor) {
// Source mode in inline editors is only available through the "sourcedialog" plugin.
if (editor.elementMode == CKEDITOR.ELEMENT_MODE_INLINE)
return;
var markdown = CKEDITOR.plugins.markdown,
rootPath = this.path,
defaultConfig = {
mode: 'gfm',
lineNumbers: true,
theme: 'default'
};
editor.config.markdown = CKEDITOR.tools.extend(defaultConfig, editor.config.markdown || {}, true);
editor.addMode('markdown', function(callback) {
var contentsSpace = editor.ui.space('contents'),
textarea = contentsSpace.getDocument().createElement('textarea'),
config = editor.config.markdown;
textarea.setStyles(
CKEDITOR.tools.extend({
// IE7 has overflow the <textarea> from wrapping table cell.
width: CKEDITOR.env.ie7Compat ? '99%' : '100%',
height: '100%',
resize: 'none',
outline: 'none',
'text-align': 'left'
},
CKEDITOR.tools.cssVendorPrefix('tab-size', editor.config.sourceAreaTabSize || 4)));
// Make sure that source code is always displayed LTR,
// regardless of editor language (#10105).
textarea.setAttribute('dir', 'ltr');
//anruo issues:Uncaught InvalidCharacterError: Failed to execute 'add' on 'DOMTokenList': The token provided ('cke_source cke_reset cke_enable_context_menu') contains HTML space characters, which are not valid in tokens.
//textarea.addClass('cke_source cke_reset cke_enable_context_menu');
textarea.addClass('cke_source cke_reset cke_enable_context_menu'.split(' '));
editor.ui.space('contents').append(textarea);
var editable = editor.editable(new sourceEditable(editor, textarea)),
htmlData = editor.getData(1);
// Convert to Markdown and Fill the textarea.
if (typeof(toMarkdown) == 'undefined') {
CKEDITOR.scriptLoader.load(rootPath + 'js/to-markdown.js', function() {
editable.setData(toMarkdown(htmlData));
});
} else {
editable.setData(toMarkdown(htmlData));
}
if (typeof (CodeMirror) == 'undefined' || typeof (CodeMirror.modes.gfm) == 'undefined') {
CKEDITOR.document.appendStyleSheet(rootPath + 'css/codemirror.min.css');
if (config.theme.length && config.theme != 'default') {
CKEDITOR.document.appendStyleSheet(rootPath + 'theme/' + config.theme + '.css');
}
CKEDITOR.scriptLoader.load(rootPath + 'js/codemirror-gfm-min.js', function() {
loadCodeMirror(editor, editable);
});
} else {
loadCodeMirror(editor, editable);
}
if (typeof(marked) == 'undefined') {
CKEDITOR.scriptLoader.load(rootPath + 'js/marked.js');
}
// Having to make <textarea> fixed sized to conquer the following bugs:
// 1. The textarea height/width='100%' doesn't constraint to the 'td' in IE6/7.
// 2. Unexpected vertical-scrolling behavior happens whenever focus is moving out of editor
// if text content within it has overflowed. (#4762)
if (CKEDITOR.env.ie) {
editable.attachListener(editor, 'resize', onResize, editable);
editable.attachListener(CKEDITOR.document.getWindow(), 'resize', onResize, editable);
CKEDITOR.tools.setTimeout(onResize, 0, editable);
}
editor.fire('ariaWidget', this);
editor.commands.maximize.modes.markdown = 1;
callback();
});
function loadCodeMirror(editor, editable){
window["codemirror_" + editor.id] = CodeMirror.fromTextArea(editable.$, editor.config.markdown);
window["codemirror_" + editor.id].setSize(null, '100%');
}
editor.addCommand('markdown', markdown.commands.markdown);
if (editor.ui.addButton) {
editor.ui.addButton('Markdown', {
label: 'Markdown',
command: 'markdown'
// toolbar: 'mode,10'
});
}
//anruo : (CKEDITOR.document.appendStyleText('.cke_button__markdown_label {display: inline;}'); // display button Label)
//CKEDITOR.document.appendStyleText('.cke_button__markdown_label {display: inline;}'); // display button Label
editor.on('mode', function() {
editor.getCommand('markdown').setState(editor.mode == 'markdown' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF);
});
var needsFocusHack = CKEDITOR.env.ie && CKEDITOR.env.version == 9;
function onResize() {
// We have to do something with focus on IE9, because if sourcearea had focus
// before being resized, the caret ends somewhere in the editor UI (#11839).
var wasActive = needsFocusHack && this.equals(CKEDITOR.document.getActive());
// Holder rectange size is stretched by textarea,
// so hide it just for a moment.
this.hide();
this.setStyle('height', this.getParent().$.clientHeight + 'px');
this.setStyle('width', this.getParent().$.clientWidth + 'px');
// When we have proper holder size, show textarea again.
this.show();
if (wasActive)
this.focus();
}
}
});
var sourceEditable = CKEDITOR.tools.createClass({
base: CKEDITOR.editable,
proto: {
setData: function(data) {
this.setValue(data);
this.status = 'ready';
this.editor.fire('dataReady');
},
getData: function() {
return this.getValue();
},
// Insertions are not supported in source editable.
insertHtml: function() {},
insertElement: function() {},
insertText: function() {},
// Read-only support for textarea.
setReadOnly: function(isReadOnly) {
this[(isReadOnly ? 'set' : 'remove') + 'Attribute']('readOnly', 'readonly');
},
detach: function() {
var editor = this.editor;
window["codemirror_" + editor.id].toTextArea(); // Remove codemirror and restore Data to origin textarea
window["codemirror_" + editor.id] = null; // Free Memory on destroy
var markdownSource = editor.getData();
editor.setData(marked(markdownSource, {langPrefix: 'language-'}));
sourceEditable.baseProto.detach.call(this);
this.clearCustomData();
this.remove();
}
}
});
})();
CKEDITOR.plugins.markdown = {
commands: {
markdown: {
modes: {
wysiwyg: 1,
markdown: 1
},
editorFocus: false,
readOnly: 1,
exec: function(editor) {
if (editor.mode == 'wysiwyg')
editor.fire('saveSnapshot');
editor.getCommand('markdown').setState(CKEDITOR.TRISTATE_DISABLED);
editor.setMode(editor.mode == 'markdown' ? 'wysiwyg' : 'markdown');
},
canUndo: false
}
}
};