@@ -21,6 +21,9 @@ import "@univerjs/ui/lib/index.css";
2121import "@univerjs/docs-ui/lib/index.css" ;
2222import "@univerjs/sheets-ui/lib/index.css" ;
2323import "@univerjs/sheets-formula-ui/lib/index.css" ;
24+ import type { IEventParamConfig } from "@univerjs/presets" ;
25+ import { debounce } from "./debounce" ;
26+ import { deduplicate } from "./deduplicate" ;
2427
2528const univerPresets = import ( "@univerjs/presets" ) ;
2629const univerPresetsSheets = import ( "@univerjs/presets/preset-sheets-core" ) ;
@@ -144,25 +147,6 @@ interface UpdateParams {
144147 errorModal : ReturnType < typeof setupErrorModal > ;
145148}
146149
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-
166150const performUpdate = async ( params : UpdateParams ) => {
167151 const { update_link, x, y, value, customId, errorModal } = params ;
168152 if ( ! update_link ) return ;
@@ -254,21 +238,25 @@ async function renderSpreadsheet(
254238 } ;
255239 if ( DEBUG ) console . log ( "sqlpage-spreadsheet: creating sheet" , sheet ) ;
256240
257- univerAPI . createUniverSheet ( sheet ) ;
241+ univerAPI . createWorkbook ( sheet ) ;
258242
259243 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+ ) ;
272260}
273261
274262function handleSetRangeValues (
@@ -354,19 +342,3 @@ const elem = elems[elems.length - 1];
354342if ( ! ( elem instanceof HTMLElement ) )
355343 throw new Error ( "No spreadsheet elements found" ) ;
356344renderSpreadsheetToElement ( 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