# HG changeset patch # User Magnus Melin # Date 1487751780 -7200 # Node ID d361db34419828cf4b29a22b4f88942cf976d665 # Parent 07e147928ee39e0e524c94cda3775891e8d388c7 Bug 1341211 - Get rid of nsIFilePicker.show() use in c-c (editor part). r=jorgk,frg diff --git a/editor/ui/composer/content/ComposerCommands.js b/editor/ui/composer/content/ComposerCommands.js --- a/editor/ui/composer/content/ComposerCommands.js +++ b/editor/ui/composer/content/ComposerCommands.js @@ -480,25 +480,26 @@ var nsOpenCommand = // Direct user to prefer HTML files and/or text files depending on whether // loading into Composer or Text editor, so we call separately to control // the order of the filter list. if (fileType == "html") fp.appendFilters(nsIFilePicker.filterHTML); fp.appendFilters(nsIFilePicker.filterText); fp.appendFilters(nsIFilePicker.filterAll); - /* doesn't handle *.shtml files */ - if (fp.show() == nsIFilePicker.returnCancel) - return; - - // editPage checks for already open window and activates it. - if (fp.fileURL.spec) { - SaveFilePickerDirectory(fp, fileType); - editPage(fp.fileURL.spec, fileType); - } + fp.open(rv => { + if (rv != nsIFilePicker.returnOK || !fp.file) { + return; + } + // editPage checks for already open window and activates it. + if (fp.fileURL.spec) { + SaveFilePickerDirectory(fp, fileType); + editPage(fp.fileURL.spec, fileType); + } + }); } }; // STRUCTURE TOOLBAR // var nsUpdateStructToolbarCommand = { isCommandEnabled: function(aCommand, dummy) @@ -529,49 +530,45 @@ var nsSaveCommand = } catch (e) {return false;} }, getCommandStateParams: function(aCommand, aParams, aRefCon) {}, doCommandParams: function(aCommand, aParams, aRefCon) {}, doCommand: function(aCommand) { - var result = false; var editor = GetCurrentEditor(); if (editor) { if (IsHTMLEditor()) SetEditMode(gPreviousNonSourceDisplayMode); - result = SaveDocument(IsUrlAboutBlank(GetDocumentUrl()), false, editor.contentsMIMEType); + SaveDocument(IsUrlAboutBlank(GetDocumentUrl()), false, editor.contentsMIMEType); } - return result; } } var nsSaveAsCommand = { isCommandEnabled: function(aCommand, dummy) { return (IsDocumentEditable()); }, getCommandStateParams: function(aCommand, aParams, aRefCon) {}, doCommandParams: function(aCommand, aParams, aRefCon) {}, doCommand: function(aCommand) { - var result = false; var editor = GetCurrentEditor(); if (editor) { if (IsHTMLEditor()) SetEditMode(gPreviousNonSourceDisplayMode); - result = SaveDocument(true, false, editor.contentsMIMEType); + SaveDocument(true, false, editor.contentsMIMEType); } - return result; } } var nsExportToTextCommand = { isCommandEnabled: function(aCommand, dummy) { return (IsDocumentEditable()); @@ -580,20 +577,18 @@ var nsExportToTextCommand = getCommandStateParams: function(aCommand, aParams, aRefCon) {}, doCommandParams: function(aCommand, aParams, aRefCon) {}, doCommand: function(aCommand) { if (GetCurrentEditor()) { SetEditMode(gPreviousNonSourceDisplayMode); - var result = SaveDocument(true, true, "text/plain"); - return result; + SaveDocument(true, true, "text/plain"); } - return false; } } var nsSaveAndChangeEncodingCommand = { isCommandEnabled: function(aCommand, dummy) { return (IsDocumentEditable()); @@ -612,26 +607,24 @@ var nsSaveAndChangeEncodingCommand = if (GetDocumentTitle() != oldTitle) UpdateWindowTitle(); if (window.ok) { if (window.exportToText) { - window.ok = SaveDocument(true, true, "text/plain"); + SaveDocument(true, true, "text/plain"); } else { var editor = GetCurrentEditor(); - window.ok = SaveDocument(true, false, editor ? editor.contentsMIMEType : null); + SaveDocument(true, false, editor ? editor.contentsMIMEType : null); } } - - return window.ok; } }; var nsPublishCommand = { isCommandEnabled: function(aCommand, dummy) { if (IsDocumentEditable()) @@ -773,17 +766,19 @@ function GetSuggestedFileName(aDocumentU // Check if there is a title we can use to generate a valid filename, // if we can't, use the default filename. var title = validateFileName(GetDocumentTitle()) || GetString("untitledDefaultFilename"); return title + extension; } -// returns file picker result +/** + * @return {Promise} dialogResult + */ function PromptForSaveLocation(aDoSaveAsText, aEditorType, aMIMEType, aDocumentURLString) { var dialogResult = {}; dialogResult.filepickerClick = nsIFilePicker.returnCancel; dialogResult.resultingURI = ""; dialogResult.resultingLocalFile = null; var fp = null; @@ -842,28 +837,32 @@ function PromptForSaveLocation(aDoSaveAs else { // Initialize to the last-used directory for the particular type (saved in prefs) SetFilePickerDirectory(fp, aEditorType); } } catch(e) {} - dialogResult.filepickerClick = fp.show(); - if (dialogResult.filepickerClick != nsIFilePicker.returnCancel) - { - // reset urlstring to new save location - dialogResult.resultingURIString = fileHandler.getURLSpecFromFile(fp.file); - dialogResult.resultingLocalFile = fp.file; - SaveFilePickerDirectory(fp, aEditorType); - } - else if ("gFilePickerDirectory" in window && gFilePickerDirectory) - fp.displayDirectory = gFilePickerDirectory; - - return dialogResult; + return new Promise(resolve => { + fp.open(rv => { + dialogResult.filepickerClick = rv; + if (rv != nsIFilePicker.returnCancel) { + // reset urlstring to new save location + dialogResult.resultingURIString = fileHandler.getURLSpecFromFile(fp.file); + dialogResult.resultingLocalFile = fp.file; + SaveFilePickerDirectory(fp, aEditorType); + resolve(dialogResult); + } + else if ("gFilePickerDirectory" in window && gFilePickerDirectory) { + fp.displayDirectory = gFilePickerDirectory; + resolve(null); + } + }); + }); } /** * If needed, prompt for document title and set the document title to the * preferred value. * @return true if the title was set up successfully; * false if the user cancelled the title prompt */ @@ -1577,20 +1576,20 @@ function IsSupportedTextMimeType(aMimeTy { if (kSupportedTextMimeTypes[i] == aMimeType) return true; } return false; } // throws an error or returns true if user attempted save; false if user canceled save -function SaveDocument(aSaveAs, aSaveCopy, aMimeType) +async function SaveDocument(aSaveAs, aSaveCopy, aMimeType) { var editor = GetCurrentEditor(); - if (!aMimeType || aMimeType == "" || !editor) + if (!aMimeType || !editor) throw Cr.NS_ERROR_NOT_INITIALIZED; var editorDoc = editor.document; if (!editorDoc) throw Cr.NS_ERROR_NOT_INITIALIZED; // if we don't have the right editor type bail (we handle text and html) var editorType = GetCurrentEditorType(); @@ -1624,18 +1623,18 @@ function SaveDocument(aSaveAs, aSaveCopy // Prompt for title if we are saving to HTML if (!saveAsTextFile && (editorType == "html")) { var userContinuing = PromptAndSetTitleIfNone(); // not cancel if (!userContinuing) return false; } - var dialogResult = PromptForSaveLocation(saveAsTextFile, editorType, aMimeType, urlstring); - if (dialogResult.filepickerClick == nsIFilePicker.returnCancel) + var dialogResult = await PromptForSaveLocation(saveAsTextFile, editorType, aMimeType, urlstring); + if (!dialogResult) return false; replacing = (dialogResult.filepickerClick == nsIFilePicker.returnReplace); urlstring = dialogResult.resultingURIString; tempLocalFile = dialogResult.resultingLocalFile; // update the new URL for the webshell unless we are saving a copy if (!aSaveCopy) @@ -2040,21 +2039,21 @@ var nsCloseCommand = doCommandParams: function(aCommand, aParams, aRefCon) {}, doCommand: function(aCommand) { CloseWindow(); } }; -function CloseWindow() +async function CloseWindow() { // Check to make sure document is saved. "true" means allow "Don't Save" button, // so user can choose to close without saving - if (CheckAndSaveDocument("cmd_close", true)) + if (await CheckAndSaveDocument("cmd_close", true)) { if (window.InsertCharWindow) SwitchInsertCharToAnotherEditorOrClose(); try { var basewin = window.QueryInterface(Ci.nsIInterfaceRequestor) .getInterface(Ci.nsIWebNavigation) .QueryInterface(Ci.nsIDocShellTreeItem) @@ -2116,21 +2115,21 @@ var nsPreviewCommand = return (IsDocumentEditable() && IsHTMLEditor() && (DocumentHasBeenSaved() || IsDocumentModified())); }, getCommandStateParams: function(aCommand, aParams, aRefCon) {}, doCommandParams: function(aCommand, aParams, aRefCon) {}, - doCommand: function(aCommand) + doCommand: async function(aCommand) { // Don't continue if user canceled during prompt for saving // DocumentHasBeenSaved will test if we have a URL and suppress "Don't Save" button if not - if (!CheckAndSaveDocument("cmd_preview", DocumentHasBeenSaved())) + if (!(await CheckAndSaveDocument("cmd_preview", DocumentHasBeenSaved()))) return; // Check if we saved again just in case? if (DocumentHasBeenSaved()) { let browser; try { // Find a browser with this URL @@ -2171,21 +2170,21 @@ var nsSendPageCommand = { return (IsDocumentEditable() && (DocumentHasBeenSaved() || IsDocumentModified())); }, getCommandStateParams: function(aCommand, aParams, aRefCon) {}, doCommandParams: function(aCommand, aParams, aRefCon) {}, - doCommand: function(aCommand) + doCommand: async function(aCommand) { // Don't continue if user canceled during prompt for saving // DocumentHasBeenSaved will test if we have a URL and suppress "Don't Save" button if not - if (!CheckAndSaveDocument("cmd_editSendPage", DocumentHasBeenSaved())) + if (!(await CheckAndSaveDocument("cmd_editSendPage", DocumentHasBeenSaved()))) return; // Check if we saved again just in case? if (DocumentHasBeenSaved()) { // Launch Messenger Composer window with current page as contents try { @@ -2361,23 +2360,23 @@ var nsValidateCommand = isCommandEnabled: function(aCommand, dummy) { return GetCurrentEditor() != null; }, getCommandStateParams: function(aCommand, aParams, aRefCon) {}, doCommandParams: function(aCommand, aParams, aRefCon) {}, - doCommand: function(aCommand) + doCommand: async function(aCommand) { // If the document hasn't been modified, // then just validate the current url. if (IsDocumentModified() || IsHTMLSourceChanged()) { - if (!CheckAndSaveDocument("cmd_validate", false)) + if (!(await CheckAndSaveDocument("cmd_validate", false))) return; // Check if we saved again just in case? if (!DocumentHasBeenSaved()) // user hit cancel? return; } URL2Validate = GetDocumentUrl(); diff --git a/editor/ui/composer/content/editingOverlay.js b/editor/ui/composer/content/editingOverlay.js --- a/editor/ui/composer/content/editingOverlay.js +++ b/editor/ui/composer/content/editingOverlay.js @@ -214,27 +214,27 @@ function EditorShutdown() "obs_documentLocationChanged"); } catch (e) { dump (e); } } // --------------------------- File menu --------------------------- // Check for changes to document and allow saving before closing // This is hooked up to the OS's window close widget (e.g., "X" for Windows) -function EditorCanClose(aCancelQuit, aTopic, aData) +async function EditorCanClose(aCancelQuit, aTopic, aData) { if (aTopic == "quit-application-requested" && aCancelQuit instanceof Ci.nsISupportsPRBool && aCancelQuit.data) return false; // Returns FALSE only if user cancels save action // "true" means allow "Don't Save" button - var canClose = CheckAndSaveDocument("cmd_close", true); + var canClose = await CheckAndSaveDocument("cmd_close", true); // This is our only hook into closing via the "X" in the caption // or "Quit" (or other paths?) // so we must shift association to another // editor or close any non-modal windows now if (canClose && "InsertCharWindow" in window && window.InsertCharWindow) SwitchInsertCharToAnotherEditorOrClose(); diff --git a/editor/ui/composer/content/editor.js b/editor/ui/composer/content/editor.js --- a/editor/ui/composer/content/editor.js +++ b/editor/ui/composer/content/editor.js @@ -499,17 +499,17 @@ function DocumentHasBeenSaved() if (!fileurl || IsUrlAboutBlank(fileurl)) return false; // We have a file URL already return true; } -function CheckAndSaveDocument(command, allowDontSave) +async function CheckAndSaveDocument(command, allowDontSave) { var document; try { // if we don't have an editor or an document, bail var editor = GetCurrentEditor(); document = editor.document; if (!document) return true; @@ -585,17 +585,17 @@ function CheckAndSaveDocument(command, a // and return as if user canceled because publishing is asynchronous // This command will be fired when publishing finishes gCommandAfterPublishing = command; goDoCommand("cmd_publish"); return false; } // Save to local disk - return SaveDocument(false, false, editor.contentsMIMEType); + return await SaveDocument(false, false, editor.contentsMIMEType); } if (result == 2) // "Don't Save" return true; // Default or result == 1 (Cancel) return false; } diff --git a/editor/ui/composer/content/editor.xul b/editor/ui/composer/content/editor.xul --- a/editor/ui/composer/content/editor.xul +++ b/editor/ui/composer/content/editor.xul @@ -27,17 +27,17 @@ %brandDTD; ]> { + if (rv != nsIFilePicker.returnOK || !fp.file) { + return; + } document.getElementById("editor.default_background_image").value = fp.fileURL.spec; - - var textbox = document.getElementById("backgroundImageInput"); - textbox.focus(); - textbox.select(); + let textbox = document.getElementById("backgroundImageInput"); + textbox.focus(); + textbox.select(); + }); } function WindowOnUnload() { Services.prefs.removeObserver("browser.display.use_system_colors", browserPrefsObserver, false); Services.prefs.removeObserver("browser.display.foreground_color", browserPrefsObserver, false); Services.prefs.removeObserver("browser.display.background_color", browserPrefsObserver, false); Services.prefs.removeObserver("browser.anchor_color", browserPrefsObserver, false); diff --git a/editor/ui/dialogs/content/EdColorProps.js b/editor/ui/dialogs/content/EdColorProps.js --- a/editor/ui/dialogs/content/EdColorProps.js +++ b/editor/ui/dialogs/content/EdColorProps.js @@ -300,30 +300,27 @@ function UseDefaultColors() SetElementEnabledById("Active", false); SetElementEnabledById("Visited", false); SetElementEnabledById("Background", false); } function chooseFile() { // Get a local image file, converted into URL format - var fileName = GetLocalFileURL("img"); - if (fileName) - { + GetLocalFileURL("img").then(fileURL => { // Always try to relativize local file URLs if (gHaveDocumentUrl) - fileName = MakeRelativeUrl(fileName); + fileURL = MakeRelativeUrl(fileURL); - gDialog.BackgroundImageInput.value = fileName; + gDialog.BackgroundImageInput.value = fileURL; SetRelativeCheckbox(); - ValidateAndPreviewImage(true); - } - SetTextboxFocus(gDialog.BackgroundImageInput); + SetTextboxFocus(gDialog.BackgroundImageInput); + }); } function ChangeBackgroundImage() { // Don't show error message for image while user is typing ValidateAndPreviewImage(false); SetRelativeCheckbox(); } diff --git a/editor/ui/dialogs/content/EdDialogCommon.js b/editor/ui/dialogs/content/EdDialogCommon.js --- a/editor/ui/dialogs/content/EdDialogCommon.js +++ b/editor/ui/dialogs/content/EdDialogCommon.js @@ -382,16 +382,19 @@ function onMoreFewer() function SwitchToValidatePanel() { // no default implementation // Only EdTableProps.js currently implements this } const nsIFilePicker = Ci.nsIFilePicker; +/** + * @return {Promise} URL spec of the file chosen, or null + */ function GetLocalFileURL(filterType) { var fp = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker); var fileType = "html"; if (filterType == "img") { fp.init(window, GetString("SelectImageFile"), nsIFilePicker.modeOpen); @@ -414,32 +417,27 @@ function GetLocalFileURL(filterType) fp.appendFilters(nsIFilePicker.filterImages); } // Default or last filter is "All Files" fp.appendFilters(nsIFilePicker.filterAll); // set the file picker's current directory to last-opened location saved in prefs SetFilePickerDirectory(fp, fileType); - - - /* doesn't handle *.shtml files */ - try { - var ret = fp.show(); - if (ret == nsIFilePicker.returnCancel) - return null; - } - catch (ex) { - dump("filePicker.chooseInputFile threw an exception\n"); - return null; - } - SaveFilePickerDirectory(fp, fileType); - var fileHandler = GetFileProtocolHandler(); - return fp.file ? fileHandler.getURLSpecFromFile(fp.file) : null; + return new Promise(resolve => { + fp.open(rv => { + if (rv != nsIFilePicker.returnOK || !fp.file) { + resolve(null); + return; + } + SaveFilePickerDirectory(fp, fileType); + resolve(fp.fileURL.spec); + }); + }); } function GetMetaElementByAttribute(name, value) { if (name) { name = name.toLowerCase(); let editor = GetCurrentEditor(); @@ -976,26 +974,21 @@ function createMenuItem(aMenuPopup, aLab menuitem.setAttribute("label", aLabel); aMenuPopup.appendChild(menuitem); return menuitem; } // Shared by Image and Link dialogs for the "Choose" button for links function chooseLinkFile() { - // Get a local file, converted into URL format - var fileName = GetLocalFileURL("html, img"); - if (fileName) - { + GetLocalFileURL("html, img").then(fileURL => { // Always try to relativize local file URLs if (gHaveDocumentUrl) - fileName = MakeRelativeUrl(fileName); + fileURL = MakeRelativeUrl(fileURL); - gDialog.hrefInput.value = fileName; + gDialog.hrefInput.value = fileURL; // Do stuff specific to a particular dialog // (This is defined separately in Image and Link dialogs) ChangeLinkLocation(); - } - // Put focus into the input field - SetTextboxFocus(gDialog.hrefInput); + }); } diff --git a/editor/ui/dialogs/content/EdImageOverlay.js b/editor/ui/dialogs/content/EdImageOverlay.js --- a/editor/ui/dialogs/content/EdImageOverlay.js +++ b/editor/ui/dialogs/content/EdImageOverlay.js @@ -211,33 +211,31 @@ function GetImageMap() return null; } function chooseFile() { if (gTimerID) clearTimeout(gTimerID); - // Get a local file, converted into URL format - var fileName = GetLocalFileURL("img"); - if (fileName) - { + + // Put focus into the input field + SetTextboxFocus(gDialog.srcInput); + + GetLocalFileURL("img").then(fileURL => { // Always try to relativize local file URLs if (gHaveDocumentUrl) - fileName = MakeRelativeUrl(fileName); + fileURL = MakeRelativeUrl(fileURL); - gDialog.srcInput.value = fileName; + gDialog.srcInput.value = fileURL; SetRelativeCheckbox(); doOverallEnabling(); - } - LoadPreviewImage(); - - // Put focus into the input field - SetTextboxFocus(gDialog.srcInput); + LoadPreviewImage(); + }); } function PreviewImageLoaded() { if (gDialog.PreviewImage) { // Image loading has completed -- we can get actual width gActualWidth = gDialog.PreviewImage.naturalWidth;