@@ -21,6 +21,9 @@ import "@univerjs/ui/lib/index.css";
21
21
import "@univerjs/docs-ui/lib/index.css" ;
22
22
import "@univerjs/sheets-ui/lib/index.css" ;
23
23
import "@univerjs/sheets-formula-ui/lib/index.css" ;
24
+ import type { IEventParamConfig } from "@univerjs/presets" ;
25
+ import { debounce } from "./debounce" ;
26
+ import { deduplicate } from "./deduplicate" ;
24
27
25
28
const univerPresets = import ( "@univerjs/presets" ) ;
26
29
const univerPresetsSheets = import ( "@univerjs/presets/preset-sheets-core" ) ;
@@ -144,25 +147,6 @@ interface UpdateParams {
144
147
errorModal : ReturnType < typeof setupErrorModal > ;
145
148
}
146
149
147
- function debounce < T > ( fn : ( items : T [ ] ) => Promise < void > , timeoutMs : number ) {
148
- let timeout : NodeJS . Timeout | null = null ;
149
- let items : T [ ] = [ ] ;
150
-
151
- return ( item : T ) => {
152
- if ( timeout ) {
153
- clearTimeout ( timeout ) ;
154
- }
155
-
156
- items . push ( item ) ;
157
-
158
- timeout = setTimeout ( async ( ) => {
159
- const currentItems = items ;
160
- items = [ ] ;
161
- await fn ( currentItems ) ;
162
- } , timeoutMs ) ;
163
- } ;
164
- }
165
-
166
150
const performUpdate = async ( params : UpdateParams ) => {
167
151
const { update_link, x, y, value, customId, errorModal } = params ;
168
152
if ( ! update_link ) return ;
@@ -254,21 +238,25 @@ async function renderSpreadsheet(
254
238
} ;
255
239
if ( DEBUG ) console . log ( "sqlpage-spreadsheet: creating sheet" , sheet ) ;
256
240
257
- univerAPI . createUniverSheet ( sheet ) ;
241
+ univerAPI . createWorkbook ( sheet ) ;
258
242
259
243
const { update_link } = props ;
260
- univerAPI . onCommandExecuted ( ( { id, params } ) => {
261
- // To debug:
262
- // console.log(id, params);
263
- if ( update_link && id === SetRangeValuesMutation . id ) {
264
- handleSetRangeValues (
265
- params as ISetRangeValuesMutationParams ,
266
- update_link ,
267
- errorModal ,
268
- cellIdMap ,
269
- ) ;
270
- }
271
- } ) ;
244
+ univerAPI . addEvent (
245
+ "CommandExecuted" ,
246
+ // @ts -ignore: https://github.com/dream-num/univer/issues/4504
247
+ ( { id, params } : IEventParamConfig [ "CommandExecuted" ] ) => {
248
+ // To debug:
249
+ // console.log(id, params);
250
+ if ( update_link && id === SetRangeValuesMutation . id ) {
251
+ handleSetRangeValues (
252
+ params as ISetRangeValuesMutationParams ,
253
+ update_link ,
254
+ errorModal ,
255
+ cellIdMap ,
256
+ ) ;
257
+ }
258
+ } ,
259
+ ) ;
272
260
}
273
261
274
262
function handleSetRangeValues (
@@ -354,19 +342,3 @@ const elem = elems[elems.length - 1];
354
342
if ( ! ( elem instanceof HTMLElement ) )
355
343
throw new Error ( "No spreadsheet elements found" ) ;
356
344
renderSpreadsheetToElement ( elem ) ;
357
-
358
- /** Keeps only the last occurrence of each item in the array */
359
- function deduplicate < T , Y > ( arr : T [ ] , getKey : ( item : T ) => Y ) : void {
360
- const keyPositions = new Map < Y , number > ( ) ;
361
- let writeAt = 0 ;
362
-
363
- for ( let readAt = 0 ; readAt < arr . length ; readAt ++ ) {
364
- const item = arr [ readAt ] ;
365
- const key = getKey ( item ) ;
366
- const keyPos = keyPositions . get ( key ) ?? writeAt ++ ;
367
- keyPositions . set ( key , keyPos ) ;
368
- arr [ keyPos ] = item ;
369
- }
370
-
371
- arr . length = writeAt ;
372
- }
0 commit comments