@@ -31,6 +31,8 @@ define(function (require, exports, module) {
3131 const ProjectManager = require ( "project/ProjectManager" ) ;
3232 const FileSystem = require ( "filesystem/FileSystem" ) ;
3333 const PathUtils = require ( "thirdparty/path-utils/path-utils" ) ;
34+ const Dialogs = require ( "widgets/Dialogs" ) ;
35+ const ImageFolderDialogTemplate = require ( "text!htmlContent/image-folder-dialog.html" ) ;
3436
3537 /**
3638 * This function syncs text content changes between the original source code
@@ -742,52 +744,99 @@ define(function (require, exports, module) {
742744 }
743745
744746 /**
745- * This function is called when 'use this image' button is clicked in the image ribbon gallery
746- * or user loads an image file from the computer
747- * this is responsible to download the image in the appropriate place
748- * and also change the src attribute of the element (by calling appropriate helper functions)
749- * @param {Object } message - the message object which stores all the required data for this operation
747+ * Downloads image to the specified folder
748+ * @private
749+ * @param {Object } message - The message containing image download info
750+ * @param {string } folderPath - Relative path to the folder
750751 */
751- function _handleUseThisImage ( message ) {
752+ function _downloadToFolder ( message , folderPath ) {
753+ const projectRoot = ProjectManager . getProjectRoot ( ) ;
754+ if ( ! projectRoot ) {
755+ console . error ( 'No project root found' ) ;
756+ return ;
757+ }
758+
752759 const filename = message . filename ;
753760 const extnName = message . extnName || "jpg" ;
754761
755- const projectRoot = ProjectManager . getProjectRoot ( ) ;
756- if ( ! projectRoot ) { return ; }
762+ // the folder path should always end with /
763+ if ( ! folderPath . endsWith ( '/' ) ) {
764+ folderPath += '/' ;
765+ }
757766
758- // phoenix-assets folder, all the images will be stored inside this
759- const phoenixAssetsPath = projectRoot . fullPath + "phoenix-code-assets/" ;
760- const phoenixAssetsDir = FileSystem . getDirectoryForPath ( phoenixAssetsPath ) ;
767+ const targetPath = projectRoot . fullPath + folderPath ;
768+ const targetDir = FileSystem . getDirectoryForPath ( targetPath ) ;
761769
762- // check if the phoenix-assets dir exists
763- // if present, download the image inside it, if not create the dir and then download the image inside it
764- phoenixAssetsDir . exists ( ( err , exists ) => {
770+ // the directory name that user wrote, first check if it exists or not
771+ // if it doesn't exist we create it and then download the image inside it
772+ targetDir . exists ( ( err , exists ) => {
765773 if ( err ) { return ; }
766774
767775 if ( ! exists ) {
768- phoenixAssetsDir . create ( ( err ) => {
769- if ( err ) {
770- console . error ( 'Error creating phoenix-code-assets directory:' , err ) ;
771- return ;
772- }
773- _downloadImageToPhoenixAssets ( message , filename , extnName , phoenixAssetsDir ) ;
776+ targetDir . create ( ( err ) => {
777+ if ( err ) { return ; }
778+ _downloadImageToDirectory ( message , filename , extnName , targetDir ) ;
774779 } ) ;
775780 } else {
776- _downloadImageToPhoenixAssets ( message , filename , extnName , phoenixAssetsDir ) ;
781+ _downloadImageToDirectory ( message , filename , extnName , targetDir ) ;
782+ }
783+ } ) ;
784+ }
785+
786+ /**
787+ * This function is called when 'use this image' button is clicked in the image ribbon gallery
788+ * or user loads an image file from the computer
789+ * this is responsible to download the image in the appropriate place
790+ * and also change the src attribute of the element (by calling appropriate helper functions)
791+ *
792+ * @param {Object } message - the message object which stores all the required data for this operation
793+ */
794+ function _handleUseThisImage ( message ) {
795+ // show the dialog with a text box to select a folder
796+ // dialog html is written in 'image-folder-dialog.html'
797+ const dialog = Dialogs . showModalDialogUsingTemplate ( ImageFolderDialogTemplate , false ) ;
798+ const $dlg = dialog . getElement ( ) ;
799+ const $input = $dlg . find ( "#folder-path-input" ) ;
800+
801+ // focus the input box
802+ setTimeout ( function ( ) {
803+ $input . focus ( ) ;
804+ } , 100 ) ;
805+
806+ // handle dialog button clicks
807+ $dlg . one ( "buttonClick" , function ( e , buttonId ) {
808+ if ( buttonId === Dialogs . DIALOG_BTN_OK ) {
809+ const folderPath = $input . val ( ) . trim ( ) ;
810+ dialog . close ( ) ;
811+ // if folder path is specified we download in that folder
812+ // else we download in the project root
813+ if ( folderPath ) {
814+ _downloadToFolder ( message , folderPath ) ;
815+ } else {
816+ _downloadToFolder ( message , '' ) ;
817+ }
818+ } else if ( buttonId === Dialogs . DIALOG_BTN_CANCEL ) {
819+ // if cancel is clicked, we abort the download
820+ dialog . close ( ) ;
777821 }
778822 } ) ;
779823 }
780824
781825 /**
782- * Helper function to download image to phoenix-assets folder
826+ * Helper function to download image to the specified directory
827+ *
828+ * @param {Object } message - Message containing image download info
829+ * @param {string } filename - Name of the image file
830+ * @param {string } extnName - File extension (e.g., "jpg")
831+ * @param {Directory } targetDir - Target directory to save the image
783832 */
784- function _downloadImageToPhoenixAssets ( message , filename , extnName , phoenixAssetsDir ) {
785- getUniqueFilename ( phoenixAssetsDir . fullPath , filename , extnName ) . then ( ( uniqueFilename ) => {
833+ function _downloadImageToDirectory ( message , filename , extnName , targetDir ) {
834+ getUniqueFilename ( targetDir . fullPath , filename , extnName ) . then ( ( uniqueFilename ) => {
786835 // check if the image is loaded from computer or from remote
787836 if ( message . isLocalFile && message . imageData ) {
788- _handleUseThisImageLocalFiles ( message , uniqueFilename , phoenixAssetsDir ) ;
837+ _handleUseThisImageLocalFiles ( message , uniqueFilename , targetDir ) ;
789838 } else {
790- _handleUseThisImageRemote ( message , uniqueFilename , phoenixAssetsDir ) ;
839+ _handleUseThisImageRemote ( message , uniqueFilename , targetDir ) ;
791840 }
792841 } ) . catch ( error => {
793842 console . error ( 'Something went wrong when trying to use this image' , error ) ;
0 commit comments