Dialog.js
3.01 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
/**
* Dialog.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
*/
/**
* Contains all dialog logic.
*
* @class tinymce.codesample.Dialog
* @private
*/
define("tinymce/codesampleplugin/Dialog", [
"tinymce/dom/DOMUtils",
"tinymce/codesampleplugin/Utils",
"tinymce/codesampleplugin/Prism"
], function(DOMUtils, Utils, Prism) {
var DOM = DOMUtils.DOM;
function getLanguages(editor) {
var defaultLanguages = [
{text: 'HTML/XML', value: 'markup'},
{text: 'JavaScript', value: 'javascript'},
{text: 'CSS', value: 'css'},
{text: 'PHP', value: 'php'},
{text: 'Ruby', value: 'ruby'},
{text: 'Python', value: 'python'},
{text: 'Java', value: 'java'},
{text: 'C', value: 'c'},
{text: 'C#', value: 'csharp'},
{text: 'C++', value: 'cpp'}
];
var customLanguages = editor.settings.codesample_languages;
return customLanguages ? customLanguages : defaultLanguages;
}
function insertCodeSample(editor, language, code) {
editor.undoManager.transact(function() {
var node = getSelectedCodeSample(editor);
code = DOM.encode(code);
if (node) {
editor.dom.setAttrib(node, 'class', 'language-' + language);
node.innerHTML = code;
Prism.highlightElement(node);
editor.selection.select(node);
} else {
editor.insertContent('<pre id="__new" class="language-' + language + '">' + code + '</pre>');
editor.selection.select(editor.$('#__new').removeAttr('id')[0]);
}
});
}
function getSelectedCodeSample(editor) {
var node = editor.selection.getNode();
if (Utils.isCodeSample(node)) {
return node;
}
return null;
}
function getCurrentCode(editor) {
var node = getSelectedCodeSample(editor);
if (node) {
return node.textContent;
}
return '';
}
function getCurrentLanguage(editor) {
var matches, node = getSelectedCodeSample(editor);
if (node) {
matches = node.className.match(/language-(\w+)/);
return matches ? matches[1] : '';
}
return '';
}
return {
open: function(editor) {
editor.windowManager.open({
title: "Insert/Edit code sample",
minWidth: Math.min(DOM.getViewPort().w, editor.getParam('codesample_dialog_width', 800)),
minHeight: Math.min(DOM.getViewPort().h, editor.getParam('codesample_dialog_height', 650)),
layout: 'flex',
direction: 'column',
align: 'stretch',
body: [
{
type: 'listbox',
name: 'language',
label: 'Language',
maxWidth: 200,
value: getCurrentLanguage(editor),
values: getLanguages(editor)
},
{
type: 'textbox',
name: 'code',
multiline: true,
spellcheck: false,
ariaLabel: 'Code view',
flex: 1,
style: 'direction: ltr; text-align: left',
classes: 'monospace',
value: getCurrentCode(editor),
autofocus: true
}
],
onSubmit: function(e) {
insertCodeSample(editor, e.data.language, e.data.code);
}
});
}
};
});