<commit_before>import html2canvas from 'html2canvas';

/**
 * Builds a subheader to be shown below the chart title, containing the scenario dataset, end year,
 * and title.
 */
const buildScenarioInfo = (): HTMLHeadingElement => {
  const header = document.createElement('h4');
  header.textContent = `${App.settings.get('area_name')} ${App.settings.get('end_year')}`;

  const scenarioName = App.settings.get('active_scenario_title') as string | null;

  if (scenarioName) {
    const title = document.createElement('span');
    title.classList.add('scenario-title');
    title.textContent = scenarioName;

    header.append(title);
  }

  return header;
};

/**
 * Saves an HTMLDivElement - which contains a chart - as a PNG.
 */
const saveAsPNG = (holder: HTMLDivElement): Promise<HTMLCanvasElement> => {
  const title = holder.querySelector('header h3').textContent;

  // Clone the chart - rather than operating directly on the visible chart - as this allows us to
  // adjust styles (add watermark, remove shadows) without the user noticing the changes to the
  // on-screen chart.
  const clone = holder.cloneNode(true) as HTMLDivElement;
  clone.removeAttribute('data-holder_id');
  clone.classList.add('html2canvas');

  holder.parentNode.insertBefore(clone, holder.nextSibling);

  clone.querySelector('header').append(buildScenarioInfo());

  const promise = html2canvas(clone, { scale: 2, scrollX: 0, scrollY: -window.scrollY });

  promise.then((canvas) => {
    clone.remove();

    const link = document.createElement('a');

    link.download = title;<commit_msg>Save as PNG accounts for horizontal scroll<commit_after>import html2canvas from 'html2canvas';

/**
 * Builds a subheader to be shown below the chart title, containing the scenario dataset, end year,
 * and title.
 */
const buildScenarioInfo = (): HTMLHeadingElement => {
  const header = document.createElement('h4');
  header.textContent = `${App.settings.get('area_name')} ${App.settings.get('end_year')}`;

  const scenarioName = App.settings.get('active_scenario_title') as string | null;

  if (scenarioName) {
    const title = document.createElement('span');
    title.classList.add('scenario-title');
    title.textContent = scenarioName;

    header.append(title);
  }

  return header;
};

/**
 * Saves an HTMLDivElement - which contains a chart - as a PNG.
 */
const saveAsPNG = (holder: HTMLDivElement): Promise<HTMLCanvasElement> => {
  const title = holder.querySelector('header h3').textContent;

  // Clone the chart - rather than operating directly on the visible chart - as this allows us to
  // adjust styles (add watermark, remove shadows) without the user noticing the changes to the
  // on-screen chart.
  const clone = holder.cloneNode(true) as HTMLDivElement;
  clone.removeAttribute('data-holder_id');
  clone.classList.add('html2canvas');

  holder.parentNode.insertBefore(clone, holder.nextSibling);

  clone.querySelector('header').append(buildScenarioInfo());

  const promise = html2canvas(clone, {
    scale: 2,
    scrollX: 0,
    scrollX: -window.scrollX,
    scrollY: -window.scrollY,
  });

  promise.then((canvas) => {
    clone.remove();

    const link = document.createElement('a');

    link.download = title;
{'fraction_non_alphanumeric': 0.12786377708978328, 'fraction_numerical': 0.004953560371517028, 'mean_word_length': 4.253658536585366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5437265', 'n_tokens_mistral': 950, 'n_tokens_neox': 935, 'n_words': 257}