@light-sheet/react
Version:
FortuneSheet is a drop-in javascript spreadsheet library that provides rich features like Excel and Google Sheets
271 lines • 13.7 kB
JavaScript
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { addSheet, api, deleteRowCol, deleteSheet, insertRowCol, opToPatch, createFilterOptions, getSheetIndex } from "@light-sheet/core";
import { applyPatches } from "immer";
import _ from "lodash";
export function generateAPIs(context, setContext, handleUndo, handleRedo, settings, cellInput, scrollbarX, scrollbarY) {
return {
applyOp: function applyOp(ops) {
setContext(function (ctx_) {
var _ops$, _ops$2, _ops$3;
var _opToPatch = opToPatch(ctx_, ops),
_opToPatch2 = _slicedToArray(_opToPatch, 2),
patches = _opToPatch2[0],
specialOps = _opToPatch2[1];
if (specialOps.length > 0) {
var _specialOps = _slicedToArray(specialOps, 1),
specialOp = _specialOps[0];
if (specialOp.op === "insertRowCol") {
try {
insertRowCol(ctx_, specialOp.value, false);
} catch (e) {
console.error(e);
}
} else if (specialOp.op === "deleteRowCol") {
deleteRowCol(ctx_, specialOp.value);
} else if (specialOp.op === "addSheet") {
var _patches$filter, _specialOp$value;
var name = (_patches$filter = patches.filter(function (path) {
return path.path[0] === "name";
})) === null || _patches$filter === void 0 || (_patches$filter = _patches$filter[0]) === null || _patches$filter === void 0 ? void 0 : _patches$filter.value;
if ((_specialOp$value = specialOp.value) !== null && _specialOp$value !== void 0 && _specialOp$value.id) {
addSheet(ctx_, settings, specialOp.value.id, false, name, specialOp.value);
}
// 添加addSheet完后,给sheet初始化data
var fileIndex = getSheetIndex(ctx_, specialOp.value.id);
api.initSheetData(ctx_, fileIndex, specialOp.value);
} else if (specialOp.op === "deleteSheet") {
deleteSheet(ctx_, specialOp.value.id);
patches.length = 0;
}
}
if (((_ops$ = ops[0]) === null || _ops$ === void 0 || (_ops$ = _ops$.path) === null || _ops$ === void 0 ? void 0 : _ops$[0]) === "filter_select") ctx_.luckysheet_filter_save = ops[0].value;else if (((_ops$2 = ops[0]) === null || _ops$2 === void 0 || (_ops$2 = _ops$2.path) === null || _ops$2 === void 0 ? void 0 : _ops$2[0]) === "hide") {
// hide sheet
if (ctx_.currentSheetId === ops[0].id) {
var shownSheets = ctx_.luckysheetfile.filter(function (sheet) {
return (_.isUndefined(sheet.hide) || (sheet === null || sheet === void 0 ? void 0 : sheet.hide) !== 1) && sheet.id !== ops[0].id;
});
ctx_.currentSheetId = _.sortBy(shownSheets, function (sheet) {
return sheet.order;
})[0].id;
}
}
createFilterOptions(ctx_, ctx_.luckysheet_filter_save, (_ops$3 = ops[0]) === null || _ops$3 === void 0 ? void 0 : _ops$3.id);
if (patches.length === 0) return;
try {
applyPatches(ctx_, patches);
} catch (e) {
console.error(e);
}
}, {
noHistory: true
});
},
getCellValue: function getCellValue(row, column) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return api.getCellValue(context, row, column, options);
},
setCellValue: function setCellValue(row, column, value) {
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
return setContext(function (draftCtx) {
return api.setCellValue(draftCtx, row, column, value, cellInput, options);
});
},
clearCell: function clearCell(row, column) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return setContext(function (draftCtx) {
return api.clearCell(draftCtx, row, column, options);
});
},
setCellFormat: function setCellFormat(row, column, attr, value) {
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
return setContext(function (draftCtx) {
return api.setCellFormat(draftCtx, row, column, attr, value, options);
});
},
autoFillCell: function autoFillCell(copyRange, applyRange, direction) {
return setContext(function (draftCtx) {
return api.autoFillCell(draftCtx, copyRange, applyRange, direction);
});
},
freeze: function freeze(type, range) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return setContext(function (draftCtx) {
return api.freeze(draftCtx, type, range, options);
});
},
insertRowOrColumn: function insertRowOrColumn(type, index, count) {
var direction = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "rightbottom";
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
return setContext(function (draftCtx) {
return api.insertRowOrColumn(draftCtx, type, index, count, direction, options);
});
},
deleteRowOrColumn: function deleteRowOrColumn(type, start, end) {
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
return setContext(function (draftCtx) {
return api.deleteRowOrColumn(draftCtx, type, start, end, options);
});
},
hideRowOrColumn: function hideRowOrColumn(rowOrColInfo, type) {
return setContext(function (draftCtx) {
return api.hideRowOrColumn(draftCtx, rowOrColInfo, type);
});
},
showRowOrColumn: function showRowOrColumn(rowOrColInfo, type) {
return setContext(function (draftCtx) {
return api.showRowOrColumn(draftCtx, rowOrColInfo, type);
});
},
setRowHeight: function setRowHeight(rowInfo) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var custom = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return setContext(function (draftCtx) {
return api.setRowHeight(draftCtx, rowInfo, options, custom);
});
},
setColumnWidth: function setColumnWidth(columnInfo) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var custom = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return setContext(function (draftCtx) {
return api.setColumnWidth(draftCtx, columnInfo, options, custom);
});
},
getRowHeight: function getRowHeight(rows) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return api.getRowHeight(context, rows, options);
},
getColumnWidth: function getColumnWidth(columns) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return api.getColumnWidth(context, columns, options);
},
getSelection: function getSelection() {
return api.getSelection(context);
},
getFlattenRange: function getFlattenRange(range) {
return api.getFlattenRange(context, range);
},
getCellsByFlattenRange: function getCellsByFlattenRange(range) {
return api.getCellsByFlattenRange(context, range);
},
getSelectionCoordinates: function getSelectionCoordinates() {
return api.getSelectionCoordinates(context);
},
getCellsByRange: function getCellsByRange(range) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return api.getCellsByRange(context, range, options);
},
getHtmlByRange: function getHtmlByRange(range) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return api.getHtmlByRange(context, range, options);
},
setSelection: function setSelection(range) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return setContext(function (draftCtx) {
return api.setSelection(draftCtx, range, options);
});
},
setCellValuesByRange: function setCellValuesByRange(data, range) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return setContext(function (draftCtx) {
return api.setCellValuesByRange(draftCtx, data, range, cellInput, options);
});
},
setCellFormatByRange: function setCellFormatByRange(attr, value, range) {
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
return setContext(function (draftCtx) {
return api.setCellFormatByRange(draftCtx, attr, value, range, options);
});
},
mergeCells: function mergeCells(ranges, type) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return setContext(function (draftCtx) {
return api.mergeCells(draftCtx, ranges, type, options);
});
},
cancelMerge: function cancelMerge(ranges) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return setContext(function (draftCtx) {
return api.cancelMerge(draftCtx, ranges, options);
});
},
getAllSheets: function getAllSheets() {
return api.getAllSheets(context);
},
getSheet: function getSheet() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return api.getSheetWithLatestCelldata(context, options);
},
addSheet: function addSheet() {
return setContext(function (draftCtx) {
return api.addSheet(draftCtx, settings);
});
},
deleteSheet: function deleteSheet() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return setContext(function (draftCtx) {
return api.deleteSheet(draftCtx, options);
});
},
updateSheet: function updateSheet(data) {
return setContext(function (draftCtx) {
return api.updateSheet(draftCtx, data);
});
},
activateSheet: function activateSheet() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return setContext(function (draftCtx) {
return api.activateSheet(draftCtx, options);
});
},
setSheetName: function setSheetName(name) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return setContext(function (draftCtx) {
return api.setSheetName(draftCtx, name, options);
});
},
setSheetOrder: function setSheetOrder(orderList) {
return setContext(function (draftCtx) {
return api.setSheetOrder(draftCtx, orderList);
});
},
scroll: function scroll(options) {
return api.scroll(context, scrollbarX, scrollbarY, options);
},
addPresences: function addPresences(newPresences) {
setContext(function (draftCtx) {
draftCtx.presences = _.differenceBy(draftCtx.presences || [], newPresences, function (v) {
return v.userId == null ? v.username : v.userId;
}).concat(newPresences);
});
},
removePresences: function removePresences(arr) {
setContext(function (draftCtx) {
if (draftCtx.presences != null) {
draftCtx.presences = _.differenceBy(draftCtx.presences, arr, function (v) {
return v.userId == null ? v.username : v.userId;
});
}
});
},
handleUndo: handleUndo,
handleRedo: handleRedo,
calculateFormula: function calculateFormula() {
setContext(function (draftCtx) {
_.forEach(draftCtx.luckysheetfile, function (sheet_obj) {
api.calculateSheetFromula(draftCtx, sheet_obj.id);
});
});
},
dataToCelldata: function dataToCelldata(data) {
return api.dataToCelldata(data);
},
celldataToData: function celldataToData(celldata, rowCount, colCount) {
return api.celldataToData(celldata, rowCount, colCount);
}
};
}