Skip to content

Commit

Permalink
chore: Changed formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankeos committed Oct 5, 2024
1 parent 9ccbe0c commit f7e9f2b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"trailingComma": "all",
"tabWidth": 2,
"printWidth": 100,
"semi": false,
"semi": true,
"singleQuote": true,
"useTabs": false,
"arrowParens": "avoid",
Expand Down
8 changes: 4 additions & 4 deletions dev/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render } from 'solid-js/web'
import './styles.css'
import { render } from 'solid-js/web';
import './styles.css';

import App from './App'
import App from './App';

render(() => <App />, document.getElementById('root')!)
render(() => <App />, document.getElementById('root')!);
12 changes: 6 additions & 6 deletions dev/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineConfig } from 'vite'
import path from 'node:path'
import solidPlugin from 'vite-plugin-solid'
import { defineConfig } from 'vite';
import path from 'node:path';
import solidPlugin from 'vite-plugin-solid';

export default defineConfig({
resolve: {
Expand All @@ -14,7 +14,7 @@ export default defineConfig({
name: 'Reaplace env variables',
transform(code, id) {
if (id.includes('node_modules')) {
return code
return code;
}
return code
.replace(/process\.env\.SSR/g, 'false')
Expand All @@ -24,7 +24,7 @@ export default defineConfig({
.replace(/import\.meta\.env\.SSR/g, 'false')
.replace(/import\.meta\.env\.DEV/g, 'true')
.replace(/import\.meta\.env\.PROD/g, 'false')
.replace(/import\.meta\.env\.NODE_ENV/g, '"development"')
.replace(/import\.meta\.env\.NODE_ENV/g, '"development"');
},
},
],
Expand All @@ -34,4 +34,4 @@ export default defineConfig({
build: {
target: 'esnext',
},
})
});
30 changes: 15 additions & 15 deletions src/use-click-outside/use-click-outside.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createEffect, createSignal, onCleanup } from 'solid-js'
import { createEffect, createSignal, onCleanup } from 'solid-js';

const DEFAULT_EVENTS = ['mousedown', 'touchstart']
const DEFAULT_EVENTS = ['mousedown', 'touchstart'];

/**
* A hook that listens to click events outside of a specified element or elements.
Expand All @@ -20,29 +20,29 @@ export function useClickOutside<T extends HTMLElement = any>(
*
* Reference: https://docs.solidjs.com/concepts/refs#signals-as-refs
*/
const [ref, setRef] = createSignal<T>()
const [ref, setRef] = createSignal<T>();

createEffect(() => {
console.log(ref, 'wowowow')
console.log(ref, 'wowowow');
const listener = (event: any) => {
const { target } = event ?? {}
const { target } = event ?? {};
if (Array.isArray(nodes)) {
const shouldIgnore =
target?.hasAttribute('data-ignore-outside-clicks') ||
(!document.body.contains(target) && target.tagName !== 'HTML')
const shouldTrigger = nodes.every(node => !!node && !event.composedPath().includes(node))
shouldTrigger && !shouldIgnore && handler()
(!document.body.contains(target) && target.tagName !== 'HTML');
const shouldTrigger = nodes.every(node => !!node && !event.composedPath().includes(node));
shouldTrigger && !shouldIgnore && handler();
} else if (ref() && !ref()!.contains(target)) {
handler()
handler();
}
}
};

;(events || DEFAULT_EVENTS).forEach(fn => document.addEventListener(fn, listener))
(events || DEFAULT_EVENTS).forEach(fn => document.addEventListener(fn, listener));

onCleanup(() => {
;(events || DEFAULT_EVENTS).forEach(fn => document.removeEventListener(fn, listener))
})
})
(events || DEFAULT_EVENTS).forEach(fn => document.removeEventListener(fn, listener));
});
});

return setRef
return setRef;
}
84 changes: 42 additions & 42 deletions src/use-hotkeys/use-hotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,108 @@
// ===========================================================================

