Skip to content

Commit

Permalink
Update Tab Position
Browse files Browse the repository at this point in the history
  • Loading branch information
theimo1221 committed Dec 13, 2021
1 parent 884c017 commit 241e46a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/html-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,8 +929,10 @@ section.${c}>article { margin-bottom: auto; }
setTimeout(() => {
var paragraph = findParent<ParagraphElement>(elem, DomType.Paragraph);

if (paragraph?.tabs == null)
if (paragraph?.tabs == null) {
updateDefaultTabStop(tabSpan, this.document.settingsPart.settings.defaultTabStopWidth.value);
return;
}

paragraph.tabs.sort((a, b) => a.position.value - b.position.value);
updateTabStop(tabSpan, paragraph.tabs);
Expand Down
35 changes: 35 additions & 0 deletions src/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,39 @@ export function updateTabStop(elem: HTMLElement, tabs: ParagraphTab[], pixelToPo
elem.style.textDecoration = "underline";
break;
}
}

export function updateDefaultTabStop(elem: HTMLElement, tabDefaultPtWidth: number, iterations: number = 3) {
const pixelToPoint = 72 / 96;
const p = elem.closest("p");
const art = elem.closest("article");
const pMarginLeft = parseFloat(p.style.marginLeft);
const abb = art.getBoundingClientRect();
const pbb = p.getBoundingClientRect();
const tbb = elem.getBoundingClientRect();
let tabLeft = (tbb.x - pbb.x) * pixelToPoint;

let nextTabStopPosition: number = tabDefaultPtWidth;
if(!Number.isNaN(pMarginLeft) && tabLeft < 0) {
tabLeft = (tbb.x - abb.x) * pixelToPoint;
nextTabStopPosition = (pbb.x - abb.x) * pixelToPoint;
} else {
if(tabLeft < 0 || tabDefaultPtWidth < 0) {
return;
}
// +1 to avoid rounding errors.
while (nextTabStopPosition < tabLeft + 1) {
nextTabStopPosition += tabDefaultPtWidth;
}
}
const desiredWidth: number = nextTabStopPosition - tabLeft;
elem.style.display = "inline-block";
elem.style.width = `${desiredWidth}pt`;

// Changing the positions we might have to recalculate these one more time
if(iterations-- > 0) {
setTimeout(() => {
updateDefaultTabStop(elem, tabDefaultPtWidth, iterations);
}, 25);
}
}

0 comments on commit 241e46a

Please sign in to comment.