From 04efd82f1dfdda7078e5711088c21b38c5983561 Mon Sep 17 00:00:00 2001 From: MrWindlike Date: Thu, 7 Apr 2022 16:40:41 +0800 Subject: [PATCH] feat: add handlers sort --- .../EventTraitForm/EventHandlerForm.tsx | 56 ++++++++++++++----- .../EventTraitForm/EventTraitForm.tsx | 30 ++++++++++ .../components/DataSource/ApiForm/Basic.tsx | 37 ++++++++++-- 3 files changed, 104 insertions(+), 19 deletions(-) diff --git a/packages/editor/src/components/ComponentForm/EventTraitForm/EventHandlerForm.tsx b/packages/editor/src/components/ComponentForm/EventTraitForm/EventHandlerForm.tsx index 4af7653d..cdd95bec 100644 --- a/packages/editor/src/components/ComponentForm/EventTraitForm/EventHandlerForm.tsx +++ b/packages/editor/src/components/ComponentForm/EventTraitForm/EventHandlerForm.tsx @@ -3,23 +3,38 @@ import { EventWidget } from '@sunmao-ui/editor-sdk'; import { Box, IconButton, VStack } from '@chakra-ui/react'; import { Static } from '@sinclair/typebox'; import { JSONSchema7 } from 'json-schema'; -import { CloseIcon } from '@chakra-ui/icons'; +import { CloseIcon, ArrowUpIcon, ArrowDownIcon } from '@chakra-ui/icons'; import { EventHandlerSchema } from '@sunmao-ui/runtime'; import { ComponentSchema } from '@sunmao-ui/core'; import { formWrapperCSS } from '../style'; import { EditorServices } from '../../../types'; type Props = { + index: number; + size: number; component: ComponentSchema; handler: Static; onChange: (handler: Static) => void; onRemove: () => void; + onUp: () => void; + onDown: () => void; services: EditorServices; schema?: JSONSchema7; }; export const EventHandlerForm: React.FC = props => { - const { schema = EventHandlerSchema, handler, component, onChange, onRemove, services } = props; + const { + index, + size, + schema = EventHandlerSchema, + handler, + component, + services, + onChange, + onRemove, + onUp, + onDown, + } = props; return ( @@ -34,17 +49,32 @@ export const EventHandlerForm: React.FC = props => { onChange={onChange} /> - } - onClick={onRemove} - position="absolute" - right="4" - size="xs" - top="4" - variant="ghost" - /> + + } + size="xs" + variant="ghost" + disabled={index === 0} + onClick={onUp} + /> + } + size="xs" + variant="ghost" + disabled={index === size - 1} + onClick={onDown} + /> + } + onClick={onRemove} + size="xs" + variant="ghost" + /> + ); }; diff --git a/packages/editor/src/components/ComponentForm/EventTraitForm/EventTraitForm.tsx b/packages/editor/src/components/ComponentForm/EventTraitForm/EventTraitForm.tsx index 2624f1d3..eb169bb8 100644 --- a/packages/editor/src/components/ComponentForm/EventTraitForm/EventTraitForm.tsx +++ b/packages/editor/src/components/ComponentForm/EventTraitForm/EventTraitForm.tsx @@ -103,12 +103,42 @@ export const EventTraitForm: React.FC = props => { }) ); }; + + const onSort = (isUp: boolean) => { + const index = component.traits.findIndex(t => t.type === 'core/v1/event'); + const newHandlers = [...handlers]; + const switchedIndex = isUp ? i - 1 : i + 1; + + if (newHandlers[switchedIndex]) { + const temp = newHandlers[switchedIndex]; + newHandlers[switchedIndex] = newHandlers[i]; + newHandlers[i] = temp; + + eventBus.send( + 'operation', + genOperation(registry, 'modifyTraitProperty', { + componentId: component.id, + traitIndex: index, + properties: { + handlers: newHandlers, + }, + }) + ); + } + }; + const onUp = ()=> onSort(true); + const onDown = ()=> onSort(false); + return ( diff --git a/packages/editor/src/components/DataSource/ApiForm/Basic.tsx b/packages/editor/src/components/DataSource/ApiForm/Basic.tsx index 9bd2ce56..d4f401ff 100644 --- a/packages/editor/src/components/DataSource/ApiForm/Basic.tsx +++ b/packages/editor/src/components/DataSource/ApiForm/Basic.tsx @@ -19,7 +19,7 @@ import { EventHandlerForm } from '../../ComponentForm/EventTraitForm/EventHandle import { FetchTraitPropertiesSchema, EventCallBackHandlerSchema, - BaseEventSchema + BaseEventSchema, } from '@sunmao-ui/runtime'; import { Static, Type } from '@sinclair/typebox'; import { EditorServices } from '../../../types'; @@ -69,16 +69,37 @@ export const Basic: React.FC = props => { {(formik.values[type] ?? []).map((handler, i) => { - const onChange = (handler: EventHandler) => { - const newOnComplete = formik.values[type].map((onComplete, index)=> index === i ? handler : onComplete); - formik.setFieldValue(type, newOnComplete); + const onChange = (bewHandler: EventHandler) => { + const newHandlers = formik.values[type].map((handler, index) => + index === i ? bewHandler : handler + ); + formik.setFieldValue(type, newHandlers); formik.submitForm(); }; const onRemove = () => { - const newOnComplete = formik.values[type].filter((_, index)=> i !== index); - formik.setFieldValue(type, newOnComplete); + const newHandlers = formik.values[type].filter((_, index) => i !== index); + formik.setFieldValue(type, newHandlers); formik.submitForm(); }; + const onSort = (isUp: boolean) => { + const newHandlers = [...formik.values[type]]; + const switchedIndex = isUp ? i - 1 : i + 1; + + if (newHandlers[switchedIndex]) { + const temp = newHandlers[switchedIndex]; + newHandlers[switchedIndex] = newHandlers[i]; + newHandlers[i] = temp; + + formik.setFieldValue(type, newHandlers); + formik.submitForm(); + } + }; + const onUp = () => { + onSort(true); + }; + const onDown = () => { + onSort(false); + }; return ( @@ -93,11 +114,15 @@ export const Basic: React.FC = props => {