Learn: section actions on the title row, scrollable tables, copy code
This commit is contained in:
@@ -123,17 +123,22 @@
|
||||
autoExpand(hoverHost(target));
|
||||
}
|
||||
|
||||
/* Пока тянем, раскрываем закрытую главу/раздел под курсором, чтобы внутрь можно было бросить. */
|
||||
/* Пока тянем, раскрываем свёрнутый раздел под курсором, чтобы внутрь можно было бросить. */
|
||||
function hoverHost(target) {
|
||||
return target.closest('details[data-chapter-id], details[data-section-id]');
|
||||
return target.closest('.sub-acc[data-section-id]');
|
||||
}
|
||||
|
||||
function autoExpand(host) {
|
||||
if (!host || host.open || host === state.element) return;
|
||||
if (!host || host === state.element) return;
|
||||
if (!host.classList.contains('collapsed')) return;
|
||||
if (hoverTarget === host) return;
|
||||
hoverTarget = host;
|
||||
clearTimeout(hoverTimer);
|
||||
hoverTimer = setTimeout(function () { host.open = true; }, 600);
|
||||
hoverTimer = setTimeout(function () {
|
||||
host.classList.remove('collapsed');
|
||||
var toggle = host.querySelector('.sub-toggle');
|
||||
if (toggle) toggle.setAttribute('aria-expanded', 'true');
|
||||
}, 600);
|
||||
}
|
||||
|
||||
function onDrop(event) {
|
||||
|
||||
80
web/src/main/resources/static/js/md-content.js
Normal file
80
web/src/main/resources/static/js/md-content.js
Normal file
@@ -0,0 +1,80 @@
|
||||
/* Блоки кода в материалах: кнопка «Копировать» в углу и копирование по клику на блок. */
|
||||
(function () {
|
||||
function fallbackCopy(text) {
|
||||
var area = document.createElement('textarea');
|
||||
area.value = text;
|
||||
area.setAttribute('readonly', '');
|
||||
area.style.position = 'fixed';
|
||||
area.style.top = '-1000px';
|
||||
document.body.appendChild(area);
|
||||
area.select();
|
||||
try { document.execCommand('copy'); } catch (e) { /* игнорируем */ }
|
||||
document.body.removeChild(area);
|
||||
}
|
||||
|
||||
function copyText(text) {
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
return navigator.clipboard.writeText(text).catch(function () { fallbackCopy(text); });
|
||||
}
|
||||
fallbackCopy(text);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
function codeText(pre) {
|
||||
var code = pre.querySelector('code');
|
||||
if (code) return code.innerText.replace(/\n+$/, '');
|
||||
var clone = pre.cloneNode(true);
|
||||
clone.querySelectorAll('.md-copy').forEach(function (btn) { btn.remove(); });
|
||||
return clone.textContent.replace(/\n+$/, '');
|
||||
}
|
||||
|
||||
function flash(btn) {
|
||||
if (btn.dataset.busy) return;
|
||||
btn.dataset.busy = '1';
|
||||
var original = btn.textContent;
|
||||
btn.textContent = 'Скопировано';
|
||||
btn.classList.add('copied');
|
||||
setTimeout(function () {
|
||||
btn.textContent = original;
|
||||
btn.classList.remove('copied');
|
||||
delete btn.dataset.busy;
|
||||
}, 1200);
|
||||
}
|
||||
|
||||
function enhance(pre) {
|
||||
if (pre.dataset.copyReady) return;
|
||||
pre.dataset.copyReady = '1';
|
||||
|
||||
var wrap = document.createElement('div');
|
||||
wrap.className = 'md-code-wrap';
|
||||
pre.parentNode.insertBefore(wrap, pre);
|
||||
wrap.appendChild(pre);
|
||||
|
||||
var btn = document.createElement('button');
|
||||
btn.type = 'button';
|
||||
btn.className = 'md-copy';
|
||||
btn.textContent = 'Копировать';
|
||||
btn.title = 'Скопировать код';
|
||||
btn.setAttribute('aria-label', 'Скопировать код');
|
||||
btn.addEventListener('click', function (event) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
copyText(codeText(pre)).then(function () { flash(btn); });
|
||||
});
|
||||
wrap.appendChild(btn);
|
||||
|
||||
pre.addEventListener('click', function () {
|
||||
copyText(codeText(pre)).then(function () { flash(btn); });
|
||||
});
|
||||
}
|
||||
|
||||
function ready() {
|
||||
document.querySelectorAll('.md-body pre').forEach(enhance);
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', ready);
|
||||
} else {
|
||||
ready();
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user