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
/
splitJoinTag.js
73 lines (62 loc) · 2.22 KB
/
splitJoinTag.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
/**
* Splits or joins tag, e.g. transforms it into a short notation and vice versa:<br>
* <div></div> → <div /> : join<br>
* <div /> → <div></div> : split
*/
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('../utils/common');
var resources = require('../assets/resources');
var matcher = require('../assets/htmlMatcher');
var editorUtils = require('../utils/editor');
var profile = require('../assets/profile');
/**
* @param {IEmmetEditor} editor
* @param {Object} profile
* @param {Object} tag
*/
function joinTag(editor, profile, tag) {
// empty closing slash is a nonsense for this action
var slash = profile.selfClosing() || ' /';
var content = tag.open.range.substring(tag.source).replace(/\s*>$/, slash + '>');
var caretPos = editor.getCaretPos();
// update caret position
if (content.length + tag.outerRange.start < caretPos) {
caretPos = content.length + tag.outerRange.start;
}
content = utils.escapeText(content);
editor.replaceContent(content, tag.outerRange.start, tag.outerRange.end);
editor.setCaretPos(caretPos);
return true;
}
function splitTag(editor, profile, tag) {
var caretPos = editor.getCaretPos();
// define tag content depending on profile
var tagContent = (profile.tag_nl === true) ? '\n\t\n' : '';
var content = tag.outerContent().replace(/\s*\/>$/, '>');
caretPos = tag.outerRange.start + content.length;
content += tagContent + '</' + tag.open.name + '>';
content = utils.escapeText(content);
editor.replaceContent(content, tag.outerRange.start, tag.outerRange.end);
editor.setCaretPos(caretPos);
return true;
}
return {
splitJoinTagAction: function(editor, profileName) {
var info = editorUtils.outputInfo(editor, null, profileName);
var curProfile = profile.get(info.profile);
// find tag at current position
var tag = matcher.tag(info.content, editor.getCaretPos());
if (tag) {
return tag.close
? joinTag(editor, curProfile, tag)
: splitTag(editor, curProfile, tag);
}
return false;
}
};
});