-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcodeit-match-braces.js
202 lines (150 loc) · 4.44 KB
/
codeit-match-braces.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
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
(function () {
if (typeof Prism === 'undefined' || typeof document === 'undefined') {
return;
}
function mapClassName(name) {
var customClass = Prism.plugins.customClass;
if (customClass) {
return customClass.apply(name, 'none');
} else {
return name;
}
}
var PARTNER = {
'(': ')',
'[': ']',
'{': '}',
};
// The names for brace types.
// These names have two purposes: 1) they can be used for styling and 2) they are used to pair braces. Only braces
// of the same type are paired.
var NAMES = {
'(': 'brace-round',
'[': 'brace-square',
'{': 'brace-curly',
};
// A map for brace aliases.
// This is useful for when some braces have a prefix/suffix as part of the punctuation token.
var BRACE_ALIAS_MAP = {
'${': '{', // JS template punctuation (e.g. `foo ${bar + 1}`)
};
var LEVEL_WARP = 12;
var pairIdCounter = 0;
var BRACE_ID_PATTERN = /^(pair-\d+-)(open|close)$/;
/**
* Returns the brace partner given one brace of a brace pair.
*
* @param {HTMLElement} brace
* @returns {HTMLElement}
*/
function getPartnerBrace(brace, cdEl) {
var match = BRACE_ID_PATTERN.exec(brace.id);
return cdEl.querySelector('#' + match[1] + (match[2] == 'open' ? 'close' : 'open'));
}
/**
* Re-match braces for element
*/
function rematch(code) {
// reset pair id counter
pairIdCounter = 0;
// find the braces to match
/** @type {string[]} */
var toMatch = ['(', '[', '{'];
/** @type {HTMLSpanElement[]} */
var punctuation = Array.prototype.slice.call(
code.querySelectorAll('span.' + mapClassName('token') + '.' + mapClassName('punctuation'))
);
/** @type {{ index: number, open: boolean, element: HTMLElement }[]} */
var allBraces = [];
toMatch.forEach(function (open) {
var close = PARTNER[open];
var name = mapClassName(NAMES[open]);
/** @type {[number, number][]} */
var pairs = [];
/** @type {number[]} */
var openStack = [];
for (var i = 0; i < punctuation.length; i++) {
var element = punctuation[i];
if (element.childElementCount == 0) {
var text = element.textContent;
text = BRACE_ALIAS_MAP[text] || text;
if (text === open) {
allBraces.push({ index: i, open: true, element: element });
element.classList.add(name);
element.classList.add(mapClassName('brace'));
openStack.push(i);
} else if (text === close) {
allBraces.push({ index: i, open: false, element: element });
element.classList.add(name);
element.classList.add(mapClassName('brace'));
if (openStack.length) {
pairs.push([i, openStack.pop()]);
}
}
}
}
pairs.forEach(function (pair) {
var pairId = 'pair-' + (pairIdCounter++) + '-';
var opening = punctuation[pair[0]];
var closing = punctuation[pair[1]];
opening.id = pairId + 'open';
closing.id = pairId + 'close';
});
});
var level = 0;
allBraces.sort(function (a, b) { return a.index - b.index; });
allBraces.forEach(function (brace) {
if (brace.open) {
brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
level++;
} else {
level = Math.max(0, level - 1);
brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
}
});
}
/**
* Global exports
*/
var config = Prism.plugins.matchBraces = {
/**
* Matches braces for an element
*/
match: function (element) {
rematch(element);
}
}
let cdElements = [];
Prism.hooks.add('complete', function (env) {
var cdEl = env.element;
rematch(cdEl);
if (!cdElements.includes(cdEl)) {
addCaretListeners(cdEl);
cdElements.push(cdEl);
}
});
function matchBraces(cdEl) {
cdEl.querySelectorAll('.token.brace.brace-active').forEach(brace => {
brace.classList.remove('brace-active');
});
if (document.activeElement === cdEl) {
if (window.getSelection().toString().length < 2) {
const cursor = cdEl.dropper.cursor();
if (cursor && cursor.in('brace')) {
const currentBrace = cursor.getParent();
if (currentBrace.id) {
currentBrace.classList.add('brace-active');
const partnerBrace = getPartnerBrace(currentBrace, cdEl);
if (partnerBrace) partnerBrace.classList.add('brace-active');
}
}
}
}
}
function addCaretListeners(cdEl) {
cdEl.on('caretmove', () => {
matchBraces(cdEl);
window.requestAnimationFrame(() => { matchBraces(cdEl) });
});
}
}());