export type KeyboardModifiers = {
alt: boolean
ctrl: boolean
meta: boolean
mod: boolean
shift: boolean
}
alt: boolean;
ctrl: boolean;
meta: boolean;
mod: boolean;
shift: boolean;
};

export type Hotkey = KeyboardModifiers & {
key?: string
}
key?: string;
};

type CheckHotkeyMatch = (event: KeyboardEvent) => boolean
type CheckHotkeyMatch = (event: KeyboardEvent) => boolean;

export function parseHotkey(hotkey: string): Hotkey {
const keys = hotkey
.toLowerCase()
.split('+')
.map(part => part.trim())
.map(part => part.trim());

const modifiers: KeyboardModifiers = {
alt: keys.includes('alt'),
ctrl: keys.includes('ctrl'),
meta: keys.includes('meta'),
mod: keys.includes('mod'),
shift: keys.includes('shift'),
}
};

const reservedKeys = ['alt', 'ctrl', 'meta', 'shift', 'mod']
const reservedKeys = ['alt', 'ctrl', 'meta', 'shift', 'mod'];

const freeKey = keys.find(key => !reservedKeys.includes(key))
const freeKey = keys.find(key => !reservedKeys.includes(key));

return {
...modifiers,
key: freeKey,
}
};
}

function isExactHotkey(hotkey: Hotkey, event: KeyboardEvent): boolean {
const { alt, ctrl, meta, mod, shift, key } = hotkey
const { altKey, ctrlKey, metaKey, shiftKey, key: pressedKey } = event
const { alt, ctrl, meta, mod, shift, key } = hotkey;
const { altKey, ctrlKey, metaKey, shiftKey, key: pressedKey } = event;

if (alt !== altKey) {
return false
return false;
}

if (mod) {
if (!ctrlKey && !metaKey) {
return false
return false;
}
} else {
if (ctrl !== ctrlKey) {
return false
return false;
}
if (meta !== metaKey) {
return false
return false;
}
}
if (shift !== shiftKey) {
return false
return false;
}

if (
key &&
(pressedKey.toLowerCase() === key.toLowerCase() ||
event.code.replace('Key', '').toLowerCase() === key.toLowerCase())
) {
return true
return true;
}

return false
return false;
}

export function getHotkeyMatcher(hotkey: string): CheckHotkeyMatch {
return event => isExactHotkey(parseHotkey(hotkey), event)
return event => isExactHotkey(parseHotkey(hotkey), event);
}

export interface HotkeyItemOptions {
preventDefault?: boolean
preventDefault?: boolean;
}

export function getHotkeyHandler(hotkeys: HotkeyItem[]) {
// TODO fix the any
return (event: any) => {
const _event = 'nativeEvent' in event ? event.nativeEvent : event
const _event = 'nativeEvent' in event ? event.nativeEvent : event;
hotkeys.forEach(([hotkey, handler, options = { preventDefault: true }]) => {
if (getHotkeyMatcher(hotkey)(_event)) {
if (options.preventDefault) {
event.preventDefault()
event.preventDefault();
}

handler(_event)
handler(_event);
}
})
}
});
};
}

// ===========================================================================
// The hook
// ===========================================================================
import { createEffect, onCleanup } from 'solid-js'
import { createEffect, onCleanup } from 'solid-js';

export type HotkeyItem = [string, (event: KeyboardEvent) => void, HotkeyItemOptions?]
export type HotkeyItem = [string, (event: KeyboardEvent) => void, HotkeyItemOptions?];

