aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNick Coutsos <[email protected]>2021-10-08 16:01:13 -0400
committerNick Coutsos <[email protected]>2021-10-08 16:01:13 -0400
commitf33a098fdf016024c6d6d492b6d7948e0c6dde53 (patch)
tree46c36602ca07afd152242cb8836c340517cf1697
parentf6eac8090af296b7b16d378656bdd248b4fb2cba (diff)
downloadkeymap-editor-f33a098fdf016024c6d6d492b6d7948e0c6dde53.tar.gz
keymap-editor-f33a098fdf016024c6d6d492b6d7948e0c6dde53.zip
Reorganize zmk service code
-rw-r--r--api/routes/keyboards.js2
-rw-r--r--api/services/github/files.js2
-rw-r--r--api/services/zmk/index.js22
-rw-r--r--api/services/zmk/keymap.js121
-rw-r--r--api/services/zmk/local-source.js (renamed from api/services/zmk/zmk.js)76
-rw-r--r--package-lock.json11
-rw-r--r--package.json1
7 files changed, 158 insertions, 77 deletions
diff --git a/api/routes/keyboards.js b/api/routes/keyboards.js
index 6c605a1..0571b6d 100644
--- a/api/routes/keyboards.js
+++ b/api/routes/keyboards.js
@@ -1,5 +1,5 @@
const { Router } = require('express')
-const zmk = require('../services/zmk/zmk')
+const zmk = require('../services/zmk')
const firmwares = { zmk }
const router = Router()
diff --git a/api/services/github/files.js b/api/services/github/files.js
index 4a33df0..5f5837f 100644
--- a/api/services/github/files.js
+++ b/api/services/github/files.js
@@ -1,6 +1,6 @@
const api = require('./api')
const auth = require('./auth')
-const zmk = require('../zmk/zmk')
+const zmk = require('../zmk')
const MODE_FILE = '100644'
diff --git a/api/services/zmk/index.js b/api/services/zmk/index.js
index e69de29..775b420 100644
--- a/api/services/zmk/index.js
+++ b/api/services/zmk/index.js
@@ -0,0 +1,22 @@
+const {
+ parseKeyBinding,
+ generateKeymap
+} = require('./keymap')
+
+const {
+ loadBehaviors,
+ loadKeycodes,
+ loadLayout,
+ loadKeymap,
+ exportKeymap
+} = require('./local-source')
+
+module.exports = {
+ parseKeyBinding,
+ generateKeymap,
+ loadBehaviors,
+ loadKeycodes,
+ loadLayout,
+ loadKeymap,
+ exportKeymap
+}
diff --git a/api/services/zmk/keymap.js b/api/services/zmk/keymap.js
new file mode 100644
index 0000000..9432627
--- /dev/null
+++ b/api/services/zmk/keymap.js
@@ -0,0 +1,121 @@
+const filter = require('lodash/filter')
+const get = require('lodash/get')
+const map = require('lodash/map')
+
+const paramsPattern = /\((.+)\)/
+
+function parse(code) {
+ const params = filter(
+ get(code.match(paramsPattern), '[1]', '')
+ .split(',')
+ .map(s => s.trim())
+ )
+
+ return {
+ value: code.replace(paramsPattern, ''),
+ params: params.map(parse)
+ }
+}
+
+/**
+ * Parse a key binding into a nested structure of values and parameters
+ * @param {String} binding the raw bind string (e.g. `&kp LS(A)`)
+ * @returns {Array}
+ */
+function parseKeyBinding (binding) {
+ const value = binding.match(/^(&.+?)\b/)[1]
+ const paramBinds = filter(binding.replace(/^&.+?\b\s*/, '').split(' '))
+ const params = map(paramBinds, parse)
+
+ return { binding, value, params }
+}
+
+function parseKeymap (keymap) {
+ return Object.assign({}, keymap, {
+ layers: keymap.layers.map(layer => {
+ return layer.map(binding => {
+ return parseKeyBinding(binding, this.sources)
+ })
+ })
+ })
+}
+
+const { renderTable } = require('./layout')
+
+const header = `
+/*
+ * Copyright (c) 2020 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+
+/* THIS FILE WAS GENERATED!
+ *
+ * This file was generated automatically. You may or may not want to
+ * edit it directly.
+ */
+
+#include <behaviors.dtsi>
+#include <dt-bindings/zmk/keys.h>
+#include <dt-bindings/zmk/bt.h>
+#include <dt-bindings/zmk/outputs.h>
+
+`
+
+function generateKeymap (layout, keymap) {
+ return {
+ code: generateKeymapCode(layout, keymap),
+ json: generateKeymapJSON(layout, keymap)
+ }
+}
+
+function generateKeymapCode (layout, keymap) {
+ const { layer_names: names = [] } = keymap
+ const layers = keymap.layers.map((layer, i) => {
+ const name = i === 0 ? 'default_layer' : `layer_${names[i] || i}`
+ const rendered = renderTable(layout, layer, {
+ linePrefix: '',
+ columnSeparator: ' '
+ })
+
+ return `
+ ${name} {
+ bindings = <
+${rendered}
+ >;
+ };
+`
+ })
+
+ const keymapOut = `${header}
+/ {
+ keymap {
+ compatible = "zmk,keymap";
+
+${layers.join('')}
+ };
+};
+`
+
+ return keymapOut
+}
+
+function generateKeymapJSON (layout, keymap) {
+ const base = JSON.stringify(Object.assign({}, keymap, { layers: null }), null, 2)
+ const layers = keymap.layers.map(layer => {
+ const rendered = renderTable(layout, layer, {
+ useQuotes: true,
+ linePrefix: ' '
+ })
+
+ return `[\n${rendered}\n ]`
+ })
+
+ return base.replace('"layers": null', `"layers": [\n ${layers.join(', ')}\n ]`)
+}
+
+module.exports = {
+ parseKeymap,
+ generateKeymap
+}
diff --git a/api/services/zmk/zmk.js b/api/services/zmk/local-source.js
index 695422f..467599f 100644
--- a/api/services/zmk/zmk.js
+++ b/api/services/zmk/local-source.js
@@ -1,7 +1,7 @@
const childProcess = require('child_process')
const fs = require('fs')
const path = require('path')
-const { renderTable } = require('./layout')
+const { parseKeymap } = require('./keymap')
const ZMK_PATH = path.join(__dirname, '..', '..', '..', 'zmk-config')
const KEYBOARD = 'dactyl'
@@ -24,79 +24,6 @@ function loadKeymap () {
return JSON.parse(fs.readFileSync(keymapPath))
}
-const header = `
-/*
- * Copyright (c) 2020 The ZMK Contributors
- *
- * SPDX-License-Identifier: MIT
- */
-
-
-/* THIS FILE WAS GENERATED!
- *
- * This file was generated automatically. You may or may not want to
- * edit it directly.
- */
-
-#include <behaviors.dtsi>
-#include <dt-bindings/zmk/keys.h>
-#include <dt-bindings/zmk/bt.h>
-#include <dt-bindings/zmk/outputs.h>
-
-`
-
-function generateKeymap (layout, keymap) {
- return {
- code: generateKeymapCode(layout, keymap),
- json: generateKeymapJSON(layout, keymap)
- }
-}
-
-function generateKeymapCode (layout, keymap) {
- const { layer_names: names = [] } = keymap
- const layers = keymap.layers.map((layer, i) => {
- const name = i === 0 ? 'default_layer' : `layer_${names[i] || i}`
- const rendered = renderTable(layout, layer, {
- linePrefix: '',
- columnSeparator: ' '
- })
-
- return `
- ${name} {
- bindings = <
-${rendered}
- >;
- };
-`
- })
-
- const keymapOut = `${header}
-/ {
- keymap {
- compatible = "zmk,keymap";
-
-${layers.join('')}
- };
-};
-`
-
- return keymapOut
-}
-
-function generateKeymapJSON (layout, keymap) {
- const base = JSON.stringify(Object.assign({}, keymap, { layers: null }), null, 2)
- const layers = keymap.layers.map(layer => {
- const rendered = renderTable(layout, layer, {
- useQuotes: true,
- linePrefix: ' '
- })
-
- return `[\n${rendered}\n ]`
- })
-
- return base.replace('"layers": null', `"layers": [\n ${layers.join(', ')}\n ]`)
-}
-
function exportKeymap (generatedKeymap, flash, callback) {
const keymapPath = path.join(ZMK_PATH, 'config')
@@ -117,6 +44,5 @@ module.exports = {
loadKeycodes,
loadLayout,
loadKeymap,
- generateKeymap,
exportKeymap
}
diff --git a/package-lock.json b/package-lock.json
index e4a875f..c0a8b7f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -17,6 +17,7 @@
"express": "^4.17.1",
"express-ws": "^4.0.0",
"jsonwebtoken": "^8.5.1",
+ "lodash": "^4.17.21",
"morgan": "^1.10.0"
}
},
@@ -391,6 +392,11 @@
"safe-buffer": "^5.0.1"
}
},
+ "node_modules/lodash": {
+ "version": "4.17.21",
+ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
+ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
+ },
"node_modules/lodash.includes": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
@@ -1014,6 +1020,11 @@
"safe-buffer": "^5.0.1"
}
},
+ "lodash": {
+ "version": "4.17.21",
+ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
+ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
+ },
"lodash.includes": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
diff --git a/package.json b/package.json
index d32b0a9..dbf6627 100644
--- a/package.json
+++ b/package.json
@@ -20,6 +20,7 @@
"express": "^4.17.1",
"express-ws": "^4.0.0",
"jsonwebtoken": "^8.5.1",
+ "lodash": "^4.17.21",
"morgan": "^1.10.0"
}
}