import { MbBookNote , MbTopic } from "types/MarginNote" ; import { postNotification } from "./common" ; import { MN } from "const" ; import { unique } from "utils" ; /**
 * 获取选中的卡片
 */ const getSelectNodes = ( ) : MbBookNote [ ] => { const MindMapNodes = MN . studyController ( ) . notebookController . mindmapView . selViewLst ; if ( MindMapNodes ?. length ) return MindMapNodes . map ( item => item . note . note ) ; else return [ ] ; } ; /**
 * 获取选中的卡片，包括子节点
 */ const getSelectNodesAll = ( onlyChildrenNode = false ) : MbBookNote [ ] => { const nodes = getSelectNodes ( ) ; const allNodes = [ ] ; const getChildren = ( nodes , onlyChildrenNode = false ) => { nodes . forEach ( ( node ) => { if ( ! onlyChildrenNode ) allNodes . push ( node ) ; if ( node . childNotes ?. length ) getChildren ( node . childNotes ) ; } ) ; } ; getChildren ( nodes , onlyChildrenNode ) ; return allNodes ; } ; /**
 * 获取卡片中的所有摘录
 */ const excerptNotes = ( node ) : MbBookNote [ ] => { const notes = [ node ] ; const comments = node . comments ; for ( const comment of comments ) { if ( comment . type == "LinkNote" ) notes . push ( MN . db . getNoteById ( comment . noteid ) ! ) ; } return notes ; } ; const getNoteById = ( noteid ) : MbBookNote => MN . db . getNoteById ( noteid ) ! ; const getNotebookById = ( notebookid ) : MbTopic => MN . db . getNotebookById ( notebookid ) ! ; /**
 * 可撤销的动作，所有修改数据的动作都应该用这个方法包裹
 */ const undoGrouping = ( f ) => { UndoManager . sharedInstance ( ) . undoGrouping ( "" , self . notebookid , f ) ; } ; const undoGroupingWithRefresh = ( f ) => { undoGrouping ( f ) ; RefreshAfterDBChange ( ) ; } ; /**
 * 保存数据，刷新界面
 */ const RefreshAfterDBChange = ( ) => { MN . db . setNotebookSyncDirty ( self . notebookid ) ; postNotification ( "RefreshAfterDBChange" , { topicid : self . notebookid } ) ; } ; /**
 * 获取评论的索引
 */ const getCommentIndex = ( note , comment ) => { const comments = note . comments ; for ( let i = 0 ; i < comments . length ; i ++ ) { const _comment = comments [ i ] ; if ( typeof comment == "string" ) { if ( _comment . type == "TextNote" && _comment . text == comment ) return i ; } else if ( _comment . type == "LinkNote" && _comment . noteid == comment . noteId ) return i ; } return - 1 ; } ; param note param separator param highlight returns const getAllText = ( note , separator = "\n" , highlight = false ) => { const textArr = [ ] ; if ( note . excerptText ) textArr . push ( highlight ? note . excerptText : note . excerptText . replace ( /\*\*/g , "" ) ) ; note . comments . forEach ( comment => { switch ( comment . type ) { case "TextNote" : case "HtmlNote" : const text = comment . text . trim ( ) ; if ( text && ! text . includes ( "marginnote3app" ) ) textArr . push ( text ) ; break ; case "LinkNote" : if ( comment . q_htext ) textArr . push ( comment . q_htext . trim ( ) ) ; } } ) ; return textArr . join ( separator ) ; } ; param force const addTags = ( node , tags , force = false ) => { const existingTags = [ ] ; const tagCommentIndex = [ ] ; node . comments . forEach ( ( comment , index ) => { if ( comment . type == "TextNote" ) { const _tags = comment . text . split ( " " ) ; if ( _tags . every ( tag => tag . startsWith ( "#" ) ) ) { existingTags . push ( ... _tags . map ( tag => tag . slice ( 1 ) ) ) ; tagCommentIndex . push ( index ) ; } } } ) ; if ( ! force && ( ! tags . length || tags . every ( tag => existingTags . includes ( tag ) ) ) ) return ; tagCommentIndex . reverse ( ) . forEach ( index => void node . removeCommentByIndex ( index ) ) ; const tagLine = unique ( [ ... existingTags , ... tags ] ) . map ( tag => `#${ tag }` ) . join ( " " ) ; if ( tagLine ) node . appendTextComment ( tagLine ) ; } ; export { getSelectNodes , getSelectNodesAll , excerptNotes , getCommentIndex , getNotebookById , getNoteById , getAllText , undoGrouping , undoGroupingWithRefresh , RefreshAfterDBChange , addTags } ;