Skip to content

Commit da2f301

Browse files
authored
Merge pull request #48 from whale4113/fix/whale/scroll
scroll up and down to fully load doc
2 parents acc02c9 + be8407a commit da2f301

File tree

10 files changed

+85
-15
lines changed

10 files changed

+85
-15
lines changed

.changeset/calm-eels-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@dolphin/chrome-extension': patch
3+
---
4+
5+
feat: adjusting the scroll down threshold

.changeset/fluffy-eels-chew.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@dolphin/lark': patch
3+
---
4+
5+
feat: `offset` option support function format

.changeset/mighty-pigs-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@dolphin/lark': patch
3+
---
4+
5+
fix: `keepAlive` option is for `Toast.loading()`

.changeset/pretty-jars-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@dolphin/chrome-extension': patch
3+
---
4+
5+
fix: scroll up and down to fully load doc
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@dolphin/common': patch
3+
---
4+
5+
feat: add `Minute` const

apps/chrome-extension/src/common/notification.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Toast } from '@dolphin/lark/env'
21
import i18next from 'i18next'
2+
import { Toast } from '@dolphin/lark/env'
3+
import { Minute } from '@dolphin/common'
34
import { CommonTranslationKey, Namespace } from './i18n'
45

56
export const confirm = (): Promise<boolean> => {
@@ -8,6 +9,7 @@ export const confirm = (): Promise<boolean> => {
89

910
Toast.info({
1011
closable: true,
12+
duration: Minute,
1113
content: i18next.t(CommonTranslationKey.CONTINUE, {
1214
ns: Namespace.COMMON,
1315
}),

apps/chrome-extension/src/scripts/download-lark-docx-as-markdown.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import i18next from 'i18next'
22
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'
44
import { fileSave, supported } from 'browser-fs-access'
55
import { fs } from '@zip.js/zip.js'
66
import 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

packages/common/src/time.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export const Second = 1000
22

3+
export const Minute = 60 * Second
4+
35
export const waitFor = (timeout = 0.4 * Second): Promise<void> =>
46
new Promise(resolve => {
57
setTimeout(() => {

packages/lark/src/docx.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,9 @@ interface ScrollToBlock {
738738
/**
739739
* @default 0
740740
*/
741-
offset?: number
741+
offset?:
742+
| number
743+
| ((options: { container: HTMLDivElement | null }) => number)
742744
},
743745
): void
744746
}
@@ -1006,7 +1008,6 @@ export class Transformer {
10061008
console.error(error)
10071009
}
10081010

1009-
const delta = OneHundred / 2
10101011
let offset = 0
10111012
try {
10121013
await waitForFunction(
@@ -1015,7 +1016,8 @@ export class Transformer {
10151016

10161017
if (!exists) {
10171018
this.options.scrollToBlock?.(whiteboard.id, {
1018-
offset: (offset += delta),
1019+
offset: ({ container }) =>
1020+
(offset += container?.clientHeight ?? OneHundred),
10191021
})
10201022
}
10211023

@@ -1176,7 +1178,9 @@ export class Docx {
11761178
}
11771179

11781180
get container() {
1179-
const container = document.querySelector('#mainBox .bear-web-x-container')
1181+
const container = document.querySelector<HTMLDivElement>(
1182+
'#mainBox .bear-web-x-container',
1183+
)
11801184

11811185
return container
11821186
}
@@ -1239,7 +1243,7 @@ export class Docx {
12391243
})
12401244

12411245
const wrapperSelector =
1242-
'.zone-container > .page-block-children .render-unit-wrapper'
1246+
'.zone-container > .page-block-children > .root-render-unit-container > .render-unit-wrapper'
12431247
const wrapper = this.container?.querySelector(wrapperSelector)
12441248

12451249
if (wrapper) {
@@ -1285,15 +1289,19 @@ export class Docx {
12851289
blockId: number,
12861290
options = {},
12871291
): void => {
1288-
const { offset = 0 } = options
1289-
12901292
const cacheScrollTop = this.blockIdToScrollTop.get(String(blockId))
12911293
if (cacheScrollTop === undefined) {
12921294
throw new Error(`Block ${blockId} no cache scroll top.`)
12931295
}
12941296

1297+
const { offset = 0 } = options
1298+
const normalizedOffset =
1299+
typeof offset === 'number'
1300+
? offset
1301+
: offset({ container: this.container })
1302+
12951303
this.scrollTo({
1296-
top: cacheScrollTop + offset,
1304+
top: cacheScrollTop + normalizedOffset,
12971305
behavior: 'instant',
12981306
})
12991307
}

packages/lark/src/env.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const noop = () => {}
22

33
interface ToastOptions {
4-
content: string
54
key?: string
6-
keepAlive?: boolean
7-
closable?: boolean
5+
content: string
86
actionText?: string
97
duration?: number
8+
keepAlive?: boolean
9+
closable?: boolean
1010
onActionClick?: () => void
1111
onClose?: () => void
1212
}

0 commit comments

Comments
 (0)