Skip to content

Commit

Permalink
Merge feature autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
lucidBrot committed Sep 5, 2018
2 parents 92cd75f + c409134 commit 5cad16b
Show file tree
Hide file tree
Showing 47 changed files with 33,352 additions and 206 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
106 changes: 105 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ Brotkeys builds upon [jaywcjaylove's hotkeys](https://github.com/jaywcjlove/hotk
* listen for specific words
* set a character that has to preceed any input
* be informed on start and end of user input, or at every character
* automatically generate an "open link" action for every anchor tag or a specific css class

## Usage

### [Autogeneration - click me to jump ahead](#Autogeneration)

### Basic Example

Executes the given javascript function when the word is typed. There is no way to start a new word while typing the old word. So `hello` will show a popup saying "general kenobi!", but if you start typing `about` and then decide to switch to `hello`, there won't be any reaction to `abhello`.
Expand Down Expand Up @@ -130,6 +133,70 @@ Current word: qwer
// manager.enable_f_mode(false);
```

### Autogeneration

You already have a webpage, filled with hundreds of links. You want for every link some link hint that displays a word, and when the user types that word it shall open the link.
If you want much control, read the rest of this readme, set up your manager, and then call `manager.autogenerate(manager.GenerationEnum.tag_anchor, undefined, swap_class);`. If you want a **quick start**, do this:

```html
<script src="./libs/jaywcjlove_hotkeys/hotkeys.min.js"></script>
<script src="./libs/lucidbrot_brotkeys/brotkeys.js"></script>
<script type="text/javascript">
brotkeys_autogenerate_manager_for_anchors();
</script>
```

Now you have exactly what I described above.

Of course, that does not sate your thirst for power, you want _exactly that, but for other things_. Here you go: simply give each of your HTML elements the same CSS class - I'll use `class="some other classes BHK"` and then use

```html
<script src="./libs/jaywcjlove_hotkeys/hotkeys.min.js"></script>
<script src="./libs/lucidbrot_brotkeys/brotkeys.js"></script>
<script type="text/javascript">
brotkeys_autogenerate_manager_for_class_tag("BHK")
</script>
```

And if you want even more control, here's what's lacking.
In the way I just showed you, you do not have access to the HotkeyManager variable where you can change settings. If you want that, you could either use the implementation of the above-called functions as inspiration, or do it like I did in my sample page:

```js
var manager;
// words of the form [f]abcdefg unless enable_f_mode is set to false
// DONT INCLUDE AN UPPERCASE X, because that is used to immediately abort f mode here.
var wordMap = new Map([
["secret", function(){window.open("https://eric.mink.li/src/php/ccount/click.php?id=sneric","_self");}],
]);
// single characters that can interrupt at any time during the word-typing mode
var interruptMap = new Map([
["X", function(){manager.abort_f_mode();}],
["D", function(){console.log("user disabled shortcuts"); manager.disable();}],
]);

manager = new HotkeyManager(wordMap, interruptMap);
manager.interrupt_caseInsensitivity = false;

manager.loadNeededJSCSSForStyleSwapping();
// please notify me on entering and leaving fmode simply by showing the link hints
// this is the simplest way to do this. for other options, see the examples in brotkeys.js#brotkeys_autogenerate_manager_for_anchors and brotkeys.js#brotkeys_autogenerate_manager_for_class_tag
var notifyFModeFunc = manager.genToggleKeysOnNotify();
manager.setNotifyFModeFunction(notifyFModeFunc);
manager.log_prefix = "[M] ";
manager.autogenerate(manager.GenerationEnum.tag_anchor);
```

In this sample, I first create `wordMap`, a mapping from words to actions. `"secret"` is something I added manually, and whenever somebody types that (while in fMode, since I did not deactivate fMode) it calls the javascript function in that map, which opens some other url.
The `interruptMap` contains things that can always happen, while in fMode, and interrupt the other actions.
I create the manager with those two arrays as start setup, tell it that I want case sensitive interrupts. The call to `loadNeededJSCSSForStyleSwapping();` is only neccessary when not using `manager.genToggleKeysOnNotify()` but still using my css class - so _it is not needed here_. But it will usually not hurt to call it anyways, since it instantly returns if it has been called before on the same manager. Generally speaking, you probably never need this, but if something doesn't look correctly (or at all), try calling this.
`genToggleKeysOnNotify` is a function that is tricky to explain, but in fact quite simple. `setNotifyFModeFunction` takes as argument any function that reacts to toggling of the fMode. `genToggleKeysOnNotify` generates such a function for you so you don't have to worry about this unless you want to use your own function instead. In that case, read the start of this readme instead, I think I have another example there.
The log prefix is just that, a prefix before any log messages.
And finally, the call to `autogenerate`.

`autogenerate` takes up to three arguments. The first is mandatory: Either `manager.GenerationEnum.tag_anchor` or `manager.GenerationEnum.class_tag`. This states what you want to autogenerate.
If you want to generate for anchors, you can pass `undefined` as the second argument. Otherwise the second argument is used as your class name that will be operated on.
The third argument is replaced with `manager.SWAP_CLASS_NAME_DEFAULT` if you're passing `undefined`. It serves as the class name used for the link hint buttons that appear and disappear. Make sure it's the same as in `genToggleKeysOnNotify` if you have passed something there - and undefined if not.

### Multiple HotkeyManagers

I don't know why you would do this, but that works as well. Just be aware that using `hotkeys.unbind('*')` would break the behaviour of the Managers (but that's always the case, not just when using multiple managers).
Expand Down Expand Up @@ -167,6 +234,39 @@ The third line is whether capitalization in words matters. True if it does not m

See also further below about an obscure setting called `ignore_ShiftAndCapslock_inWordMode`.

### Modify The Link Hints Style

Simply provide your own css class and set `manager.LINKHINT_STYLE_CLASS` to it.
The currently used one:

```css
kbd.eric-reverse {
background-color: #fff;
color: #333;
border-style: dashed;
}
```

## Using Defer

It seems like a cool feature, `defer`. But it makes it impossible for Brotkeys to figure out where it is stored. There are several ways to solve this problem, but the simplest way is not to use `defer`.
The second simplest way is to tell Brotkey where it is stored:

```html
<!-- main.html -->
<script src="./libs/jaywcjlove_hotkeys/hotkeys.min.js" defer></script>
<script src="./libs/lucidbrot_brotkeys/brotkeys.js" defer></script>
<script src="./keyjs/main.js" defer></script>
```

```js
// main.js
// since we used defer in main.html for brotkeys, we have to tell brotkeys where it lives. Absolute, or relative to the HTML file where this js file is included in.
_brotkeysjs__src__path = './libs/lucidbrot_brotkeys/brotkeys.js';
```

This is only relevant if you are using [autogenerate](#Autogeneration) or anything else that uses [loadNeededJSCSSForStyleSwapping](#loadNeededJSCSSForStyleSwapping).

## Implementation Details

If you want to know what's going on, without reading the actual code.
Expand Down Expand Up @@ -229,6 +329,10 @@ Does not unregister the listener to hotkeys.js - it simply does not act on the e

There's no harm in calling this, I think. But there's also no reason to. It's called from the constructor.

### loadNeededJSCSSForStyleSwapping

This loads the css needed for toggling the style of the link hints when hiding them.

### Anything Else

Don't touch unless you've read the code.
Expand All @@ -254,4 +358,4 @@ [email protected]

## Note on the Beauty of the Code

It's probably ugly. I've tried a little, but not too hard, and javascript does not offer the options I'm used to. This is not a "sorry", it's just a "I knew that it was ugly when I wrote it, I'm not incompetent".
It's probably ugly. I've tried a little, but not too hard, and javascript does not offer the options I'm used to. This is not a "sorry", it's just a "I knew that it was ugly when I wrote it, I'm not that incompetent".
Loading

0 comments on commit 5cad16b

Please sign in to comment.