function shouldFireEvent(
event: KeyboardEvent,
Expand All @@ -113,13 +113,13 @@ function shouldFireEvent(
) {
if (event.target instanceof HTMLElement) {
if (triggerOnContentEditable) {
return !tagsToIgnore.includes(event.target.tagName)
return !tagsToIgnore.includes(event.target.tagName);
}

return !event.target.isContentEditable && !tagsToIgnore.includes(event.target.tagName)
return !event.target.isContentEditable && !tagsToIgnore.includes(event.target.tagName);
}

return true
return true;
}

/**
Expand All @@ -142,16 +142,16 @@ export function useHotkeys(
shouldFireEvent(event, tagsToIgnore, triggerOnContentEditable)
) {
if (options.preventDefault) {
event.preventDefault()
event.preventDefault();
}

handler(event)
handler(event);
}
})
}
});
};

document.documentElement.addEventListener('keydown', keydownListener)
document.documentElement.addEventListener('keydown', keydownListener);

onCleanup(() => document.documentElement.removeEventListener('keydown', keydownListener))
})
onCleanup(() => document.documentElement.removeEventListener('keydown', keydownListener));
});
}
50 changes: 25 additions & 25 deletions src/use-os/use-os.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
import { Accessor, createEffect, createSignal } from 'solid-js'
import { Accessor, createEffect, createSignal } from 'solid-js';

export type OS = 'undetermined' | 'macos' | 'ios' | 'windows' | 'android' | 'linux'
export type OS = 'undetermined' | 'macos' | 'ios' | 'windows' | 'android' | 'linux';

function isMacOS(userAgent: string): boolean {
const macosPattern = /(Macintosh)|(MacIntel)|(MacPPC)|(Mac68K)/i
const macosPattern = /(Macintosh)|(MacIntel)|(MacPPC)|(Mac68K)/i;

return macosPattern.test(userAgent)
return macosPattern.test(userAgent);
}

function isIOS(userAgent: string): boolean {
const iosPattern = /(iPhone)|(iPad)|(iPod)/i
const iosPattern = /(iPhone)|(iPad)|(iPod)/i;

return iosPattern.test(userAgent)
return iosPattern.test(userAgent);
}

function isWindows(userAgent: string): boolean {
const windowsPattern = /(Win32)|(Win64)|(Windows)|(WinCE)/i
const windowsPattern = /(Win32)|(Win64)|(Windows)|(WinCE)/i;

return windowsPattern.test(userAgent)
return windowsPattern.test(userAgent);
}

function isAndroid(userAgent: string): boolean {
const androidPattern = /Android/i
const androidPattern = /Android/i;

return androidPattern.test(userAgent)
return androidPattern.test(userAgent);
}

function isLinux(userAgent: string): boolean {
const linuxPattern = /Linux/i
const linuxPattern = /Linux/i;

return linuxPattern.test(userAgent)
return linuxPattern.test(userAgent);
}

function getOS(): OS {
if (typeof window === 'undefined') {
return 'undetermined'
return 'undetermined';
}

const { userAgent } = window.navigator
const { userAgent } = window.navigator;

if (isIOS(userAgent) || (isMacOS(userAgent) && 'ontouchend' in document)) {
return 'ios'
return 'ios';
}
if (isMacOS(userAgent)) {
return 'macos'
return 'macos';
}
if (isWindows(userAgent)) {
return 'windows'
return 'windows';
}
if (isAndroid(userAgent)) {
return 'android'
return 'android';
}
if (isLinux(userAgent)) {
return 'linux'
return 'linux';
}

return 'undetermined'
return 'undetermined';
}

interface UseOsOptions {
getValueInEffect: boolean
getValueInEffect: boolean;
}

/**
Expand All @@ -69,13 +69,13 @@ interface UseOsOptions {
* @returns The current operating system.
*/
export function useOs(options: UseOsOptions = { getValueInEffect: true }): Accessor<OS> {
const [value, setValue] = createSignal<OS>(options.getValueInEffect ? 'undetermined' : getOS())
const [value, setValue] = createSignal<OS>(options.getValueInEffect ? 'undetermined' : getOS());

createEffect(() => {
if (options.getValueInEffect) {
setValue(getOS)
setValue(getOS);
}
})
});

return value
return value;
}

0 comments on commit f7e9f2b

Please sign in to comment.