Deepseek's Final Solution to the Truncation Question
document.addEventListener('DOMContentLoaded', function() {
const originalNameLinks = document.querySelectorAll('.originalNameLink:not([data-processed])');
originalNameLinks.forEach(link => {
link.dataset.processed = 'true';
const fullName = link.getAttribute('download');
if (!fullName) return;
const lastDot = fullName.lastIndexOf('.');
if (lastDot === -1) return;
const nameWithoutExt = fullName.substring(0, lastDot);
const extension = fullName.substring(lastDot);
if (!link.hasAttribute('title')) {
link.setAttribute('title', fullName);
}
const displaySpan = document.createElement('span');
displaySpan.style.display = 'inline-flex';
displaySpan.style.maxWidth = '100%';
displaySpan.style.overflow = 'hidden';
const nameSpan = document.createElement('span');
nameSpan.style.overflow = 'hidden';
nameSpan.style.textOverflow = 'ellipsis';
nameSpan.style.whiteSpace = 'nowrap';
nameSpan.style.flexShrink = '1';
nameSpan.style.minWidth = '0';
nameSpan.textContent = nameWithoutExt;
nameSpan.dataset.fullName = nameWithoutExt;
const extSpan = document.createElement('span');
extSpan.style.flexShrink = '0';
extSpan.style.whiteSpace = 'nowrap';
extSpan.textContent = extension;
displaySpan.appendChild(nameSpan);
displaySpan.appendChild(extSpan);
link.innerHTML = '';[Expand Post]
link.appendChild(displaySpan);
updateTruncation(link);
});
document.querySelectorAll('details').forEach(details => {
details.addEventListener('toggle', function() {
setTimeout(() => {
const link = this.querySelector('.originalNameLink');
if (link) updateTruncation(link);
}, 10);
});
});
function updateTruncation(link) {
const displaySpan = link.querySelector('span');
if (!displaySpan) return;
const nameSpan = displaySpan.querySelector('span[data-full-name]');
const extSpan = displaySpan.querySelector('span:last-child');
if (!nameSpan || !extSpan) return;
const fullName = nameSpan.dataset.fullName;
if (!fullName) return;
nameSpan.textContent = fullName;
const isExpanded = link.closest('.expandedCell');
const parentContainer = link.closest('.uploadDetails');
if (!parentContainer) return;
const siblings = Array.from(parentContainer.children);
const linkIndex = siblings.indexOf(link);
let availableSpace = parentContainer.offsetWidth;
for (let i = 0; i < linkIndex; i++) {
const sibling = siblings[i];
if (sibling !== link) {
availableSpace -= sibling.offsetWidth + 5;
}
}
const extWidth = extSpan.offsetWidth;
const availableWidth = availableSpace - extWidth - 10;
if (nameSpan.offsetWidth <= availableWidth) {
return;
}
const charWidth = nameSpan.offsetWidth / fullName.length;
let maxChars = Math.floor(availableWidth / charWidth);
maxChars = Math.max(5, maxChars - 1);
if (maxChars < fullName.length) {
nameSpan.textContent = fullName.substring(0, maxChars) + '…';
}
}
setTimeout(() => {
document.querySelectorAll('.originalNameLink[data-processed]').forEach(link => {
updateTruncation(link);
});
}, 100);
});