From 025be2d4a4eb5300d3e437b42528d09d0941fecf Mon Sep 17 00:00:00 2001 From: zhangjinlong Date: Sun, 11 Oct 2020 22:19:27 +0800 Subject: [PATCH] add: delete and reset by contextmenu --- src/pages/editor/SourceBox.tsx | 72 +++++++++++++++++++++++++++++----- src/pages/editor/index.less | 18 +++++++++ 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/src/pages/editor/SourceBox.tsx b/src/pages/editor/SourceBox.tsx index e27a1cf..8f9aea6 100644 --- a/src/pages/editor/SourceBox.tsx +++ b/src/pages/editor/SourceBox.tsx @@ -1,8 +1,17 @@ -import React, { memo, useContext, useEffect, useMemo, useState } from 'react'; +import React, { + FC, + memo, + useCallback, + useContext, + useEffect, + useMemo, + useState, + createRef, +} from 'react'; import { useDrop } from 'react-dnd'; import Draggable, { DraggableData, DraggableEvent } from 'react-draggable'; import GridLayout, { ItemCallback } from 'react-grid-layout'; -import { Tooltip } from 'antd'; +import { Popover, Button, Tooltip } from 'antd'; import { connect } from 'dva'; import DynamicEngine from 'components/DynamicEngine'; import styles from './index.less'; @@ -11,7 +20,7 @@ import { Dispatch } from 'umi'; import { StateWithHistory } from 'redux-undo'; import { dooringContext } from '@/layouts'; interface SourceBoxProps { - pstate: { pointData: { id: string; item: any; point: any }[]; curPoint: any }; + pstate: { pointData: { id: string; item: any; point: any; isMenu?: any }[]; curPoint: any }; cstate: { pointData: { id: string; item: any; point: any }[]; curPoint: any }; scaleNum: number; canvasId: string; @@ -30,11 +39,13 @@ const SourceBox = memo((props: SourceBoxProps) => { const { pstate, scaleNum, canvasId, allType, dispatch, dragState, setDragState, cstate } = props; const context = useContext(dooringContext); - const pointData = pstate ? pstate.pointData : []; + let pointData = pstate ? pstate.pointData : []; const cpointData = cstate ? cstate.pointData : []; const [canvasRect, setCanvasRect] = useState([]); const [isShowTip, setIsShowTip] = useState(true); + const [clonePointData, setPointData] = useState(pointData); + const [isMenu, setIsMenu] = useState(false); const [{ isOver }, drop] = useDrop({ accept: allType, drop: (item: { h: number; type: string; x: number }, monitor) => { @@ -137,7 +148,15 @@ const SourceBox = memo((props: SourceBoxProps) => { } }; }, [context.theme, cpointData, dispatch, pointData]); - + const initSelect: any = (data: any = []) => { + return ( + data && + data.map((itemData: any) => ({ + ...itemData, + isMenu: false, + })) + ); + }; useEffect(() => { let { width, height } = document.getElementById(canvasId)!.getBoundingClientRect(); console.log(width, height); @@ -145,6 +164,7 @@ const SourceBox = memo((props: SourceBoxProps) => { }, [canvasId, context.theme]); useEffect(() => { + setPointData(initSelect(pointData)); let timer = window.setTimeout(() => { setIsShowTip(false); }, 3000); @@ -153,8 +173,24 @@ const SourceBox = memo((props: SourceBoxProps) => { }; }, []); const opacity = isOver ? 0.7 : 1; - // const backgroundColor = isOver ? 1 : 0.7; - + const handleCurrentCard: Function = useCallback( + (e: Event, status: boolean, index: number) => { + e.preventDefault(); + setIsMenu(false); + if (status) { + setIsMenu(true); + } else { + setIsMenu(false); + } + (pointData = initSelect(pointData)) && + Object.keys(pointData).map(keyId => { + (+pointData[+keyId].id === +index && (pointData[+keyId].isMenu = status)) || + (pointData[+keyId].isMenu = false); + }); + setPointData(pointData); + }, + [status, pointData], + ); const render = useMemo(() => { if (context.theme === 'h5') { return ( @@ -199,7 +235,7 @@ const SourceBox = memo((props: SourceBoxProps) => { /> - {pointData.length > 0 ? ( + {clonePointData.length > 0 ? ( { onDragStart={onDragStart} onResizeStop={onResizeStop} > - {pointData.map(value => ( -
+ {clonePointData.map(value => ( +
handleCurrentCard(e, true, value.id)} + > +
+
+ 恢复 +
+
+ 删除 +
+
))} diff --git a/src/pages/editor/index.less b/src/pages/editor/index.less index cdb9a51..9b37948 100644 --- a/src/pages/editor/index.less +++ b/src/pages/editor/index.less @@ -129,6 +129,24 @@ pointer-events: none; } } + .selected { + display: inline-block; + position: absolute; + z-index: 2; + border: 2px solid transparent; + cursor: move; + :global(a) { + display: block; + pointer-events: none; + } + border: 2px solid #06c; + .tooltip { + position: relative; + z-index: 999999; + width: 388px; + text-align: right; + } + } } } .canvasBox2 {