Fix dialog spacing and mini refactor

This commit is contained in:
Hamcha 2020-06-18 11:49:48 +02:00
parent 377a549d17
commit 1591dc013b
Signed by untrusted user: hamcha
GPG Key ID: 41467804B19A3315
2 changed files with 37 additions and 13 deletions

View File

@ -1,5 +1,6 @@
import { darken, ColorFmt, lighten } from "./darkmode"; import { darken, ColorFmt, lighten } from "./darkmode";
import searchBox from "./search"; import searchBox from "./search";
import { findParent } from "./utils";
export default function userscript(root: HTMLElement, docname: string): void { export default function userscript(root: HTMLElement, docname: string): void {
root.querySelectorAll(".mw-editsection").forEach((editLink) => { root.querySelectorAll(".mw-editsection").forEach((editLink) => {
@ -45,6 +46,15 @@ export default function userscript(root: HTMLElement, docname: string): void {
} }
}); });
// Fixup spacing on top quotes
root
.querySelectorAll<HTMLImageElement>("table .floatright > a > img")
.forEach((img) => {
const row = findParent(img, (el) => el instanceof HTMLTableRowElement);
const td = document.createElement("td");
row.appendChild(td);
});
// Group headers and content so stickies don't overlap // Group headers and content so stickies don't overlap
root.querySelectorAll("h3,h2").forEach((h3) => { root.querySelectorAll("h3,h2").forEach((h3) => {
const parent = h3.parentNode; const parent = h3.parentNode;
@ -60,17 +70,16 @@ export default function userscript(root: HTMLElement, docname: string): void {
div.className = "mw-headline-cont"; div.className = "mw-headline-cont";
}); });
// Move id from header to container, if one is found
root.querySelectorAll<HTMLElement>(".mw-headline").forEach((span) => { root.querySelectorAll<HTMLElement>(".mw-headline").forEach((span) => {
// Find nearest container // Find nearest container
let parent = span.parentElement; const container = findParent(span, (el) =>
while (parent !== null) { el.classList.contains("mw-headline-cont")
if (parent.classList.contains("mw-headline-cont")) { );
parent.id = span.id; if (container) {
span.id += "-span"; container.id = span.id;
parent.dataset.name = span.innerText; span.id += "-span";
return; container.dataset.name = span.innerText;
}
parent = parent.parentElement;
} }
}); });
@ -216,10 +225,10 @@ export default function userscript(root: HTMLElement, docname: string): void {
root root
.querySelectorAll<HTMLElement>("div[data-name] .wikitable.sortable tr") .querySelectorAll<HTMLElement>("div[data-name] .wikitable.sortable tr")
.forEach((row) => { .forEach((row) => {
let sectionEl = row.parentElement; const sectionEl = findParent(
while (!sectionEl.dataset.name) { row,
sectionEl = sectionEl.parentElement; (sel) => "name" in sel.dataset && sel.dataset.name !== ""
} );
const section = sectionEl.dataset.name; const section = sectionEl.dataset.name;
if (row.querySelector("td") === null) { if (row.querySelector("td") === null) {
// Remove unused rows if found // Remove unused rows if found

15
lib/utils.ts Normal file
View File

@ -0,0 +1,15 @@
export function findParent(
base: HTMLElement,
matchFn: (candidate: HTMLElement) => boolean
): HTMLElement | null {
let parent = base.parentElement;
while (parent != null) {
if (matchFn(parent)) {
break;
}
parent = parent.parentElement;
}
return parent;
}
export default { findParent };