-
Notifications
You must be signed in to change notification settings - Fork 156
Recent docs #92
Recent docs #92
Changes from 21 commits
1ab38e1
a9c98ee
66339d2
42742ca
76ab14a
5468543
016b982
7b4bad8
1f8543b
ed46e67
ad3a65c
49fc653
026267b
2be8edc
1d04673
33c8ff8
190b823
1e119ef
9c4f91f
5b01ed6
b076fb2
73f4b69
e5f5c6b
38a6999
21f4780
de8881c
99d9bdb
d5af0df
8cfab90
8311eb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
var AbrMenu = require.main.require("./abr-menu.js"), | ||
AbrWindow = require.main.require("./abr-window.js"), | ||
app = require.main.require("electron").app, | ||
BrowserWindow = require("electron").BrowserWindow, | ||
commands = require.main.require("./commands-main.js"), | ||
files = require.main.require("./files.js"), | ||
|
@@ -32,6 +33,45 @@ AbrApplication.prototype = { | |
this.windows[winId].path = path; | ||
}, | ||
|
||
updateRecentPaths: function (recentPaths) { | ||
if (recentPaths) { | ||
if (recentPaths.length > 0) { | ||
// May not work on all OS'es (electron doc says Windows and MacOS are OK, tests on Ubuntu 16 show it is OK too) | ||
// See: https://github.com/electron/electron/blob/master/docs/tutorial/desktop-environment-integration.md#recent-documents-windows--macos | ||
app.addRecentDocument(recentPaths[0]); | ||
} | ||
|
||
this.updateRecentPathsMenus(recentPaths); | ||
} | ||
}, | ||
|
||
updateRecentPathsMenus: function(recentPaths) { | ||
this.menu.setRecentDocsMenu(recentPaths); | ||
|
||
for (var winId in this.windows) { | ||
if (!this.windows.hasOwnProperty(winId)) continue; | ||
var abrWin = this.windows[winId]; | ||
if (abrWin) { | ||
// There are some cases when abrWin is null: | ||
// Scenario 1: using menu "File > Close Document" on Windows that has no doc | ||
// Scenario 2: | ||
// 1. Start Abricotine ==> Opens a new empty window | ||
// 2. Open one of the recent files using the "File > Recent" menu ==> Selected doc is opened in a new window (unlike the "File > Open menu") | ||
// 3. Close the empty window (the one opened when Abricotine started) | ||
// 4. Return the window opened at step 2, open a file using the "File > Open" menu | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this comment is useless, code is clear enough. |
||
abrWin.menu.setRecentDocsMenu(recentPaths); | ||
} | ||
} | ||
}, | ||
|
||
clearRecentDocs: function(abrWin) { | ||
// See: https://github.com/electron/electron/blob/master/docs/tutorial/desktop-environment-integration.md#recent-documents-windows--macos | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here. We won't add links to electron documentation for each method we use. |
||
app.clearRecentDocuments(); | ||
// update storage | ||
var webContents = abrWin.browserWindow.webContents; | ||
webContents.send("command", "clearRecentDocs"); | ||
}, | ||
|
||
// trigger | ||
getPathToLoad: function (arg, winId, callback) { | ||
var win = this.getFocusedAbrWindow(winId), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ var constants = require.main.require("./constants"), | |
fs = require("fs"), | ||
langmap = require("langmap"), | ||
Menu = require("electron").Menu, | ||
MenuItem = require("electron").MenuItem, | ||
pathModule = require("path"), | ||
spellchecker = require('spellchecker'); | ||
|
||
|
@@ -176,17 +177,18 @@ AbrMenu.prototype = { | |
var items = menu.items, | ||
menuItem; | ||
for (var i=0; i <items.length; i++) { | ||
if (items[i].submenu) { | ||
if (items[i].id === id) { | ||
menuItem = items[i]; | ||
} | ||
else if (items[i].submenu) { | ||
menuItem = doFindItem(items[i].submenu, id); | ||
} else { | ||
menuItem = items[i].id === id ? items[i] : undefined; | ||
} | ||
if (menuItem) { | ||
return menuItem; | ||
} | ||
} | ||
} | ||
return doFindItem(this, id); | ||
return doFindItem(this.menu, id); | ||
}, | ||
|
||
popup: function () { | ||
|
@@ -197,6 +199,50 @@ AbrMenu.prototype = { | |
attach: function () { | ||
// FIXME: use win.setMenu(menu) for Linux and Windows instead | ||
Menu.setApplicationMenu(this.menu); | ||
}, | ||
|
||
openRecentDoc: function (recentFile) { | ||
this.abrWin.abrApp.open(recentFile); | ||
// FIXCC recent-docs: Recent doc list is not updated after re-opening a recent file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remaining FIX/TODO: update recent doc list when reopening a recent file. |
||
}, | ||
|
||
clearRecentDocs: function () { | ||
this.abrWin.abrApp.clearRecentDocs(this.abrWin); | ||
}, | ||
|
||
setRecentDocsMenu: function(recentPaths) { | ||
var submenu = this.findItem("recentDocs").submenu, i, itemData, that = this; | ||
|
||
// clear menu (this API is not public and may change between Electron releases) | ||
submenu.clear(); | ||
|
||
function createOpenRecentCallback(recent) { | ||
return function() { | ||
that.openRecentDoc(recent); | ||
}; | ||
} | ||
|
||
// create items for recent files | ||
for (i=0; i <recentPaths.length; i++) { | ||
itemData = { | ||
label: recentPaths[i], | ||
click: createOpenRecentCallback(recentPaths[i]) | ||
}; | ||
submenu.append(new MenuItem(itemData)); | ||
} | ||
|
||
submenu.append(new MenuItem({ | ||
type: "separator" | ||
})); | ||
|
||
// append "clear recent" item | ||
submenu.append(new MenuItem({ | ||
label: "Clear", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, I could use the "i18n" branch (still to me merged "develop") to i18nize the "Clear" menu label. |
||
enabled: (recentPaths.length > 0), | ||
click: function() { | ||
that.clearRecentDocs(); | ||
} | ||
})); | ||
} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,11 @@ var remote = require("electron").remote, | |
shell = require("electron").shell, | ||
spellchecker = require('spellchecker'); | ||
|
||
function AbrDocument () { | ||
function AbrDocument (theLocalStorage) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This theLocalStorage argument is totally confusing. Why don't you simply use localStorage ? |
||
var that = this; | ||
|
||
this.localStorage = theLocalStorage; | ||
|
||
// IPC init | ||
var ipcClient = this.ipcClient = new IpcClient(); | ||
|
||
|
@@ -187,6 +189,7 @@ AbrDocument.prototype = { | |
this.setClean(); | ||
this.updateWindowTitle(); | ||
this.cm.refresh(); // CodeMirror scrollbar bug workaround | ||
this.updateRecentPath(path); | ||
}, | ||
|
||
close: function (force) { | ||
|
@@ -286,6 +289,7 @@ AbrDocument.prototype = { | |
if (!path) { | ||
return false; | ||
} | ||
this.updateRecentPath(path); | ||
var that = this; | ||
if (!this.path && this.isClean()) { | ||
files.readFile(path, function (data, path) { | ||
|
@@ -307,6 +311,7 @@ AbrDocument.prototype = { | |
|
||
save: function (path, callback) { | ||
path = path || this.path; | ||
this.updateRecentPath(path); | ||
if (!path) { | ||
return this.saveAs(callback); | ||
} | ||
|
@@ -528,6 +533,51 @@ AbrDocument.prototype = { | |
// About | ||
about: function () { | ||
dialogs.about(); | ||
}, | ||
|
||
updateRecentPath: function (path) { | ||
var thisDoc = this; | ||
|
||
this.getConfig(undefined, function(theConfig) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The correct way to do is: this.getConfig("editor:max-recent", function(max) { |
||
|
||
var max = theConfig.editor["max-recent"]; | ||
|
||
var recentPaths = thisDoc.localStorage.getItem("recent-docs"); | ||
if (recentPaths) { | ||
try { | ||
recentPaths = JSON.parse(recentPaths); | ||
} | ||
catch (e) { | ||
recentPaths = []; | ||
} | ||
} | ||
if (!recentPaths) recentPaths = []; | ||
|
||
if (path) { | ||
var indexOfPath = recentPaths.indexOf(path); | ||
if (indexOfPath >= 0) { | ||
recentPaths.splice(indexOfPath, 1); | ||
} | ||
recentPaths.unshift(path); | ||
if (recentPaths.length > max) { | ||
recentPaths = recentPaths.slice(0, max); | ||
} | ||
} | ||
|
||
thisDoc.localStorage.setItem("recent-docs", JSON.stringify(recentPaths)); | ||
|
||
thisDoc.ipcClient.trigger("updateRecentPaths", recentPaths); | ||
}); | ||
}, | ||
|
||
clearRecentDocs: function() { | ||
var thisDoc = this; | ||
|
||
this.getConfig(undefined, function(theConfig) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to get the config here. |
||
var recentPaths = []; | ||
thisDoc.localStorage.setItem("recent-docs", JSON.stringify(recentPaths)); | ||
thisDoc.ipcClient.trigger("updateRecentPaths", recentPaths); | ||
}); | ||
} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,6 @@ | |
var AbrDocument = require.main.require("./abr-document.js"); | ||
|
||
$( function () { | ||
var abrDoc = new AbrDocument(); | ||
var abrDoc = new AbrDocument(localStorage); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good! Don't forget this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I missed that one... Fixed. |
||
window.abrDoc = abrDoc; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,8 @@ | |
}, | ||
"autopreview-security": true, | ||
"editor": { | ||
"font-size": "16px" | ||
"font-size": "16px", | ||
"max-recent": 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
"highlight-modes": "", | ||
"preview-template": "default", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to document this here.