import { TextEditor , TextDocumentContentProvider , EventEmitter , Event , Uri , ViewColumn } from "vscode" ; import * as path from "path" ; import { DocumentContentManagerInterface } from "./manager/documentContentManagerInterface" ; import * as htmlDocumentContentManager from "./manager/htmlDocumentContentManager" ; import * as pugDocumentContentManager from "./manager/pugDocumentContentManager" ; import * as markdownDocumentContentManager from "./manager/markdownDocumentContentManager" ; import * as imageDocumentContentManager from "./manager/imageDocumentContentManager" ; import * as cssDocumentContentManager from "./manager/cssDocumentContentManager" ; import * as mermaidDocumentContentManager from "./manager/mermaidDocumentContentManager" ; import * as reStructuredTextDocumentContentManager from "./manager/reStructuredTextDocumentContentManager" ; import * as noneDocumentContentManager from "./manager/noneDocumentContentManager" ; import { VscodeHelper } from "./util/vscodeHelper" ; export class PreviewDocumentContentProvider implements TextDocumentContentProvider { static PREVIEW_SCHEME : string = "vscode-preview" ; private _onDidChange = new EventEmitter < Uri > ( ) ; private _documentContentManager : DocumentContentManagerInterface | null = null ; private static _instance : PreviewDocumentContentProvider ; private constructor ( ) { return this ; } public static getInstance ( ) { if ( ! PreviewDocumentContentProvider . _instance ) { PreviewDocumentContentProvider . _instance = new PreviewDocumentContentProvider ( ) ; } return PreviewDocumentContentProvider . _instance ; } static get previewScheme ( ) : string { return PreviewDocumentContentProvider . PREVIEW_SCHEME ; } private async refreshCurrentDocumentContentProvider ( editor ) { if ( ! editor || ! editor . document ) { return Promise . reject ( "editor or editor.document is undefined." ) ; } let thiz = this ; let previewType = await VscodeHelper . getActivePreviewType ( editor , false ) ; switch ( previewType ) { case "html" : thiz . _documentContentManager = new htmlDocumentContentManager . HtmlDocumentContentManager ( editor ) ; break ; case "jade" : case "pug" : thiz . _documentContentManager = new pugDocumentContentManager . PugDocumentContentManager ( editor ) ; break ; case "markdown" : thiz . _documentContentManager = new markdownDocumentContentManager . MarkdownDocumentContentManager ( editor ) ; break ; case "css" : thiz . _documentContentManager = new cssDocumentContentManager . CssDocumentContentManager ( editor ) ; break ; case "mermaid" : thiz . _documentContentManager = new mermaidDocumentContentManager . MermaidDocumentContentManager ( editor ) ; break ; case "rst" : thiz . _documentContentManager = new reStructuredTextDocumentContentManager . ReStructuredTextDocumentContentManager ( editor ) ; break ; case "image" : thiz . _documentContentManager = new imageDocumentContentManager . ImageDocumentContentManager ( editor ) ; break ; default : if ( ! thiz . _documentContentManager ) { thiz . _documentContentManager = new noneDocumentContentManager . NoneDocumentContentManager ( editor ) ; } break ; } return Promise . resolve ( ) ; } public provideTextDocumentContent ( _uri ) { let content = async ( ) => { if ( ! this . _documentContentManager ) { return "" ; } return this . _documentContentManager . createContentSnippet ( ) ; } ; return content ( ) ; } get onDidChange ( ) : Event < Uri > { return this . _onDidChange . event ; } public update ( ) { if ( ! this . _documentContentManager ) { return ; } let fileName = this . _documentContentManager . editor ( ) . document . fileName ; let previewUri = PreviewDocumentContentProvider . getPreviewUri ( fileName ) ; this . _onDidChange . fire ( previewUri ) ; } public async sendPreviewCommand ( displayColumn , editor ) { await this . refreshCurrentDocumentContentProvider ( editor ) ; if ( ! this . _documentContentManager ) { return ; } let fileName = this . _documentContentManager . editor ( ) . document . fileName ; let previewUri = await PreviewDocumentContentProvider . getPreviewUri ( fileName ) ; await this . _documentContentManager . sendPreviewCommand ( previewUri , displayColumn , editor ) ; return this . update ( ) ; } static getPreviewTitle ( fileName ) { return `Preview: '${ path . basename ( fileName ) }'` ; } static getPreviewUri ( fileName ) { let previewTitle = this . getPreviewTitle ( fileName ) ; return Uri . parse ( `${ PreviewDocumentContentProvider . previewScheme }://preview/${ previewTitle }` ) ; } }