11import { HTTPError } from 'koajax' ;
22import { BaseListModel } from 'mobx-restful' ;
3- import { cache } from 'web-utility' ;
3+ import { cache , formatDate , uniqueID } from 'web-utility' ;
44
55import { isLarkError , LarkData } from '../../type' ;
66import { createPageStream } from '../base' ;
7+ import { TaskModel } from '../Task' ;
78import { User } from '../User/type' ;
9+ import { WikiNode , WikiNodeModel } from '../Wiki' ;
810import {
911 BiTableBlock ,
1012 Block ,
@@ -14,10 +16,15 @@ import {
1416 IframeBlock ,
1517 IframeComponentType ,
1618 ImageBlock ,
19+ OrderedBlock ,
20+ QuoteContainerBlock ,
1721 SheetBlock ,
22+ SubPageList ,
23+ TaskBlock ,
1824 TextBlock ,
1925 TextElement ,
20- TextRun
26+ TextRun ,
27+ WikiCatalog
2128} from './type' ;
2229
2330export * from './type' ;
@@ -93,10 +100,33 @@ export abstract class DocumentModel extends BaseListModel<Document> {
93100 }
94101 }
95102
103+ async #getWikiSubDocuments( token : string ) {
104+ const { client } = this ;
105+
106+ class MyWikiNodeModel extends WikiNodeModel {
107+ client = client ;
108+ }
109+ const { body } = await client . get < LarkData < { node : WikiNode } > > (
110+ `wiki/v2/spaces/get_node?token=${ token } `
111+ ) ;
112+ const { space_id } = body ! . data ! . node ;
113+
114+ const wikiNodeStore = new MyWikiNodeModel ( this . domain , space_id ) ;
115+
116+ return wikiNodeStore . getAll ( { parent_node_token : token } ) ;
117+ }
118+
96119 async * #resolveBlocks(
97120 stream : AsyncIterable < Block < any , any , any > > ,
98121 resolveFileURL ?: FileURLResolver
99122 ) {
123+ const { client } = this ;
124+
125+ class MyTaskModel extends TaskModel {
126+ client = client ;
127+ }
128+ const taskStore = new MyTaskModel ( ) ;
129+
100130 for await ( let block of stream ) {
101131 if ( block . block_type === BlockType . text ) {
102132 const { text } = block as TextBlock ;
@@ -135,6 +165,61 @@ export abstract class DocumentModel extends BaseListModel<Document> {
135165 `https://${ this . domain } /base/${ token } ?table=${ table } `
136166 ) ;
137167 continue ;
168+ } else if (
169+ block . block_type === BlockType . wiki_catalog ||
170+ block . block_type === BlockType . sub_page_list
171+ ) {
172+ const parentBlock = {
173+ ...block ,
174+ block_type : BlockType . quote_container ,
175+ quote_container : { } ,
176+ children : [ ]
177+ } as QuoteContainerBlock ;
178+
179+ yield parentBlock ;
180+
181+ const { wiki_token } =
182+ 'wiki_catalog' in block
183+ ? ( block as WikiCatalog ) . wiki_catalog
184+ : ( block as SubPageList ) . sub_page_list ;
185+
186+ for ( const { title, node_token } of await this . #getWikiSubDocuments( wiki_token ) ) {
187+ const text_run = {
188+ content : title ,
189+ text_element_style : {
190+ link : { url : `https://${ this . domain } /wiki/${ node_token } ` }
191+ }
192+ } as TextRun ;
193+
194+ const block_id = uniqueID ( ) ;
195+
196+ yield {
197+ parent_id : parentBlock . block_id ,
198+ block_id,
199+ block_type : BlockType . ordered ,
200+ ordered : { elements : [ { text_run } ] }
201+ } as OrderedBlock ;
202+
203+ parentBlock . children ! . push ( block_id ) ;
204+ }
205+ continue ;
206+ } else if ( block . block_type === BlockType . task ) {
207+ const { task_id } = ( block as TaskBlock ) . task ;
208+
209+ const { url, summary, members, due, status } = await taskStore . getOne ( task_id ) ;
210+ const content = [
211+ summary ,
212+ members . map ( ( { name } ) => `@${ name } ` ) . join ( ' ' ) ,
213+ due && formatDate ( due . timestamp )
214+ ]
215+ . filter ( Boolean )
216+ . join ( ' ' ) ;
217+ const newBlock = this . #createLinkBlock( block , url , content ) ;
218+
219+ newBlock . text . style = { done : status === 'done' } ;
220+
221+ yield newBlock ;
222+ continue ;
138223 }
139224 yield block ;
140225 }
0 commit comments