ui-thing 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import { Command } from "commander";
4
4
  import { add } from "./commands/add";
5
5
  import { init } from "./commands/init";
6
6
  import { addPrettier } from "./commands/prettier";
7
+ import { addShortcuts } from "./commands/shortcuts";
7
8
  import { theme } from "./commands/theme";
8
9
  import { printFancyBoxMessage } from "./utils/printFancyBoxMessage";
9
10
 
@@ -25,6 +26,7 @@ program
25
26
  .addCommand(init)
26
27
  .addCommand(add)
27
28
  .addCommand(theme)
29
+ .addCommand(addShortcuts)
28
30
  .addCommand(addPrettier);
29
31
 
30
32
  program.parse(process.argv);
@@ -0,0 +1,217 @@
1
+ export const DEFINE_SHORTCUT = `import { logicAnd, logicNot } from "@vueuse/math";
2
+ import { computed, ref } from "vue";
3
+ import type { ComputedRef, WatchSource } from "vue";
4
+
5
+ export interface ShortcutConfig {
6
+ handler: Function;
7
+ usingInput?: string | boolean;
8
+ whenever?: WatchSource<Boolean>[];
9
+ }
10
+
11
+ export interface ShortcutsConfig {
12
+ [key: string]: ShortcutConfig | Function;
13
+ }
14
+
15
+ export interface ShortcutsOptions {
16
+ chainDelay?: number;
17
+ }
18
+
19
+ interface Shortcut {
20
+ handler: Function;
21
+ condition: ComputedRef<Boolean>;
22
+ chained: boolean;
23
+ // KeyboardEvent attributes
24
+ key: string;
25
+ ctrlKey: boolean;
26
+ metaKey: boolean;
27
+ shiftKey: boolean;
28
+ altKey: boolean;
29
+ // code?: string
30
+ // keyCode?: number
31
+ }
32
+
33
+ export const defineShortcuts = (config: ShortcutsConfig, options: ShortcutsOptions = {}) => {
34
+ const { macOS, usingInput } = useShortcuts();
35
+
36
+ let shortcuts: Shortcut[] = [];
37
+
38
+ const chainedInputs = ref<any[]>([]);
39
+ const clearChainedInput = () => {
40
+ chainedInputs.value.splice(0, chainedInputs.value.length);
41
+ };
42
+ const debouncedClearChainedInput = useDebounceFn(clearChainedInput, options.chainDelay ?? 800);
43
+
44
+ const onKeyDown = (e: KeyboardEvent) => {
45
+ // Input autocomplete triggers a keydown event
46
+ if (!e.key) {
47
+ return;
48
+ }
49
+
50
+ const alphabeticalKey = /^[a-z]{1}$/i.test(e.key);
51
+
52
+ let chainedKey;
53
+ chainedInputs.value.push(e.key);
54
+ // try matching a chained shortcut
55
+ if (chainedInputs.value.length >= 2) {
56
+ chainedKey = chainedInputs.value.slice(-2).join("-");
57
+
58
+ for (const shortcut of shortcuts.filter((s) => s.chained)) {
59
+ if (shortcut.key !== chainedKey) {
60
+ continue;
61
+ }
62
+
63
+ if (shortcut.condition.value) {
64
+ e.preventDefault();
65
+ shortcut.handler();
66
+ }
67
+ clearChainedInput();
68
+ return;
69
+ }
70
+ }
71
+
72
+ // try matching a standard shortcut
73
+ for (const shortcut of shortcuts.filter((s) => !s.chained)) {
74
+ if (e.key.toLowerCase() !== shortcut.key) {
75
+ continue;
76
+ }
77
+ if (e.metaKey !== shortcut.metaKey) {
78
+ continue;
79
+ }
80
+ if (e.ctrlKey !== shortcut.ctrlKey) {
81
+ continue;
82
+ }
83
+ // shift modifier is only checked in combination with alphabetical keys
84
+ // (shift with non-alphabetical keys would change the key)
85
+ if (alphabeticalKey && e.shiftKey !== shortcut.shiftKey) {
86
+ continue;
87
+ }
88
+ // alt modifier changes the combined key anyways
89
+ // if (e.altKey !== shortcut.altKey) { continue }
90
+
91
+ if (shortcut.condition.value) {
92
+ e.preventDefault();
93
+ shortcut.handler();
94
+ }
95
+ clearChainedInput();
96
+ return;
97
+ }
98
+
99
+ debouncedClearChainedInput();
100
+ };
101
+
102
+ // Map config to full detailled shortcuts
103
+ shortcuts = Object.entries(config)
104
+ .map(([key, shortcutConfig]) => {
105
+ if (!shortcutConfig) {
106
+ return null;
107
+ }
108
+
109
+ // Parse key and modifiers
110
+ let shortcut: Partial<Shortcut>;
111
+
112
+ if (key.includes("-") && key.includes("_")) {
113
+ console.trace("[Shortcut] Invalid key");
114
+ return null;
115
+ }
116
+
117
+ const chained = key.includes("-");
118
+ if (chained) {
119
+ shortcut = {
120
+ key: key.toLowerCase(),
121
+ metaKey: false,
122
+ ctrlKey: false,
123
+ shiftKey: false,
124
+ altKey: false,
125
+ };
126
+ } else {
127
+ const keySplit = key
128
+ .toLowerCase()
129
+ .split("_")
130
+ .map((k) => k);
131
+ shortcut = {
132
+ key: keySplit.filter((k) => !["meta", "ctrl", "shift", "alt"].includes(k)).join("_"),
133
+ metaKey: keySplit.includes("meta"),
134
+ ctrlKey: keySplit.includes("ctrl"),
135
+ shiftKey: keySplit.includes("shift"),
136
+ altKey: keySplit.includes("alt"),
137
+ };
138
+ }
139
+ shortcut.chained = chained;
140
+
141
+ // Convert Meta to Ctrl for non-MacOS
142
+ if (!macOS.value && shortcut.metaKey && !shortcut.ctrlKey) {
143
+ shortcut.metaKey = false;
144
+ shortcut.ctrlKey = true;
145
+ }
146
+
147
+ // Retrieve handler function
148
+ if (typeof shortcutConfig === "function") {
149
+ shortcut.handler = shortcutConfig;
150
+ } else if (typeof shortcutConfig === "object") {
151
+ shortcut = { ...shortcut, handler: shortcutConfig.handler };
152
+ }
153
+
154
+ if (!shortcut.handler) {
155
+ console.trace("[Shortcut] Invalid value");
156
+ return null;
157
+ }
158
+
159
+ // Create shortcut computed
160
+ const conditions: ComputedRef<Boolean>[] = [];
161
+ if (!(shortcutConfig as ShortcutConfig).usingInput) {
162
+ conditions.push(logicNot(usingInput));
163
+ } else if (typeof (shortcutConfig as ShortcutConfig).usingInput === "string") {
164
+ conditions.push(
165
+ computed(() => usingInput.value === (shortcutConfig as ShortcutConfig).usingInput)
166
+ );
167
+ }
168
+ shortcut.condition = logicAnd(
169
+ ...conditions,
170
+ ...((shortcutConfig as ShortcutConfig).whenever || [])
171
+ );
172
+
173
+ return shortcut as Shortcut;
174
+ })
175
+ .filter(Boolean) as Shortcut[];
176
+
177
+ useEventListener("keydown", onKeyDown);
178
+ };
179
+ `;
180
+
181
+ export const USE_SHORTCUTS = `export const _useShortcuts = () => {
182
+ const macOS = computed(
183
+ () =>
184
+ process.client && navigator && navigator.userAgent && navigator.userAgent.match(/Macintosh;/)
185
+ );
186
+
187
+ const metaSymbol = ref(" ");
188
+
189
+ const activeElement = useActiveElement();
190
+ const usingInput = computed(() => {
191
+ const usingInput = !!(
192
+ activeElement.value?.tagName === "INPUT" ||
193
+ activeElement.value?.tagName === "TEXTAREA" ||
194
+ activeElement.value?.contentEditable === "true"
195
+ );
196
+
197
+ if (usingInput) {
198
+ return ((activeElement.value as any)?.name as string) || true;
199
+ }
200
+
201
+ return false;
202
+ });
203
+
204
+ onMounted(() => {
205
+ metaSymbol.value = macOS.value ? "⌘" : "Ctrl";
206
+ });
207
+
208
+ return {
209
+ macOS,
210
+ metaSymbol,
211
+ activeElement,
212
+ usingInput,
213
+ };
214
+ };
215
+
216
+ export const useShortcuts = createSharedComposable(_useShortcuts);
217
+ `;
@@ -0,0 +1,12 @@
1
+ import fse from "fs-extra";
2
+
3
+ import { DEFINE_SHORTCUT, USE_SHORTCUTS } from "../templates/shortcuts";
4
+
5
+ export const addShortcutFiles = async (cwd = process.cwd()) => {
6
+ // ensure that the composable folder exists
7
+ await fse.ensureDir(`${cwd}/composables`);
8
+ // write the defineShortcuts composable
9
+ await fse.writeFile(`${cwd}/composables/defineShortcuts.ts`, DEFINE_SHORTCUT, "utf-8");
10
+ // write the useShortcuts composable
11
+ await fse.writeFile(`${cwd}/composables/useShortcuts.ts`, USE_SHORTCUTS, "utf-8");
12
+ };
@@ -3,7 +3,6 @@ export const INIT_DEV_DEPS = [
3
3
  "typescript",
4
4
  "tailwindcss-animate",
5
5
  "nuxt-icon",
6
- "nuxt-lodash",
7
6
  "prettier-plugin-tailwindcss",
8
7
  "prettier",
9
8
  "@nuxtjs/tailwindcss",
@@ -16,7 +15,6 @@ export const INIT_MODULES = [
16
15
  "@nuxtjs/tailwindcss",
17
16
  "@nuxtjs/color-mode",
18
17
  "@vueuse/nuxt",
19
- "nuxt-lodash",
20
18
  "nuxt-icon",
21
19
  ];
22
20