src/document/DocumentCommandHandlers.js
function doSave(docToSave, force) {
        var result = new $.Deferred(),
            file = docToSave.file;

        function handleError(error) {
            _showSaveFileError(error, file.fullPath)
                .done(function () {
                    result.reject(error);
                });
        }

        function handleContentsModified() {
            Dialogs.showModalDialog(
                DefaultDialogs.DIALOG_ID_ERROR,
                Strings.EXT_MODIFIED_TITLE,
                StringUtils.format(
                    Strings.EXT_MODIFIED_WARNING,
                    StringUtils.breakableUrl(docToSave.file.fullPath)
                ),
                [
                    {
                        className : Dialogs.DIALOG_BTN_CLASS_LEFT,
                        id        : Dialogs.DIALOG_BTN_SAVE_AS,
                        text      : Strings.SAVE_AS
                    },
                    {
                        className : Dialogs.DIALOG_BTN_CLASS_NORMAL,
                        id        : Dialogs.DIALOG_BTN_CANCEL,
                        text      : Strings.CANCEL
                    },
                    {
                        className : Dialogs.DIALOG_BTN_CLASS_PRIMARY,
                        id        : Dialogs.DIALOG_BTN_OK,
                        text      : Strings.SAVE_AND_OVERWRITE
                    }
                ]
            )
                .done(function (id) {
                    if (id === Dialogs.DIALOG_BTN_CANCEL) {
                        result.reject();
                    } else if (id === Dialogs.DIALOG_BTN_OK) {
                        // Re-do the save, ignoring any CONTENTS_MODIFIED errors
                        doSave(docToSave, true).then(result.resolve, result.reject);
                    } else if (id === Dialogs.DIALOG_BTN_SAVE_AS) {
                        // Let the user choose a different path at which to write the file
                        handleFileSaveAs({doc: docToSave}).then(result.resolve, result.reject);
                    }
                });
        }

        function trySave() {
            // We don't want normalized line endings, so it's important to pass true to getText()
            FileUtils.writeText(file, docToSave.getText(true), force)
                .done(function () {
                    docToSave.notifySaved();
                    result.resolve(file);
                    HealthLogger.fileSaved(docToSave);
                })
                .fail(function (err) {
                    if (err === FileSystemError.CONTENTS_MODIFIED) {
                        handleContentsModified();
                    } else {
                        handleError(err);
                    }
                });
        }

        if (docToSave.isDirty) {
            if (docToSave.keepChangesTime) {
                // The user has decided to keep conflicting changes in the editor. Check to make sure
                // the file hasn't changed since they last decided to do that.
                docToSave.file.stat(function (err, stat) {
                    // If the file has been deleted on disk, the stat will return an error, but that's fine since
                    // that means there's no file to overwrite anyway, so the save will succeed without us having
                    // to set force = true.
                    if (!err && docToSave.keepChangesTime === stat.mtime.getTime()) {
                        // OK, it's safe to overwrite the file even though we never reloaded the latest version,
                        // since the user already said s/he wanted to ignore the disk version.
                        force = true;
                    }
                    trySave();
                });
            } else {
                trySave();
            }
        } else {
            result.resolve(file);
        }
        result.always(function () {
            MainViewManager.focusActivePane();
        });
        return result.promise();
    }
function doSave(docToSave, force) {
        var result = new $.Deferred(),
            file = docToSave.file;

        function handleError(error) {
            _showSaveFileError(error, file.fullPath)
                .done(function () {
                    result.reject(error);
                });
        }

        function handleContentsModified() {
            Dialogs.showModalDialog(
                DefaultDialogs.DIALOG_ID_ERROR,
                Strings.EXT_MODIFIED_TITLE,
                StringUtils.format(
                    Strings.EXT_MODIFIED_WARNING,
                    StringUtils.breakableUrl(docToSave.file.fullPath)
                ),
                [
                    {
                        className : Dialogs.DIALOG_BTN_CLASS_LEFT,
                        id        : Dialogs.DIALOG_BTN_SAVE_AS,
                        text      : Strings.SAVE_AS
                    },
                    {
                        className : Dialogs.DIALOG_BTN_CLASS_NORMAL,
                        id        : Dialogs.DIALOG_BTN_CANCEL,
                        text      : Strings.CANCEL
                    },
                    {
                        className : Dialogs.DIALOG_BTN_CLASS_PRIMARY,
                        id        : Dialogs.DIALOG_BTN_OK,
                        text      : Strings.SAVE_AND_OVERWRITE
                    }
                ]
            )
                .done(function (id) {
                    if (id === Dialogs.DIALOG_BTN_CANCEL) {
                        result.reject();
                    } else if (id === Dialogs.DIALOG_BTN_OK) {
                        // Re-do the save, ignoring any CONTENTS_MODIFIED errors
                        doSave(docToSave, true).then(result.resolve, result.reject);
                    } else if (id === Dialogs.DIALOG_BTN_SAVE_AS) {
                        // Let the user choose a different path at which to write the file
                        handleFileSaveAs({doc: docToSave}).then(result.resolve, result.reject);
                    }
                });
        }

        function trySave() {
            // We don't want normalized line endings, so it's important to pass true to getText()
            FileUtils.writeText(file, docToSave.getText(true), force)
                .done(function () {
                    docToSave.notifySaved();
                    result.resolve(file);
                    HealthLogger.fileSaved(docToSave);
                })
                .fail(function (err) {
                    if (err === FileSystemError.CONTENTS_MODIFIED) {
                        handleContentsModified();
                    } else {
                        handleError(err);
                    }
                });
        }

        if (docToSave.isDirty) {
            if (docToSave.keepChangesTime) {
                // The user has decided to keep conflicting changes in the editor. Check to make sure
                // the file hasn't changed since they last decided to do that.
                docToSave.file.stat(function (err, stat) {
                    // If the file has been deleted on disk, the stat will return an error, but that's fine since
                    // that means there's no file to overwrite anyway, so the save will succeed without us having
                    // to set force = true.
                    if (!err && docToSave.keepChangesTime === stat.mtime.getTime()) {
                        // OK, it's safe to overwrite the file even though we never reloaded the latest version,
                        // since the user already said s/he wanted to ignore the disk version.
                        force = true;
                    }
                    trySave();
                });
            } else {
                trySave();
            }
        } else {
            result.resolve(file);
        }
        result.always(function () {
            MainViewManager.focusActivePane();
        });
        return result.promise();
    }
