11import i18next from 'i18next'
22import { Toast , Docx , docx , mdast } from '@dolphin/lark'
3- import { OneHundred , Second , waitFor } from '@dolphin/common'
3+ import { Minute , OneHundred , Second , waitFor } from '@dolphin/common'
44import { fileSave , supported } from 'browser-fs-access'
55import { fs } from '@zip.js/zip.js'
66import normalizeFileName from 'filenamify/browser'
@@ -448,9 +448,34 @@ const prepare = async (): Promise<PrepareResult> => {
448448 } )
449449 }
450450
451+ enum Direction {
452+ Up ,
453+ Down ,
454+ }
455+ let direction = Direction . Down
456+ const calculateDirection = ( ) => {
457+ const scrollHeight = docx . container ?. scrollHeight ?? 0
458+ const clientHeight = docx . container ?. clientHeight ?? 0
459+
460+ if (
461+ direction === Direction . Down &&
462+ top > scrollHeight + 2 * clientHeight
463+ ) {
464+ return Direction . Up
465+ }
466+
467+ if ( direction === Direction . Up && top < 0 ) {
468+ return Direction . Down
469+ }
470+
471+ return direction
472+ }
473+
451474 let top = 0
475+
452476 docx . scrollTo ( {
453477 top,
478+ behavior : 'instant' ,
454479 } )
455480
456481 const maxTryTimes = OneHundred
@@ -471,12 +496,20 @@ const prepare = async (): Promise<PrepareResult> => {
471496 while ( ! checkIsReady ( ) && tryTimes <= maxTryTimes ) {
472497 docx . scrollTo ( {
473498 top,
499+ behavior : 'smooth' ,
474500 } )
475501
476502 await waitFor ( 0.4 * Second )
477503
504+ direction = calculateDirection ( )
505+
478506 tryTimes ++
479- top += ( docx . container ?. clientHeight ?? 4 * OneHundred ) * 2
507+
508+ const sign = direction === Direction . Down ? 1 : - 1
509+ const containerClientHeight =
510+ docx . container ?. clientHeight ?? 4 * OneHundred
511+
512+ top += sign * containerClientHeight
480513 }
481514
482515 Toast . remove ( TranslationKey . SCROLL_DOCUMENT )
@@ -641,7 +674,7 @@ main({
641674 actionText : i18next . t ( CommonTranslationKey . CONFIRM_REPORT_BUG , {
642675 ns : Namespace . COMMON ,
643676 } ) ,
644- duration : Number . POSITIVE_INFINITY ,
677+ duration : Minute ,
645678 onActionClick : ( ) => {
646679 reportBug ( error )
647680
0 commit comments