<commit_before>'use strict';

const HISTORY = JSON.parse(localStorage.getItem('history')) || [];
const SETTINGS = JSON.parse(localStorage.getItem('settings')) || {dice: 'New', dict: 'NWL', grade: 'C', display: 'Show'};

var STATE = null;
var RANDOM = null;
var TRAINING = null;
var DICT = null;
var TRIE = null;
var STATS = null;
var HASH_REFRESH = true;
var SEED = 0;
var ORIGINAL = {settings: Object.assign({}, SETTINGS), seed: SEED};
var LAST = '';
var LAST_DEFINITION = '';

let kept = false;

const TOUCH = ('ontouchstart' in window) ||
    (navigator.maxTouchPoints > 0) ||
    (navigator.msMaxTouchPoints > 0);

(async () => {
  setTimeout(() => window.scrollTo(0, 1), 0);

  // If theme has been explicitly set by the user then that trumps the system value
  if (SETTINGS.theme !== undefined) {
    setTheme(SETTINGS.theme);
  } else {
    setTheme(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'Dark' : 'Light');
  }

  const dict = await fetch('data/dict.json', {mode: 'no-cors'});
  const stats = await fetch('data/stats.json', {mode: 'no-cors'});
  DICT = await dict.json();
  STATS = new Stats(await stats.json(), DICT);
  TRIE = Trie.create(DICT);

  const initial = setup();
  SEED = initial.seed;
  Object.assign(SETTINGS, initial.settings);
  localStorage.setItem('settings', JSON.stringify(SETTINGS));

  STATE = refresh();

  document.getElementById('display').removeChild(document.getElementById('loader'));
  document.getElementById('game').classList.remove('hidden');

  const menu = document.getElementById('menu');
  const word = document.getElementById('word');
  const defn = document.getElementById('defn');
  if (!TOUCH) {
    word.contentEditable = true;
    permaFocus(word);
  }

  window.addEventListener('hashchange', e => {
    if (!document.location.hash) return;
    const [settings, seed] = Game.decodeID(document.location.hash.slice(1));
    if (!isNaN(seed)) {
      updateSettings(settings);
      SEED = seed;
    }
    if (HASH_REFRESH) {
      STATE = refresh();
    } else {
      updateDOMSettings();
    }
  });

  document.getElementById('refresh').addEventListener('mouseup', refreshClick);

  document.getElementById('timer').addEventListener('click', () => {
    STATE.timer.pause();
  });

  document.getElementById('score').addEventListener('long-press', e => {
    const size = STATE.game.size;
    const weights = [];
    for (let row = 0; row < size; row++) {
      const a = [];
      for (let col = 0; col < size; col++) {
        a.push(0);
      }
      weights.push(a);
    }
    let total = 0;
    for (const word in STATE.game.possible) {
      if (STATE.game.played[word]) continue;
      const score = Game.score(word);
      total += score;
      for (const p of STATE.game.possible[word]) {
        weights[p[1]][p[0]] += score;
      }
    }

    for (const td of document.getElementById('board').getElementsByTagName('td')) {
      const w = weights[Number(td.dataset.x)][Number(td.dataset.y)] / total;
      td.style.backgroundColor = `rgba(255,0,0,${w})`;
    }
  });

  document.getElementById('score').addEventListener('long-press-up', e => {
    for (const td of document.getElementById('board').getElementsByTagName('td')) {
      td.style.removeProperty('background-color');
    }
  });

  document.getElementById('practice').addEventListener('click', train);

  document.getElementById('play').addEventListener('click', () => {
    const game = document.getElementById('game');
    const board = document.getElementById('board');
    const wrapper = document.getElementById('wrapper');

    if (wrapper) game.removeChild(wrapper);

    board.classList.remove('hidden');
    word.classList.remove('hidden');
    if (!TOUCH) focusContentEditable(word);
    defn.classList.remove('hidden');

    updateVisibility({show: ['refresh', 'score', 'timer'], hide: ['back', 'play']});

    STATE = refresh();
  });

  document.getElementById('back').addEventListener('click', backToGame);

  // TODO: shouldnt work when in score mode or settings!
  document.addEventListener('keydown', e => {
    const board = document.getElementById('board');
    const settings = document.getElementById('settings');
    const define = document.getElementById('define');

    const isBoard = board && !board.classList.contains('hidden');
    const isSettings = settings && !settings.classList.contains('hidden');
    const isDefine = !!define;

    if (board) {
      if (kept) clearWord();
      focusContentEditable(word);
    } else if (isDefine) {
      focusContentEditable(document.getElementById('search'));
    }

    // TODO support 3/4/5 in settings mode
    const key = e.keyCode;
    if (key === 191 && e.shiftKey) {
      e.preventDefault(); 
      toggleDefine();
    } else if (key === 13 || key === 32) {
      e.preventDefault();
      if (board) {
        play(word);
        focusContentEditable(word);
      }
    } else if ((key < 65 || key > 90) && key !== 8) {
      e.preventDefault();
    }
  });

  document.addEventListener('swiped-left', toggleDefine);
  document.addEventListener('swiped-right', toggleDefine);

  function toggleDefine(e) {
    const display = document.getElementById('display');
    const game = document.getElementById('game');

    if (!game.classList.contains('hidden')) {
      const def = document.createElement('div');
      def.setAttribute('id', 'define');

      // TODO: save last search?
      const search = document.createElement('div');
      search.setAttribute('id', 'search');
      search.contentEditable = true;
      search.textContent = LAST_DEFINITION;

      def.appendChild(search);
      display.appendChild(def);

      const updateDetails = (word) => {
       if (DICT[word]) {
          const defn = getOrCreateElementById('defineDefinition', 'div');
          defn.textContent = define(word, DICT);
          if (DICT[word].dict && !DICT[word].dict.includes(SETTINGS.type)) {
            def.classList.add('hard');
          } else {
            def.classList.remove('hard');
          }
          def.appendChild(defn);
        } else {
          removeChildById(def, 'defineDefinition');
        }<commit_msg>Fix define search<commit_after>'use strict';

const HISTORY = JSON.parse(localStorage.getItem('history')) || [];
const SETTINGS = JSON.parse(localStorage.getItem('settings')) || {dice: 'New', dict: 'NWL', grade: 'C', display: 'Show'};

var STATE = null;
var RANDOM = null;
var TRAINING = null;
var DICT = null;
var TRIE = null;
var STATS = null;
var HASH_REFRESH = true;
var SEED = 0;
var ORIGINAL = {settings: Object.assign({}, SETTINGS), seed: SEED};
var LAST = '';
var LAST_DEFINITION = '';

let kept = false;

const TOUCH = ('ontouchstart' in window) ||
    (navigator.maxTouchPoints > 0) ||
    (navigator.msMaxTouchPoints > 0);

(async () => {
  setTimeout(() => window.scrollTo(0, 1), 0);

  // If theme has been explicitly set by the user then that trumps the system value
  if (SETTINGS.theme !== undefined) {
    setTheme(SETTINGS.theme);
  } else {
    setTheme(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'Dark' : 'Light');
  }

  const dict = await fetch('data/dict.json', {mode: 'no-cors'});
  const stats = await fetch('data/stats.json', {mode: 'no-cors'});
  DICT = await dict.json();
  STATS = new Stats(await stats.json(), DICT);
  TRIE = Trie.create(DICT);

  const initial = setup();
  SEED = initial.seed;
  Object.assign(SETTINGS, initial.settings);
  localStorage.setItem('settings', JSON.stringify(SETTINGS));

  STATE = refresh();

  document.getElementById('display').removeChild(document.getElementById('loader'));
  document.getElementById('game').classList.remove('hidden');

  const menu = document.getElementById('menu');
  const word = document.getElementById('word');
  const defn = document.getElementById('defn');
  if (!TOUCH) {
    word.contentEditable = true;
    permaFocus(word);
  }

  window.addEventListener('hashchange', e => {
    if (!document.location.hash) return;
    const [settings, seed] = Game.decodeID(document.location.hash.slice(1));
    if (!isNaN(seed)) {
      updateSettings(settings);
      SEED = seed;
    }
    if (HASH_REFRESH) {
      STATE = refresh();
    } else {
      updateDOMSettings();
    }
  });

  document.getElementById('refresh').addEventListener('mouseup', refreshClick);

  document.getElementById('timer').addEventListener('click', () => {
    STATE.timer.pause();
  });

  document.getElementById('score').addEventListener('long-press', e => {
    const size = STATE.game.size;
    const weights = [];
    for (let row = 0; row < size; row++) {
      const a = [];
      for (let col = 0; col < size; col++) {
        a.push(0);
      }
      weights.push(a);
    }
    let total = 0;
    for (const word in STATE.game.possible) {
      if (STATE.game.played[word]) continue;
      const score = Game.score(word);
      total += score;
      for (const p of STATE.game.possible[word]) {
        weights[p[1]][p[0]] += score;
      }
    }

    for (const td of document.getElementById('board').getElementsByTagName('td')) {
      const w = weights[Number(td.dataset.x)][Number(td.dataset.y)] / total;
      td.style.backgroundColor = `rgba(255,0,0,${w})`;
    }
  });

  document.getElementById('score').addEventListener('long-press-up', e => {
    for (const td of document.getElementById('board').getElementsByTagName('td')) {
      td.style.removeProperty('background-color');
    }
  });

  document.getElementById('practice').addEventListener('click', train);

  document.getElementById('play').addEventListener('click', () => {
    const game = document.getElementById('game');
    const board = document.getElementById('board');
    const wrapper = document.getElementById('wrapper');

    if (wrapper) game.removeChild(wrapper);

    board.classList.remove('hidden');
    word.classList.remove('hidden');
    if (!TOUCH) focusContentEditable(word);
    defn.classList.remove('hidden');

    updateVisibility({show: ['refresh', 'score', 'timer'], hide: ['back', 'play']});

    STATE = refresh();
  });

  document.getElementById('back').addEventListener('click', backToGame);

  // TODO: shouldnt work when in score mode or settings!
  document.addEventListener('keydown', e => {
    const board = document.getElementById('board');
    const settings = document.getElementById('settings');
    const define = document.getElementById('define');

    const isBoard = board && !board.classList.contains('hidden');
    const isSettings = settings && !settings.classList.contains('hidden');
    const isDefine = !!define;

    if (board) {
      if (kept) clearWord();
      focusContentEditable(word);
    } else if (isDefine) {
      focusContentEditable(document.getElementById('search'));
    }

    // TODO support 3/4/5 in settings mode
    const key = e.keyCode;
    if (key === 191 && e.shiftKey) {
      e.preventDefault(); 
      toggleDefine();
    } else if (key === 13 || key === 32) {
      e.preventDefault();
      if (board) {
        play(word);
        focusContentEditable(word);
      }
    } else if ((key < 65 || key > 90) && key !== 8) {
      e.preventDefault();
    }
  });

  document.addEventListener('swiped-left', toggleDefine);
  document.addEventListener('swiped-right', toggleDefine);

  function toggleDefine(e) {
    const display = document.getElementById('display');
    const game = document.getElementById('game');

    if (!game.classList.contains('hidden')) {
      const def = document.createElement('div');
      def.setAttribute('id', 'define');

      // TODO: save last search?
      const search = document.createElement('div');
      search.setAttribute('id', 'search');
      search.contentEditable = true;
      search.textContent = LAST_DEFINITION;

      def.appendChild(search);
      display.appendChild(def);

      const updateDetails = (word) => {
       if (DICT[word]) {
          const defn = getOrCreateElementById('defineDefinition', 'div');
          defn.textContent = define(word, DICT);
          if (DICT[word].dict && !DICT[word].dict.includes(SETTINGS.dict.charAt(0))) {
            def.classList.add('hard');
          } else {
            def.classList.remove('hard');
          }
          def.appendChild(defn);
        } else {
          removeChildById(def, 'defineDefinition');
        }
{'fraction_non_alphanumeric': 0.1642312063646696, 'fraction_numerical': 0.005439194674460139, 'mean_word_length': 3.3965024982155603, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '451202', 'n_tokens_mistral': 3738, 'n_tokens_neox': 3634, 'n_words': 794}