import { TextHighlightRecords } from './TextHighlightRecords' ; import { IRect } from 'polar-shared/src/util/rects/IRect' ; import { TextRect } from './TextRect' ; import { TextHighlight } from './TextHighlight' ; import { Image } from './Image' ; import { notNull } from 'polar-shared/src/Preconditions' ; import { DocMetas } from './DocMetas' ; import { IPageMeta } from "polar-shared/src/metadata/IPageMeta" ; import { IDocMeta } from "polar-shared/src/metadata/IDocMeta" ; import { ITextHighlight } from "polar-shared/src/metadata/ITextHighlight" ; import { ITextHighlights } from "polar-shared/src/metadata/ITextHighlights" ; import { HTMLStr , IDStr } from "polar-shared/src/util/Strings" ; import { Hashcodes } from "polar-shared/src/util/Hashcodes" ; import { ISODateTimeStrings } from "polar-shared/src/metadata/ISODateTimeStrings" ; import { Texts } from "polar-shared/src/metadata/Texts" ; import { TextType } from "polar-shared/src/metadata/TextType" ; export class TextHighlights { public static update ( id , docMeta , pageMeta , updates ) { const existing = pageMeta . textHighlights [ id ] ! ; if ( ! existing ) { throw new Error ( "No existing for id: " + id ) ; } const updated = new TextHighlight ( { ... existing , ... updates } ) ; DocMetas . withBatchedMutations ( docMeta , ( ) => { pageMeta . textHighlights [ id ] = updated ; } ) ; return updated ; } public static resetRevisedText ( docMeta , pageMeta , id ) { this . setRevisedText ( docMeta , pageMeta , id , undefined ) ; } public static setRevisedText ( docMeta , pageMeta , id , html ) { pageMeta = DocMetas . getPageMeta ( docMeta , pageMeta . pageInfo . num ) ; const textHighlight = pageMeta . textHighlights [ id ] ; if ( textHighlight ) { DocMetas . withBatchedMutations ( docMeta , ( ) => { delete pageMeta . textHighlights [ id ] ; id = Hashcodes . createRandomID ( ) ; const revisedText = html ? Texts . create ( html , TextType . HTML ) : undefined ; const newTextHighlight = { ... textHighlight , id , lastUpdated : ISODateTimeStrings . create ( ) , revisedText } ; pageMeta . textHighlights [ id ] = newTextHighlight ; } ) ; } } return { * } public static createMockTextHighlight ( ) { const rects = [ { top : 100 , left : 100 , right : 200 , bottom : 200 , width : 100 , height : 100 } ] ; const textSelections = [ new TextRect ( { text : "hello world" } ) ] ; const text = "hello world" ; return TextHighlightRecords . create ( rects , textSelections , { TEXT : text } ) . value ; } public static attachImage ( textHighlight , image ) { textHighlight . images [ notNull ( image . rel ) ] = image ; } public static deleteTextHighlight ( pageMeta , textHighlight ) { if ( textHighlight . images ) { Object . values ( textHighlight . images ) . forEach ( image => { } ) ; } delete pageMeta . textHighlights [ textHighlight . id ] ; } Deprecated param textHighlight public static toHTML ( textHighlight ) { return ITextHighlights . toHTML ( textHighlight ) ; } }