From 9f4dff04178a9bc569baa22b23777d668b76072e Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Fri, 29 Sep 2023 10:14:49 +0100 Subject: [PATCH] Improve alias support --- src/App.tsx | 78 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 19 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 747f2cf..96736df 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,10 +1,20 @@ -import {ChangeEventHandler, KeyboardEventHandler, ReactNode, useCallback, useEffect, useRef, useState} from "react"; +import { + ChangeEventHandler, Dispatch, + KeyboardEventHandler, + MouseEventHandler, + ReactNode, SetStateAction, + useCallback, + useEffect, + useRef, + 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"; +// TODO: Fix this for wrapping items, esp on phones const height = 35; function MenuList(props: MenuListProps) { @@ -80,6 +90,10 @@ export function LargeSelect() { /> {selected &&
+
+ +
+ {selected.data.aliases?.map(alias => (
@@ -87,33 +101,54 @@ export function LargeSelect() { ))}
- - [[ - {selected.label} - | - - - - ]] - +
}
) } -function CustomAliasField({selected}: { +function CustomAlias({selected}: { selected: Option }) { - const [content, setContent] = useState(''); - const [width, setWidth] = useState(0); - const span = useRef(null); + const [alias, setAlias] = useState(''); // Reset when selection changes useEffect(() => { - setContent(''); + 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); @@ -141,7 +176,8 @@ function CustomAliasField({selected}: { }} ref={span}>{content} + type="text" style={{width: `calc(${width}px + 0.25rem)`}} autoFocus onChange={changeHandler} + onKeyDown={onCustomElementKeyDown}/> ); } @@ -154,15 +190,19 @@ function Alias({ alias: string }) { const onClick = useCallback(() => { - navigator.clipboard.writeText(`[[${original}|${alias}]]`) + if (alias) { + navigator.clipboard.writeText(`[[${original}|${alias}]]`) + } else { + navigator.clipboard.writeText(`[[${original}]]`) + } }, [original, alias]); return ( [[ {original} - | - {alias} + {alias && <>| + {alias}} ]] );