import { addTags , getCommentIndex , getNotebookById , getNoteById , undoGroupingWithRefresh } from "utils/note" ; import { delayBreak } from "utils/common" ; import { newColorStyle , newTag , newTitleText } from "./newExcerptGenerater" ; import { MbBookNote } from "types/MarginNote" ; import { HasTitleThen } from "addons/ohmymn" ; let note ; let nodeNote ; let isOCR ; let isComment ; export default async ( _note , lastExcerptText ? ) => { console . log ( "正在处理摘录" , "excerpt" ) ; note = _note ; isOCR = false ; nodeNote = note . groupNoteId ? getNoteById ( note . groupNoteId ) : note ; isComment = nodeNote != note ; if ( self . profile . ohmymn . lockExcerpt && lastExcerptText !== undefined && lastExcerptText != "😎" ) { console . log ( "检测到开启锁定摘录选项，还原摘录" , "excerpt" ) ; processExcerpt ( lastExcerptText ! ) ; return ; } decorateExecrpt ( ) ; if ( note . excerptPic ) { const autoOCR = getNotebookById ( note . notebookId ! ) ?. options ?. autoOCRMode ?? false ; console . log ( "摘录是图片" , "excerpt" ) ; if ( autoOCR ) { const success = await delayBreak ( 20 , 0.1 , ( ) => note . excerptText ? true : false ) ; if ( success ) { console . log ( "OCR 成功" , "excerpt" ) ; isOCR = true ; } else { console . log ( "OCR 失败，没有文字" , "excerpt" ) ; return ; } } else { console . log ( "没有开启自动 OCR 选项，不处理图片" , "excerpt" ) ; return ; } } if ( self . docProfile . ohmymn . autoCorrect ) { console . log ( "开始矫正" , "excerpt" ) ; const originText = note . excerptText ! ; note . excerptText = originText + "??????????" ; const success = await delayBreak ( 20 , 0.1 , ( ) => note . excerptText != originText + "??????????" ) ; if ( success ) console . log ( "矫正成功" , "excerpt" ) ; else { console . log ( "矫正失败" , "excerpt" ) ; note . excerptText = originText ; } } excerptHandler ( ) ; } ; const excerptHandler = async ( ) => { const excerptText = note . excerptText ?. trim ( ) ; if ( ! excerptText ) return ; let { title , text } = await newTitleText ( excerptText , nodeNote ) ; const tags = newTag ( excerptText ) ; if ( isComment && title ) { console . log ( "当前摘录作为评论" , "excerpt" ) ; switch ( self . profile . ohmymn . hasTitleThen [ 0 ] ) { case HasTitleThen . TitleLink : const nodeTitle = nodeNote . noteTitle ; if ( nodeTitle && ! nodeTitle . includes ( title ) ) title = nodeTitle + "; " + title ; break ; case HasTitleThen . ExpertText : text = title ; title = undefined ; break ; } } console . log ( title ? `当前标题是：${ title }` : "没有标题" , "excerpt" ) ; console . log ( text ? `当前摘录内容是：${ text }` : "摘录转为了标题" , "excerpt" ) ; processExcerpt ( text , title , tags ) ; } ; const processExcerpt = ( text , title ? , tags ? ) => { undoGroupingWithRefresh ( ( ) => { if ( text ) note . excerptText = text ; else { if ( isComment ) { const index = getCommentIndex ( nodeNote , note ) ; if ( index != - 1 ) nodeNote . removeCommentByIndex ( index ) ; if ( isOCR && nodeNote . excerptText == nodeNote . noteTitle ) nodeNote . excerptText = title ; } else if ( isOCR ) note . excerptText = title ; else note . excerptText = "" ; } if ( title ) nodeNote . noteTitle = title ; if ( tags ?. length ) addTags ( nodeNote , tags ) ; } ) ; } ; const decorateExecrpt = ( ) => { const res = newColorStyle ( note ) ; if ( ! res ) return ; const { color , style } = res ; if ( color === undefined && style == undefined ) return ; undoGroupingWithRefresh ( ( ) => { if ( color !== undefined ) note . colorIndex = color !== - 1 ? color : 12 ; if ( style !== undefined ) note . fillIndex = style ; } ) ; } ;