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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// HyperMD, copyright (c) by laobubu
// Distributed under an MIT license: http://laobubu.net/HyperMD/LICENSE
//
// DESCRIPTION: Auto show/hide markdown tokens like `##` or `*`
//
// Only works with `hypermd` mode, require special CSS rules
//
// PATCHED:
// - linkHref (url) in internal links (*.md) is always hidden, even when cursor is on the line
// - Arrow keys skip over hidden linkHref spans
// - Shift+Arrow at hidden linkHref selects to start/end of line
(function (mod){ //[HyperMD] UMD patched!
/*commonjs*/ ("object"==typeof exports&&"undefined"!=typeof module) ? mod(null, exports, require("codemirror"), require("../core"), require("../core"), require("../core")) :
/*amd*/ ("function"==typeof define&&define.amd) ? define(["require","exports","codemirror","../core","../core","../core"], mod) :
/*plain env*/ mod(null, (this.HyperMD.HideToken = this.HyperMD.HideToken || {}), CodeMirror, HyperMD, HyperMD, HyperMD);
})(function (require, exports, CodeMirror, core_1, cm_utils_1, line_spans_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var DEBUG = false;
//#region Internal Function...
/** check if has the class and remove it */
function rmClass(el, className) {
var c = ' ' + el.className + ' ', cnp = ' ' + className + ' ';
if (c.indexOf(cnp) === -1)
return false;
el.className = c.replace(cnp, '').trim();
return true;
}
/** check if NOT has the class and add it */
function addClass(el, className) {
var c = ' ' + el.className + ' ', cnp = ' ' + className + ' ';
if (c.indexOf(cnp) !== -1)
return false;
el.className = (el.className + ' ' + className);
return true;
}
exports.defaultOption = {
enabled: false,
line: true,
tokenTypes: "em|strong|strikethrough|code|linkText|linkHref|task".split("|"),
};
exports.suggestedOption = {
enabled: true,
};
core_1.suggestedEditorConfig.hmdHideToken = exports.suggestedOption;
core_1.normalVisualConfig.hmdHideToken = false;
CodeMirror.defineOption("hmdHideToken", exports.defaultOption, function (cm, newVal) {
///// convert newVal's type to `Partial<Options>`, if it is not.
if (!newVal || typeof newVal === "boolean") {
newVal = { enabled: !!newVal };
}
else if (typeof newVal === "string") {
newVal = { enabled: true, tokenTypes: newVal.split("|") };
}
else if (newVal instanceof Array) {
newVal = { enabled: true, tokenTypes: newVal };
}
///// apply config and write new values into cm
var inst = exports.getAddon(cm);
for (var k in exports.defaultOption) {
inst[k] = (k in newVal) ? newVal[k] : exports.defaultOption[k];
}
});
//#endregion
/********************************************************************************** */
//#region Addon Class
var hideClassName = "hmd-hidden-token";
var lineInactiveClassName = "hmd-inactive-line";
var HideToken = /** @class */ (function () {
function HideToken(cm) {
var _this = this;
this.cm = cm;
this.renderLineHandler = function (cm, line, el) {
// TODO: if we procLine now, we can only get the outdated lineView, lineViewMeasure and lineViewMap. Calling procLine will be wasteful!
var changed = _this.procLine(line, el);
if (DEBUG)
console.log("renderLine return " + changed);
};
this.cursorActivityHandler = function (doc) {
// PATCHED, if we don't do this, autoscroll is not working.
if (cm.somethingSelected()) return;
// PATCHED, prevent blinking
// Actually that works bad when we show/hide a header with 2 lines of text (expanded) and 1 line of text hidden.
// Cursor jumps to wrong positions
// _this.updateImmediately();
_this.update();
};
this.update = core_1.debounce(function () { return _this.updateImmediately(); }, 100);
// PATCHED, run hide-token synchronously with text edits so word/line
// deletions and mouse cuts don't leave revealed tokens visible during
// the 100ms cursor-activity debounce window (visible jitter).
this.changesHandler = function () { _this.updateImmediately(); };
/** Current user's selections, in each line */
this._rangesInLine = {};
// PATCHED, skip cursor over always-hidden linkHref spans.
this._skipLinkHref = function (cm, e) {
if (e.key !== 'ArrowLeft' && e.key !== 'ArrowRight') return;
var cursor = cm.getCursor();
var spans = line_spans_1.getLineSpanExtractor(cm).extract(cursor.line);
for (var i = 0; i < spans.length; i++) {
var span = spans[i];
if (span.type !== 'linkHref') continue;
if (span.text.includes('://')) continue;
if (!span.text.endsWith('.md') && !span.text.endsWith('.md)')) continue;
if (e.key === 'ArrowRight' && cursor.ch >= span.begin && cursor.ch < span.end) {
e.preventDefault();
// PATCHED, shift+arrow at hidden linkHref selects to end/start of line.
if (e.shiftKey) {
var lineLen = cm.getLine(cursor.line).length;
cm.setSelection(cm.getCursor('anchor'), { line: cursor.line, ch: lineLen });
} else {
if (cm.somethingSelected()) return;
cm.setCursor({ line: cursor.line, ch: span.end });
}
return;
}
if (e.key === 'ArrowLeft' && cursor.ch > span.begin && cursor.ch <= span.end) {
e.preventDefault();
// PATCHED, shift+arrow at hidden linkHref selects to end/start of line.
if (e.shiftKey) {
cm.setSelection(cm.getCursor('anchor'), { line: cursor.line, ch: 0 });
} else {
if (cm.somethingSelected()) return;
cm.setCursor({ line: cursor.line, ch: span.begin });
}
return;
}
}
};
new core_1.FlipFlop(
/* ON */ function () {
cm.on("cursorActivity", _this.cursorActivityHandler);
cm.on("renderLine", _this.renderLineHandler);
cm.on("update", _this.update);
cm.on("changes", _this.changesHandler);
cm.on("keydown", _this._skipLinkHref);
_this.update();
cm.refresh();
},
/* OFF */ function () {
cm.off("cursorActivity", _this.cursorActivityHandler);
cm.off("renderLine", _this.renderLineHandler);
cm.off("update", _this.update);
cm.off("changes", _this.changesHandler);
cm.off("keydown", _this._skipLinkHref);
_this.update.stop();
cm.refresh();
}).bind(this, "enabled", true);
}
/**
* hide/show <span>s in one line, based on `this._rangesInLine`
* @returns line changed or not
*/
HideToken.prototype.procLine = function (line, pre) {
var cm = this.cm;
var lineNo = typeof line === 'number' ? line : line.lineNo();
if (typeof line === 'number')
line = cm.getLineHandle(line);
var rangesInLine = this._rangesInLine[lineNo] || [];
var lv = core_1.cm_internal.findViewForLine(cm, lineNo);
if (!lv || lv.hidden || !lv.measure)
return false;
if (!pre)
pre = lv.text;
if (!pre)
return false;
if (DEBUG)
if (!pre.isSameNode(lv.text))
console.warn("procLine got different node... " + lineNo);
var mapInfo = core_1.cm_internal.mapFromLineView(lv, line, lineNo);
var map = mapInfo.map;
var nodeCount = map.length / 3;
var changed = false;
// change line status
if (rangesInLine.length === 0) { // inactiveLine
if (addClass(pre, lineInactiveClassName))
changed = true;
}
else { // activeLine
if (rmClass(pre, lineInactiveClassName))
changed = true;
}
// show or hide tokens
/**
* @returns if there are Span Nodes changed
*/
function changeVisibilityForSpan(span, shallHideTokens, iNodeHint) {
var changed = false;
iNodeHint = iNodeHint || 0;
// iterate the map
for (var i = iNodeHint; i < nodeCount; i++) {
var begin = map[i * 3], end = map[i * 3 + 1];
var domNode = map[i * 3 + 2];
if (begin === span.head.start) {
// find the leading token!
if (/formatting-/.test(span.head.type) && domNode.nodeType === Node.TEXT_NODE) {
// if (DEBUG) console.log("DOMNODE", shallHideTokens, domNode, begin, span)
// good. this token can be changed
var domParent = domNode.parentElement;
if (domNode.textContent === "(") {
if (shallHideTokens ? addClass(domNode.parentElement.nextSibling, hideClassName) : rmClass(domNode.parentElement.nextSibling, hideClassName)) {
// if (DEBUG) console.log("HEAD DOM PATCHED")
changed = true;
}
}
if (shallHideTokens ? addClass(domParent, hideClassName) : rmClass(domParent, hideClassName)) {
// if (DEBUG) console.log("HEAD DOM PATCHED")
changed = true;
}
}
//FIXME: if leading formatting token is separated into two, the latter will not be hidden/shown!
// search for the tailing token
if (span.tail && /formatting-/.test(span.tail.type)) {
for (var j = i + 1; j < nodeCount; j++) {
var begin_1 = map[j * 3], end_1 = map[j * 3 + 1];
var domNode_1 = map[j * 3 + 2];
if (begin_1 == span.tail.start) {
// if (DEBUG) console.log("TAIL DOM PATCHED", domNode)
if (domNode_1.nodeType === Node.TEXT_NODE) {
// good. this token can be changed
var domParent = domNode_1.parentElement;
if (domNode.textContent === "```") {
if (shallHideTokens ? addClass(domParent, hideClassName) : rmClass(domParent, hideClassName)) {
changed = true;
}
}
if (shallHideTokens ? addClass(domParent, hideClassName) : rmClass(domParent, hideClassName)) {
changed = true;
}
}
}
if (begin_1 >= span.tail.end)
break;
}
}
}
// whoops, next time we can start searching since here
// return the hint value
if (begin >= span.begin)
break;
}
return changed;
}
var spans = line_spans_1.getLineSpanExtractor(cm).extract(lineNo);
var iNodeHint = 0;
for (var iSpan = 0; iSpan < spans.length; iSpan++) {
var span = spans[iSpan];
if (this.tokenTypes.indexOf(span.type) === -1)
continue; // not-interested span type
/* TODO: Use AST, instead of crafted Position */
var spanRange = [{ line: lineNo, ch: span.begin }, { line: lineNo, ch: span.end }];
/* TODO: If use AST, compute `spanBeginCharInCurrentLine` in another way */
var spanBeginCharInCurrentLine = span.begin;
while (iNodeHint < nodeCount && map[iNodeHint * 3 + 1] < spanBeginCharInCurrentLine)
iNodeHint++;
var shallHideTokens = true;
// PATCHED, always hide linkHref when path ends with .md — internal links.
var isHiddenLinkHref = span.type === 'linkHref' &&
!span.text.includes('://') &&
(span.text.endsWith('.md') || span.text.endsWith('.md)'));
if (!isHiddenLinkHref) {
for (var iLineRange = 0; iLineRange < rangesInLine.length; iLineRange++) {
var userRange = rangesInLine[iLineRange];
if (cm_utils_1.rangesIntersect(spanRange, userRange)) {
shallHideTokens = false;
break;
}
}
}
// PATCHED, if we are at the task line and before checkbox, don't hide tokens.
if (span.type === 'task') {
var cursorPos = cm.getCursor();
if (cursorPos.line === lineNo) {
var tokens = cm.getLineTokens(lineNo);
// Check if cursor is on list formatting that comes before this task
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.type && token.type.indexOf('formatting-list') !== -1) {
// If cursor is on the list formatting token
if (cursorPos.ch >= token.start && cursorPos.ch <= token.end) {
shallHideTokens = false; // Don't hide task when cursor is on adjacent list
break;
}
}
}
}
}
// PATCHED
if (changeVisibilityForSpan(span, shallHideTokens, iNodeHint))
changed = true;
}
// finally clean the cache (if needed) and report the result
if (changed) {
// clean CodeMirror measure cache
delete lv.measure.heights;
lv.measure.cache = {};
}
return changed;
};
HideToken.prototype.updateImmediately = function () {
var _this = this;
this.update.stop();
var cm = this.cm;
var selections = cm.listSelections();
var caretAtLines = {};
var activedLines = {};
var lastActivedLines = this._rangesInLine;
// update this._activedLines and caretAtLines
for (var _i = 0, selections_1 = selections; _i < selections_1.length; _i++) {
var selection = selections_1[_i];
var oRange = cm_utils_1.orderedRange(selection);
var line0 = oRange[0].line, line1 = oRange[1].line;
caretAtLines[line0] = caretAtLines[line1] = true;
for (var line = line0; line <= line1; line++) {
if (!activedLines[line])
activedLines[line] = [oRange];
else
activedLines[line].push(oRange);
}
}
this._rangesInLine = activedLines;
if (DEBUG)
console.log("======= OP START " + Object.keys(activedLines));
cm.operation(function () {
// adding "inactive" class
for (var line in lastActivedLines) {
if (activedLines[line])
continue; // line is still active. do nothing
_this.procLine(~~line); // or, try adding "inactive" class to the <pre>s
}
var caretLineChanged = false;
// process active lines
for (var line in activedLines) {
var lineChanged = _this.procLine(~~line);
if (lineChanged && caretAtLines[line])
caretLineChanged = true;
}
// refresh cursor position if needed
if (caretLineChanged) {
if (DEBUG)
console.log("caretLineChanged");
cm.refreshCursor();
// legacy unstable way to update display and caret position:
// updateCursorDisplay(cm, true)
// if (cm.hmd.TableAlign && cm.hmd.TableAlign.enabled) cm.hmd.TableAlign.updateStyle()
}
});
if (DEBUG)
console.log("======= OP END ");
};
return HideToken;
}());
exports.HideToken = HideToken;
//#endregion
/** ADDON GETTER (Singleton Pattern): a editor can have only one HideToken instance */
exports.getAddon = core_1.Addon.Getter("HideToken", HideToken, exports.defaultOption /** if has options */);
});