Skip to content

Commit

Permalink
Deploying to gh-pages from @ 5ea95f8 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzokuken committed Nov 6, 2024
1 parent 0ade0e0 commit e197cd1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
5 changes: 5 additions & 0 deletions ecmarkup.css
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ body.oldtoc {

span[aria-hidden='true'] {
font-size: 0;
white-space: pre;
}

a {
Expand Down Expand Up @@ -222,6 +223,10 @@ var.field {
font: inherit;
color: inherit;
}
/* suppress line break opportunities between `.` and `[[FieldName]]` */
var.field::before {
content: '\2060'
}

var.referenced {
color: inherit;
Expand Down
54 changes: 52 additions & 2 deletions ecmarkup.js
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,7 @@ const counterByDepth = [];
function addStepNumberText(
ol,
depth = 0,
indent = '',
special = [...ol.classList].some(c => c.startsWith('nested-')),
) {
let counter = !special && counterByDepth[depth];
Expand All @@ -1461,16 +1462,19 @@ function addStepNumberText(
let i = (Number(ol.getAttribute('start')) || 1) - 1;
for (const li of ol.children) {
const marker = document.createElement('span');
marker.textContent = `${i < cache.length ? cache[i] : getTextForIndex(i)}. `;
const markerText = i < cache.length ? cache[i] : getTextForIndex(i);
const extraIndent = ' '.repeat(markerText.length + 2);
marker.textContent = `${indent}${markerText}. `;
marker.setAttribute('aria-hidden', 'true');
marker.setAttribute('class', 'list-marker');
const attributesContainer = li.querySelector('.attributes-tag');
if (attributesContainer == null) {
li.prepend(marker);
} else {
attributesContainer.insertAdjacentElement('afterend', marker);
}
for (const sublist of li.querySelectorAll(':scope > ol')) {
addStepNumberText(sublist, depth + 1, special);
addStepNumberText(sublist, depth + 1, indent + extraIndent, special);
}
i++;
}
Expand All @@ -1482,6 +1486,52 @@ document.addEventListener('DOMContentLoaded', () => {
});
});

// Omit indendation when copying a single algorithm step.
document.addEventListener('copy', evt => {
// Construct a DOM from the selection.
const doc = document.implementation.createHTMLDocument('');
const domRoot = doc.createElement('div');
const html = evt.clipboardData.getData('text/html');
if (html) {
domRoot.innerHTML = html;
} else {
const selection = getSelection();
const singleRange = selection?.rangeCount === 1 && selection.getRangeAt(0);
const container = singleRange?.commonAncestorContainer;
if (!container?.querySelector?.('.list-marker')) {
return;
}
domRoot.append(singleRange.cloneContents());
}

// Preserve the indentation if there is no hidden list marker, or if selection
// of more than one step is indicated by either multiple such markers or by
// visible text before the first one.
const listMarkers = domRoot.querySelectorAll('.list-marker');
if (listMarkers.length !== 1) {
return;
}
const treeWalker = document.createTreeWalker(domRoot, undefined, {
acceptNode(node) {
return node.nodeType === Node.TEXT_NODE || node === listMarkers[0]
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_SKIP;
},
});
while (treeWalker.nextNode()) {
const node = treeWalker.currentNode;
if (node.nodeType === Node.ELEMENT_NODE) break;
if (/\S/u.test(node.data)) return;
}

// Strip leading indentation from the plain text representation.
evt.clipboardData.setData('text/plain', domRoot.textContent.trimStart());
if (!html) {
evt.clipboardData.setData('text/html', domRoot.innerHTML);
}
evt.preventDefault();
});

'use strict';

// Update superscripts to not suffer misinterpretation when copied and pasted as plain text.
Expand Down
8 changes: 4 additions & 4 deletions index.html

Large diffs are not rendered by default.

0 comments on commit e197cd1

Please sign in to comment.