function doSave ( docToSave , force ) { var result = new $ . Deferred ( ) , file = docToSave . file ; function handleError ( error ) { _showSaveFileError ( error , file . fullPath ) . done ( function ( ) { result . reject ( error ) ; } ) ; } function handleContentsModified ( ) { Dialogs . showModalDialog ( DefaultDialogs . DIALOG_ID_ERROR , Strings . EXT_MODIFIED_TITLE , StringUtils . format ( Strings . EXT_MODIFIED_WARNING , StringUtils . breakableUrl ( docToSave . file . fullPath ) ) , [ { className : Dialogs . DIALOG_BTN_CLASS_LEFT , id : Dialogs . DIALOG_BTN_SAVE_AS , text : Strings . SAVE_AS } , { className : Dialogs . DIALOG_BTN_CLASS_NORMAL , id : Dialogs . DIALOG_BTN_CANCEL , text : Strings . CANCEL } , { className : Dialogs . DIALOG_BTN_CLASS_PRIMARY , id : Dialogs . DIALOG_BTN_OK , text : Strings . SAVE_AND_OVERWRITE } ] ) . done ( function ( id ) { if ( id === Dialogs . DIALOG_BTN_CANCEL ) { result . reject ( ) ; } else if ( id === Dialogs . DIALOG_BTN_OK ) { // Re-do the save, ignoring any CONTENTS_MODIFIED errors doSave ( docToSave , true ) . then ( result . resolve , result . reject ) ; } else if ( id === Dialogs . DIALOG_BTN_SAVE_AS ) { // Let the user choose a different path at which to write the file handleFileSaveAs ( { doc : docToSave } ) . then ( result . resolve , result . reject ) ; } } ) ; } function trySave ( ) { // We don't want normalized line endings, so it's important to pass true to getText() FileUtils . writeText ( file , docToSave . getText ( true ) , force ) . done ( function ( ) { docToSave . notifySaved ( ) ; result . resolve ( file ) ; HealthLogger . fileSaved ( docToSave ) ; } ) . fail ( function ( err ) { if ( err === FileSystemError . CONTENTS_MODIFIED ) { handleContentsModified ( ) ; } else { handleError ( err ) ; } } ) ; } if ( docToSave . isDirty ) { if ( docToSave . keepChangesTime ) { // The user has decided to keep conflicting changes in the editor. Check to make sure // the file hasn't changed since they last decided to do that. docToSave . file . stat ( function ( err , stat ) { // If the file has been deleted on disk, the stat will return an error, but that's fine since // that means there's no file to overwrite anyway, so the save will succeed without us having // to set force = true. if ( ! err && docToSave . keepChangesTime === stat . mtime . getTime ( ) ) { // OK, it's safe to overwrite the file even though we never reloaded the latest version, // since the user already said s/he wanted to ignore the disk version. force = true ; } trySave ( ) ; } ) ; } else { trySave ( ) ; } } else { result . resolve ( file ) ; } result . always ( function ( ) { MainViewManager . focusActivePane ( ) ; } ) ; return result . promise ( ) ; }
Saves a document to its existing path. Does NOT support untitled documents.
@param {!Document} docToSave
@param {boolean=} force Ignore CONTENTS_MODIFIED errors from the FileSystem
@return {$.Promise} a promise that is resolved with the File of docToSave (to mirror
the API of _doSaveAs()). Rejected in case of IO error (after error dialog dismissed).
Saves a document to its existing path . Does NOT support untitled documents .