import JSFiddle = require ( "jsfiddle" ) ; import { QuickDiffProvider , Uri , CancellationToken , ProviderResult , WorkspaceFolder , workspace , window , env } from "vscode" ; import * as path from 'path' ; /** Represents one JSFiddle data and meta-data. */ export class Fiddle { constructor ( public slug , public version , public data ) { } } /** Represents JSFiddle HTML, JavaScript and CSS text. */ export interface FiddleData { html : string ; js : string ; css : string ; } export function areIdentical ( first , second ) { return first . html === second . html && first . css === second . css && first . js === second . js ; } export const JSFIDDLE_SCHEME = 'jsfiddle' ; export class FiddleRepository implements QuickDiffProvider { constructor ( private workspaceFolder , private fiddleSlug ) { } provideOriginalResource ? ( uri , token ) { const relativePath = workspace . asRelativePath ( uri . fsPath ) ; return Uri . parse ( `${ JSFIDDLE_SCHEME }:${ relativePath }` ) ; } /**
     * Enumerates the resources under source control.
     */ provideSourceControlledResources ( ) { return [ Uri . file ( this . createLocalResourcePath ( 'html' ) ) , Uri . file ( this . createLocalResourcePath ( 'js' ) ) , Uri . file ( this . createLocalResourcePath ( 'css' ) ) ] ; } param extension returns createLocalResourcePath ( extension ) { return path . join ( this . workspaceFolder . uri . fsPath , this . fiddleSlug + '.' + extension ) ; } } const DEMO = [ { html : '<div class="hi">Hi</div>' , css : `.hi {\n	color: red;\n}` , js : '$(".hi").fadeOut();' } ] ; let demoVersionOffset = undefined ; export async function downloadFiddle ( slug , version ) { if ( slug === "demo" ) { if ( demoVersionOffset === undefined && version === undefined ) { version = 0 ; } if ( demoVersionOffset === undefined ) { demoVersionOffset = version ; } const maxDemoVersion = DEMO . length - 1 + demoVersionOffset ; if ( version === undefined ) { version = maxDemoVersion ; } if ( version >= 0 && version <= maxDemoVersion ) { const index = Math . max ( 0 , version - demoVersionOffset ) ; const fiddleData = DEMO [ index ] ; return new Fiddle ( slug , version , fiddleData ) ; } else { throw new Error ( "Invalid demo fiddle version." ) ; } } const id = toFiddleId ( slug , version ) ; return new Promise < Fiddle > ( ( resolve , reject ) => { JSFiddle . getFiddle ( id , ( err , fiddleData ) => { if ( err ) { reject ( err ) ; } const fiddle = new Fiddle ( slug , version , fiddleData ) ; resolve ( fiddle ) ; } ) ; } ) ; } export async function uploadFiddle ( slug , version , html , js , css ) { if ( slug === "demo" ) { const fiddleData = { html : html , js : js , css : css } ; DEMO . push ( fiddleData ) ; return new Fiddle ( slug , version , fiddleData ) ; } else { const answer = await window . showQuickPick ( [ "Yes, open in the browser. I will paste the new Fiddle code, discard changes, refresh source control and checkout latest." , "No, I was just clicking around." ] , { placeHolder : "JS Fiddle saving is not supported. Do you want to open the JSFiddle in the browser?" } ) ; if ( answer && answer . toLowerCase ( ) . startsWith ( "yes" ) ) { env . openExternal ( Uri . parse ( `https://jsfiddle.net/${ slug }/` ) ) ; return undefined ; } if ( false ) { const data = { slug : slug , version : version , html : html , js : js , css : css } ; return new Promise < Fiddle > ( ( resolve , reject ) => { JSFiddle . saveFiddle ( data , ( err , fiddleData ) => { if ( err ) { reject ( err ) ; } else { const fiddle = new Fiddle ( slug , version , fiddleData ) ; resolve ( fiddle ) ; } } ) ; } ) ; } else { return undefined ; } } } export function toFiddleId ( slug , version ) { if ( version === undefined ) { return slug ; } else { return slug + '/' + version ; } } param uri export function toExtension ( uri ) { return path . extname ( uri . fsPath ) . substr ( 1 ) ; }