-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Codeview.js
115 lines (97 loc) · 2.81 KB
/
Codeview.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
define([
'summernote/base/core/agent',
'summernote/base/core/dom'
], function (agent, dom) {
var CodeMirror;
if (agent.hasCodeMirror) {
if (agent.isSupportAmd) {
require(['CodeMirror'], function (cm) {
CodeMirror = cm;
});
} else {
CodeMirror = window.CodeMirror;
}
}
/**
* @class Codeview
*/
var Codeview = function (summernote) {
var self = this;
var ui = $.summernote.ui;
var $editor = summernote.layoutInfo.editor;
var $editable = summernote.layoutInfo.editable;
var $codable = summernote.layoutInfo.codable;
var options = summernote.options;
this.sync = function () {
var isCodeview = this.isActivated();
if (isCodeview && agent.hasCodeMirror) {
$codable.data('cmEditor').save();
}
};
/**
* @return {Boolean}
*/
this.isActivated = function () {
return $editor.hasClass('codeview');
};
/**
* toggle codeview
*/
this.toggle = function () {
if (this.isActivated()) {
this.deactivate();
} else {
this.activate();
}
};
/**
* activate code view
*/
this.activate = function () {
$codable.val(dom.html($editable, options.prettifyHtml));
$codable.height($editable.height());
summernote.invoke('toolbar.updateCodeview', [true]);
$editor.addClass('codeview');
$codable.focus();
// activate CodeMirror as codable
if (agent.hasCodeMirror) {
var cmEditor = CodeMirror.fromTextArea($codable[0], options.codemirror);
// CodeMirror TernServer
if (options.codemirror.tern) {
var server = new CodeMirror.TernServer(options.codemirror.tern);
cmEditor.ternServer = server;
cmEditor.on('cursorActivity', function (cm) {
server.updateArgHints(cm);
});
}
// CodeMirror hasn't Padding.
cmEditor.setSize(null, $editable.outerHeight());
$codable.data('cmEditor', cmEditor);
}
};
/**
* deactivate code view
*
* @param {Object} layoutInfo
*/
this.deactivate = function (layoutInfo) {
// deactivate CodeMirror as codable
if (agent.hasCodeMirror) {
var cmEditor = $codable.data('cmEditor');
$codable.val(cmEditor.getValue());
cmEditor.toTextArea();
}
var value = dom.value($codable, options.prettifyHtml) || dom.emptyPara;
var isChange = $editable.html() !== value;
$editable.html(value);
$editable.height(options.height ? $codable.height() : 'auto');
$editor.removeClass('codeview');
if (isChange) {
summernote.triggerEvent('change', [$editable.html(), $editable]);
}
$editable.focus();
summernote.invoke('toolbar.updateCodeview', [false]);
};
};
return Codeview;
});