@codedoc/core
Version:
Create beautiful modern documentation websites.
107 lines • 5.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.codeSelection$ = exports.codeSelection = void 0;
var transport_1 = require("@connectv/sdh/transport");
var clipboard_1 = require("../../transport/clipboard");
var copy_confirm_1 = require("./copy-confirm");
var smart_copy_1 = require("./smart-copy");
function codeSelection() {
transport_1.onReady(function () {
var _exec = function () {
document.querySelectorAll('pre>code').forEach(function (code$) {
code$.querySelectorAll('div').forEach(function (line$) {
line$.addEventListener('mouseenter', function (event) {
if (event.buttons === 1) {
line$.classList.add('selected');
line$.setAttribute('data-just-selected', 'true');
}
});
line$.addEventListener('mousedown', function (event) {
if (!event.shiftKey) {
code$.childNodes.forEach(function (child) {
if (child !== line$)
child.classList.remove('selected');
});
}
line$.classList.toggle('selected');
if (line$.classList.contains('selected'))
line$.setAttribute('data-just-selected', 'true');
});
line$.addEventListener('click', function (event) {
if (!event.shiftKey) {
line$.classList.remove('selected');
line$.removeAttribute('data-just-selected');
if (!code$.classList.contains('has-selection')) {
var copyContent = line$.getAttribute('data-content') || '';
if (copyContent.trim().length > 0)
clipboard_1.copyToClipboard(copyContent, function () { return copy_confirm_1.copyConfirm(line$); });
}
}
});
});
code$.addEventListener('mouseup', function () {
setTimeout(function () {
if (code$.querySelectorAll('.selected').length > 0) {
document.querySelectorAll('pre>code').forEach(function (c$) {
if (c$ === code$)
return;
c$.classList.remove('has-selection');
c$.querySelectorAll('.selected').forEach(function (l$) { return l$.classList.remove('selected'); });
});
code$.classList.add('has-selection');
var start_1 = -1;
var end_1 = -1;
code$.querySelectorAll('div').forEach(function (line$, index) {
if (line$.hasAttribute('data-just-selected')) {
if (start_1 === -1)
start_1 = end_1 = index;
else
end_1 = index;
}
});
code$.querySelectorAll('div').forEach(function (line$, index) {
if (index >= start_1 && index <= end_1) {
line$.classList.add('selected');
line$.removeAttribute('data-just-selected');
}
});
}
else
code$.classList.remove('has-selection');
}, 5);
});
code$.addEventListener('mouseleave', function () {
var start = -1;
var end = -1;
code$.querySelectorAll('div').forEach(function (line$, index) {
if (line$.hasAttribute('data-just-selected')) {
if (start === -1)
start = end = index;
else
end = index;
}
});
code$.querySelectorAll('div').forEach(function (line$, index) {
if (index >= start && index <= end) {
line$.classList.add('selected');
line$.removeAttribute('data-just-selected');
}
});
});
code$.addEventListener('keydown', function (ev) {
var event = ev;
if (event.key === 'c' && (event.metaKey || event.ctrlKey)) {
if (code$.classList.contains('has-selection')) {
smart_copy_1.smartCopy(code$, true);
}
}
});
});
};
_exec();
window.addEventListener('navigation', _exec);
});
}
exports.codeSelection = codeSelection;
exports.codeSelection$ = transport_1.funcTransport(codeSelection);
//# sourceMappingURL=selection.js.map