1
1
import i18next from 'i18next'
2
2
import { Toast , Docx , docx , mdast } from '@dolphin/lark'
3
- import { OneHundred , Second , waitFor } from '@dolphin/common'
3
+ import { Minute , OneHundred , Second , waitFor } from '@dolphin/common'
4
4
import { fileSave , supported } from 'browser-fs-access'
5
5
import { fs } from '@zip.js/zip.js'
6
6
import normalizeFileName from 'filenamify/browser'
@@ -448,9 +448,34 @@ const prepare = async (): Promise<PrepareResult> => {
448
448
} )
449
449
}
450
450
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
+
451
474
let top = 0
475
+
452
476
docx . scrollTo ( {
453
477
top,
478
+ behavior : 'instant' ,
454
479
} )
455
480
456
481
const maxTryTimes = OneHundred
@@ -471,12 +496,20 @@ const prepare = async (): Promise<PrepareResult> => {
471
496
while ( ! checkIsReady ( ) && tryTimes <= maxTryTimes ) {
472
497
docx . scrollTo ( {
473
498
top,
499
+ behavior : 'smooth' ,
474
500
} )
475
501
476
502
await waitFor ( 0.4 * Second )
477
503
504
+ direction = calculateDirection ( )
505
+
478
506
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
480
513
}
481
514
482
515
Toast . remove ( TranslationKey . SCROLL_DOCUMENT )
@@ -641,7 +674,7 @@ main({
641
674
actionText : i18next . t ( CommonTranslationKey . CONFIRM_REPORT_BUG , {
642
675
ns : Namespace . COMMON ,
643
676
} ) ,
644
- duration : Number . POSITIVE_INFINITY ,
677
+ duration : Minute ,
645
678
onActionClick : ( ) => {
646
679
reportBug ( error )
647
680
0 commit comments