'use strict' ; import * as vscode from 'vscode' ; import tinify from 'tinify' ; import * as path from 'path' ; import * as fs from 'fs' ; import { spawn } from 'child_process' ; import * as Constant from './const' ; import { createCdnUploader } from './CdnUploader/index' ; import { guid } from './tools' ; const ipc = require ( 'node-ipc' ) ; let configuration = vscode . workspace . getConfiguration ( 'markdownPasteImage' ) ; tinify . key = configuration . get ( 'tinyPngKey' ) || '' ; let cdnType = configuration . get < String > ( 'cdnType' ) || '' ; let ipcChannel ; export function activate ( context ) { console . log ( 'Congratulations, your extension "markdown-image-paste" is now active!' ) ; initPlugin ( true ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'extension.markdownPasteImage' , async ( ) => { if ( ipcChannel ) { ipcChannel . emit ( Constant . msg_getClipboardContent ) ; } } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'extension.markdownPasteImage.reInit' , async ( ) => { initPlugin ( false ) ; } ) ) ; } export function deactivate ( ) { ipc . disconnect ( Constant . childIpcId ) ; } function initPlugin ( initInActivate ) { spawn ( 'eiis' ) ; ipc . config . id = Constant . parentIpcId ; ipc . config . retry = 1500 ; ipc . config . silent = true ; ipc . connectTo ( Constant . childIpcId , ( ) => { ipcChannel = ipc . of [ Constant . childIpcId ] ; ipcChannel . on ( 'connect' , ( ) => { if ( initInActivate ) { ipcChannel . emit ( Constant . msg_getClipboardContent , '' ) ; } } ) ; ipcChannel . on ( Constant . msg_resClipboardContent , ( data ) => { let buffer = new Buffer ( data . data ) ; if ( ! data || ! data . data || ! data . data . length ) { return vscode . window . showWarningMessage ( 'Please copy a picture to clipboard for paste!' ) ; } if ( ( tinify as any ) . _key ) { tinify . fromBuffer ( buffer ) . toBuffer ( ( err , data ) => { if ( err ) { throw err ; } if ( data ) { buffer = new Buffer ( data ) ; } } ) ; console . log ( 'It\'s optimized!' ) ; } let currentFilePath = getCurrentFilePath ( ) ; console . log ( currentFilePath ) ; if ( currentFilePath . match ( /^Untitled-.*|.*\.md$/ ) ) { let uploader = createCdnUploader ( cdnType , configuration ) ; console . log ( uploader ) ; if ( uploader ) { uploader . upload ( buffer ) . then ( url => { insertImageToMd ( url ) ; } ) . catch ( e => { vscode . window . showInformationMessage ( 'upload to cdn fail' ) ; } ) ; } else { copyAssetToCurrentFolder ( buffer , currentFilePath ) ; } } else { copyAssetToCurrentFolder ( buffer , currentFilePath ) ; } } ) ; } ) ; } function insertImageToMd ( url ) { let editor = vscode . window . activeTextEditor ; if ( editor ) { editor . edit ( ( edit ) => { let current = ( editor as vscode . TextEditor ) . selection ; if ( current . isEmpty ) { edit . insert ( current . start , `![](${ url })` ) ; } else { edit . replace ( current , `![](${ url })` ) ; } } ) ; } } function getCurrentFilePath ( ) { let currentFilePath = ( vscode . window . activeTextEditor && vscode . window . activeTextEditor . document . fileName ) || '' ; return currentFilePath ; } function copyAssetToCurrentFolder ( buffer , currentFilePath , fileExtension = 'png' ) { if ( ! currentFilePath || ( currentFilePath && currentFilePath . match ( /^Untitled-.*/ ) ) ) { vscode . window . showWarningMessage ( 'The traget file is temporary, please save it to disk so that picture in clipboard can be copied to the folder of the file when cdn can not work!' ) ; return '' ; } let currentFolder = path . dirname ( currentFilePath ) ; vscode . window . showInformationMessage ( 'save to current folder of the editing file' ) ; fs . writeFile ( `${ currentFolder }${ path . sep }clipbord-${ guid ( ) . slice ( 0 , 5 ) }.${ fileExtension }` , buffer , function ( err ) { if ( err ) { throw err ; } console . log ( 'It\'s saved!' ) ; } ) ; }