Bug 1592550 - Allow saveAs util function to be passed an array of filters. r=Honza.

A new parameter is added, and it expect an array
of filter objects (which contain a pattern and
a label properties), which are going to be used
by the save file dialog.

Depends on D51070

Differential Revision: https://phabricator.services.mozilla.com/D51078

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Nicolas Chevobbe 2019-11-01 09:26:18 +00:00
Родитель 03fa12d685
Коммит 07a531158b
2 изменённых файлов: 41 добавлений и 10 удалений

Просмотреть файл

@ -267,7 +267,12 @@ class JSTerm extends Component {
`${date.getMonth() + 1}-${date.getDate()}_${date.getHours()}-` +
`${date.getMinutes()}-${date.getSeconds()}.js`;
const data = new TextEncoder().encode(value);
return saveAs(window, data, suggestedName);
return saveAs(window, data, suggestedName, [
{
pattern: "*.js",
label: l10n.getStr("webconsole.input.openJavaScriptFileFilter"),
},
]);
},
[Editor.accel("O")]: async () => this._openFile(),

Просмотреть файл

@ -804,11 +804,24 @@ exports.openFileStream = function(filePath) {
* The data to write to the file.
* @param {String} fileName
* The suggested filename.
* @param {Array} filters
* An array of object of the following shape:
* - pattern: A pattern for accepted files (example: "*.js")
* - label: The label that will be displayed in the save file dialog.
*/
exports.saveAs = async function(parentWindow, dataArray, fileName = "") {
exports.saveAs = async function(
parentWindow,
dataArray,
fileName = "",
filters = []
) {
let returnFile;
try {
returnFile = await exports.showSaveFileDialog(parentWindow, fileName);
returnFile = await exports.showSaveFileDialog(
parentWindow,
fileName,
filters
);
} catch (ex) {
return;
}
@ -821,16 +834,23 @@ exports.saveAs = async function(parentWindow, dataArray, fileName = "") {
/**
* Show file picker and return the file user selected.
*
* @param nsIWindow parentWindow
* @param {nsIWindow} parentWindow
* Optional parent window. If null the parent window of the file picker
* will be the window of the attached input element.
* @param AString suggestedFilename
* The suggested filename when toSave is true.
*
* @return Promise
* @param {String} suggestedFilename
* The suggested filename.
* @param {Array} filters
* An array of object of the following shape:
* - pattern: A pattern for accepted files (example: "*.js")
* - label: The label that will be displayed in the save file dialog.
* @return {Promise}
* A promise that is resolved after the file is selected by the file picker
*/
exports.showSaveFileDialog = function(parentWindow, suggestedFilename) {
exports.showSaveFileDialog = function(
parentWindow,
suggestedFilename,
filters = []
) {
const fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
if (suggestedFilename) {
@ -838,7 +858,13 @@ exports.showSaveFileDialog = function(parentWindow, suggestedFilename) {
}
fp.init(parentWindow, null, fp.modeSave);
fp.appendFilters(fp.filterAll);
if (Array.isArray(filters) && filters.length > 0) {
for (const { pattern, label } of filters) {
fp.appendFilter(label, pattern);
}
} else {
fp.appendFilters(fp.filterAll);
}
return new Promise((resolve, reject) => {
fp.open(result => {