Files
univer/examples/src/plugins/debugger/commands/operations/save-snapshot.operations.ts
T
白熱 ee85ccbef9 chore: migrate to eslint 8+ (#1235)
* chore: migrate to eslint 8+

* chore: remove unused deps

* fix: fix type check

* chore(eslint): add missing rules
2024-01-24 10:57:03 +08:00

98 lines
3.9 KiB
TypeScript

/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable node/prefer-global/process */
import type { ICommand, IStyleData, IWorkbookData } from '@univerjs/core';
import { CommandType, ISnapshotPersistenceService, IUniverInstanceService, ObjectMatrix } from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';
import { ExportController, RecordController } from '../../../local-save';
export interface ISaveSnapshotParams {
value: 'sheet' | 'workbook' | 'record';
}
const filterStyle = (workbookData: IWorkbookData) => {
const sheets = workbookData.sheets;
const cacheStyle: Record<string, IStyleData> = {};
Object.keys(sheets).forEach((sheetId) => {
const sheet = sheets[sheetId];
new ObjectMatrix(sheet.cellData).forValue((_r, _c, value) => {
const s = value?.s;
if (s && typeof s === 'string') {
const style = workbookData.styles[s];
if (style) {
cacheStyle[s] = style;
}
}
});
});
workbookData.styles = cacheStyle;
return workbookData;
};
export const SaveSnapshotOptions: ICommand = {
id: 'debugger.operation.saveSnapshot',
type: CommandType.OPERATION,
handler: async (accessor: IAccessor, params: ISaveSnapshotParams) => {
const univerInstanceService = accessor.get(IUniverInstanceService);
const localPersistenceService = accessor.get(ISnapshotPersistenceService);
const exportController = accessor.get(ExportController);
const recordController = accessor.get(RecordController);
const workbook = univerInstanceService.getCurrentUniverSheetInstance();
const worksheet = workbook.getActiveSheet();
const snapshot = localPersistenceService.saveWorkbook(workbook);
const gitHash = process.env.GIT_COMMIT_HASH;
const gitBranch = process.env.GIT_REF_NAME;
const buildTime = process.env.BUILD_TIME;
(snapshot as any).__env__ = { gitHash, gitBranch, buildTime };
const preName = new Date().toLocaleString();
switch (params.value) {
case 'sheet': {
const sheetId = worksheet.getSheetId();
const sheet = snapshot.sheets[sheetId];
snapshot.sheets = { [sheetId]: sheet };
snapshot.sheetOrder = [sheetId];
break;
}
case 'workbook': {
const text = JSON.stringify(filterStyle(snapshot), null, 2);
// navigator.clipboard.writeText(text);
exportController.exportJson(text, `${preName} snapshot`);
break;
}
case 'record': {
let endCommands = () => [];
recordController.record().subscribe((v) => {
if (v.type === 'start') {
endCommands = recordController.startSaveCommands() as any;
}
if (v.type === 'finish') {
const commands = endCommands();
exportController.exportWebm(v.data, `${preName} video`);
const commandsText = JSON.stringify(commands, null, 2);
exportController.exportJson(commandsText, `${preName} commands`);
}
});
}
}
return true;
},
};