Skip to content

Commit eaa5e8e

Browse files
authored
Merge pull request #4 from tomoemon/tree-shakable
Tree shakable にして余計なコードがバンドルされないようにする
2 parents 14ba2e4 + 2d266e0 commit eaa5e8e

File tree

23 files changed

+287
-359
lines changed

23 files changed

+287
-359
lines changed

examples/cli/index.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
//@ts-check
2-
import * as emiel from "emiel";
2+
import {
3+
InputEvent,
4+
InputStroke,
5+
KeyboardState,
6+
VirtualKeys,
7+
loadPresetKeyboardLayoutQwertyJis,
8+
loadPresetRuleRoman,
9+
} from "emiel";
310

4-
const automaton = emiel.rule.getRoman().build("かった");
5-
const Key = emiel.VirtualKeys;
11+
const layout = loadPresetKeyboardLayoutQwertyJis();
12+
const automaton = loadPresetRuleRoman(layout).build("かった");
13+
const Key = VirtualKeys;
614
const inputResult = [Key.K, Key.A, Key.F, Key.T, Key.T, Key.A].map((k) => [
715
k,
816
automaton.input(
9-
new emiel.InputEvent(
10-
new emiel.InputStroke(k, "keydown"),
11-
new emiel.KeyboardState(),
17+
new InputEvent(
18+
new InputStroke(k, "keydown"),
19+
new KeyboardState(),
1220
new Date()
1321
)
1422
),

examples/react-backspace/src/App.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1+
import { activate, detectKeyboardLayout, InputStroke, KeyboardLayout, loadPresetRuleRoman, VirtualKeys } from "emiel";
2+
import { useEffect, useMemo, useState } from "react";
13
import "./App.css";
2-
import * as emiel from "emiel";
3-
import { useEffect, useState } from "react";
44
import { BackspaceRequirdAutomaton } from "./backspace";
55

66
function App() {
7-
const [layout, setLayout] = useState<emiel.KeyboardLayout | undefined>();
7+
const [layout, setLayout] = useState<KeyboardLayout | undefined>();
88
useEffect(() => {
9-
emiel.keyboard.detect(window).then(setLayout).catch(console.error);
9+
detectKeyboardLayout(window).then(setLayout).catch(console.error);
1010
}, []);
1111
return layout ? <Typing layout={layout} /> : <></>;
1212
}
1313

14-
function Typing(props: { layout: emiel.KeyboardLayout }) {
15-
const words = ["おをひく", "こんとん", "がっこう", "aから@"];
14+
function Typing(props: { layout: KeyboardLayout }) {
15+
const romanRule = useMemo(() => loadPresetRuleRoman(props.layout), [props.layout]);
16+
const words = useMemo(() => ["おをひく", "こんとん", "がっこう", "aから@"], []);
1617
const [automatons] = useState(
1718
words.map((w) => {
1819
return new BackspaceRequirdAutomaton(
19-
emiel.rule.getRoman(props.layout).build(w)
20+
romanRule.build(w)
2021
);
2122
})
2223
);
2324
const [index, setIndex] = useState(0);
2425
const [lastInputKey, setLastInputKey] = useState<
25-
emiel.InputStroke | undefined
26+
InputStroke | undefined
2627
>();
2728
const automaton = automatons[index];
2829
useEffect(() => {
29-
return emiel.activate(window, (e) => {
30+
return activate(window, (e) => {
3031
setLastInputKey(e.input);
31-
if (e.input.key === emiel.VirtualKeys.Backspace) {
32+
if (e.input.key === VirtualKeys.Backspace) {
3233
automaton.backFailedInput();
3334
return;
3435
}
@@ -38,7 +39,7 @@ function Typing(props: { layout: emiel.KeyboardLayout }) {
3839
setIndex((current) => (current + 1) % words.length);
3940
}
4041
});
41-
}, [index]);
42+
}, [index, automaton, words]);
4243
return (
4344
<>
4445
<h1>
@@ -71,8 +72,8 @@ function Typing(props: { layout: emiel.KeyboardLayout }) {
7172
.getCharByKey(
7273
f.input.key,
7374
f.keyboardState.isAnyKeyDowned(
74-
emiel.VirtualKeys.ShiftLeft,
75-
emiel.VirtualKeys.ShiftRight
75+
VirtualKeys.ShiftLeft,
76+
VirtualKeys.ShiftRight
7677
)
7778
)
7879
.replace(" ", "_")

examples/react-keyboardguide/src/App.tsx

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { KeyRect, KeyTop, KeyboardState, guide, keyboard } from 'emiel'
1+
import { KeyRect, KeyTop, KeyboardState, KeyboardStateReader, PhysicalKeyboardLayoutName, activate, loadPresetKeyboardGuideJis106Default, loadPresetKeyboardGuideJis106JisKana, loadPresetKeyboardGuideJis106Nicola, loadPresetKeyboardGuideUs101Default, loadPresetKeyboardLayoutDvorak, loadPresetKeyboardLayoutQwertyJis, loadPresetKeyboardLayoutQwertyUs } from 'emiel';
22
import { useEffect, useState } from 'react';
3-
import * as emiel from 'emiel';
3+
4+
type LayoutName = "qwerty-jis" | "qwerty-us" | "dvorak";
5+
type GuideName = "us_101_default" | "jis_106_default" | "jis_106_jis_kana" | "jis_106_nicola";
46

57
function App() {
6-
const [keyboardState, setKeyboardState] = useState<emiel.KeyboardStateReader>(new KeyboardState([]));
8+
const [keyboardState, setKeyboardState] = useState<KeyboardStateReader>(new KeyboardState([]));
79
useEffect(() => {
8-
emiel.activate(window, (evt) => {
10+
activate(window, (evt) => {
911
console.log("down", evt.input.key);
1012
setKeyboardState(evt.keyboardState)
1113
}, (evt) => {
@@ -14,15 +16,15 @@ function App() {
1416
}
1517
);
1618
}, []);
17-
const [physicalLayoutName, setPhysicalLayoutName] = useState<emiel.PhysicalKeyboardLayoutName>("jis_106");
18-
const [layoutName, setLayoutName] = useState("qwerty-jis");
19-
const [guideName, setGuideName] = useState("us_101_default");
19+
const [physicalLayoutName, setPhysicalLayoutName] = useState<PhysicalKeyboardLayoutName>("jis_106");
20+
const [layoutName, setLayoutName] = useState<LayoutName>("qwerty-jis");
21+
const [guideName, setGuideName] = useState<GuideName>("us_101_default");
2022
const [showVirtualKeyCodes, setShowVirtualKeyCodes] = useState(false);
2123
return <>
2224
<h1>Keyboard Guide</h1>
2325
<div style={{ height: "20px" }}></div>
24-
<PhysicalLayoutSelector onLayoutChange={(layout: emiel.PhysicalKeyboardLayoutName) => setPhysicalLayoutName(layout)} />
25-
<LayoutSelector onLayoutChange={(layoutName: string) => setLayoutName(layoutName)} />
26+
<PhysicalLayoutSelector onLayoutChange={(layout: PhysicalKeyboardLayoutName) => setPhysicalLayoutName(layout)} />
27+
<LayoutSelector onLayoutChange={(layoutName: LayoutName) => setLayoutName(layoutName)} />
2628
<GuideSelector onGuideChange={setGuideName} />
2729
<label><input type="checkbox" onClick={(e) => setShowVirtualKeyCodes(e.currentTarget.checked)} />仮想キーコードの表示</label>
2830
<div style={{ height: "20px" }}></div>
@@ -31,7 +33,7 @@ function App() {
3133
}
3234

3335
function PhysicalLayoutSelector(props: {
34-
onLayoutChange: (physicalLayoutName: emiel.PhysicalKeyboardLayoutName) => void,
36+
onLayoutChange: (physicalLayoutName: PhysicalKeyboardLayoutName) => void,
3537
}) {
3638
const [selected, setSelected] = useState(0);
3739
return <>
@@ -59,7 +61,7 @@ function PhysicalLayoutSelector(props: {
5961
}
6062

6163
function LayoutSelector(props: {
62-
onLayoutChange: (layoutName: string) => void,
64+
onLayoutChange: (layoutName: LayoutName) => void,
6365
}) {
6466
const [selected, setSelected] = useState(0);
6567
return <>
@@ -87,7 +89,7 @@ function LayoutSelector(props: {
8789
}
8890

8991
function GuideSelector(props: {
90-
onGuideChange: (guideName: string) => void,
92+
onGuideChange: (guideName: GuideName) => void,
9193
}) {
9294
const [selected, setSelected] = useState(0);
9395
return <>
@@ -122,15 +124,24 @@ function GuideSelector(props: {
122124
}
123125

124126
function KeyboardGuideComponent(props: {
125-
layoutName: string,
127+
layoutName: LayoutName,
126128
physicalLayoutName: string,
127-
guideName: string,
128-
kbdState: emiel.KeyboardStateReader,
129+
guideName: GuideName,
130+
kbdState: KeyboardStateReader,
129131
showVirtualKeyCodes: boolean,
130132
}) {
131133
// console.log("guide component", props.layout.name, props.kbdGuide.guideData.name);
132-
const layout = keyboard.get(props.layoutName);
133-
const kbdGuide = guide.get(props.guideName).swapPhysicalLayout(props.physicalLayoutName as emiel.PhysicalKeyboardLayoutName);
134+
const layout = {
135+
"qwerty-jis": loadPresetKeyboardLayoutQwertyJis,
136+
"qwerty-us": loadPresetKeyboardLayoutQwertyUs,
137+
"dvorak": loadPresetKeyboardLayoutDvorak
138+
}[props.layoutName]();
139+
const kbdGuide = {
140+
"us_101_default": loadPresetKeyboardGuideUs101Default,
141+
"jis_106_default": loadPresetKeyboardGuideJis106Default,
142+
"jis_106_jis_kana": loadPresetKeyboardGuideJis106JisKana,
143+
"jis_106_nicola": loadPresetKeyboardGuideJis106Nicola,
144+
}[props.guideName]().swapPhysicalLayout(props.physicalLayoutName as PhysicalKeyboardLayoutName);
134145
return (
135146
<>
136147
<div style={{ position: "relative" }}>

examples/react-mixed-guide/src/App.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
import { activate, detectKeyboardLayout, InputStroke, KeyboardLayout, loadPresetRuleRoman } from "emiel";
2+
import { useEffect, useMemo, useState } from "react";
13
import "./App.css";
2-
import * as emiel from "emiel";
3-
import { useEffect, useState } from "react";
44
import { MixedText, MixedTextAutomaton } from "./MixedGuide";
55

66
function App() {
7-
const [layout, setLayout] = useState<emiel.KeyboardLayout | undefined>();
7+
const [layout, setLayout] = useState<KeyboardLayout | undefined>();
88
useEffect(() => {
9-
emiel.keyboard.detect(window).then(setLayout).catch(console.error);
9+
detectKeyboardLayout(window).then(setLayout).catch(console.error);
1010
}, []);
1111
return layout ? <Typing layout={layout} /> : <></>;
1212
}
1313

14-
function Typing(props: { layout: emiel.KeyboardLayout }) {
14+
function Typing(props: { layout: KeyboardLayout }) {
15+
const romanRule = useMemo(() => loadPresetRuleRoman(props.layout), [props.layout]);
1516
const words = [
1617
new MixedText("お,を,ひ,く", "尾,を,引,く"),
1718
new MixedText("こん,とん", "混,沌"),
@@ -21,17 +22,17 @@ function Typing(props: { layout: emiel.KeyboardLayout }) {
2122
const [automatons] = useState(
2223
words.map((w) =>
2324
new MixedTextAutomaton(
24-
emiel.rule.getRoman(props.layout).build(w.kanaText),
25+
romanRule.build(w.kanaText),
2526
w,
2627
)
2728
)
2829
);
2930
const [index, setIndex] = useState(0);
3031
const [lastInputKey, setLastInputKey] = useState<
31-
emiel.InputStroke | undefined
32+
InputStroke | undefined
3233
>();
3334
useEffect(() => {
34-
return emiel.activate(window, (e) => {
35+
return activate(window, (e) => {
3536
setLastInputKey(e.input);
3637
const result = automatons[index].base.input(e);
3738
if (result.isFinished) {

examples/react-multi-word/src/App.tsx

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { activate, Automaton, detectKeyboardLayout, InputEvent, InputResult, InputStroke, Inputtable, KeyboardLayout, loadPresetRuleRoman, Selector, VirtualKeys } from "emiel";
2+
import { useEffect, useMemo, useState } from "react";
13
import "./App.css";
2-
import * as emiel from "emiel";
3-
import { useEffect, useState } from "react";
44
import { Word } from "./word";
55

66
// 繰り返し次のワードを生成するジェネレータ
@@ -22,10 +22,10 @@ const wordGen = (function* wordGenerator(): Generator<string, string> {
2222
// 初期ワード3つ
2323
const initialWords = Array.from({ length: 3 }, () => wordGen.next().value);
2424

25-
class PositionAutomaton implements emiel.Inputtable {
26-
constructor(readonly base: emiel.Automaton, readonly position: number) {
25+
class PositionAutomaton implements Inputtable {
26+
constructor(readonly base: Automaton, readonly position: number) {
2727
}
28-
input(stroke: emiel.InputEvent): emiel.InputResult {
28+
input(stroke: InputEvent): InputResult {
2929
return this.base.input(stroke);
3030
}
3131
reset(): void {
@@ -34,57 +34,57 @@ class PositionAutomaton implements emiel.Inputtable {
3434
}
3535

3636
function App() {
37-
const [layout, setLayout] = useState<emiel.KeyboardLayout | undefined>();
37+
const [layout, setLayout] = useState<KeyboardLayout | undefined>();
3838
useEffect(() => {
39-
emiel.keyboard.detect(window).then(setLayout).catch(console.error);
39+
detectKeyboardLayout(window).then(setLayout).catch(console.error);
4040
}, []);
4141
return layout ? <Typing layout={layout} /> : <></>;
4242
}
4343

44-
function Typing(props: { layout: emiel.KeyboardLayout }) {
44+
function Typing(props: { layout: KeyboardLayout }) {
45+
const romanRule = useMemo(() => loadPresetRuleRoman(props.layout), [props.layout]);
4546
const [selector, setSelector] = useState(
46-
new emiel.Selector(
47+
new Selector(
4748
// 各 automaton の metadata として表示位置をもたせる
4849
initialWords.map(
4950
(w, i) =>
5051
new PositionAutomaton(
51-
emiel.rule.getRoman(props.layout).build(w),
52+
romanRule.build(w),
5253
i
5354
)
5455
)
5556
)
5657
);
5758
const [lastInputKey, setLastInputKey] = useState<
58-
emiel.InputStroke | undefined
59+
InputStroke | undefined
5960
>();
6061
useEffect(() => {
61-
return emiel.activate(window, (e) => {
62+
return activate(window, (e) => {
6263
setLastInputKey(e.input);
6364
console.log("input:", e);
64-
if (e.input.key === emiel.VirtualKeys.Escape) {
65+
if (e.input.key === VirtualKeys.Escape) {
6566
console.log("reset");
6667
selector.reset();
6768
return;
6869
}
69-
selector.input(e, {
70-
finished: (a) => {
71-
console.log("finished", a);
72-
const newAutomaton =
73-
new PositionAutomaton(
74-
emiel.rule.getRoman(props.layout).build(wordGen.next().value),
75-
a.position
76-
);
77-
setSelector((current) => current.replaced(a, newAutomaton));
78-
},
79-
succeeded: (a) => {
80-
console.log("succeeded", a);
81-
},
82-
failed: (a) => {
83-
console.log("failed", a);
84-
},
70+
const { finished, succeeded, failed } = selector.input(e);
71+
finished.forEach((a) => {
72+
console.log("finished", a);
73+
const newAutomaton =
74+
new PositionAutomaton(
75+
romanRule.build(wordGen.next().value),
76+
a.position
77+
);
78+
setSelector((current) => current.replaced(a, newAutomaton));
79+
});
80+
succeeded.forEach((a) => {
81+
console.log("succeeded", a);
82+
});
83+
failed.forEach((a) => {
84+
console.log("failed", a);
8585
});
8686
});
87-
}, [selector]);
87+
}, [selector, romanRule]);
8888

8989
return (
9090
<>

0 commit comments

Comments
 (0)