This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
116 lines (103 loc) · 3.17 KB
/
editor.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
116
/**
* Utility module used to prepare text for pasting into back-end editor
* @author Sergey Chikuyonok ([email protected]) <http://chikuyonok.ru>
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var utils = require('./common');
var resources = require('../assets/resources');
return {
/**
* Check if cursor is placed inside XHTML tag
* @param {String} html Contents of the document
* @param {Number} caretPos Current caret position inside tag
* @return {Boolean}
*/
isInsideTag: function(html, caretPos) {
var reTag = /^<\/?\w[\w\:\-]*.*?>/;
// search left to find opening brace
var pos = caretPos;
while (pos > -1) {
if (html.charAt(pos) == '<')
break;
pos--;
}
if (pos != -1) {
var m = reTag.exec(html.substring(pos));
if (m && caretPos > pos && caretPos < pos + m[0].length)
return true;
}
return false;
},
/**
* Sanitizes incoming editor data and provides default values for
* output-specific info
* @param {IEmmetEditor} editor
* @param {String} syntax
* @param {String} profile
*/
outputInfo: function(editor, syntax, profile) {
// most of this code makes sense for Java/Rhino environment
// because string that comes from Java are not actually JS string
// but Java String object so the have to be explicitly converted
// to native string
profile = profile || editor.getProfileName();
return {
/** @memberOf outputInfo */
syntax: String(syntax || editor.getSyntax()),
profile: profile || null,
content: String(editor.getContent())
};
},
/**
* Unindent content, thus preparing text for tag wrapping
* @param {IEmmetEditor} editor Editor instance
* @param {String} text
* @return {String}
*/
unindent: function(editor, text) {
return utils.unindentString(text, this.getCurrentLinePadding(editor));
},
/**
* Returns padding of current editor's line
* @param {IEmmetEditor} Editor instance
* @return {String}
*/
getCurrentLinePadding: function(editor) {
return utils.getLinePadding(editor.getCurrentLine());
},
/**
* Normalizes content according to given preferences, e.g.
* replaces newlines and indentation with ones defined in
* `options`. If options are not provided or incomplete,
* values will be taken from current user environment
* @param {String} text
* @param {Object} options
* @return {String}
*/
normalize: function(text, options) {
options = utils.extend({
newline: resources.getNewline(),
indentation: resources.getVariable('indentation')
}, options);
var indent = function(tabs) {
return utils.repeatString(options.indentation, tabs.length);
};
var lines = utils.splitByLines(text);
// normailze indentation if it’s not tabs
if (options.indentation !== '\t') {
lines = lines.map(function(line) {
return line.replace(/^\s+/, function(space) {
return space.replace(/\t/g, indent);
});
});
}
// normalize newlines
return lines.join(options.newline);
}
};
});