mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-22 11:57:34 +00:00
feat(metadata-table): update operations (#6862)
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
import { EVENT_BUS_TYPE, PER_LOAD_NUMBER, PRIVATE_COLUMN_KEY } from '../constants';
|
||||
import DataProcessor from './data-processor';
|
||||
import ServerOperator from './server-operator';
|
||||
import LocalOperator from './local-operator';
|
||||
import Metadata from '../model/metadata';
|
||||
import { checkIsDir } from '../utils/row';
|
||||
import { Utils } from '../../utils/utils';
|
||||
@@ -27,6 +28,7 @@ class Store {
|
||||
this.isSendingOperation = false;
|
||||
this.isReadonly = false;
|
||||
this.serverOperator = new ServerOperator();
|
||||
this.localOperator = new LocalOperator();
|
||||
this.collaborators = props.collaborators || [];
|
||||
}
|
||||
|
||||
@@ -108,6 +110,11 @@ class Store {
|
||||
|
||||
applyOperation(operation, undoRedoHandler = { handleUndo: true }) {
|
||||
const { op_type } = operation;
|
||||
|
||||
if (LOCAL_APPLY_OPERATION_TYPE.includes(op_type)) {
|
||||
this.localOperator.applyOperation(operation);
|
||||
}
|
||||
|
||||
if (!NEED_APPLY_AFTER_SERVER_OPERATION.includes(op_type)) {
|
||||
this.handleUndoRedos(undoRedoHandler, operation);
|
||||
this.data = deepCopy(operation.apply(this.data));
|
||||
@@ -115,10 +122,6 @@ class Store {
|
||||
this.context.eventBus.dispatch(EVENT_BUS_TYPE.LOCAL_TABLE_CHANGED);
|
||||
}
|
||||
|
||||
if (LOCAL_APPLY_OPERATION_TYPE.includes(op_type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.addPendingOperations(operation, undoRedoHandler);
|
||||
}
|
||||
|
||||
@@ -153,7 +156,9 @@ class Store {
|
||||
this.sendNextOperation(undoRedoHandler);
|
||||
return;
|
||||
}
|
||||
if (NEED_APPLY_AFTER_SERVER_OPERATION.includes(operation.op_type)) {
|
||||
|
||||
const isAfterServerOperation = NEED_APPLY_AFTER_SERVER_OPERATION.includes(operation.op_type);
|
||||
if (isAfterServerOperation) {
|
||||
this.handleUndoRedos(undoRedoHandler, operation);
|
||||
this.data = deepCopy(operation.apply(this.data));
|
||||
this.syncOperationOnData(operation);
|
||||
@@ -163,7 +168,9 @@ class Store {
|
||||
window.sfMetadataContext.eventBus.dispatch(EVENT_BUS_TYPE.VIEW_CHANGED, this.data.view);
|
||||
}
|
||||
|
||||
if (isAfterServerOperation) {
|
||||
this.context.eventBus.dispatch(EVENT_BUS_TYPE.SERVER_TABLE_CHANGED);
|
||||
}
|
||||
operation.success_callback && operation.success_callback();
|
||||
|
||||
// need reload records if has related formula columns
|
||||
@@ -335,10 +342,13 @@ class Store {
|
||||
return;
|
||||
}
|
||||
|
||||
const deleted_rows = valid_rows_ids.map((rowId) => getRowById(this.data, rowId));
|
||||
|
||||
const operation = this.createOperation({
|
||||
type,
|
||||
repo_id: this.repoId,
|
||||
rows_ids: valid_rows_ids,
|
||||
deleted_rows,
|
||||
fail_callback,
|
||||
success_callback,
|
||||
});
|
||||
|
27
frontend/src/metadata/store/local-operator.js
Normal file
27
frontend/src/metadata/store/local-operator.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { OPERATION_TYPE } from './operations';
|
||||
|
||||
class LocalOperator {
|
||||
|
||||
applyOperation(operation) {
|
||||
const { op_type } = operation;
|
||||
|
||||
switch (op_type) {
|
||||
case OPERATION_TYPE.MODIFY_COLUMN_WIDTH: {
|
||||
const { column_key, new_width } = operation;
|
||||
try {
|
||||
const oldValue = window.sfMetadataContext.localStorage.getItem('columns_width') || {};
|
||||
window.sfMetadataContext.localStorage.setItem('columns_width', { ...oldValue, [column_key]: new_width });
|
||||
} catch (err) {
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default LocalOperator;
|
@@ -29,7 +29,7 @@ export const COLUMN_DATA_OPERATION_TYPE = {
|
||||
|
||||
export const OPERATION_ATTRIBUTES = {
|
||||
[OPERATION_TYPE.MODIFY_RECORDS]: ['repo_id', 'row_ids', 'id_row_updates', 'id_original_row_updates', 'id_old_row_data', 'id_original_old_row_data', 'is_copy_paste', 'is_rename', 'id_obj_id'],
|
||||
[OPERATION_TYPE.DELETE_RECORDS]: ['repo_id', 'rows_ids'],
|
||||
[OPERATION_TYPE.DELETE_RECORDS]: ['repo_id', 'rows_ids', 'deleted_rows'],
|
||||
[OPERATION_TYPE.RESTORE_RECORDS]: ['repo_id', 'rows_data', 'original_rows', 'link_infos', 'upper_row_ids'],
|
||||
[OPERATION_TYPE.RELOAD_RECORDS]: ['repo_id', 'row_ids'],
|
||||
[OPERATION_TYPE.MODIFY_FILTERS]: ['repo_id', 'view_id', 'filter_conjunction', 'filters', 'basic_filters'],
|
||||
@@ -54,23 +54,14 @@ export const UNDO_OPERATION_TYPE = [
|
||||
|
||||
// only apply operation on the local
|
||||
export const LOCAL_APPLY_OPERATION_TYPE = [
|
||||
|
||||
OPERATION_TYPE.MODIFY_COLUMN_WIDTH,
|
||||
];
|
||||
|
||||
// apply operation after exec operation on the server
|
||||
export const NEED_APPLY_AFTER_SERVER_OPERATION = [
|
||||
OPERATION_TYPE.MODIFY_RECORDS,
|
||||
OPERATION_TYPE.DELETE_RECORDS,
|
||||
OPERATION_TYPE.INSERT_COLUMN,
|
||||
OPERATION_TYPE.MODIFY_FILTERS,
|
||||
OPERATION_TYPE.MODIFY_SORTS,
|
||||
OPERATION_TYPE.MODIFY_GROUPBYS,
|
||||
OPERATION_TYPE.MODIFY_HIDDEN_COLUMNS,
|
||||
OPERATION_TYPE.INSERT_COLUMN,
|
||||
OPERATION_TYPE.DELETE_COLUMN,
|
||||
OPERATION_TYPE.RENAME_COLUMN,
|
||||
OPERATION_TYPE.MODIFY_COLUMN_DATA,
|
||||
OPERATION_TYPE.MODIFY_COLUMN_WIDTH,
|
||||
OPERATION_TYPE.MODIFY_COLUMN_ORDER,
|
||||
];
|
||||
|
||||
export const VIEW_OPERATION = [
|
||||
|
@@ -16,12 +16,14 @@ class ServerOperator {
|
||||
|
||||
switch (op_type) {
|
||||
case OPERATION_TYPE.MODIFY_RECORDS: {
|
||||
const { repo_id, row_ids, id_row_updates, id_original_row_updates, is_copy_paste, is_rename, id_obj_id } = operation;
|
||||
const { repo_id, row_ids, id_row_updates, id_original_row_updates, id_original_old_row_data, is_copy_paste, is_rename, id_obj_id } = operation;
|
||||
if (is_rename) {
|
||||
const rowId = row_ids[0];
|
||||
const oldRowData = id_original_old_row_data[rowId];
|
||||
const rowUpdates = id_original_row_updates[rowId];
|
||||
const oldName = getFileNameFromRecord(oldRowData);
|
||||
const newName = getFileNameFromRecord(rowUpdates);
|
||||
this.renameFile(newName, repo_id, rowId, data, {
|
||||
this.renameFile(newName, oldName, repo_id, rowId, data, {
|
||||
fail_callback: (error) => {
|
||||
callback({ error });
|
||||
},
|
||||
@@ -42,9 +44,8 @@ class ServerOperator {
|
||||
break;
|
||||
}
|
||||
case OPERATION_TYPE.DELETE_RECORDS: {
|
||||
const { repo_id, rows_ids } = operation;
|
||||
const file_names = rows_ids.map((rowId) => {
|
||||
const row = getRowById(data, rowId);
|
||||
const { repo_id, deleted_rows } = operation;
|
||||
const file_names = deleted_rows.map((row) => {
|
||||
const { _parent_dir, _name } = row || {};
|
||||
if (_parent_dir && _name) {
|
||||
return Utils.joinPath(_parent_dir, _name);
|
||||
@@ -112,17 +113,6 @@ class ServerOperator {
|
||||
});
|
||||
break;
|
||||
}
|
||||
case OPERATION_TYPE.MODIFY_COLUMN_WIDTH: {
|
||||
const { column_key, new_width } = operation;
|
||||
try {
|
||||
const oldValue = window.sfMetadataContext.localStorage.getItem('columns_width') || {};
|
||||
window.sfMetadataContext.localStorage.setItem('columns_width', { ...oldValue, [column_key]: new_width });
|
||||
callback({ operation });
|
||||
} catch {
|
||||
callback({ error: gettext('Failed to modify property width') });
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OPERATION_TYPE.MODIFY_COLUMN_ORDER: {
|
||||
const { repo_id, view_id, new_columns_keys } = operation;
|
||||
window.sfMetadataContext.modifyView(repo_id, view_id, { columns_keys: new_columns_keys }).then(res => {
|
||||
@@ -334,14 +324,15 @@ class ServerOperator {
|
||||
}, []);
|
||||
}
|
||||
|
||||
renameFile = (newName, repo_id, rowId, data, { fail_callback, success_callback }) => {
|
||||
renameFile = (newName, oldName, repo_id, rowId, data, { fail_callback, success_callback }) => {
|
||||
const row = getRowById(data, rowId);
|
||||
if (!row) {
|
||||
fail_callback();
|
||||
return;
|
||||
}
|
||||
|
||||
const { _parent_dir, _name } = row;
|
||||
const path = Utils.joinPath(_parent_dir, _name);
|
||||
const { _parent_dir } = row;
|
||||
const path = Utils.joinPath(_parent_dir, oldName);
|
||||
|
||||
// rename folder
|
||||
if (checkIsDir(row)) {
|
||||
@@ -350,7 +341,7 @@ class ServerOperator {
|
||||
}).catch((error) => {
|
||||
let errMessage = Utils.getErrorMsg(error);
|
||||
if (errMessage === gettext('Error')) {
|
||||
errMessage = gettext('Renaming {name} failed').replace('{name}', _name);
|
||||
errMessage = gettext('Renaming {name} failed').replace('{name}', oldName);
|
||||
}
|
||||
fail_callback(errMessage);
|
||||
});
|
||||
@@ -368,7 +359,7 @@ class ServerOperator {
|
||||
errMessage = Utils.getErrorMsg(error);
|
||||
}
|
||||
if (errMessage === gettext('Error')) {
|
||||
errMessage = gettext('Renaming {name} failed').replace('{name}', _name);
|
||||
errMessage = gettext('Renaming {name} failed').replace('{name}', oldName);
|
||||
}
|
||||
fail_callback(errMessage);
|
||||
});
|
||||
|
@@ -97,7 +97,7 @@ const Table = () => {
|
||||
}
|
||||
store.modifyRecords(rowIds, idRowUpdates, idOriginalRowUpdates, idOldRowData, idOriginalOldRowData, isCopyPaste, isRename, {
|
||||
fail_callback: (error) => {
|
||||
toaster.danger(error);
|
||||
error && toaster.danger(error);
|
||||
},
|
||||
success_callback: () => {
|
||||
if (isRename) {
|
||||
|
Reference in New Issue
Block a user