@@ -731,19 +731,6 @@ type Mutate<T extends Block> = T extends PageBlock
731
731
: T extends IframeBlock
732
732
? mdast . Html
733
733
: 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
- }
747
734
748
735
interface TransformerOptions {
749
736
/**
@@ -757,9 +744,9 @@ interface TransformerOptions {
757
744
*/
758
745
file ?: boolean
759
746
/**
760
- * Scroll document to specific block .
747
+ * Locate block with record id .
761
748
*/
762
- scrollToBlock ?: ScrollToBlock
749
+ locateBlockWithRecordId ?: ( recordId : string ) => Promise < boolean >
763
750
}
764
751
765
752
interface TransformResult < T > {
@@ -1003,28 +990,18 @@ export class Transformer {
1003
990
data : {
1004
991
fetchBlob : async ( ) => {
1005
992
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
1010
996
1011
- let offset = 0
1012
- try {
1013
997
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
+ ) ,
1026
1003
{
1027
- timeout : 10 * Second ,
1004
+ timeout : 3 * Second ,
1028
1005
} ,
1029
1006
)
1030
1007
} catch ( error ) {
@@ -1144,8 +1121,6 @@ export class Transformer {
1144
1121
}
1145
1122
1146
1123
export class Docx {
1147
- private blockIdToScrollTop : Map < string , number > = new Map ( )
1148
-
1149
1124
static stringify ( root : mdast . Root ) {
1150
1125
return toMarkdown ( root , {
1151
1126
extensions : [
@@ -1200,67 +1175,20 @@ export class Docx {
1200
1175
this . rootBlock . children . every ( block => {
1201
1176
const prerequisite = block . snapshot . type !== 'pending'
1202
1177
1203
- const isWhiteboard = ( block : Blocks ) : block is Whiteboard =>
1178
+ const isWhiteboard = ( block : Blocks ) : boolean =>
1204
1179
block . type === BlockType . WHITEBOARD ||
1205
1180
( block . type === BlockType . FALLBACK &&
1206
1181
block . snapshot . type === BlockType . WHITEBOARD )
1207
1182
1208
1183
if ( checkWhiteboard && isWhiteboard ( block ) ) {
1209
- return (
1210
- prerequisite &&
1211
- this . blockIdToScrollTop . get ( String ( block . id ) ) !== undefined
1212
- )
1184
+ return prerequisite && block . type !== BlockType . FALLBACK
1213
1185
}
1214
1186
1215
1187
return prerequisite
1216
1188
} )
1217
1189
)
1218
1190
}
1219
1191
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
-
1264
1192
scrollTo ( options : ScrollToOptions ) {
1265
1193
const container = this . container
1266
1194
if ( container ) {
@@ -1285,29 +1213,24 @@ export class Docx {
1285
1213
return { root : { type : 'root' , children : [ ] } , images : [ ] , files : [ ] }
1286
1214
}
1287
1215
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
+ }
1296
1223
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
+ }
1302
1228
1303
- this . scrollTo ( {
1304
- top : cacheScrollTop + normalizedOffset ,
1305
- behavior : 'instant' ,
1306
- } )
1229
+ return false
1307
1230
}
1308
1231
1309
1232
const transformer = new Transformer ( {
1310
- scrollToBlock ,
1233
+ locateBlockWithRecordId ,
1311
1234
...transformerOptions ,
1312
1235
} )
1313
1236
0 commit comments