Plugin.js
2.51 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
/**
* Plugin.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* Main plugin logic.
*
* @class tinymce.codesample.Plugin
* @private
*/
define("tinymce/codesampleplugin/Plugin", [
"tinymce/Env",
"tinymce/PluginManager",
"tinymce/codesampleplugin/Prism",
"tinymce/codesampleplugin/Dialog",
"tinymce/codesampleplugin/Utils"
], function(Env, PluginManager, Prism, Dialog, Utils) {
var addedInlineCss, trimArg = Utils.trimArg;
PluginManager.add('codesample', function(editor, pluginUrl) {
var $ = editor.$, addedCss;
if (!Env.ceFalse) {
return;
}
// Todo: use a proper css loader here
function loadCss() {
var linkElm;
if (editor.inline && addedInlineCss) {
return;
}
if (!editor.inline && addedCss) {
return;
}
if (editor.inline) {
addedInlineCss = true;
} else {
addedCss = true;
}
linkElm = editor.dom.create('link', {
rel: 'stylesheet',
href: pluginUrl + '/css/prism.css'
});
editor.getDoc().getElementsByTagName('head')[0].appendChild(linkElm);
}
editor.on('PreProcess', function(e) {
$('pre[contenteditable=false]', e.node).
filter(trimArg(Utils.isCodeSample)).
each(function(idx, elm) {
var $elm = $(elm), code = elm.textContent;
$elm.attr('class', $.trim($elm.attr('class')));
$elm.removeAttr('contentEditable');
$elm.empty().append($('<code></code>').each(function() {
// Needs to be textContent since innerText produces BR:s
this.textContent = code;
}));
});
});
editor.on('SetContent', function() {
var unprocessedCodeSamples = $('pre').filter(trimArg(Utils.isCodeSample)).filter(function(idx, elm) {
return elm.contentEditable !== "false";
});
if (unprocessedCodeSamples.length) {
editor.undoManager.transact(function() {
unprocessedCodeSamples.each(function(idx, elm) {
$(elm).find('br').each(function(idx, elm) {
elm.parentNode.replaceChild(editor.getDoc().createTextNode('\n'), elm);
});
elm.contentEditable = false;
elm.innerHTML = editor.dom.encode(elm.textContent);
Prism.highlightElement(elm);
elm.className = $.trim(elm.className);
});
});
}
});
editor.addCommand('codesample', function() {
Dialog.open(editor);
});
editor.addButton('codesample', {
cmd: 'codesample',
title: 'Insert/Edit code sample'
});
editor.on('init', loadCss);
});
});