Fix code line wrapping + other improvements (#275)

This commit is contained in:
Ryo Hilmawan 2020-11-19 16:00:15 +07:00 committed by GitHub
parent ca9788136c
commit e2d088d312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,7 +14,7 @@ module.exports = {
tags: ['extension'], tags: ['extension'],
name: 'code line numbers', name: 'code line numbers',
desc: 'adds line numbers to code blocks.', desc: 'adds line numbers to code blocks.',
version: '1.0.0', version: '1.1.0',
author: 'CloudHill', author: 'CloudHill',
options: [ options: [
{ {
@ -37,21 +37,37 @@ module.exports = {
childList: true, childList: true,
subtree: true, subtree: true,
}); });
const resizeObserver = new ResizeObserver(
(list, observer) => number(list[0].target)
);
function handle(list) { function handle(list) {
queue = []; queue = [];
for (let { addedNodes } of list) { for (let { addedNodes } of list) {
if ( if (
addedNodes[0] && addedNodes[0] &&
addedNodes[0].dataset && (
addedNodes[0].dataset.tokenIndex && addedNodes[0].className === 'notion-page-content' ||
addedNodes[0].parentElement (
) { addedNodes[0].querySelector &&
const block = addedNodes[0].parentElement.parentElement; addedNodes[0].querySelector('.notion-code-block.line-numbers')
if ( )
block && )
block.classList &&
block.classList.contains('notion-code-block')
) { ) {
resizeObserver.disconnect();
const codeBlocks = document.querySelectorAll('.notion-code-block.line-numbers');
codeBlocks.forEach(block => {
number(block);
resizeObserver.observe(block);
})
}
}
}
function number(block) {
let codeLineNumbers = ''
let numbers = block.querySelector('#code-line-numbers'); let numbers = block.querySelector('#code-line-numbers');
if (!numbers) { if (!numbers) {
numbers = createElement( numbers = createElement(
@ -63,26 +79,38 @@ module.exports = {
numbers.style.bottom = blockStyle.paddingBottom; numbers.style.bottom = blockStyle.paddingBottom;
block.append(numbers); block.append(numbers);
}
const temp = createElement('<div>A</div>'); const temp = createElement('<div>A</div>');
block.children[0].append(temp); block.children[0].append(temp);
block.lineHeight = temp.getBoundingClientRect().height; const lineWidth = temp.getBoundingClientRect().width;
temp.style.display = 'inline';
const charWidth = temp.getBoundingClientRect().width;
temp.remove(); temp.remove();
let codeString = ''
let lineCounter = 0;
const codeSpans = block.firstChild.querySelectorAll('span');
codeSpans.forEach(s => codeString += s.innerText)
const lines = codeString.split(/\r\n|\r|\n/);
for (let i = 0; i < lines.length - 1; i++) {
lineCounter++;
codeLineNumbers += `${lineCounter}\n`;
const lineWrap = (lines[i].length * charWidth) / lineWidth;
for (let j = 1; j < Math.ceil(lineWrap); j++)
codeLineNumbers += '\n';
} }
const lines = Math.round( console.log(codeLineNumbers.length)
numbers.getBoundingClientRect().height / block.lineHeight if (store().single_lined || codeLineNumbers.length > 2) {
);
if (lines > 1) {
block.children[0].classList.add('code-numbered'); block.children[0].classList.add('code-numbered');
numbers.innerText = Array.from( numbers.innerText = codeLineNumbers;
Array(lines), } else {
(e, i) => i + 1 block.children[0].classList.remove('code-numbered');
).join('\n'); numbers.innerText = ''
}
}
}
} }
} }
}); });