import {Button, message, Modal} from 'antd'; import React, {useEffect, useState} from 'react'; import {PageHeaderWrapper} from '@ant-design/pro-layout'; import {useSingleState} from 'nice-hooks'; import Quill from '@/pages/Memory/components/Quill'; import UpdateForm from '@/pages/Memory/components/UpdateForm'; import {TableListItem} from '../MemoryList/data.d'; import service from './service'; import styles from './index.less'; import {DownOutlined, ExclamationCircleOutlined} from '@ant-design/icons/lib'; import {getRandom, getTextFromHtml} from "@/utils/utils"; enum Step { MAIN = 0, MEMORY = 1, ADD = 2, } interface State { remindCount: number; step: Step; showBack?: boolean; } const TableList: React.FC<{}> = () => { const [btnDisable, setBtnDisable] = useState(false); const [showMore, setShowMore] = useState(false); let counting = false; const {confirm} = Modal; const [state, setState] = useSingleState({ remindCount: 0, step: Step.MAIN, // 0 主页,1 复习,2 添加 showBack: false, }); const [memory, setMemory] = useSingleState({ back: '', front: '', id: undefined, period: 0, remindTime: new Date(), tag: '', updateTime: '', userId: 0, }); /** * 查询用户待提醒数量 * @param fields */ const countRemind = async () => { // 如果上一个查询还没有结束,则直接返回 console.log(counting); if (counting) { return; } counting = true; const hide = message.loading('正在查询'); try { const res = await service.countRemind({}); hide(); if (res.success) { setState({remindCount: parseInt(res.data, 10)}); } counting = false; } catch (error) { hide(); message.error('查询异常!'); counting = false; } }; /** * 清空memory */ const clearMemory = () => { setMemory({ id: undefined, front: '', back: '', }); }; /** * 处理返回值 * @param res */ const memoryRes = (res: any) => { // 只要有返回值,就使按钮可见 setBtnDisable(false); if (res.success) { setMemory({...memory, ...res.data}); } else { setState({step: Step.MAIN}); } }; const memorySuccess = async (factorInt: number) => { setBtnDisable(true); memoryRes(await service.memorySuccess({id: memory.id, factor: factorInt * getRandom()})); }; const findNextLine = async () => { const res = await service.findNext({}); if (res.success) { setMemory({...memory, ...res.data}); } else { setState({step: Step.MAIN}); } }; // 绑定快捷键 const bindShortKey = () => { document.onkeydown = (ev) => { // 空格显示背面和默认选择 if (ev.key === ' ' && state.step === Step.MEMORY) { ev.preventDefault(); // 关闭浏览器快捷键 if (state.showBack === false) { setState({showBack: true}); } else { memorySuccess(2); } } else if (ev.key === ' ' && state.step === Step.MAIN) { // 空格刷新 // 如果待复习大于0,则直接进去 if (state.remindCount > 0) { setState({step: Step.MEMORY}); } else { // 刷新一下,再判断是否应该去复习 countRemind(); if (state.remindCount > 0) { setState({step: Step.MEMORY}); } } } else if (ev.key.toLowerCase() === 'e') { // 快捷 E,进入编辑、新建页面 setState({step: Step.ADD}); } else if (ev.key.toLowerCase() === 'escape') { // esc键回到主页 setState({step: Step.MAIN}); } }; } useEffect(() => { countRemind(); setTimeout(() => { countRemind(); }, 1000 * 60 * 60); // 每隔一个小时查询一次 bindShortKey() }, []); useEffect(() => { // 关闭背面,清空 memory setState({showBack: false}); clearMemory(); if (state.step === Step.MAIN) { // 进入统计页面 countRemind(); } else if (state.step === Step.MEMORY) { findNextLine(); } }, [state.step]) useEffect(() => { // 判断是否为主页面刷新 if (memory.front.length === 0) { return; } // 进入复习页面后 // 如果没有文字,并且也没有图片 if (getTextFromHtml(memory.back).length === 0 && memory.back.indexOf("img") < 0) { setState({showBack: true}); } else { setState({showBack: false}); } // 隐藏更多按钮 setShowMore(false); }, [memory.front]); return ( {state.step === Step.MAIN ? (
待复习: {state.remindCount}

) : null} {state.step === Step.MEMORY ? (
正面:

{ }} value={memory.front}/>

{state.showBack ? (

反面:

{ }} value={memory.back}/>

{showMore ? (

) : null}
) : (
)}
) : null} {state.step === Step.ADD ? ( { bindShortKey(); setState({step: Step.MAIN}); }} modalVisible={state.step === Step.ADD} values={memory} /> ) : null}
); }; export default TableList;