-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource.html
225 lines (191 loc) · 5.78 KB
/
source.html
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
/**
* source.html
*
* Copyright 2013 Web Power, www.webpower.nl
* @author Arjan Haverkamp
*/
// Global vars:
var tinymce, // Reference to TinyMCE
editor, // Reference to TinyMCE editor
codemirror, // CodeMirror instance
chr = 0, // Unused utf-8 character, placeholder for cursor
CMsettings; // CodeMirror settings
function inArray(key, arr)
{
"use strict";
arr = '|' + arr.join('|') + '|';
return arr.indexOf('|'+key+'|') != -1;
}
(function()
{// Initialise (before load)
"use strict";
tinymce = parent.tinymce;
editor = tinymce.activeEditor;
var i, userSettings = editor.settings.codemirror ? editor.settings.codemirror : null;
CMsettings = {
path: userSettings.path || 'CodeMirror',
indentOnInit: userSettings.indentOnInit || false,
config: {// Default config
mode: 'htmlmixed',
lineNumbers: true,
lineWrapping: true,
indentUnit: 1,
tabSize: 1,
matchBrackets: true,
styleActiveLine: true
},
jsFiles: [// Default JS files
'lib/codemirror.js',
'addon/edit/matchbrackets.js',
'mode/xml/xml.js',
'mode/javascript/javascript.js',
'mode/css/css.js',
'mode/htmlmixed/htmlmixed.js',
'addon/dialog/dialog.js',
'addon/search/searchcursor.js',
'addon/search/search.js',
'addon/selection/active-line.js'
],
cssFiles: [// Default CSS files
'lib/codemirror.css',
'addon/dialog/dialog.css'
]
};
// Merge config
for (i in userSettings.config) {
CMsettings.config[i] = userSettings.config[i];
}
// Merge jsFiles
for (i in userSettings.jsFiles) {
if (!inArray(userSettings.jsFiles[i], CMsettings.jsFiles)) {
CMsettings.jsFiles.push(userSettings.jsFiles[i]);
}
}
// Merge cssFiles
for (i in userSettings.cssFiles) {
if (!inArray(userSettings.cssFiles[i], CMsettings.cssFiles)) {
CMsettings.cssFiles.push(userSettings.cssFiles[i]);
}
}
// Add trailing slash to path
if (!/\/$/.test(CMsettings.path)) {
CMsettings.path += '/';
}
// Write stylesheets
for (i = 0; i < CMsettings.cssFiles.length; i++) {
document.write('<li'+'nk rel="stylesheet" type="text/css" href="' + CMsettings.path + CMsettings.cssFiles[i] + '" />');
}
// Write JS source files
for (i = 0; i < CMsettings.jsFiles.length; i++) {
document.write('<scr'+'ipt type="text/javascript" src="' + CMsettings.path + CMsettings.jsFiles[i] + '"></scr'+'ipt>');
}
window.onload = start;
}());
function start()
{// Initialise (on load)
"use strict";
if (typeof(window.CodeMirror) !== 'function') {
alert('CodeMirror not found in "' + CMsettings.path + '", aborting...');
return;
}
// Create legend for keyboard shortcuts for find & replace:
var head = parent.document.querySelectorAll('.mce-foot')[0],
div = parent.document.createElement('div'),
td1 = '<td style="font-size:11px;background:#777;color:#fff;padding:0 4px">',
td2 = '<td style="font-size:11px;padding-right:5px">';
div.innerHTML = '<table cellspacing="0" cellpadding="0" style="border-spacing:4px"><tr>' + td1 + 'Ctrl-F</td>' + td2 + tinymce.translate('Start search') + '</td>' + td1 + 'Ctrl-G</td>' + td2 + tinymce.translate('Find next') + '</td>' + td1 + 'Shift-Ctrl-G</td>' + td2 + tinymce.translate('Find previous') + '</td></tr>' + '<tr>' + td1 + 'Shift-Ctrl-F</td>' + td2 + tinymce.translate('Replace') + '</td>' + td1 + 'Shift-Ctrl-R</td>' + td2 + tinymce.translate('Replace all') + '</td></tr></table>';
div.style.position = 'absolute';
div.style.left = div.style.bottom = '5px';
head.appendChild(div);
// Set CodeMirror cursor to same position as cursor was in TinyMCE:
var html = editor.getContent({source_view: true});
html = html.replace(/<span\s+class="CmCaReT"([^>]*)>([^<]*)<\/span>/gm, String.fromCharCode(chr));
editor.dom.remove(editor.dom.select('.CmCaReT'));
CodeMirror.defineInitHook(function(inst)
{
// Move cursor to correct position:
inst.focus();
var cursor = inst.getSearchCursor(String.fromCharCode(chr), false);
if (cursor.findNext()) {
inst.setCursor(cursor.to());
cursor.replace('');
}
// Indent all code, if so requested:
if (editor.settings.codemirror.indentOnInit) {
var last = inst.lineCount();
inst.operation(function() {
for (var i = 0; i < last; ++i) {
inst.indentLine(i);
}
});
}
});
CMsettings.config.value = html;
// Instantiante CodeMirror:
codemirror = CodeMirror(document.body, CMsettings.config);
codemirror.isDirty = false;
codemirror.on('change', function(inst) {
inst.isDirty = true;
});
}
function findDepth(haystack, needle)
{
"use strict";
var idx = haystack.indexOf(needle), depth = 0, x;
for (x = idx; x >= 0; x--) {
switch(haystack.charAt(x)) {
case '<': depth--; break;
case '>': depth++; break;
}
}
return depth;
}
// This function is called by plugin.js, when user clicks 'Ok' button
function submit()
{
"use strict";
var cc = '�', isDirty = codemirror.isDirty;
// Insert cursor placeholder (�)
codemirror.doc.replaceSelection(cc);
var pos = codemirror.getCursor(),
curLineHTML = codemirror.doc.getLine(pos.line);
if (findDepth(curLineHTML, cc) !== 0) {
// Cursor is inside a <tag>, don't set cursor:
curLineHTML = curLineHTML.replace(cc, '');
codemirror.doc.setLine(pos.line, curLineHTML);
}
// Submit HTML to TinyMCE:
editor.setContent(codemirror.getValue().replace(cc, '<span id="CmCaReT"></span>'));
editor.isNotDirty = !isDirty;
if (isDirty) {
editor.nodeChanged();
}
// Set cursor:
var el = editor.dom.select('span#CmCaReT')[0];
if (el) {
editor.selection.scrollIntoView(el);
editor.selection.setCursorLocation(el,0);
editor.dom.remove(el);
}
}
</script>
<style type="text/css">
body {
margin: 0;
}
.CodeMirror {
height: 100%;
font-size: 12px;
line-height: 18px;
}
.CodeMirror-activeline-background {
background: #e8f2ff !important;
}
</style>
</head>
<body></body>
</html>