UNPKG

isomorphic-qwerty

Version:

Isomorphic coordinate-system for the QWERTY keyboard with sustain using the Shift key

56 lines 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pianoMap = void 0; const coordinates_1 = require("./coordinates"); /** * Convert a linear sequence of note types into a piano-style layout * @param ys Desired vertical coordinates for the notes/keys * @param shifts Adjustments to the first three rows. A shift difference of 1 puts a row behind the next row. Defaults to `[0, 0, 0]` so that the all rows are ahead of the rows below. * @returns `indexByCode`: a `Map` instance from keycodes to note indices. `coordsByIndex`: location of each note on layer 1 (the one with QWERTY/ASDF keys). */ function pianoMap(ys, shifts) { var _a, _b, _c; // We skip Backquote because it's not connected to KeyQ. const nextXs = [0, 0, 0, -1]; shifts !== null && shifts !== void 0 ? shifts : (shifts = [0, 0, 0]); (_a = shifts[0]) !== null && _a !== void 0 ? _a : (shifts[0] = 0); (_b = shifts[1]) !== null && _b !== void 0 ? _b : (shifts[1] = 0); (_c = shifts[2]) !== null && _c !== void 0 ? _c : (shifts[2] = 0); const coordss = []; for (const y of ys) { const x = nextXs[y]; coordss.push([x, y, 1]); nextXs[y]++; // == Sync everything vertically // A is before Z. S is after Z. nextXs[2] = Math.max(nextXs[2], nextXs[3] - shifts[2]); // Q before A. W is after A. nextXs[1] = Math.max(nextXs[1], nextXs[2] - shifts[1]); // 1 is befor Q. 2 is after Q. nextXs[0] = Math.max(nextXs[0], nextXs[1] - shifts[0]); // Sync the other way too but with less "force". nextXs[1] = Math.max(nextXs[1], nextXs[0] + shifts[0] - 1); nextXs[2] = Math.max(nextXs[2], nextXs[1] + shifts[1] - 1); nextXs[3] = Math.max(nextXs[3], nextXs[2] + shifts[1] - 1); } const indexByCode = new Map(); const coordsByIndex = []; let i = 0; for (const xyz of coordss) { const code = (0, coordinates_1.codeByCoords)(xyz); if (code === undefined) { coordsByIndex.push(undefined); } else { indexByCode.set(code, i); coordsByIndex.push(xyz); } i++; } return { coordsByIndex, indexByCode, }; } exports.pianoMap = pianoMap; //# sourceMappingURL=piano.js.map