import sketch from 'sketch/dom' ; import ui from 'sketch/ui' ; import BrowserWindow from 'sketch-module-web-view' ; import { getWebview } from 'sketch-module-web-view/remote' ; import getStore from '../resources/store' ; import * as pluginExport from '../resources/export' ; const appWindowIdentifier = 'next.appWindow' ; const loadingWindowIdentifier = 'next.loadingWindow' ; export default ( context ) => { const existingAppWindow = getWebview ( appWindowIdentifier ) ; const existingLoadingWindow = getWebview ( loadingWindowIdentifier ) ; if ( existingAppWindow ) { existingAppWindow . close ( ) ; } else if ( existingLoadingWindow ) { existingLoadingWindow . close ( ) ; } const document = sketch . getSelectedDocument ( ) ; const page = document . selectedPage ; const selectedLayers = document . selectedLayers ; const selectedArtboard = selectedLayers . layers . find ( ( layer ) => { return layer . type === 'Artboard' && layer . selected ; } ) ; if ( selectedArtboard ) { let store = null ; let baseStyleContent ; const theme = ui . getTheme ( ) ; const loadingWindow = new BrowserWindow ( { identifier : loadingWindowIdentifier , parent : document , modal : true , show : false } ) ; const appWindow = new BrowserWindow ( { identifier : appWindowIdentifier , width : 1200 , height : 900 , fullscreenable : false , show : false } ) ; loadingWindow . loadURL ( require ( `../resources/ui/loading.html` ) ) ; const loadingWebContents = loadingWindow . webContents ; loadingWebContents . on ( 'did-finish-load' , ( ) => { loadingWebContents . executeJavaScript ( `setLoadingColor('${ theme }')` ) . then ( ( ) => { loadingWindow . show ( ) ; appWindow . loadURL ( require ( '../resources/ui/index.html' ) ) ; } ) ; } ) ; loadingWebContents . on ( 'escape' , ( ) => { if ( ! appWebContents . isLoading ( ) ) { loadingWindow . close ( ) ; } } ) ; const appWebContents = appWindow . webContents ; appWebContents . on ( 'did-finish-load' , ( ) => { getStore ( page , selectedArtboard , sketch , ( appStore ) => { loadingWebContents . executeJavaScript ( `setLoadingText('Rendering', 'Building Spec')` ) . then ( ( ) => { store = appStore ; appWebContents . executeJavaScript ( `renderApp(
            ${ JSON . stringify ( appStore ) },
            ${ JSON . stringify ( theme ) }
          )` ) . then ( ( ) => { let stylesPath = require ( '../resources/ui/style.css' ) . replace ( 'file://' , '' ) ; let styleName = stylesPath . substr ( - 36 ) ; baseStyleContent = pluginExport . getFileContent ( stylesPath ) ; pluginExport . writeFile ( { content : pluginExport . embedTempFonts ( appStore . fonts as any , `${ baseStyleContent }` ) , path : stylesPath , fileName : styleName } ) ; loadingWindow . close ( ) ; appWindow . maximize ( ) ; appWindow . center ( ) ; appWindow . show ( ) ; } ) ; } ) ; } ) ; } ) ; appWebContents . on ( 'did-fail-load' , ( ) => { loadingWindow . close ( ) ; appWindow . close ( ) ; ui . alert ( 'Error' , 'Spec failed to load.' ) ; } ) ; appWebContents . on ( 'save' , ( params ) => { let saveStore = store as next . Store ; let saveParams = JSON . parse ( params ) ; saveStore . notes = saveParams . notes ; let finalStore = pluginExport . getFinalStore ( saveStore ) ; let savePath = pluginExport . getSavePath ( context ) ; let pluginRoot = pluginExport . getRoot ( context ) ; let scriptPath = `${ pluginRoot }/Contents/Resources/resources_ui_spec.js` ; let scriptSourceMapPath = `${ pluginRoot }/Contents/Resources/resources_ui_spec.js.map` ; let stylesPath = require ( '../resources/ui/style.css' ) . replace ( 'file://' , '' ) ; let templatePath = require ( '../resources/ui/spec.html' ) . replace ( 'file://' , '' ) ; let styleName = stylesPath . substr ( - 36 ) ; let template = pluginExport . getFileContent ( templatePath ) ; let script = pluginExport . getFileContent ( scriptPath ) ; let scriptWithGlobals = `var SRM_APP_THEME = '${ saveParams . theme }'; var SRM_APP_STORE = ${ finalStore }; ${ script }` ; let scriptSourceMap = pluginExport . getFileContent ( scriptSourceMapPath ) ; pluginExport . writeFile ( { content : template , path : savePath , fileName : 'spec.html' } ) ; pluginExport . writeFile ( { content : pluginExport . embedFonts ( store ?. fonts as any , `${ baseStyleContent }` ) , path : savePath , fileName : styleName } ) ; pluginExport . writeFile ( { content : scriptWithGlobals , path : savePath , fileName : 'resources_ui_spec.js' } ) ; pluginExport . writeFile ( { content : scriptSourceMap , path : savePath , fileName : 'resources_ui_spec.js.map' } ) ; if ( saveStore . images . length > 0 ) { pluginExport . moveImages ( saveStore . images , savePath ) ; } if ( saveStore . svgs . length > 0 ) { pluginExport . moveSVGs ( saveStore . svgs , savePath ) ; } if ( Object . keys ( saveStore . fonts ) . length > 0 ) { pluginExport . moveFonts ( saveStore . fonts as any , savePath ) ; } } ) ; } else { ui . alert ( 'Invalid Selection' , 'Select an artboard to export.' ) ; } } ; export const onShutdown = ( ) => { const existingAppWindow = getWebview ( appWindowIdentifier ) ; const existingLoadingWindow = getWebview ( loadingWindowIdentifier ) ; if ( existingAppWindow ) { existingAppWindow . close ( ) ; } else if ( existingLoadingWindow ) { existingLoadingWindow . close ( ) ; } } ;