@@ -31,7 +31,10 @@ 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" ) ;
3435 const ProDialogs = require ( "services/pro-dialogs" ) ;
36+ const ImageFolderDialogTemplate = require ( "text!htmlContent/image-folder-dialog.html" ) ;
37+
3538
3639 const KernalModeTrust = window . KernalModeTrust ;
3740 if ( ! KernalModeTrust ) {
@@ -757,52 +760,99 @@ define(function (require, exports, module) {
757760 }
758761
759762 /**
760- * This function is called when 'use this image' button is clicked in the image ribbon gallery
761- * or user loads an image file from the computer
762- * this is responsible to download the image in the appropriate place
763- * and also change the src attribute of the element (by calling appropriate helper functions)
764- * @param {Object } message - the message object which stores all the required data for this operation
763+ * Downloads image to the specified folder
764+ * @private
765+ * @param {Object } message - The message containing image download info
766+ * @param {string } folderPath - Relative path to the folder
765767 */
766- function _handleUseThisImage ( message ) {
768+ function _downloadToFolder ( message , folderPath ) {
769+ const projectRoot = ProjectManager . getProjectRoot ( ) ;
770+ if ( ! projectRoot ) {
771+ console . error ( 'No project root found' ) ;
772+ return ;
773+ }
774+
767775 const filename = message . filename ;
768776 const extnName = message . extnName || "jpg" ;
769777
770- const projectRoot = ProjectManager . getProjectRoot ( ) ;
771- if ( ! projectRoot ) { return ; }
778+ // the folder path should always end with /
779+ if ( ! folderPath . endsWith ( '/' ) ) {
780+ folderPath += '/' ;
781+ }
772782
773- // phoenix-assets folder, all the images will be stored inside this
774- const phoenixAssetsPath = projectRoot . fullPath + "phoenix-code-assets/" ;
775- const phoenixAssetsDir = FileSystem . getDirectoryForPath ( phoenixAssetsPath ) ;
783+ const targetPath = projectRoot . fullPath + folderPath ;
784+ const targetDir = FileSystem . getDirectoryForPath ( targetPath ) ;
776785
777- // check if the phoenix-assets dir exists
778- // if present, download the image inside it, if not create the dir and then download the image inside it
779- phoenixAssetsDir . exists ( ( err , exists ) => {
786+ // the directory name that user wrote, first check if it exists or not
787+ // if it doesn't exist we create it and then download the image inside it
788+ targetDir . exists ( ( err , exists ) => {
780789 if ( err ) { return ; }
781790
782791 if ( ! exists ) {
783- phoenixAssetsDir . create ( ( err ) => {
784- if ( err ) {
785- console . error ( 'Error creating phoenix-code-assets directory:' , err ) ;
786- return ;
787- }
788- _downloadImageToPhoenixAssets ( message , filename , extnName , phoenixAssetsDir ) ;
792+ targetDir . create ( ( err ) => {
793+ if ( err ) { return ; }
794+ _downloadImageToDirectory ( message , filename , extnName , targetDir ) ;
789795 } ) ;
790796 } else {
791- _downloadImageToPhoenixAssets ( message , filename , extnName , phoenixAssetsDir ) ;
797+ _downloadImageToDirectory ( message , filename , extnName , targetDir ) ;
792798 }
793799 } ) ;
794800 }
795801
796802 /**
797- * Helper function to download image to phoenix-assets folder
803+ * This function is called when 'use this image' button is clicked in the image ribbon gallery
804+ * or user loads an image file from the computer
805+ * this is responsible to download the image in the appropriate place
806+ * and also change the src attribute of the element (by calling appropriate helper functions)
807+ *
808+ * @param {Object } message - the message object which stores all the required data for this operation
809+ */
810+ function _handleUseThisImage ( message ) {
811+ // show the dialog with a text box to select a folder
812+ // dialog html is written in 'image-folder-dialog.html'
813+ const dialog = Dialogs . showModalDialogUsingTemplate ( ImageFolderDialogTemplate , false ) ;
814+ const $dlg = dialog . getElement ( ) ;
815+ const $input = $dlg . find ( "#folder-path-input" ) ;
816+
817+ // focus the input box
818+ setTimeout ( function ( ) {
819+ $input . focus ( ) ;
820+ } , 100 ) ;
821+
822+ // handle dialog button clicks
823+ $dlg . one ( "buttonClick" , function ( e , buttonId ) {
824+ if ( buttonId === Dialogs . DIALOG_BTN_OK ) {
825+ const folderPath = $input . val ( ) . trim ( ) ;
826+ dialog . close ( ) ;
827+ // if folder path is specified we download in that folder
828+ // else we download in the project root
829+ if ( folderPath ) {
830+ _downloadToFolder ( message , folderPath ) ;
831+ } else {
832+ _downloadToFolder ( message , '' ) ;
833+ }
834+ } else if ( buttonId === Dialogs . DIALOG_BTN_CANCEL ) {
835+ // if cancel is clicked, we abort the download
836+ dialog . close ( ) ;
837+ }
838+ } ) ;
839+ }
840+
841+ /**
842+ * Helper function to download image to the specified directory
843+ *
844+ * @param {Object } message - Message containing image download info
845+ * @param {string } filename - Name of the image file
846+ * @param {string } extnName - File extension (e.g., "jpg")
847+ * @param {Directory } targetDir - Target directory to save the image
798848 */
799- function _downloadImageToPhoenixAssets ( message , filename , extnName , phoenixAssetsDir ) {
800- getUniqueFilename ( phoenixAssetsDir . fullPath , filename , extnName ) . then ( ( uniqueFilename ) => {
849+ function _downloadImageToDirectory ( message , filename , extnName , targetDir ) {
850+ getUniqueFilename ( targetDir . fullPath , filename , extnName ) . then ( ( uniqueFilename ) => {
801851 // check if the image is loaded from computer or from remote
802852 if ( message . isLocalFile && message . imageData ) {
803- _handleUseThisImageLocalFiles ( message , uniqueFilename , phoenixAssetsDir ) ;
853+ _handleUseThisImageLocalFiles ( message , uniqueFilename , targetDir ) ;
804854 } else {
805- _handleUseThisImageRemote ( message , uniqueFilename , phoenixAssetsDir ) ;
855+ _handleUseThisImageRemote ( message , uniqueFilename , targetDir ) ;
806856 }
807857 } ) . catch ( error => {
808858 console . error ( 'Something went wrong when trying to use this image' , error ) ;
0 commit comments