Improved header editor

This commit is contained in:
Gregory Schier
2023-03-14 00:54:41 -07:00
parent 4a949b2720
commit e67118bfa9
3 changed files with 55 additions and 29 deletions

View File

@@ -15,10 +15,6 @@ type PairWithId = { header: Partial<HttpHeader>; id: string };
export function HeaderEditor({ request, className }: Props) { export function HeaderEditor({ request, className }: Props) {
const updateRequest = useUpdateRequest(request); const updateRequest = useUpdateRequest(request);
const saveHeaders = (pairs: PairWithId[]) => {
const headers = pairs.map((p) => ({ name: '', value: '', ...p.header }));
updateRequest.mutate({ headers });
};
const newPair = () => { const newPair = () => {
return { header: { name: '', value: '' }, id: Math.random().toString() }; return { header: { name: '', value: '' }, id: Math.random().toString() };
@@ -31,7 +27,8 @@ export function HeaderEditor({ request, className }: Props) {
const setPairsAndSave = (fn: (pairs: PairWithId[]) => PairWithId[]) => { const setPairsAndSave = (fn: (pairs: PairWithId[]) => PairWithId[]) => {
setPairs((oldPairs) => { setPairs((oldPairs) => {
const newPairs = fn(oldPairs); const newPairs = fn(oldPairs);
saveHeaders(newPairs); const headers = newPairs.map((p) => ({ name: '', value: '', ...p.header }));
updateRequest.mutate({ headers });
return newPairs; return newPairs;
}); });
}; };
@@ -47,7 +44,7 @@ export function HeaderEditor({ request, className }: Props) {
useEffect(() => { useEffect(() => {
const lastPair = pairs[pairs.length - 1]; const lastPair = pairs[pairs.length - 1];
if (lastPair === undefined) { if (lastPair === undefined) {
setPairs([newPair()]); setPairsAndSave((pairs) => [...pairs, newPair()]);
return; return;
} }
@@ -63,14 +60,18 @@ export function HeaderEditor({ request, className }: Props) {
return ( return (
<div className={classnames(className, 'pb-6 grid')}> <div className={classnames(className, 'pb-6 grid')}>
<VStack space={2}> <VStack space={2}>
{pairs.map((p, i) => ( {pairs.map((p, i) => {
<FormRow const isLast = i === pairs.length - 1;
key={p.id} return (
pair={p} <FormRow
onChange={handleChangeHeader} key={p.id}
onDelete={i < pairs.length - 1 ? handleDelete : undefined} pair={p}
/> isLast={isLast}
))} onChange={handleChangeHeader}
onDelete={isLast ? undefined : handleDelete}
/>
);
})}
</VStack> </VStack>
</div> </div>
); );
@@ -80,30 +81,38 @@ function FormRow({
pair, pair,
onChange, onChange,
onDelete, onDelete,
onFocus,
isLast,
}: { }: {
pair: PairWithId; pair: PairWithId;
onChange: (pair: PairWithId) => void; onChange: (pair: PairWithId) => void;
onDelete?: (pair: PairWithId) => void; onDelete?: (pair: PairWithId) => void;
onFocus?: (pair: PairWithId) => void;
isLast?: boolean;
}) { }) {
return ( return (
<div className="group grid grid-cols-[1fr_1fr_2.5rem] grid-rows-1 gap-2 items-center"> <div className="group grid grid-cols-[1fr_1fr_2.5rem] grid-rows-1 gap-2 items-center">
<Input <Input
hideLabel hideLabel
useEditor={{ useTemplating: true }} containerClassName={classnames(isLast && 'border-dashed')}
name="name"
label="Name"
placeholder="name"
defaultValue={pair.header.name} defaultValue={pair.header.name}
label="Name"
name="name"
onChange={(name) => onChange({ id: pair.id, header: { name } })} onChange={(name) => onChange({ id: pair.id, header: { name } })}
onFocus={() => onFocus?.(pair)}
placeholder="name"
useEditor={{ useTemplating: true }}
/> />
<Input <Input
hideLabel hideLabel
name="value" containerClassName={classnames(isLast && 'border-dashed')}
label="Value"
useEditor={{ useTemplating: true }}
placeholder="value"
defaultValue={pair.header.value} defaultValue={pair.header.value}
label="Value"
name="value"
onChange={(value) => onChange({ id: pair.id, header: { value } })} onChange={(value) => onChange({ id: pair.id, header: { value } })}
onFocus={() => onFocus?.(pair)}
placeholder="value"
useEditor={{ useTemplating: true }}
/> />
{onDelete && ( {onDelete && (
<IconButton <IconButton

View File

@@ -5,8 +5,7 @@ import classnames from 'classnames';
import { EditorView } from 'codemirror'; import { EditorView } from 'codemirror';
import { formatSdl } from 'format-graphql'; import { formatSdl } from 'format-graphql';
import { useEffect, useRef } from 'react'; import { useEffect, useRef } from 'react';
import { useDebounce, useUnmount } from 'react-use'; import { useUnmount } from 'react-use';
import { debounce } from '../../../lib/debounce';
import { IconButton } from '../IconButton'; import { IconButton } from '../IconButton';
import './Editor.css'; import './Editor.css';
import { baseExtensions, getLanguageExtension, multiLineExtensions } from './extensions'; import { baseExtensions, getLanguageExtension, multiLineExtensions } from './extensions';
@@ -24,6 +23,7 @@ export interface _EditorProps {
tooltipContainer?: HTMLElement; tooltipContainer?: HTMLElement;
useTemplating?: boolean; useTemplating?: boolean;
onChange?: (value: string) => void; onChange?: (value: string) => void;
onFocus?: () => void;
singleLine?: boolean; singleLine?: boolean;
} }
@@ -36,6 +36,7 @@ export function _Editor({
useTemplating, useTemplating,
defaultValue, defaultValue,
onChange, onChange,
onFocus,
className, className,
singleLine, singleLine,
}: _EditorProps) { }: _EditorProps) {
@@ -72,6 +73,7 @@ export function _Editor({
placeholder, placeholder,
singleLine, singleLine,
onChange, onChange,
onFocus,
contentType, contentType,
useTemplating, useTemplating,
}), }),
@@ -100,7 +102,7 @@ export function _Editor({
{contentType?.includes('graphql') && ( {contentType?.includes('graphql') && (
<IconButton <IconButton
icon="eye" icon="eye"
className="absolute right-3 bottom-3 z-10" className="absolute right-0 bottom-0 z-10"
onClick={() => { onClick={() => {
const doc = cm.current?.view.state.doc ?? ''; const doc = cm.current?.view.state.doc ?? '';
const insert = formatSdl(doc.toString()); const insert = formatSdl(doc.toString());
@@ -118,11 +120,18 @@ function getExtensions({
singleLine, singleLine,
placeholder, placeholder,
onChange, onChange,
onFocus,
contentType, contentType,
useTemplating, useTemplating,
}: Pick< }: Pick<
_EditorProps, _EditorProps,
'singleLine' | 'onChange' | 'contentType' | 'useTemplating' | 'placeholder' | 'readOnly' | 'singleLine'
| 'onChange'
| 'contentType'
| 'useTemplating'
| 'placeholder'
| 'readOnly'
| 'onFocus'
> & { container: HTMLDivElement | null }) { > & { container: HTMLDivElement | null }) {
const ext = getLanguageExtension({ contentType, useTemplating }); const ext = getLanguageExtension({ contentType, useTemplating });
@@ -160,6 +169,13 @@ function getExtensions({
] ]
: []), : []),
// Handle onFocus
EditorView.domEventHandlers({
focus: () => {
onFocus?.();
},
}),
// Handle onChange // Handle onChange
EditorView.updateListener.of((update) => { EditorView.updateListener.of((update) => {
if (typeof onChange === 'function' && update.docChanged) { if (typeof onChange === 'function' && update.docChanged) {

View File

@@ -1,16 +1,17 @@
import classnames from 'classnames'; import classnames from 'classnames';
import type { ReactNode } from 'react'; import type { HTMLAttributes, ReactNode } from 'react';
import type { EditorProps } from './Editor'; import type { EditorProps } from './Editor';
import { Editor } from './Editor'; import { Editor } from './Editor';
import { HStack, VStack } from './Stacks'; import { HStack, VStack } from './Stacks';
interface Props { type Props = Omit<HTMLAttributes<HTMLInputElement>, 'onChange' | 'onFocus'> & {
name: string; name: string;
label: string; label: string;
hideLabel?: boolean; hideLabel?: boolean;
labelClassName?: string; labelClassName?: string;
containerClassName?: string; containerClassName?: string;
onChange?: (value: string) => void; onChange?: (value: string) => void;
onFocus?: () => void;
useEditor?: Pick<EditorProps, 'contentType' | 'useTemplating'>; useEditor?: Pick<EditorProps, 'contentType' | 'useTemplating'>;
defaultValue?: string; defaultValue?: string;
leftSlot?: ReactNode; leftSlot?: ReactNode;
@@ -19,7 +20,7 @@ interface Props {
className?: string; className?: string;
placeholder?: string; placeholder?: string;
autoFocus?: boolean; autoFocus?: boolean;
} };
export function Input({ export function Input({
label, label,