1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-16 07:08:55 +00:00

optimized code

This commit is contained in:
shanshuirenjia
2019-05-07 17:58:53 +08:00
parent 6137b29c2d
commit 615330980f
7 changed files with 197 additions and 211 deletions

View File

@@ -1,207 +1,139 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import isHotkey from 'is-hotkey';
import ReactDataGrid from '@seafile/react-data-grid/'; import ReactDataGrid from '@seafile/react-data-grid/';
import update from 'immutability-helper'; import { Menu } from '@seafile/react-data-grid-addons';
import { Menu, Editors } from '@seafile/react-data-grid-addons'; import { seafileAPI } from '../../utils/seafile-api';
import GridHeaderContextMenu from './grid-header-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'; import GridHeaderContextMenu from './grid-header-contextmenu';
import GridContentContextMenu from './grid-content-contextmenu';
import DTableStore from './store/dtable-store'; import DTableStore from './store/dtable-store';
const propTypes = { const { repoID, filePath } = window.app.pageOptions;
initData: PropTypes.object.isRequired,
}; const DEFAULT_DATA = {
columns: [
{
key: 'name',
name: 'Name',
type: '',
width: 80,
editable: true,
resizable: true
}
],
rows: [{name: 'name_' + 0}]
}
class AppMain extends React.Component { class AppMain extends React.Component {
constructor(props, context) { constructor(props, context) {
super(props, context); super(props, context);
this._columns = [
{
key: 'name',
name: 'Name',
width: 80,
editable: true,
resizable: true
}
];
let initData = props.initData;
this.state = { this.state = {
columns: initData.columns.length ? this.deseralizeGridData(initData.columns) : this._columns, value: null,
rows: initData.rows.length ? initData.rows : this.createRows(1),
isNewColumnDialogShow: false, isNewColumnDialogShow: false,
}; };
this.dTableStore = new DTableStore();
this.dTableStore = new DTableStore(initData);
} }
componentDidMount() { componentDidMount() {
seafileAPI.getFileDownloadLink(repoID, filePath).then(res => {
let url = res.data;
seafileAPI.getFileContent(url).then(res => {
let data = res.data ? res.data : JSON.stringify(DEFAULT_DATA);
let value = this.dTableStore.deseralizeGridData(data);
this.setState({value});
});
});
document.addEventListener('keydown', this.onHotKey); document.addEventListener('keydown', this.onHotKey);
} }
componentWillReceiveProps(nextProps) {
if (nextProps.isContentChanged) {
return;
}
let data = nextProps.initData;
this.deseralizeGridData(data);
}
componentWillUnmount() { componentWillUnmount() {
document.removeEventListener('keydown', this.onHotKey); document.removeEventListener('keydown', this.onHotKey);
} }
createRows = (numberOfRows) => {
let rows = [];
for (let i = 0; i < numberOfRows; i++) {
rows[i] = this.createFakeRowObjectData(i);
}
return rows;
};
createFakeRowObjectData = (index) => {
return {name: 'name_' + index};
};
getColumns = () => {
let clonedColumns = this.state.columns.slice();
return clonedColumns;
};
handleGridRowsUpdated = ({ fromRow, toRow, updated }) => {
let rows = this.state.rows.slice();
for (let i = fromRow; i <= toRow; i++) {
let rowToUpdate = rows[i];
let updatedRow = update(rowToUpdate, {$merge: updated});
rows[i] = updatedRow;
}
this.setState({ rows });
this.props.onContentChanged();
};
handleAddRow = ({ newRowIndex }) => {
const newRow = {
name: 'name_' + newRowIndex,
};
let rows = this.state.rows.slice();
rows = update(rows, {$push: [newRow]});
this.setState({ rows });
this.props.onContentChanged();
};
getRowAt = (index) => { getRowAt = (index) => {
if (index < 0 || index > this.getSize()) { if (index < 0 || index > this.getSize()) {
return undefined; return undefined;
} }
return this.state.rows[index]; return this.state.value.rows[index];
}; };
getSize = () => { getSize = () => {
return this.state.rows.length; return this.state.value.rows.length;
}; };
onInsertRow = () => { onInsertRow = () => {
let newRowIndex = this.getSize(); let newRowIndex = this.getSize();
let rows = this.dTableStore.insertRow(newRowIndex); let value = this.dTableStore.insertRow(newRowIndex);
this.setState({rows}); this.setState({value});
this.props.onContentChanged(); 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) => {
let idx = this.state.columns.length;
let columns = this.dTableStore.insertColumn(idx, columnName, columnType); let idx = this.state.value.columns.length;
columns = this.formatColumnsData(columns); let value = this.dTableStore.insertColumn(idx, columnName, columnType);
this.setState({columns: columns}); this.setState({value});
this.onNewColumnCancel(); this.onNewColumnCancel();
this.props.onContentChanged(); this.props.onContentChanged();
} }
onNewColumnCancel = () => { onNewColumnCancel = () => {
this.setState({isNewColumnDialogShow: false}); this.setState({isNewColumnDialogShow: false});
this.props.onContentChanged(); this.props.onContentChanged();
} }
onRowDelete = (e, data) => { onRowDelete = (e, data) => {
let { rowIdx } = data; let { rowIdx } = data;
let rows = this.dTableStore.deleteRow(rowIdx); let value = this.dTableStore.deleteRow(rowIdx);
this.setState({rows}); this.setState({value});
this.props.onContentChanged(); this.props.onContentChanged();
} }
onColumnDelete = (e, data) => { onColumnDelete = (e, data) => {
let column = data.column; let column = data.column;
let idx = column.idx - 1; let idx = column.idx - 1;
let columns = this.dTableStore.deleteColumn(idx); let value = this.dTableStore.deleteColumn(idx);
this.setState({columns}); this.setState({value});
this.props.onContentChanged();
}
onColumnResize = (index, width) => {
let idx = index - 1;
let value = this.dTableStore.resizeColumn(idx, width);
this.setState({value});
this.props.onContentChanged();
}
handleGridRowsUpdated = ({ cellKey, fromRow, updated }) => {
let rowIdx = fromRow;
let value = this.dTableStore.modifyCell(rowIdx, cellKey, updated[cellKey]);
this.setState({value});
this.forceUpdate();
this.props.onContentChanged(); this.props.onContentChanged();
} }
serializeGridData = () => { serializeGridData = () => {
let gridData = { return this.dTableStore.serializeGridData();
columns: JSON.stringify(this.state.columns),
rows: JSON.stringify(this.state.rows),
};
return gridData;
}
deseralizeGridData = (data) => {
let columns = JSON.parse(data.columns);
let rows = JSON.parse(data.rows);
columns = this.formatColumnsData(columns);
this.setState({
columns: columns,
rows: rows,
});
this.dTableStore.updateStoreValues({columns, rows});
}
formatColumnsData = (columns) => {
return columns.map(column => {
if (column.editor) {
let editor = this.createEditor(column.editor);
column.editor = editor;
}
return column;
});
}
createEditor = (editorType) => {
let editor = null;
switch (editorType) {
case 'number':
editor = <Editors.NumberEditor />;
break;
case 'text':
editor = null;
break;
default:
break;
}
return editor;
} }
onHotKey = (event) => { onHotKey = (event) => {
@@ -213,13 +145,17 @@ class AppMain extends React.Component {
} }
render() { render() {
let columns = this.getColumns();
if (!this.state.value) {
return '';
}
return ( return (
<div id="main"> <div id="main">
<ReactDataGrid <ReactDataGrid
ref={ node => this.grid = node } ref={ node => this.grid = node }
enableCellSelect={true} enableCellSelect={true}
columns={columns} columns={this.state.value.columns}
rowGetter={this.getRowAt} rowGetter={this.getRowAt}
rowsCount={this.getSize()} rowsCount={this.getSize()}
onGridRowsUpdated={this.handleGridRowsUpdated} onGridRowsUpdated={this.handleGridRowsUpdated}
@@ -249,6 +185,4 @@ class AppMain extends React.Component {
} }
} }
AppMain.propTypes = propTypes;
export default AppMain; export default AppMain;

View File

@@ -3,7 +3,8 @@ export default class GridColumn {
constructor(object) { constructor(object) {
this.key = object.columnName || object.name; this.key = object.columnName || object.name;
this.name = object.columnName || object.name; this.name = object.columnName || object.name;
this.editor = object.columnType || null; this.type = object.columnType || null;
this.editor = null;
this.editable = object.editable || true; this.editable = object.editable || true;
this.width = object.width || 200; this.width = object.width || 200;
this.resizable = object.resizable || true; this.resizable = object.resizable || true;

View File

@@ -35,8 +35,8 @@ function apply(value, op) {
} }
case OperationTypes.MODIFY_CELL : { case OperationTypes.MODIFY_CELL : {
let { rowIdx, key, newCellValue } = op; let { rowIdx, key, newValue } = op;
next[rowIdx][key] = newCellValue; next[rowIdx][key] = newValue;
return next; return next;
} }
@@ -46,6 +46,13 @@ function apply(value, op) {
next[idx]['name'] = newColumnName; next[idx]['name'] = newColumnName;
return next; return next;
} }
case OperationTypes.RESIZE_COLUMN : {
let { idx, width } = op;
next[idx].width = width;
return next;
}
} }
} }

View File

@@ -1,18 +1,49 @@
import Operation from './operation'; import Operation from './operation';
import OperationTypes from './operation-types'; import OperationTypes from './operation-types';
import editorFactory from '../utils/editor-factory';
// todo Immutable // todo Immutable
// Implement the current version with an array // Implement the current version with an array
export default class DTableStore { export default class DTableStore {
constructor(value) { constructor() {
this.value = {};
this.value.columns = [];
this.value.rows = [];
this.operations = []; this.operations = [];
this.columns = value.columns || [];
this.rows = value.rows || [];
} }
updateStoreValues({ columns, rows }) { serializeGridData() {
this.columns = columns;
this.rows = rows; let value = this.value;
let columns = value.columns.map(column => {
delete column.editor; // delete editor attr;
return column;
});
value.columns = columns;
return JSON.stringify(value);
}
deseralizeGridData(gridData) {
gridData = JSON.parse(gridData);
let columns = gridData.columns;
let rows = gridData.rows;
columns = columns.map(column => {
if (column.type) {
let editor = editorFactory.createEditor(column.type);
column.editor = editor;
}
return column;
});
this.value.columns = columns;
this.value.rows = rows;
return this.value;
} }
createOperation(op) { createOperation(op) {
@@ -22,68 +53,81 @@ export default class DTableStore {
deleteRow(rowIdx) { deleteRow(rowIdx) {
let type = OperationTypes.DELETE_ROW; let type = OperationTypes.DELETE_ROW;
let operation = this.createOperation({type, rowIdx}); let operation = this.createOperation({type, rowIdx});
let next = operation.apply(this.rows); let next = operation.apply(this.value.rows);
this.operations.push(operation); this.operations.push(operation);
this.rows = next; this.value.rows = next;
return next; return this.value;
} }
insertRow(newRowIdx) { insertRow(newRowIdx) {
let type = OperationTypes.INSERT_ROW; let type = OperationTypes.INSERT_ROW;
let operation = this.createOperation({type, newRowIdx}); let operation = this.createOperation({type, newRowIdx});
let next = operation.apply(this.rows); let next = operation.apply(this.value.rows);
this.operations.push(operation); this.operations.push(operation);
this.rows = next; this.value.rows = next;
return next; return this.value;
} }
deleteColumn(idx) { deleteColumn(idx) {
let type = OperationTypes.DELETE_COLUMN; let type = OperationTypes.DELETE_COLUMN;
let operation = this.createOperation({type, idx}); let operation = this.createOperation({type, idx});
let next = operation.apply(this.columns); let next = operation.apply(this.value.columns);
this.operations.push(operation); this.operations.push(operation);
this.columns = next; this.value.columns = next;
return next; return this.value;
} }
insertColumn(idx, columnName, columnType) { insertColumn(idx, columnName, columnType) {
let type = OperationTypes.INSERT_COLUMN; let type = OperationTypes.INSERT_COLUMN;
let operation = this.createOperation({type, idx, columnName, columnType}); let operation = this.createOperation({type, idx, columnName, columnType});
let next = operation.apply(this.columns); let next = operation.apply(this.value.columns);
this.operations.push(operation); this.operations.push(operation);
this.columns = next; this.value.columns = next;
return next; let value = this.serializeGridData();
this.deseralizeGridData(value);
return this.value;
} }
modifyColumn(idx, oldColumnName, newColumnName) { modifyColumn(idx, oldColumnName, newColumnName) {
let type = OperationTypes.MODIFY_COLUMN; let type = OperationTypes.MODIFY_COLUMN;
let operation = this.createOperation({type, idx, oldColumnName, newColumnName}); let operation = this.createOperation({type, idx, oldColumnName, newColumnName});
let next = operation.apply(this.columns); let next = operation.apply(this.value.columns);
this.operations.push(operation); this.operations.push(operation);
this.columns = next; this.value.columns = next;
return next; return this.value;
} }
modifyCell(idx, rowIdx, oldCellValue, newCellValue) { modifyCell(rowIdx, key, newValue) {
let type = OperationTypes.MODIFY_CELL; let type = OperationTypes.MODIFY_CELL;
let key = this.columns[idx].key; let operation = this.createOperation({type, rowIdx, key, newValue});
let operation = this.createOperation({type, rowIdx, key, oldCellValue, newCellValue}); let next = operation.apply(this.value.rows);
let next = operation.apply(this.rows);
this.operations.push(operation); this.operations.push(operation);
this.rows = next; this.value.rows = next;
return next; return this.value;
}
resizeColumn(idx, width) {
let type = OperationTypes.RESIZE_COLUMN;
let operation = this.createOperation({type, idx, width});
let next = operation.apply(this.value.columns);
this.operations.push(operation);
this.value.columns = next;
return this.value;
} }
} }

View File

@@ -5,6 +5,7 @@ const OperationTypes = {
INSERT_COLUMN: 'INSERT_COLUMN', INSERT_COLUMN: 'INSERT_COLUMN',
MODIFY_CELL: 'MODIFY_CELL', MODIFY_CELL: 'MODIFY_CELL',
MODIFY_COLUMN: 'MODIFY_COLUMN', MODIFY_COLUMN: 'MODIFY_COLUMN',
RESIZE_COLUMN: 'RESIZE_COLUMN',
}; };
export default OperationTypes; export default OperationTypes;

View File

@@ -0,0 +1,28 @@
import React from 'react'
import { Editors } from '@seafile/react-data-grid-addons';
const EDITOR_NUMBER = 'number';
const EDITOR_TEXT = 'text';
class EditorFactory {
createEditor(editorType) {
switch(editorType) {
case EDITOR_NUMBER: {
return <Editors.NumberEditor />
}
case EDITOR_TEXT: {
return '';
}
default: {
return '';
}
}
}
}
let editorFactory = new EditorFactory();
export default editorFactory;

View File

@@ -11,46 +11,30 @@ import './css/layout.css';
import './css/file-view-data-grid.css'; import './css/file-view-data-grid.css';
import './css/react-context-menu.css'; import './css/react-context-menu.css';
const { repoID, fileName, filePath, err, enableWatermark, userNickName } = window.app.pageOptions; const { repoID, fileName, filePath } = window.app.pageOptions;
class ViewFileSDB extends React.Component { class ViewFileSDB extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
initData: {
columns: [],
rows: [],
},
isContentChanged: false, isContentChanged: false,
}; };
} }
componentDidMount() { onContentChanged = () => {
seafileAPI.getFileDownloadLink(repoID, filePath).then(res => { this.setState({isContentChanged: true});
let url = res.data;
seafileAPI.getFileContent(url).then(res => {
let data = res.data;
if (data) {
this.setState({initData: data});
}
});
});
} }
onSave = () => { onSave = () => {
this.setState({isContentChanged: false});
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;
let updateData = JSON.stringify(data); seafileAPI.updateFile(updateLink, filePath, fileName, JSON.stringify(data)).then(res => {
seafileAPI.updateFile(updateLink, filePath, fileName, updateData).then(res => {
toaster.success(gettext('File saved.')); toaster.success(gettext('File saved.'));
}).catch(() => { }).catch(() => {
toaster.success(gettext('File save failed.')); toaster.success(gettext('File save failed.'));
@@ -58,24 +42,11 @@ class ViewFileSDB extends React.Component {
}); });
} }
onContentChanged = () => {
this.setState({isContentChanged: true});
}
render() { render() {
return ( return (
<Fragment> <Fragment>
<AppHeader <AppHeader onSave={this.onSave} isContentChanged={this.state.isContentChanged} />
onSave={this.onSave} <AppMain ref="data_grid" onContentChanged={this.onContentChanged} 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>
); );
} }