import styled from '@emotion/styled'; import { GlobalToken } from 'antd/es/theme/interface'; import React, { useCallback, useEffect, useState } from 'react'; import { CopyOutlined, DeleteOutlined } from '@ant-design/icons'; import { Button, Modal, theme, Typography } from 'antd'; import { WindowMultipleFilled } from '@fluentui/react-icons'; import { checkNodePopupIsMain } from 'modules/popup/utils'; import { useBuilder, useEditor, getEditorName } from '@tempi/core-editor'; import { PopupEditorV1 } from '@tempi/core-elements'; import { ROOT } from '@tempi/core-renderer'; import { SidebarDrawer, SidebarItem, SideBarMenuProps, t, } from '@tempi/editor-site'; export const PopupMenu: React.FC = ({ title = 'Popup', description, disabled, }) => { const { setDraggingNode, getAttrTracking, openElementMenu, setShowModalSelectPopup, } = useBuilder(); const [open, setOpen] = useState(false); const { useToken } = theme; const { token: themeToken } = useToken(); const { query, actions, nodes } = useEditor((state) => { return { nodes: state.nodes, enabled: state.options.enabled, }; }); const allStandalonePopupIds = [ ...new Set( nodes[ROOT]?.data.nodes.filter( (key) => nodes?.[key]?.data?.name === getEditorName({ PopupEditorV1 }) && !checkNodePopupIsMain(nodes[key] as any), ), ), ]; const handleCloseDrawer = useCallback(() => { setDraggingNode(undefined); setOpen(false); }, [setDraggingNode]); const handleClickPopupItem = (id) => { // hide all popups before select a popup allStandalonePopupIds.forEach((popupId) => { actions.setHidden(popupId, true); }); actions.setHidden(id, false); actions.selectNode(id); handleCloseDrawer(); }; const handleDeletePopup = (id: string) => { const name = nodes[id]?.data?.props?.name ? ( {nodes[id]?.data?.props?.name} ) : ( t('này') ); Modal.confirm({ title: t('Thông báo'), content: (
{t('Bạn muốn xóa popup')} {name} ?
), closable: true, maskClosable: true, okText: t('Xóa'), cancelText: t('common.cancel'), onOk: () => actions.delete(id), }); }; const renderPopupList = () => { if (!allStandalonePopupIds?.length) return null; return ( {allStandalonePopupIds.map((id) => ( handleClickPopupItem(id)}> {query.node(id).get()?.data?.props?.name || id} { e.stopPropagation(); handleDeletePopup(id); }} > ))} ); }; useEffect(() => { if (openElementMenu) setOpen(false); }, [openElementMenu]); return ( <> setOpen(true)} trackingContentName={'PopupBtn'} active={open} tooltip="Popup" disabled={disabled} > { setOpen(false); }} > <> {!!description && {description}} {renderPopupList()} ); }; const PopupList = styled.div` display: flex; flex-wrap: wrap; margin-top: 16px; `; const PopupBtn = styled.div` cursor: pointer; width: 100%; padding: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; border-radius: 4px; `; const PopupTitle = styled.div<{ theme?: GlobalToken; }>( ({ theme }) => ` flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; &:hover { color: ${theme.colorPrimary} !important; path[stroke], rect[stroke], line { stroke: ${theme.colorPrimary} !important; } path:not([stroke]), rect:not([stroke]) { fill: ${theme.colorPrimary} !important; } } `, ); const PopupDeleteBtn = styled.div<{ theme?: GlobalToken; }>( ({ theme }) => ` cursor: pointer; padding: 8px; display: flex; gap: 4px; align-items: center; justify-content: flex-end; &:hover { color: ${theme.colorPrimary} !important; path[stroke], rect[stroke], line { stroke: ${theme.colorPrimary} !important; } path:not([stroke]), rect:not([stroke]) { fill: ${theme.colorPrimary} !important; } } `, );