UNPKG

suranadira-keyboard

Version:

A component for displaying Suranadira texts.

55 lines (50 loc) 1.59 kB
import suranadiraCore from "../../suranadira-core"; const words = suranadiraCore.suranadiraWords.suranadiraWords; // Get initial layout values function _getInitialLayout(truthValue) { let values = new Set(); for (let word in words) { if (words[word].values[0] === truthValue) { values.add(words[word].elements[0][0]); } } return values; } // Get next layout const _getNextLayout = composition => { if (composition.length > 3) return; let layout = new Set(); let t = composition; let tp = JSON.stringify(t); let len = composition.length; let a = []; for (let word in words) { for (let n = 0; n < len; n++) { a.push(words[word].elements[n][0]); } if (tp === JSON.stringify(a)) { layout.add(words[word].elements[len][0]); } a = []; } return layout; }; // Update the composition according to the new key ID // return 1) the new layout and 2) the updated composition export const updateComposition = (keyID, position, layout, composition) => { if (keyID === "") return; if (position > -1) { // Get keyboard layout by composition let index = parseInt(keyID, 10); let a = Array.from(layout); if (index > a.length - 1) return false; composition.push(a[index]); layout = _getNextLayout(composition); } else { // Get initial keyboard layout by truth value let truthValue = parseInt(keyID, 10); if (truthValue !== 0 && truthValue !== 1) return false; layout = _getInitialLayout(truthValue); } return [layout, composition]; };