|
| 1 | +import { type KeyId } from "@keybr/keyboard"; |
| 2 | +import { exportLayout, importLayout, LayoutBuilder } from "@keybr/keyboard-io"; |
| 3 | +import { Button, ErrorAlert, Field, FieldList } from "@keybr/widget"; |
| 4 | +import { useRef, useState } from "react"; |
| 5 | +import { CustomLayoutProvider, useCustomLayout } from "./context.tsx"; |
| 6 | +import { LayoutView } from "./LayoutView.tsx"; |
| 7 | +import { LiveImport } from "./LiveImport.tsx"; |
| 8 | + |
| 9 | +export function CustomLayoutDesigner() { |
| 10 | + return ( |
| 11 | + <CustomLayoutProvider> |
| 12 | + <DesignPane /> |
| 13 | + </CustomLayoutProvider> |
| 14 | + ); |
| 15 | +} |
| 16 | + |
| 17 | +function DesignPane() { |
| 18 | + const exportRef = useRef<HTMLAnchorElement>(null); |
| 19 | + const importRef = useRef<HTMLInputElement>(null); |
| 20 | + const { layout, setLayout } = useCustomLayout(); |
| 21 | + const [keyId, setKeyId] = useState<KeyId>("Space"); |
| 22 | + return ( |
| 23 | + <section> |
| 24 | + <a |
| 25 | + ref={exportRef} |
| 26 | + href="#" |
| 27 | + download="layout.json" |
| 28 | + hidden={true} |
| 29 | + style={{ inlineSize: 0, blockSize: 0, overflow: "hidden" }} |
| 30 | + /> |
| 31 | + <input |
| 32 | + ref={importRef} |
| 33 | + type="file" |
| 34 | + accept="*/*" |
| 35 | + hidden={true} |
| 36 | + style={{ inlineSize: 0, blockSize: 0, overflow: "hidden" }} |
| 37 | + onChange={() => { |
| 38 | + const el = importRef.current!; |
| 39 | + const files = el.files; |
| 40 | + if (files != null && files.length > 0) { |
| 41 | + importLayout(files[0]) |
| 42 | + .then((result) => { |
| 43 | + if (result == null) { |
| 44 | + ErrorAlert.report("Invalid layout file"); |
| 45 | + } else { |
| 46 | + const { layout, warnings } = result; |
| 47 | + setLayout(layout); |
| 48 | + if (warnings.length > 0) { |
| 49 | + ErrorAlert.report(new AggregateError(warnings)); |
| 50 | + } |
| 51 | + } |
| 52 | + }) |
| 53 | + .catch((err) => { |
| 54 | + console.error(err); |
| 55 | + }) |
| 56 | + .finally(() => { |
| 57 | + el.value = ""; |
| 58 | + }); |
| 59 | + } |
| 60 | + }} |
| 61 | + /> |
| 62 | + <LayoutView keyId={keyId} setKeyId={setKeyId} /> |
| 63 | + <LiveImport onChange={setKeyId} /> |
| 64 | + <FieldList> |
| 65 | + <Field> |
| 66 | + <Button |
| 67 | + label="Export" |
| 68 | + onClick={() => { |
| 69 | + exportLayout(layout) |
| 70 | + .then((blob) => { |
| 71 | + const el = exportRef.current!; |
| 72 | + el.setAttribute("href", URL.createObjectURL(blob)); |
| 73 | + el.click(); |
| 74 | + }) |
| 75 | + .catch((err) => { |
| 76 | + console.error(err); |
| 77 | + }); |
| 78 | + }} |
| 79 | + /> |
| 80 | + </Field> |
| 81 | + <Field> |
| 82 | + <Button |
| 83 | + label="Import" |
| 84 | + onClick={() => { |
| 85 | + const el = importRef.current!; |
| 86 | + el.click(); |
| 87 | + }} |
| 88 | + /> |
| 89 | + </Field> |
| 90 | + <Field.Filler /> |
| 91 | + <Field> |
| 92 | + <Button |
| 93 | + label="Reset" |
| 94 | + onClick={() => { |
| 95 | + setLayout(new LayoutBuilder()); |
| 96 | + }} |
| 97 | + /> |
| 98 | + </Field> |
| 99 | + </FieldList> |
| 100 | + </section> |
| 101 | + ); |
| 102 | +} |
0 commit comments