import { TextEditor , Uri , ViewColumn } from "vscode" ; import { DocumentContentManagerInterface } from "./documentContentManagerInterface" ; import { HtmlPreview , SourceType } from "../util/htmlPreview" ; import { TextEditorHelper , TextEditorHelperReturnType } from "../util/textEditorHelper" ; export class ImageDocumentContentManager implements DocumentContentManagerInterface { private IMAGE_TYPE_REGREX_PREFFIX = /http[s]{0,1}:\/\/|file:\/\/|\s[.]{0,2}\// ; private IMAGE_TYPE_REGREX_SUFFIX = /png|jpg|jpeg|gif|bmp|\s/ ; private IMAGE_TYPE_REGREX_SPLIT = /\s/ ; private _editor : TextEditor ; public constructor ( editor ) { this . _editor = editor ; return this ; } public editor ( ) { return this . _editor ; } public async createContentSnippet ( ) { const editor = this . _editor ; if ( ! editor ) { return HtmlPreview . errorSnippet ( this . getWindowErrorMessage ( ) ) ; } const previewSnippet = await this . generatePreviewSnippet ( editor ) ; if ( ! previewSnippet || previewSnippet . length <= 0 ) { return HtmlPreview . errorSnippet ( this . getErrorMessage ( ) ) ; } console . info ( "previewSnippet = " + previewSnippet ) ; return previewSnippet ; } public sendPreviewCommand ( previewUri , displayColumn ) { return HtmlPreview . sendPreviewCommand ( previewUri , displayColumn ) ; } private getErrorMessage ( ) { return `Active editor doesn't show any  ${ this . IMAGE_TYPE_REGREX_SUFFIX } - no properties to preview.` ; } private getWindowErrorMessage ( ) { return `No Active editor - no properties to preview.` ; } private imageSrcSnippet ( imageUri ) { return HtmlPreview . createRemoteSource ( SourceType . IMAGE , imageUri ) ; } private indexOfSplit ( editor , startPos ) { return TextEditorHelper . regexIndexOf ( editor , startPos , this . IMAGE_TYPE_REGREX_SPLIT ) ; } private lastIndexOfPrefix ( editor , startPos ) { return TextEditorHelper . regexLastIndexOf ( editor , startPos , this . IMAGE_TYPE_REGREX_PREFFIX ) ; } private lastIndexOfSuffix ( editor , startPos ) { return TextEditorHelper . regexLastIndexOf ( editor , startPos , this . IMAGE_TYPE_REGREX_SUFFIX ) ; } private getEndOfImageUrl ( editor , startPosOfImageUrl , startPosOfSplit ) { if ( ! editor ) { return - 1 ; } const startIndexOfSuffix = this . lastIndexOfSuffix ( editor , startPosOfSplit ) ; const startPosOfSuffix = startIndexOfSuffix . pos ; const selectedSuffix = startIndexOfSuffix . mark ; if ( startPosOfSuffix < 0 ) { return startPosOfSplit ; } else { if ( startPosOfSuffix < startPosOfImageUrl ) { return - 1 ; } if ( selectedSuffix . match ( /\s+/ ) ) { return startPosOfSuffix ; } return startPosOfSuffix + selectedSuffix . length ; } } private getSplitOfImageUrl ( editor , startIndexOfImageUrl ) { if ( ! editor ) { return - 1 ; } let startPosOfSplit = this . indexOfSplit ( editor , startIndexOfImageUrl . pos + startIndexOfImageUrl . mark . length ) . pos ; if ( startPosOfSplit < 0 ) { startPosOfSplit = editor . document . getText ( ) . length ; } return startPosOfSplit ; } private getFirstSelectedImageUri ( editor ) { if ( ! editor ) { return "" ; } const startPosOfSelectionText = editor . document . offsetAt ( editor . selection . anchor ) ; const startIndexOfImageUrl = this . lastIndexOfPrefix ( editor , startPosOfSelectionText ) ; const startPosOfImageUrl = startIndexOfImageUrl . pos ; if ( startPosOfImageUrl < 0 ) { return "" ; } const startPosOfSplit = this . getSplitOfImageUrl ( editor , startIndexOfImageUrl ) ; const endNextPosOfImageUrl = this . getEndOfImageUrl ( editor , startPosOfImageUrl , startPosOfSplit ) ; if ( endNextPosOfImageUrl < 0 ) { return "" ; } const imgSrcUri = editor . document . getText ( ) . slice ( startPosOfImageUrl , endNextPosOfImageUrl ) ; return imgSrcUri . trim ( ) ; } private async generatePreviewSnippet ( editor ) { if ( ! editor ) { return HtmlPreview . errorSnippet ( this . getWindowErrorMessage ( ) ) ; } const imageUri = this . getFirstSelectedImageUri ( editor ) ; if ( ! imageUri || imageUri . length <= 0 ) { return HtmlPreview . errorSnippet ( this . getErrorMessage ( ) ) ; } const targetImageUri = imageUri ; const head = HtmlPreview . fixImageSrcLinks ( HtmlPreview . createLocalSource ( SourceType . LINK , "header_fix.css" ) ) ; const body = HtmlPreview . createRemoteSource ( SourceType . DIVISION , targetImageUri ) + HtmlPreview . createRemoteSourceAtNewline ( SourceType . HR ) + HtmlPreview . createRemoteSource ( SourceType . CUSTOM_NEWLINE ) + HtmlPreview . fixImageSrcLinks ( this . imageSrcSnippet ( targetImageUri ) ) ; return HtmlPreview . createFullHtmlSnippetFrom ( head , body ) ; } }