okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221merge f-split-ui4 a7cd2b38e into f-split-int7c4b3d743

main
1.8 KiB29 linesraw
1/** Capture a complete view, including external SVG sprite symbols. */
2export async function monitorPng(element: HTMLElement) {
3  await document.fonts.ready;
4  const [{ toPng }, spriteResponse] = await Promise.all([import('html-to-image'), fetch('/icons/sprite.svg')]);
5  if (!spriteResponse.ok) throw new Error('Screenshot icons could not be loaded. Try again.');
6  const sprite = new DOMParser().parseFromString(await spriteResponse.text(), 'image/svg+xml');
7  const clone = element.cloneNode(true) as HTMLElement;
8  clone.style.width = `${element.getBoundingClientRect().width}px`;
9  clone.style.position = 'fixed'; clone.style.left = '-100000px'; clone.style.top = '0';
10  clone.setAttribute('aria-hidden', 'true');
11  // What is only live state (the model state and the conversation cache) is not part of the picture.
12  clone.querySelectorAll('[data-snapshot-skip]').forEach(node => node.remove());
13  for (const use of clone.querySelectorAll('svg use')) {
14    const id = use.getAttribute('href')?.split('#')[1];
15    const symbol = id && sprite.getElementById(id), svg = use.parentElement;
16    if (!symbol || !svg) continue;
17    const group = document.createElementNS('http://www.w3.org/2000/svg', 'g');
18    for (const attr of symbol.attributes) {
19      if (attr.name === 'viewBox') svg.setAttribute(attr.name, attr.value);
20      else if (attr.name !== 'id') group.setAttribute(attr.name, attr.value);
21    }
22    for (const child of symbol.childNodes) group.appendChild(child.cloneNode(true));
23    use.replaceWith(group);
24  }
25  document.body.appendChild(clone);
26  try {
27    return await toPng(clone, { pixelRatio: 2, backgroundColor: getComputedStyle(document.documentElement).getPropertyValue('--st-surface').trim(), style: { position: 'static', left: 'auto', top: 'auto', margin: '0' } });
28  } finally { clone.remove(); }
29}