ui-thing 0.0.22 → 0.0.23

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.
@@ -1,217 +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
- `;
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
+ `;
package/src/types.ts CHANGED
@@ -15,9 +15,9 @@ export type InitOptions = { force?: boolean };
15
15
  export type Component = {
16
16
  name: string;
17
17
  value: string;
18
- deps: string[];
19
- devDeps: string[];
20
- nuxtModules: string[];
18
+ deps?: string[];
19
+ devDeps?: string[];
20
+ nuxtModules?: string[];
21
21
  instructions?: string[];
22
22
  files: Composable[];
23
23
  utils: Composable[];
@@ -1,8 +1,8 @@
1
- import axios from "axios";
2
-
3
- import { Component } from "../types";
4
-
5
- export const fetchComponents = async () => {
6
- const { data } = await axios.get<Component[]>("https://ui-thing.behonbaker.com/api/components");
7
- return data;
8
- };
1
+ import axios from "axios";
2
+
3
+ import { Component } from "../types";
4
+
5
+ export const fetchComponents = async () => {
6
+ const { data } = await axios.get<Component[]>("https://ui-thing.behonbaker.com/api/components");
7
+ return data;
8
+ };
@@ -1,19 +1,19 @@
1
- import ora from "ora";
2
- import prompts from "prompts";
3
-
4
- import allComponents from "../comps";
5
- import { Component } from "../types";
6
-
7
- export const promptUserForComponents = async (): Promise<string[]> => {
8
- const { components } = await prompts({
9
- type: "autocompleteMultiselect",
10
- name: "components",
11
- message: "Select the components you want to add",
12
- choices: allComponents.map((c: Component) => ({ title: c.name, value: c.value })),
13
- onRender(kleur) {
14
- // @ts-ignore
15
- this.msg = kleur.bgCyan(" Choose components ") + " Select the components you want to add";
16
- },
17
- });
18
- return components;
19
- };
1
+ import ora from "ora";
2
+ import prompts from "prompts";
3
+
4
+ import allComponents from "../comps";
5
+ import { Component } from "../types";
6
+
7
+ export const promptUserForComponents = async (): Promise<string[]> => {
8
+ const { components } = await prompts({
9
+ type: "autocompleteMultiselect",
10
+ name: "components",
11
+ message: "Select the components you want to add",
12
+ choices: allComponents.map((c: Component) => ({ title: c.name, value: c.value })),
13
+ onRender(kleur) {
14
+ // @ts-ignore
15
+ this.msg = kleur.bgCyan(" Choose components ") + " Select the components you want to add";
16
+ },
17
+ });
18
+ return components;
19
+ };
@@ -1,73 +1,73 @@
1
- import kleur from "kleur";
2
- import prompts from "prompts";
3
-
4
- import { CSS_THEME_OPTIONS } from "./constants";
5
-
6
- export const initPrompts = async () => {
7
- const response = await prompts([
8
- {
9
- name: "theme",
10
- type: "autocomplete",
11
- message: "Which theme do you want to start with?",
12
- choices: CSS_THEME_OPTIONS,
13
- },
14
- {
15
- name: "tailwindCSSLocation",
16
- type: "text",
17
- message: "Where is your tailwind.css file located?",
18
- initial: "assets/css/tailwind.css",
19
- },
20
- {
21
- name: "tailwindConfigLocation",
22
- type: "text",
23
- message: "Where is your tailwind.config file located?",
24
- initial: "tailwind.config.js",
25
- },
26
- {
27
- name: "componentsLocation",
28
- type: "text",
29
- message: "Where should your components be stored?",
30
- initial: "components/Ui",
31
- },
32
- {
33
- name: "composablesLocation",
34
- type: "text",
35
- message: "Where should your composables be stored?",
36
- initial: "composables",
37
- },
38
- {
39
- name: "utilsLocation",
40
- type: "text",
41
- message: "Where should your utils be stored?",
42
- initial: "utils",
43
- },
44
- {
45
- name: "force",
46
- type: "confirm",
47
- message: "Should we just replace component files if they already exist?",
48
- initial: true,
49
- },
50
- {
51
- name: "useDefaultFilename",
52
- type: "confirm",
53
- message: "Would you like to use the default filename when adding components?",
54
- initial: true,
55
- },
56
- {
57
- name: "packageManager",
58
- type: "select",
59
- message: "Which package manager do you use?",
60
- choices: [
61
- { title: "NPM", value: "npm" },
62
- { title: "Yarn", value: "yarn" },
63
- { title: "PNPM", value: "pnpm" },
64
- { title: "Bun", value: "bun" },
65
- ],
66
- },
67
- ]);
68
- if (!response || Object.keys(response).length < 9) {
69
- console.log(kleur.red("Incomplete configuration submitted. Exiting..."));
70
- return process.exit(0);
71
- }
72
- return response;
73
- };
1
+ import kleur from "kleur";
2
+ import prompts from "prompts";
3
+
4
+ import { CSS_THEME_OPTIONS } from "./constants";
5
+
6
+ export const initPrompts = async () => {
7
+ const response = await prompts([
8
+ {
9
+ name: "theme",
10
+ type: "autocomplete",
11
+ message: "Which theme do you want to start with?",
12
+ choices: CSS_THEME_OPTIONS,
13
+ },
14
+ {
15
+ name: "tailwindCSSLocation",
16
+ type: "text",
17
+ message: "Where is your tailwind.css file located?",
18
+ initial: "assets/css/tailwind.css",
19
+ },
20
+ {
21
+ name: "tailwindConfigLocation",
22
+ type: "text",
23
+ message: "Where is your tailwind.config file located?",
24
+ initial: "tailwind.config.js",
25
+ },
26
+ {
27
+ name: "componentsLocation",
28
+ type: "text",
29
+ message: "Where should your components be stored?",
30
+ initial: "components/Ui",
31
+ },
32
+ {
33
+ name: "composablesLocation",
34
+ type: "text",
35
+ message: "Where should your composables be stored?",
36
+ initial: "composables",
37
+ },
38
+ {
39
+ name: "utilsLocation",
40
+ type: "text",
41
+ message: "Where should your utils be stored?",
42
+ initial: "utils",
43
+ },
44
+ {
45
+ name: "force",
46
+ type: "confirm",
47
+ message: "Should we just replace component files if they already exist?",
48
+ initial: true,
49
+ },
50
+ {
51
+ name: "useDefaultFilename",
52
+ type: "confirm",
53
+ message: "Would you like to use the default filename when adding components?",
54
+ initial: true,
55
+ },
56
+ {
57
+ name: "packageManager",
58
+ type: "select",
59
+ message: "Which package manager do you use?",
60
+ choices: [
61
+ { title: "NPM", value: "npm" },
62
+ { title: "Yarn", value: "yarn" },
63
+ { title: "PNPM", value: "pnpm" },
64
+ { title: "Bun", value: "bun" },
65
+ ],
66
+ },
67
+ ]);
68
+ if (!response || Object.keys(response).length < 9) {
69
+ console.log(kleur.red("Incomplete configuration submitted. Exiting..."));
70
+ return process.exit(0);
71
+ }
72
+ return response;
73
+ };