Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Recent docs #92

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1ab38e1
Store recent files in config
cicithesquirrel Jul 10, 2016
a9c98ee
Added submenu for recent docs
cicithesquirrel Jul 10, 2016
66339d2
Added TODO
cicithesquirrel Jul 10, 2016
42742ca
Added TODO
cicithesquirrel Jul 10, 2016
76ab14a
Added MacOS and Winsows integration
cicithesquirrel Jul 10, 2016
5468543
Fix: bad "require"
cicithesquirrel Jul 10, 2016
016b982
Recent docs are now stored in the browser localStorage
cicithesquirrel Jul 10, 2016
7b4bad8
(beginning) update the recent-docs menu
cicithesquirrel Jul 10, 2016
1f8543b
Update windows' menus
cicithesquirrel Jul 11, 2016
ed46e67
Clear and recrate the "recent docs" menu
cicithesquirrel Jul 12, 2016
ad3a65c
Removed console log
cicithesquirrel Jul 12, 2016
49fc653
Update "recent docs" menu on when the window is opened
cicithesquirrel Jul 12, 2016
026267b
Removed unused code
cicithesquirrel Jul 12, 2016
2be8edc
Re-open a recent file
cicithesquirrel Jul 12, 2016
1d04673
Remove max-recent config value
cicithesquirrel Jul 13, 2016
33c8ff8
Clear recent docs (beginning)
cicithesquirrel Jul 13, 2016
190b823
Remove console log
cicithesquirrel Jul 13, 2016
1e119ef
Clearing the recent docs menu
cicithesquirrel Jul 13, 2016
9c4f91f
Update recent paths menu when selecting one of the recent files
cicithesquirrel Aug 16, 2016
5b01ed6
Fix: Error when closing document on empty window & when opening file …
cicithesquirrel Aug 23, 2016
b076fb2
Fix: Label of menu "File > Recent > Clear"
cicithesquirrel Aug 23, 2016
73f4b69
Enhance comment
cicithesquirrel Aug 23, 2016
e5f5c6b
Moved TODO
cicithesquirrel Nov 28, 2016
38a6999
Remove comments
cicithesquirrel Nov 28, 2016
21f4780
Better way of getting the config
cicithesquirrel Nov 28, 2016
de8881c
Use global localStorage
cicithesquirrel Nov 29, 2016
99d9bdb
Fix: debug is always enabled
cicithesquirrel Nov 29, 2016
d5af0df
Change "max-recent" location in config
cicithesquirrel Nov 29, 2016
8cfab90
No need to get config
cicithesquirrel Nov 29, 2016
8311eb7
Remove unused parameter
cicithesquirrel Dec 1, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions app/abr-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -32,6 +33,35 @@ AbrApplication.prototype = {
this.windows[winId].path = path;
},

updateRecentPaths: function (recentPaths) {
if (recentPaths) {
if (recentPaths.length > 0) {
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) {
abrWin.menu.setRecentDocsMenu(recentPaths);
}
}
},

clearRecentDocs: function(abrWin) {
app.clearRecentDocuments();
// update storage
var webContents = abrWin.browserWindow.webContents;
webContents.send("command", "clearRecentDocs");
},

// trigger
getPathToLoad: function (arg, winId, callback) {
var win = this.getFocusedAbrWindow(winId),
Expand Down
53 changes: 49 additions & 4 deletions app/abr-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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 () {
Expand All @@ -197,6 +199,49 @@ 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);
},

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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
}
}));
}
};

Expand Down
5 changes: 5 additions & 0 deletions app/abr-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ function AbrWindow (abrApp, path) {
// Context
this.contextMenu = new AbrMenu(abrApp, this, contextMenuTemplate, this.config);
this.open();

// FIXCC recent-docs: Recent doc list is not updated after re-opening a recent file (using the recent menu)
// need trigger abrDoc.updateRecentPath to IPC client
}

AbrWindow.prototype = {
Expand Down Expand Up @@ -131,6 +134,8 @@ AbrWindow.prototype = {
if (this.config.get("debug")) {
win.openDevTools();
}

win.send('updateRecentPath', {path: this.path});
}
};

Expand Down
5 changes: 5 additions & 0 deletions app/menu-window.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
"command": "open",
"permanent": true
},
{
"label": "Recent",
"id": "recentDocs",
"submenu": []
},
{
"type": "separator"
},
Expand Down
44 changes: 43 additions & 1 deletion app/renderer/abr-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ AbrDocument.prototype = {
this.setClean();
this.updateWindowTitle();
this.cm.refresh(); // CodeMirror scrollbar bug workaround
this.updateRecentPath(path);
},

close: function (force) {
Expand Down Expand Up @@ -286,6 +287,7 @@ AbrDocument.prototype = {
if (!path) {
return false;
}
this.updateRecentPath(path);
var that = this;
if (!this.path && this.isClean()) {
files.readFile(path, function (data, path) {
Expand All @@ -307,6 +309,7 @@ AbrDocument.prototype = {

save: function (path, callback) {
path = path || this.path;
this.updateRecentPath(path);
if (!path) {
return this.saveAs(callback);
}
Expand Down Expand Up @@ -528,7 +531,46 @@ AbrDocument.prototype = {
// About
about: function () {
dialogs.about();
}
},

updateRecentPath: function (path) {
var thisDoc = this;

this.getConfig("max-recent", function(max) {

var recentPaths = 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);
}
}

localStorage.setItem("recent-docs", JSON.stringify(recentPaths));

thisDoc.ipcClient.trigger("updateRecentPaths", recentPaths);
});
},

clearRecentDocs: function() {
var recentPaths = [];
localStorage.setItem("recent-docs", JSON.stringify(recentPaths));
this.ipcClient.trigger("updateRecentPaths", recentPaths);
}
};

module.exports = AbrDocument;
6 changes: 5 additions & 1 deletion app/renderer/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ var commands = {
homepage: function (win, abrDoc, cm) {
var homepageURL = constants.homepageURL;
shell.openExternal(homepageURL);
}
},

clearRecentDocs: function(win, abrDoc, cm) {
abrDoc.clearRecentDocs();
},
};

module.exports = commands;
2 changes: 1 addition & 1 deletion app/renderer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
var AbrDocument = require.main.require("./abr-document.js");

$( function () {
var abrDoc = new AbrDocument();
var abrDoc = new AbrDocument(localStorage);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good! Don't forget this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed that one... Fixed.

window.abrDoc = abrDoc;
});
1 change: 1 addition & 0 deletions default/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"editor": {
"font-size": "16px"
},
"max-recent": 5,
"highlight-modes": "",
"preview-template": "default",
"startup-commands": {
Expand Down