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
/
mergeLines.js
52 lines (44 loc) · 1.39 KB
/
mergeLines.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
/**
* Merges selected lines or lines between XHTML tag pairs
* @param {Function} require
* @param {Underscore} _
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var htmlMatcher = require('../assets/htmlMatcher');
var utils = require('../utils/common');
var editorUtils = require('../utils/editor');
var range = require('../assets/range');
return {
mergeLinesAction: function(editor) {
var info = editorUtils.outputInfo(editor);
var selection = range(editor.getSelectionRange());
if (!selection.length()) {
// find matching tag
var pair = htmlMatcher.find(info.content, editor.getCaretPos());
if (pair) {
selection = pair.outerRange;
}
}
if (selection.length()) {
// got range, merge lines
var text = selection.substring(info.content);
var lines = utils.splitByLines(text);
for (var i = 1; i < lines.length; i++) {
lines[i] = lines[i].replace(/^\s+/, '');
}
text = lines.join('').replace(/\s{2,}/, ' ');
var textLen = text.length;
text = utils.escapeText(text);
editor.replaceContent(text, selection.start, selection.end);
editor.createSelection(selection.start, selection.start + textLen);
return true;
}
return false;
}
};
});