mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-11 03:41:12 +00:00
react-grid-data interaction improvement
This commit is contained in:
@@ -11,10 +11,11 @@ const { fileName } = window.app.pageOptions;
|
|||||||
class AddHeader extends React.Component {
|
class AddHeader extends React.Component {
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
let {isContentChanged} = this.props;
|
||||||
return (
|
return (
|
||||||
<div id="header">
|
<div id="header">
|
||||||
<div className="sf-font">{fileName}</div>
|
<div className="sf-font">{fileName}</div>
|
||||||
<Button color="primary" onClick={this.props.onSave}>保存</Button>
|
<Button color="primary" onClick={this.props.onSave} disabled={!isContentChanged}>保存</Button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -7,6 +7,7 @@ import GridHeaderContextMenu from './grid-header-contextmenu';
|
|||||||
import GridContentContextMenu from './grid-content-contextmenu';
|
import GridContentContextMenu from './grid-content-contextmenu';
|
||||||
import ModalPortal from '../../components/modal-portal';
|
import ModalPortal from '../../components/modal-portal';
|
||||||
import NewColumnDialog from './new-column-dialog';
|
import NewColumnDialog from './new-column-dialog';
|
||||||
|
import isHotkey from 'is-hotkey';
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
initData: PropTypes.object.isRequired,
|
initData: PropTypes.object.isRequired,
|
||||||
@@ -35,11 +36,24 @@ class AppMain extends React.Component {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
document.addEventListener('keydown', this.onHotKey);
|
||||||
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
|
|
||||||
|
if (nextProps.isContentChanged) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let data = nextProps.initData;
|
let data = nextProps.initData;
|
||||||
this.deseralizeGridData(data);
|
this.deseralizeGridData(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
document.removeEventListener('keydown', this.onHotKey);
|
||||||
|
}
|
||||||
|
|
||||||
createRows = (numberOfRows) => {
|
createRows = (numberOfRows) => {
|
||||||
let rows = [];
|
let rows = [];
|
||||||
for (let i = 0; i < numberOfRows; i++) {
|
for (let i = 0; i < numberOfRows; i++) {
|
||||||
@@ -67,6 +81,7 @@ class AppMain extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.setState({ rows });
|
this.setState({ rows });
|
||||||
|
this.props.onContentChanged();
|
||||||
};
|
};
|
||||||
|
|
||||||
handleAddRow = ({ newRowIndex }) => {
|
handleAddRow = ({ newRowIndex }) => {
|
||||||
@@ -77,6 +92,7 @@ class AppMain extends React.Component {
|
|||||||
let rows = this.state.rows.slice();
|
let rows = this.state.rows.slice();
|
||||||
rows = update(rows, {$push: [newRow]});
|
rows = update(rows, {$push: [newRow]});
|
||||||
this.setState({ rows });
|
this.setState({ rows });
|
||||||
|
this.props.onContentChanged();
|
||||||
};
|
};
|
||||||
|
|
||||||
getRowAt = (index) => {
|
getRowAt = (index) => {
|
||||||
@@ -94,10 +110,19 @@ class AppMain extends React.Component {
|
|||||||
onInsertRow = () => {
|
onInsertRow = () => {
|
||||||
let newRowIndex = this.getSize();
|
let newRowIndex = this.getSize();
|
||||||
this.handleAddRow({ newRowIndex });
|
this.handleAddRow({ newRowIndex });
|
||||||
|
this.props.onContentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
onInsertColumn = () => {
|
onInsertColumn = () => {
|
||||||
this.setState({isNewColumnDialogShow: true});
|
this.setState({isNewColumnDialogShow: true});
|
||||||
|
this.props.onContentChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
onColumnResize = (index, width) => {
|
||||||
|
let columns = this.state.columns.slice();
|
||||||
|
columns[index - 1].width = width;
|
||||||
|
this.setState({columns: columns});
|
||||||
|
this.props.onContentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
onNewColumn = (columnName, columnType) => {
|
onNewColumn = (columnName, columnType) => {
|
||||||
@@ -115,10 +140,12 @@ class AppMain extends React.Component {
|
|||||||
columns.push(newColumn);
|
columns.push(newColumn);
|
||||||
this.setState({columns: columns});
|
this.setState({columns: columns});
|
||||||
this.onNewColumnCancel();
|
this.onNewColumnCancel();
|
||||||
|
this.props.onContentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
onNewColumnCancel = () => {
|
onNewColumnCancel = () => {
|
||||||
this.setState({isNewColumnDialogShow: false});
|
this.setState({isNewColumnDialogShow: false});
|
||||||
|
this.props.onContentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
onRowDelete = (e, data) => {
|
onRowDelete = (e, data) => {
|
||||||
@@ -126,6 +153,7 @@ class AppMain extends React.Component {
|
|||||||
let newRows = this.state.rows.slice(0); // copy array;
|
let newRows = this.state.rows.slice(0); // copy array;
|
||||||
newRows.splice(rowIdx, 1);
|
newRows.splice(rowIdx, 1);
|
||||||
this.setState({rows: newRows});
|
this.setState({rows: newRows});
|
||||||
|
this.props.onContentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
onColumnDelete = (e, data) => {
|
onColumnDelete = (e, data) => {
|
||||||
@@ -140,6 +168,7 @@ class AppMain extends React.Component {
|
|||||||
columns: columns,
|
columns: columns,
|
||||||
rows: rows
|
rows: rows
|
||||||
});
|
});
|
||||||
|
this.props.onContentChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
serializeGridData = () => {
|
serializeGridData = () => {
|
||||||
@@ -186,6 +215,14 @@ class AppMain extends React.Component {
|
|||||||
return editor;
|
return editor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onHotKey = (event) => {
|
||||||
|
if (isHotkey('mod+s', event)) {
|
||||||
|
event.preventDefault();
|
||||||
|
this.props.onSave();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
let columns = this.getColumns();
|
let columns = this.getColumns();
|
||||||
return (
|
return (
|
||||||
@@ -208,6 +245,7 @@ class AppMain extends React.Component {
|
|||||||
RowsContainer={Menu.ContextMenuTrigger}
|
RowsContainer={Menu.ContextMenuTrigger}
|
||||||
headerContextMenu={<GridHeaderContextMenu id="grid-header-contxtmenu" onColumnDelete={this.onColumnDelete} />}
|
headerContextMenu={<GridHeaderContextMenu id="grid-header-contxtmenu" onColumnDelete={this.onColumnDelete} />}
|
||||||
contextMenu={<GridContentContextMenu id="grid-content-contxtmenu" onRowDelete={this.onRowDelete} />}
|
contextMenu={<GridContentContextMenu id="grid-content-contxtmenu" onRowDelete={this.onRowDelete} />}
|
||||||
|
onColumnResize={this.onColumnResize}
|
||||||
/>
|
/>
|
||||||
{this.state.isNewColumnDialogShow && (
|
{this.state.isNewColumnDialogShow && (
|
||||||
<ModalPortal>
|
<ModalPortal>
|
||||||
|
@@ -22,6 +22,7 @@ class ViewFileSDB extends React.Component {
|
|||||||
columns: [],
|
columns: [],
|
||||||
rows: [],
|
rows: [],
|
||||||
},
|
},
|
||||||
|
isContentChanged: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -40,6 +41,11 @@ class ViewFileSDB extends React.Component {
|
|||||||
|
|
||||||
onSave = () => {
|
onSave = () => {
|
||||||
let data = this.refs.data_grid.serializeGridData();
|
let data = this.refs.data_grid.serializeGridData();
|
||||||
|
this.setState({
|
||||||
|
initData: data,
|
||||||
|
isContentChanged: false
|
||||||
|
})
|
||||||
|
|
||||||
let dirPath = Utils.getDirName(filePath);
|
let dirPath = Utils.getDirName(filePath);
|
||||||
seafileAPI.getUpdateLink(repoID, dirPath).then(res => {
|
seafileAPI.getUpdateLink(repoID, dirPath).then(res => {
|
||||||
let updateLink = res.data;
|
let updateLink = res.data;
|
||||||
@@ -52,11 +58,24 @@ class ViewFileSDB extends React.Component {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onContentChanged = () => {
|
||||||
|
this.setState({isContentChanged: true});
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
<AppHeader onSave={this.onSave}/>
|
<AppHeader
|
||||||
<AppMain initData={this.state.initData} ref="data_grid"/>
|
onSave={this.onSave}
|
||||||
|
isContentChanged={this.state.isContentChanged}
|
||||||
|
/>
|
||||||
|
<AppMain
|
||||||
|
initData={this.state.initData}
|
||||||
|
ref="data_grid"
|
||||||
|
onContentChanged={this.onContentChanged}
|
||||||
|
isContentChanged={this.state.isContentChanged}
|
||||||
|
onSave={this.onSave}
|
||||||
|
/>
|
||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user