|
| 1 | +import type { ICommand, IMutationInfo, Workbook } from '@univerjs/presets' |
| 2 | +import type { |
| 3 | + ISetRangeValuesMutationParams, |
| 4 | + ISetWorksheetColumnCountMutationParams, |
| 5 | + ISetWorksheetRowCountMutationParams, |
| 6 | +} from '@univerjs/presets/preset-sheets-core' |
| 7 | +import { FolderIcon } from '@univerjs/icons' |
| 8 | +import { |
| 9 | + CommandType, |
| 10 | + covertCellValues, |
| 11 | + ICommandService, |
| 12 | + Inject, |
| 13 | + Injector, |
| 14 | + IUndoRedoService, |
| 15 | + IUniverInstanceService, |
| 16 | + Plugin, |
| 17 | + sequenceExecute, |
| 18 | + UniverInstanceType, |
| 19 | +} from '@univerjs/presets' |
| 20 | +import { |
| 21 | + ComponentManager, |
| 22 | + IMenuManagerService, |
| 23 | + MenuItemType, |
| 24 | + RibbonStartGroup, |
| 25 | + SetRangeValuesMutation, |
| 26 | + SetRangeValuesUndoMutationFactory, |
| 27 | + SetWorksheetColumnCountMutation, |
| 28 | + SetWorksheetColumnCountUndoMutationFactory, |
| 29 | + SetWorksheetRowCountMutation, |
| 30 | + SetWorksheetRowCountUndoMutationFactory, |
| 31 | +} from '@univerjs/presets/preset-sheets-core' |
| 32 | +import { waitUserSelectCSVFile } from './utils' |
| 33 | + |
| 34 | +/** |
| 35 | + * Import CSV Button Plugin |
| 36 | + * A simple Plugin example, show how to write a plugin. |
| 37 | + */ |
| 38 | +export class ImportCSVButtonPlugin extends Plugin { |
| 39 | + static override pluginName = 'import-csv-plugin' |
| 40 | + |
| 41 | + constructor( |
| 42 | + // inject injector, required |
| 43 | + @Inject(Injector) readonly _injector: Injector, |
| 44 | + // inject menu service, to add toolbar button |
| 45 | + @Inject(IMenuManagerService) private readonly menuManagerService: IMenuManagerService, |
| 46 | + // inject command service, to register command handler |
| 47 | + @Inject(ICommandService) private readonly commandService: ICommandService, |
| 48 | + // inject component manager, to register icon component |
| 49 | + @Inject(ComponentManager) private readonly componentManager: ComponentManager, |
| 50 | + ) { |
| 51 | + super() |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * The first lifecycle of the plugin mounted on the Univer instance, |
| 56 | + * the Univer business instance has not been created at this time. |
| 57 | + * The plugin should add its own module to the dependency injection system at this lifecycle. |
| 58 | + * It is not recommended to initialize the internal module of the plugin outside this lifecycle. |
| 59 | + */ |
| 60 | + |
| 61 | + override onStarting() { |
| 62 | + // register icon component |
| 63 | + this.componentManager.register('FolderIcon', FolderIcon) |
| 64 | + |
| 65 | + const buttonId = 'import-csv-button' |
| 66 | + |
| 67 | + const command: ICommand = { |
| 68 | + type: CommandType.OPERATION, |
| 69 | + id: buttonId, |
| 70 | + handler: (accessor) => { |
| 71 | + // inject univer instance service |
| 72 | + const univerInstanceService = accessor.get(IUniverInstanceService) |
| 73 | + const commandService = accessor.get(ICommandService) |
| 74 | + const undoRedoService = accessor.get(IUndoRedoService) |
| 75 | + |
| 76 | + // get current sheet |
| 77 | + const worksheet = univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet() |
| 78 | + const unitId = worksheet.getUnitId() |
| 79 | + const subUnitId = worksheet.getSheetId() |
| 80 | + |
| 81 | + // wait user select csv file, then assemble multiple mutations operation to enable correct undo/redo |
| 82 | + return waitUserSelectCSVFile(({ data, rowsCount, colsCount }) => { |
| 83 | + const redoMutations: IMutationInfo[] = [] |
| 84 | + const undoMutations: IMutationInfo[] = [] |
| 85 | + |
| 86 | + // set sheet row count |
| 87 | + const setRowCountMutationRedoParams: ISetWorksheetRowCountMutationParams = { |
| 88 | + unitId, |
| 89 | + subUnitId, |
| 90 | + rowCount: rowsCount, |
| 91 | + } |
| 92 | + const setRowCountMutationUndoParams: ISetWorksheetRowCountMutationParams = SetWorksheetRowCountUndoMutationFactory( |
| 93 | + accessor, |
| 94 | + setRowCountMutationRedoParams, |
| 95 | + ) |
| 96 | + redoMutations.push({ id: SetWorksheetRowCountMutation.id, params: setRowCountMutationRedoParams }) |
| 97 | + undoMutations.push({ id: SetWorksheetRowCountMutation.id, params: setRowCountMutationUndoParams }) |
| 98 | + |
| 99 | + // set sheet column count |
| 100 | + const setColumnCountMutationRedoParams: ISetWorksheetColumnCountMutationParams = { |
| 101 | + unitId, |
| 102 | + subUnitId, |
| 103 | + columnCount: colsCount, |
| 104 | + } |
| 105 | + const setColumnCountMutationUndoParams: ISetWorksheetColumnCountMutationParams = SetWorksheetColumnCountUndoMutationFactory( |
| 106 | + accessor, |
| 107 | + setColumnCountMutationRedoParams, |
| 108 | + ) |
| 109 | + redoMutations.push({ id: SetWorksheetColumnCountMutation.id, params: setColumnCountMutationRedoParams }) |
| 110 | + undoMutations.unshift({ id: SetWorksheetColumnCountMutation.id, params: setColumnCountMutationUndoParams }) |
| 111 | + |
| 112 | + // parse csv to univer data |
| 113 | + const cellValue = covertCellValues(data, { |
| 114 | + startColumn: 0, // start column index |
| 115 | + startRow: 0, // start row index |
| 116 | + endColumn: colsCount - 1, // end column index |
| 117 | + endRow: rowsCount - 1, // end row index |
| 118 | + }) |
| 119 | + |
| 120 | + // set sheet data |
| 121 | + const setRangeValuesMutationRedoParams: ISetRangeValuesMutationParams = { |
| 122 | + unitId, |
| 123 | + subUnitId, |
| 124 | + cellValue, |
| 125 | + } |
| 126 | + const setRangeValuesMutationUndoParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory( |
| 127 | + accessor, |
| 128 | + setRangeValuesMutationRedoParams, |
| 129 | + ) |
| 130 | + redoMutations.push({ id: SetRangeValuesMutation.id, params: setRangeValuesMutationRedoParams }) |
| 131 | + undoMutations.unshift({ id: SetRangeValuesMutation.id, params: setRangeValuesMutationUndoParams }) |
| 132 | + |
| 133 | + const result = sequenceExecute(redoMutations, commandService) |
| 134 | + |
| 135 | + if (result.result) { |
| 136 | + undoRedoService.pushUndoRedo({ |
| 137 | + unitID: unitId, |
| 138 | + undoMutations, |
| 139 | + redoMutations, |
| 140 | + }) |
| 141 | + |
| 142 | + return true |
| 143 | + } |
| 144 | + |
| 145 | + return false |
| 146 | + }) |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + const menuItemFactory = () => ({ |
| 151 | + id: buttonId, |
| 152 | + title: 'Import CSV', |
| 153 | + tooltip: 'Import CSV', |
| 154 | + icon: 'FolderIcon', // icon name |
| 155 | + type: MenuItemType.BUTTON, |
| 156 | + }) |
| 157 | + |
| 158 | + this.menuManagerService.mergeMenu({ |
| 159 | + [RibbonStartGroup.OTHERS]: { |
| 160 | + [buttonId]: { |
| 161 | + order: 10, |
| 162 | + menuItemFactory, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }) |
| 166 | + |
| 167 | + this.commandService.registerCommand(command) |
| 168 | + } |
| 169 | +} |
0 commit comments