-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
codeLineNumbers.js
174 lines (138 loc) · 4.42 KB
/
codeLineNumbers.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
import { getElement, onElementLoaded, toElement } from "../utility";
const notionAppInnerCls = ".notion-app-inner";
const notionPageContentCls = ".notion-page-content";
const notionCursorListenerCls = ".notion-cursor-listener";
const codeLineNumbersCls = "codeLineNumbers";
const DEBUG = false;
let docEditObserverObj = {};
export function codeLineNumbers(isEnabled) {
try {
console.log(`feature: codeLineNumbers: ${isEnabled}`);
// triggers on page load
// it waits for doc to be loaded
onElementLoaded(notionPageContentCls)
.then((isPresent) => {
if (isPresent) {
if (isEnabled) {
addCodeLineNumbers();
docEditListener();
} else {
removeCodeLineNumbers();
}
}
return true;
})
.catch((e) => console.log(e));
} catch (e) {
console.log(e);
}
}
// Internal methods //
function removeDocEditListener() {
if (isObserverType(docEditObserverObj)) {
DEBUG && console.log("disconnected docEditObserver");
docEditObserverObj.disconnect();
}
}
function isObserverType(obj) {
return obj.disconnect !== undefined;
}
// add/update line if any code block change occurs
// works for page change or window reload
function docEditListener() {
DEBUG && console.log("listening for doc edit changes...");
let isCodeChanged = false;
let block = "";
docEditObserverObj = new MutationObserver((mutationList, obsrvr) => {
DEBUG && console.log("found changes in doc content");
for (let i = 0; i < mutationList.length; i++) {
const m = mutationList[i];
// case: check for text change
if (m.type === "childList") {
if (
m.target &&
m.target.parentNode &&
m.target.parentNode.classList.contains("line-numbers")
) {
isCodeChanged = true;
block = m.target.parentNode;
// briefly pause listener to avoid recursive triggers
removeDocEditListener();
updateCodeline(block);
docEditListener();
}
}
}
if (isCodeChanged) {
console.log("code changed");
isCodeChanged = false;
}
});
// now add listener for doc text change
const pageContentEl = getElement(notionAppInnerCls);
docEditObserverObj.observe(pageContentEl, {
childList: true,
subtree: true,
});
}
function addCodeLineNumbers() {
const el1 = getElement(notionCursorListenerCls);
el1.classList.add(codeLineNumbersCls);
const el = document.querySelectorAll(
"div.notion-page-content .notion-code-block.line-numbers"
);
el.forEach((x) => {
updateCodeline(x);
return true;
});
}
function removeCodeLineNumbers() {
console.log("removing removeCodeLineNumbers feature...");
removeDocEditListener();
const el = getElement(notionCursorListenerCls);
el.classList.remove(codeLineNumbersCls);
document.querySelectorAll(".code-line-numbers").forEach((x) => x.remove());
// clearOutline();
console.log("removed removeCodeLineNumbers feature");
}
// credit @CloudHill
function updateCodeline(block) {
let lineNumbers = "";
let numbers = block.querySelector(".code-line-numbers");
if (!numbers) {
numbers = toElement('<span class="code-line-numbers"></span>');
const blockStyle = window.getComputedStyle(block.children[0]);
numbers.style.top = blockStyle.paddingTop;
numbers.style.bottom = blockStyle.paddingBottom;
block.append(numbers);
const temp = toElement("<span>A</span>");
block.firstChild.append(temp);
block.lineHeight = temp.getBoundingClientRect().height;
temp.remove();
}
const lines = block.firstChild.innerText.split(/\r\n|\r|\n/);
if (lines[lines.length - 1] === "") lines.pop();
let lineCounter = 0;
const wordWrap = block.firstChild.style.wordBreak === "break-all";
for (let i = 0; i < lines.length; i++) {
lineCounter++;
lineNumbers += `${lineCounter}\n`;
if (wordWrap) {
const temp = document.createElement("span");
temp.innerText = lines[i];
block.firstChild.append(temp);
const lineHeight = temp.getBoundingClientRect().height;
temp.remove();
for (let j = 1; j < lineHeight / block.lineHeight - 1; j++) {
lineNumbers += "\n";
}
}
}
if (lineNumbers.length > 2) {
block.firstChild.classList.add("code-numbered");
numbers.innerText = lineNumbers;
} else {
block.firstChild.classList.remove("code-numbered");
numbers.innerText = "";
}
}