Skip to content

Commit

Permalink
docs: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
eyuelberga committed Jun 27, 2021
1 parent b073ef6 commit 44cc16c
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ The aim of this project is to enable web developers to add support for typing no

**What does Keywrite do?**

1. It allows you to map certain keyboard inputs to characters, which you define in a keyboard layout configuration file.
1. It binds with web input controls to add multi-lingual typing to your website
1. It allows you to map keyboard inputs to symbols, which you define in a keyboard layout configuration file.
1. It binds with web input controls to add multi-lingual typing to your web application

**Things you can build with Keywrite**

Expand Down
8 changes: 7 additions & 1 deletion docs/advanced_usage.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Advanced Features
# Advanced Usage

## Extending the core package

By extending the exported class from `@keywrite/core` you can add
app-specific logic. The most important method in the core class would be the
`write` method. This method takes in a character and resolves it into a special
symbol as per the layout specification and returns the symbol and replacement
state (if previous character needs to be replaced with the new symbol).
6 changes: 2 additions & 4 deletions docs/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ myKeywriteInstance.on = true;

It is possible to add more than one keyboard layout to an instance. This could
be useful if you want users to pick from a selection of
input methods.

multiple layout definitions can be added during initialization.
input methods. Multiple layout definitions can be added during initialization.

```javascript
// keyboard layout definitions
Expand All @@ -68,7 +66,7 @@ const myKeywriteInstance = new KeywriteWeb(document.querySelector('input'), {
});
```

You switch between the layouts by setting the `current` value in the instance to
You can switch between the layouts by setting the `current` value in the instance to
the key name of the layout you want.

```javascript
Expand Down
8 changes: 0 additions & 8 deletions docs/custom_layout.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Examples
188 changes: 185 additions & 3 deletions docs/keyboard_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,191 @@ A keyboard layout maps keyboard inputs to symbols. It allows us to define key co
produce a certain symbol. It is generally a JavaScript object with an
input character, an output value or a next value.

A simple keyboard layouts looks something like this:

```javascript
const myLayout = {
a: { value: '', next: null },
e: {
value: '',
next: {
e: {
value: '',
next: null,
},
},
},
};
```

In the above layout, the `a` keyboard input is mapped to the `` symbol.
Pressing `e` will output ``, pressing `e` again will output ``.

## Components of a keyboard layout

A keyboard layout has two basic components:

1. Symbol map
1. Layout
1. **SymbolMap:** object with a `value` and `next` keys.
1. **KeyboardLayout:** object mapping an input key to a symbol map object

Keyboard layout is a nested structure of these two components. Lets see this
components in more detail.

The interface for `SymbolMap` and `KeyboardLayout` is shown below:

```typescript
interface SymbolMap {
value: string | null;
next: KeyboardLayout | null;
}

type KeyboardLayout = Record<string, SymbolMap>;
```

`value` represents the output symbol and `next` represents the next keyboard
layout definition.

From our previous example, the symbol map for the `a` key is this:

```javascript
const symbolMapA = { value: '', next: null };
```

The keyboard layout is as follows:

```javascript
const layoutA = { a: { value: '', next: null } };
```

`layoutA` is a valid layout that you can provide to a Keywrite instance.
But to build a more complete keyboard layout, you can add additional input key
and symbol map pairs.

```javascript
const layoutWithMoreInputKeys = {
a: { value: '', next: null },
b: { value: '', next: null },
c: { value: '', next: null },
d: { value: '', next: null },
};
```

Our inputs and outputs for the above layout are as follows:

| Input Key | Output Symbol |
| --------- | ------------- |
| `a` ||
| `b` ||
| `c` ||
| `d` ||

We would also need to map certain combination of key inputs to a single character.
We can do that by nesting additional layouts in the symbol map `next` field.

```javascript
const myLayout = {
a: {
value: '',
next: null,
},
b: {
value: '',
next: {
l: {
value: '',
next: null,
},
r: {
value: '',
next: null,
},
},
},
c: {
value: '',
next: {
n: {
value: '',
next: {
u: {
value: '',
next: null,
},
},
},
u: {
value: '',
next: {
n: {
value: '',
next: null,
},
},
},
l: {
value: '',
next: {
n: {
value: '',
next: {
u: {
value: '',
next: null,
},
},
},
u: {
value: '',
next: {
n: {
value: '',
next: null,
},
},
},
},
},
},
},
d: { value: '', next: null },
};
```

Now our input output table looks like this:

| Input Key | Output Symbol |
| --------------------- | ------------- |
| `a` ||
| `b` ||
| `b` + `l` ||
| `b` + `r` ||
| `c` ||
| `c` + `u` ||
| `c` + `u` + `n` ||
| `c` + `n` ||
| `c` + `n` + `u` ||
| `c` + `l` ||
| `c` + `l` + `n` ||
| `c` + `l` + `n` + `u` ||
| `c` + `l` + `u` ||
| `c` + `u` ||
| `c` + `u` + `n` ||
| `d` ||

The complete code is shown below:

<iframe src="https://codesandbox.io/embed/keywrite-layouts-example-149ml?fontsize=14&hidenavigation=1&theme=dark"
style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"
title="keywrite-layouts-example"
allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
></iframe>
## Creating keyboard layouts

Keyboard layout definitions are simple json objects. It is possible to manually write a layout definition that will work with Keywrite, but a better way would be to use the [Layout Generator](layout_generator.md).

## Pre-made layouts

Symbol map is an object with a `value` and `next` keys. Value specifies
Some layouts are avaliable pre-made and you add simply add them to your project.
A list of all available layouts can be found [here](pre_made_layouts.md)
1 change: 1 addition & 0 deletions docs/layout_generator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Examples
1 change: 1 addition & 0 deletions docs/packages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Packages
1 change: 1 addition & 0 deletions docs/pre_made_layouts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Pre-made Layouts

0 comments on commit 44cc16c

Please sign in to comment.