Home

Awesome

kle-serial

Build Status Coverage Status npm version Dependency Status GitHub

This is a MIT-licensed javascript library for parsing the serialized format used on keyboard-layout-editor.com (KLE) and converting it into something that is easier to understand and use in third-party applications.

KLE is frequently used to prototype and generate a rough keyboard layout, that is then used by other applications to create plates, circuit boards, etc. These third-party applications currently use their own parsing logic.

Unfortunately, the KLE format was designed to be compact (due to some original limitations), and the format has evolved considerably from its original versions. As a result, third-party parsing implementations aren't always 100% compatible with KLE itself, particularly with respect to certain corner-cases or older / deprecated properties.

This library is the same code that KLE itself uses to parse serialized layouts, so by using it, you can be sure that you are 100% compatible with the editor.

Installation

Install the package via NPM:

npm install @ijprest/kle-serial --save

Usage

var kle = require("@ijprest/kle-serial");

var keyboard = kle.Serial.deserialize([
  { name: "Sample", author: "Your Name" },
  ["Q", "W", "E", "R", "T", "Y"]
]);

// or

var keyboard = kle.Serial.parse(`[
  { name: "Sample", author: "Your Name" },
  ["Q", "W", "E", "R", "T", "Y"]
]`);

API

kle.Serial.deserialize(rows: Array<any>): Keyboard
kle.Serial.parse(json5: string): Keyboard

Keyboard Objects

class Keyboard {
  meta: KeyboardMetadata;
  keys: Key[];
}

A Keyboard is an object containg keyboard metadata (meta) and an array of keys.

Keyboard Metadata

The meta object contains several fields:

class KeyboardMetadata {
  author: string;
  backcolor: string;
  background: { name: string; style: string } | null;
  name: string;
  notes: string;
  radii: string;
  switchBrand: string;
  switchMount: string;
  switchType: string;
}

Keys

Each key in the keys array contains the following data:

export class Key {
  color: string;
  labels: string[];
  textColor: Array<string | undefined>;
  textSize: Array<number | undefined>;
  default: { textColor: string; textSize: number };

  x: number;
  y: number;
  width: number;
  height: number;

  x2: number;
  y2: number;
  width2: number;
  height2: number;

  rotation_x: number;
  rotation_y: number;
  rotation_angle: number;

  decal: boolean;
  ghost: boolean;
  stepped: boolean;
  nub: boolean;

  profile: string;

  sm: string; // switch mount
  sb: string; // switch brand
  st: string; // switch type
}

Future Work

In rough order of priority:

  1. This library is based on the original KLE code, but it has been converted to a TypeScript and modularized to make it convenient for others to consume; the KLE site itself is not yet using this actual code.
    • So the first order of business is to update KLE to use this exact NPM module.
    • That will ensure that the code is correct, and that nothing has been missed, as well as guarantee that the two projects are kept in sync.
  2. This library currently only handles deserialization; the serialization code still needs to be ported.
  3. More tests (particularly on the serialization side, once it's ported; it's much more error-prone than deserialization).
  4. Migrate some of the supporting data from KLE to this project, so you don't have to look it up elsewhere, e.g.:
    • Switch mount / brand / type definitions.
    • Color palettes.
  5. Migrate HTML key rendering templates (and supporting stylesheets) from KLE to this project, so anyone can render a key identically to KLE.

Tests

npm test