mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-17 07:41:26 +00:00
fix rename page logic
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback, useEffect, useState } from 'react';
|
import React, { useCallback, useEffect, useState, useRef } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import classnames from 'classnames';
|
import classnames from 'classnames';
|
||||||
import { Input } from 'reactstrap';
|
import { Input } from 'reactstrap';
|
||||||
@@ -16,18 +16,55 @@ const propTypes = {
|
|||||||
|
|
||||||
const PageTitle = ({ currentPageConfig, onUpdatePage }) => {
|
const PageTitle = ({ currentPageConfig, onUpdatePage }) => {
|
||||||
const [isShowController, setIsShowController] = useState(false);
|
const [isShowController, setIsShowController] = useState(false);
|
||||||
|
const [pageName, setPageName] = useState(currentPageConfig.name);
|
||||||
|
const isChineseInput = useRef(false);
|
||||||
|
const isTyping = useRef(false);
|
||||||
|
const timer = useRef(null);
|
||||||
|
|
||||||
const handleRenameDocument = useCallback((e) => {
|
useEffect(() => {
|
||||||
const { nativeEvent: { isComposing } } = e;
|
if (pageName !== currentPageConfig.name && isTyping.current === false) {
|
||||||
if (isComposing) return;
|
setPageName(currentPageConfig.name);
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [currentPageConfig.name]);
|
||||||
|
|
||||||
|
const onKeyDown = useCallback(() => {
|
||||||
|
isTyping.current = true;
|
||||||
|
if (timer.current) {
|
||||||
|
clearTimeout(timer.current);
|
||||||
|
timer.current = null;
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onKeyUp = useCallback(() => {
|
||||||
|
timer.current = setTimeout(() => {
|
||||||
|
isTyping.current = false;
|
||||||
|
}, 500);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onCompositionStart = useCallback(() => {
|
||||||
|
isChineseInput.current = true;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onCompositionEnd = useCallback((e) => {
|
||||||
|
isChineseInput.current = false;
|
||||||
const newName = e.target.value.trim();
|
const newName = e.target.value.trim();
|
||||||
const { id, name, icon } = currentPageConfig;
|
const { id, icon } = currentPageConfig;
|
||||||
if (newName === name) return;
|
|
||||||
const pageConfig = { name: newName, icon };
|
const pageConfig = { name: newName, icon };
|
||||||
onUpdatePage && onUpdatePage(id, pageConfig);
|
onUpdatePage && onUpdatePage(id, pageConfig);
|
||||||
}, [currentPageConfig, onUpdatePage]);
|
}, [currentPageConfig, onUpdatePage]);
|
||||||
|
|
||||||
|
const onChange = useCallback((e) => {
|
||||||
|
setPageName(e.target.value);
|
||||||
|
if (isChineseInput.current === false) {
|
||||||
|
const newName = e.target.value.trim();
|
||||||
|
if (newName === pageName) return;
|
||||||
|
const { id, icon } = currentPageConfig;
|
||||||
|
const pageConfig = { name: newName, icon };
|
||||||
|
onUpdatePage && onUpdatePage(id, pageConfig);
|
||||||
|
}
|
||||||
|
}, [currentPageConfig, onUpdatePage, pageName]);
|
||||||
|
|
||||||
const onMouseEnter = useCallback(() => {
|
const onMouseEnter = useCallback(() => {
|
||||||
setIsShowController(true);
|
setIsShowController(true);
|
||||||
}, []);
|
}, []);
|
||||||
@@ -79,7 +116,16 @@ const PageTitle = ({ currentPageConfig, onUpdatePage }) => {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<Input className='wiki-sdoc-title' onCompositionEnd={handleRenameDocument} bsSize="lg" onChange={handleRenameDocument} defaultValue={currentPageConfig.name} />
|
<Input
|
||||||
|
className='wiki-sdoc-title'
|
||||||
|
bsSize="lg"
|
||||||
|
onCompositionStart={onCompositionStart}
|
||||||
|
onCompositionEnd={onCompositionEnd}
|
||||||
|
onKeyDown={onKeyDown}
|
||||||
|
onKeyUp={onKeyUp}
|
||||||
|
onChange={onChange}
|
||||||
|
value={pageName}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user