From 26d5677546eeed50fe8b8ea0eae4233d3b2e0b84 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Fri, 13 Oct 2023 17:17:31 +0100 Subject: [PATCH] Split out aliases components --- src/App.tsx | 121 +----------------------------------------------- src/aliases.tsx | 120 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 119 deletions(-) create mode 100644 src/aliases.tsx diff --git a/src/App.tsx b/src/App.tsx index 6b235e1..81d334b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,18 +1,10 @@ -import { - ChangeEventHandler, Dispatch, - KeyboardEventHandler, - MouseEventHandler, - ReactNode, SetStateAction, - useCallback, - useEffect, - useRef, - useState -} from "react"; +import {ReactNode, useCallback, useState} from "react"; import {FixedSizeList as List} from "react-window"; import Select, {createFilter, MenuListProps} from "react-select"; import * as R from 'ramda'; import {useQuery} from "@tanstack/react-query"; +import {Alias, CustomAlias, Option} from "./aliases"; // TODO: Fix this for wrapping items, esp on phones const height = 35; @@ -44,15 +36,6 @@ function MenuList(props: MenuListProps) { ); } -interface Option { - label: string, - value: string, - data: { - backlinks: unknown[] - aliases?: string[] - } -} - export function LargeSelect() { const { data: options, @@ -110,103 +93,3 @@ export function LargeSelect() { ) } - -function CustomAlias({selected}: { - selected: Option -}) { - const [alias, setAlias] = useState(''); - - // Reset when selection changes - useEffect(() => { - setAlias(''); - }, [selected.value]); - - const onClick: MouseEventHandler = useCallback((e) => { - if ((e.target as HTMLElement).tagName == 'SPAN' && alias.length > 0) { - navigator.clipboard.writeText(`[[${selected!.label}|${alias}]]`); - } - }, [selected.value, selected.label, alias]); - - return ( - - [[ - {selected.label} - | - - - - ]] - - ) -} - -function CustomAliasField({ - selected, - content, - setContent - }: { - selected: Option, - content: string, - setContent: Dispatch>, -}) { - const [width, setWidth] = useState(0); - const span = useRef(null); - - // Resize on change of content - useEffect(() => { - setWidth(span.current!.offsetWidth); - // setWidth(content.length + 'ch'); - }, [content]); - - const changeHandler: ChangeEventHandler = useCallback(evt => { - setContent(evt.target.value); - }, []); - - const onCustomElementKeyDown: KeyboardEventHandler = useCallback((e) => { - if (e.key == 'Enter') { - navigator.clipboard.writeText(`[[${selected!.label}|${content}]]`); - (e.target as HTMLInputElement).blur() - } - }, [selected.value, selected.label, content]); - - return ( - - {content} - - - ); -} - -function Alias({ - original, - alias - }: { - original: string, - alias?: string -}) { - const onClick = useCallback(() => { - if (alias) { - navigator.clipboard.writeText(`[[${original}|${alias}]]`) - } else { - navigator.clipboard.writeText(`[[${original}]]`) - } - }, [original, alias]); - - return ( - - [[ - {original} - {alias && <>| - {alias}} - ]] - - ); -} diff --git a/src/aliases.tsx b/src/aliases.tsx new file mode 100644 index 0000000..2215e48 --- /dev/null +++ b/src/aliases.tsx @@ -0,0 +1,120 @@ +import { + ChangeEventHandler, + Dispatch, + KeyboardEventHandler, + MouseEventHandler, + SetStateAction, + useCallback, + useEffect, + useRef, + useState +} from "react"; + +export interface Option { + label: string, + value: string, + data: { + backlinks: unknown[] + aliases?: string[] + } +} + +export function CustomAlias({selected}: { + selected: Option +}) { + const [alias, setAlias] = useState(''); + + // Reset when selection changes + useEffect(() => { + setAlias(''); + }, [selected.value]); + + const onClick: MouseEventHandler = useCallback((e) => { + if ((e.target as HTMLElement).tagName == 'SPAN' && alias.length > 0) { + navigator.clipboard.writeText(`[[${selected!.label}|${alias}]]`); + } + }, [selected.value, selected.label, alias]); + + return ( + + [[ + {selected.label} + | + + + + ]] + + ) +} + +function CustomAliasField({ + selected, + content, + setContent + }: { + selected: Option, + content: string, + setContent: Dispatch>, +}) { + const [width, setWidth] = useState(0); + const span = useRef(null); + + // Resize on change of content + useEffect(() => { + setWidth(span.current!.offsetWidth); + // setWidth(content.length + 'ch'); + }, [content]); + + const changeHandler: ChangeEventHandler = useCallback(evt => { + setContent(evt.target.value); + }, []); + + const onCustomElementKeyDown: KeyboardEventHandler = useCallback((e) => { + if (e.key == 'Enter') { + navigator.clipboard.writeText(`[[${selected!.label}|${content}]]`); + (e.target as HTMLInputElement).blur() + } + }, [selected.value, selected.label, content]); + + return ( + + {content} + + + ); +} + +export function Alias({ + original, + alias + }: { + original: string, + alias?: string +}) { + const onClick = useCallback(() => { + if (alias) { + navigator.clipboard.writeText(`[[${original}|${alias}]]`) + } else { + navigator.clipboard.writeText(`[[${original}]]`) + } + }, [original, alias]); + + return ( + + [[ + {original} + {alias && <>| + {alias}} + ]] + + ); +}