Skip to content

Commit ea29c26

Browse files
authored
Merge pull request #51 from whale4113/fix/whale/locate-block
fix: doc is too large to scroll to completion
2 parents 586abba + 5e704cb commit ea29c26

File tree

7 files changed

+51
-139
lines changed

7 files changed

+51
-139
lines changed

.changeset/eight-ears-develop.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: document is too large to scroll to completion
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: `waitForFunction()` support async function

.changeset/pink-rivers-reply.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: use api to locate blocks

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

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -448,29 +448,6 @@ 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-
474451
let top = 0
475452

476453
docx.scrollTo({
@@ -491,8 +468,6 @@ const prepare = async (): Promise<PrepareResult> => {
491468
},
492469
})
493470

494-
const { disconnect } = docx.observeScrollTopOfBlock()
495-
496471
while (!checkIsReady() && tryTimes <= maxTryTimes) {
497472
docx.scrollTo({
498473
top,
@@ -501,20 +476,12 @@ const prepare = async (): Promise<PrepareResult> => {
501476

502477
await waitFor(0.4 * Second)
503478

504-
direction = calculateDirection()
505-
506479
tryTimes++
507480

508-
const sign = direction === Direction.Down ? 1 : -1
509-
const containerClientHeight =
510-
docx.container?.clientHeight ?? 4 * OneHundred
511-
512-
top += sign * containerClientHeight
481+
top = docx.container?.scrollHeight ?? 0
513482
}
514483

515484
Toast.remove(TranslationKey.SCROLL_DOCUMENT)
516-
517-
disconnect()
518485
}
519486

520487
return {

packages/common/src/dom.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const waitForSelector = async (
1111
) => waitForFunction(() => document.querySelector(selector) !== null, options)
1212

1313
export const waitForFunction = async (
14-
func: () => boolean,
14+
func: () => boolean | Promise<boolean>,
1515
options: {
1616
/**
1717
* @default 400
@@ -27,7 +27,9 @@ export const waitForFunction = async (
2727

2828
const isTimeout = () => timeoutId === null
2929

30-
while (!func() && !isTimeout()) {
30+
const _func = () => Promise.resolve(func()).catch(() => false)
31+
32+
while (!(await _func()) && !isTimeout()) {
3133
await waitFor(0.1 * Second)
3234
}
3335

packages/lark/src/docx.ts

Lines changed: 26 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -731,19 +731,6 @@ type Mutate<T extends Block> = T extends PageBlock
731731
: T extends IframeBlock
732732
? mdast.Html
733733
: null
734-
interface ScrollToBlock {
735-
(
736-
blockId: number,
737-
options?: {
738-
/**
739-
* @default 0
740-
*/
741-
offset?:
742-
| number
743-
| ((options: { container: HTMLDivElement | null }) => number)
744-
},
745-
): void
746-
}
747734

748735
interface TransformerOptions {
749736
/**
@@ -757,9 +744,9 @@ interface TransformerOptions {
757744
*/
758745
file?: boolean
759746
/**
760-
* Scroll document to specific block.
747+
* Locate block with record id.
761748
*/
762-
scrollToBlock?: ScrollToBlock
749+
locateBlockWithRecordId?: (recordId: string) => Promise<boolean>
763750
}
764751

765752
interface TransformResult<T> {
@@ -1003,28 +990,18 @@ export class Transformer {
1003990
data: {
1004991
fetchBlob: async () => {
1005992
try {
1006-
this.options.scrollToBlock?.(whiteboard.id)
1007-
} catch (error) {
1008-
console.error(error)
1009-
}
993+
const {
994+
locateBlockWithRecordId = () => Promise.resolve(false),
995+
} = this.options
1010996

1011-
let offset = 0
1012-
try {
1013997
await waitForFunction(
1014-
() => {
1015-
const exists = whiteboard.whiteboardBlock !== undefined
1016-
1017-
if (!exists) {
1018-
this.options.scrollToBlock?.(whiteboard.id, {
1019-
offset: ({ container }) =>
1020-
(offset += container?.clientHeight ?? OneHundred),
1021-
})
1022-
}
1023-
1024-
return exists
1025-
},
998+
() =>
999+
locateBlockWithRecordId(whiteboard.record?.id ?? '').then(
1000+
isSuccess =>
1001+
isSuccess && whiteboard.whiteboardBlock !== undefined,
1002+
),
10261003
{
1027-
timeout: 10 * Second,
1004+
timeout: 3 * Second,
10281005
},
10291006
)
10301007
} catch (error) {
@@ -1144,8 +1121,6 @@ export class Transformer {
11441121
}
11451122

11461123
export class Docx {
1147-
private blockIdToScrollTop: Map<string, number> = new Map()
1148-
11491124
static stringify(root: mdast.Root) {
11501125
return toMarkdown(root, {
11511126
extensions: [
@@ -1200,67 +1175,20 @@ export class Docx {
12001175
this.rootBlock.children.every(block => {
12011176
const prerequisite = block.snapshot.type !== 'pending'
12021177

1203-
const isWhiteboard = (block: Blocks): block is Whiteboard =>
1178+
const isWhiteboard = (block: Blocks): boolean =>
12041179
block.type === BlockType.WHITEBOARD ||
12051180
(block.type === BlockType.FALLBACK &&
12061181
block.snapshot.type === BlockType.WHITEBOARD)
12071182

12081183
if (checkWhiteboard && isWhiteboard(block)) {
1209-
return (
1210-
prerequisite &&
1211-
this.blockIdToScrollTop.get(String(block.id)) !== undefined
1212-
)
1184+
return prerequisite && block.type !== BlockType.FALLBACK
12131185
}
12141186

12151187
return prerequisite
12161188
})
12171189
)
12181190
}
12191191

1220-
observeScrollTopOfBlock() {
1221-
let observer: null | MutationObserver = new MutationObserver(mutations => {
1222-
const container = this.container
1223-
if (!container) {
1224-
return
1225-
}
1226-
1227-
for (const mutation of mutations) {
1228-
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
1229-
for (const node of mutation.addedNodes) {
1230-
if (node instanceof Element) {
1231-
if (
1232-
node.getAttribute('data-block-type') === BlockType.WHITEBOARD
1233-
) {
1234-
const id = node.getAttribute('data-block-id')
1235-
if (id) {
1236-
this.blockIdToScrollTop.set(id, container.scrollTop)
1237-
}
1238-
}
1239-
}
1240-
}
1241-
}
1242-
}
1243-
})
1244-
1245-
const wrapperSelector =
1246-
'.zone-container > .page-block-children > .root-render-unit-container > .render-unit-wrapper'
1247-
const wrapper = this.container?.querySelector(wrapperSelector)
1248-
1249-
if (wrapper) {
1250-
observer?.observe(wrapper, {
1251-
childList: true,
1252-
})
1253-
}
1254-
1255-
return {
1256-
disconnect: () => {
1257-
observer?.disconnect()
1258-
1259-
observer = null
1260-
},
1261-
}
1262-
}
1263-
12641192
scrollTo(options: ScrollToOptions) {
12651193
const container = this.container
12661194
if (container) {
@@ -1285,29 +1213,24 @@ export class Docx {
12851213
return { root: { type: 'root', children: [] }, images: [], files: [] }
12861214
}
12871215

1288-
const scrollToBlock: ScrollToBlock = (
1289-
blockId: number,
1290-
options = {},
1291-
): void => {
1292-
const cacheScrollTop = this.blockIdToScrollTop.get(String(blockId))
1293-
if (cacheScrollTop === undefined) {
1294-
throw new Error(`Block ${blockId} no cache scroll top.`)
1295-
}
1216+
const locateBlockWithRecordId = async (
1217+
recordId: string,
1218+
): Promise<boolean> => {
1219+
try {
1220+
if (!PageMain) {
1221+
return false
1222+
}
12961223

1297-
const { offset = 0 } = options
1298-
const normalizedOffset =
1299-
typeof offset === 'number'
1300-
? offset
1301-
: offset({ container: this.container })
1224+
return await PageMain.locateBlockWithRecordIdImpl(recordId)
1225+
} catch (error) {
1226+
console.error(error)
1227+
}
13021228

1303-
this.scrollTo({
1304-
top: cacheScrollTop + normalizedOffset,
1305-
behavior: 'instant',
1306-
})
1229+
return false
13071230
}
13081231

13091232
const transformer = new Transformer({
1310-
scrollToBlock,
1233+
locateBlockWithRecordId,
13111234
...transformerOptions,
13121235
})
13131236

packages/lark/src/env.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export interface PageMain {
4747
}
4848
rootBlockModel: import('./docx').PageBlock
4949
}
50+
51+
locateBlockWithRecordIdImpl(
52+
recordId: string,
53+
options?: Record<string, unknown>,
54+
): Promise<boolean>
5055
}
5156

5257
export const PageMain = window.PageMain

0 commit comments

Comments
 (0)