Skip to content

Commit bea8c26

Browse files
author
peterdotjs
committed
Adding undo shortcut option.
1 parent 65f9b97 commit bea8c26

File tree

8 files changed

+183
-132
lines changed

8 files changed

+183
-132
lines changed

js/background.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,80 @@ var util = {
194194
}
195195
},
196196

197+
198+
/*
199+
* undo previous resize option
200+
*/
201+
202+
/**
203+
* undo the previous resize that was selected
204+
*/
205+
undoResize: function(resize,callback) {
206+
var that = this,
207+
lastTab = localStorage.getItem('lastTab');
208+
209+
//undo not available
210+
if(!lastTab){
211+
return;
212+
}
213+
214+
resize.lastTab = JSON.parse(lastTab);
215+
var tabIndex = resize.lastTab.lastTabIndex;
216+
var windowId = resize.lastTab.lastWindowId;
217+
var tabsArray = resize.lastTab.lastTabsArray;
218+
219+
window.chrome.windows.get(windowId, {}, function(window){
220+
if(window){
221+
that.recombineTabs(resize,tabIndex,windowId,tabsArray,callback);
222+
} else {
223+
chrome.tabs.query({status: "complete"}, function(tabs){
224+
var currentExistingTabs = {};
225+
var newTabsArray = [];
226+
for(var i=0; i< tabs.length; i++){
227+
currentExistingTabs[tabs[i].id] = true;
228+
}
229+
for(var j = 0; j< tabsArray.length; j++){
230+
if(currentExistingTabs[tabsArray[j]]){
231+
newTabsArray.push(tabsArray[j]);
232+
}
233+
}
234+
if(newTabsArray.length !==0){
235+
chrome.windows.create({tabId: newTabsArray[0]},function(window){
236+
that.recombineTabs(resize,1,window.id,newTabsArray.slice(1),callback);
237+
});
238+
} else {
239+
if(!resize.isMac){
240+
alert("Previous tabs were closed.");
241+
}
242+
if(callback){
243+
callback();
244+
}
245+
}
246+
});
247+
}
248+
});
249+
},
250+
251+
/**
252+
* recombine the tabs into one window
253+
* @param {object} resize object passed in for modification
254+
* @param {number} tabIndex Starting tab index in previous window of first tab
255+
* @param {number} windowId Id of final window holding recombined tabs
256+
* @param {array} tabsArray Array of tab objects to be moved back to the previous window
257+
*/
258+
recombineTabs: function(resize,tabIndex, windowId, tabsArray, callback) {
259+
var indexCounter = tabIndex;
260+
window.chrome.tabs.move(tabsArray,{windowId: windowId, index: indexCounter});
261+
var updateInfo = resize.lastTab.lastWindowInfo;
262+
var updateInfoForUpdate = $.extend(true, {}, updateInfo);
263+
delete updateInfoForUpdate.incognito;
264+
window.chrome.windows.update(windowId,updateInfoForUpdate);
265+
if(callback){
266+
callback();
267+
}
268+
},
269+
270+
197271
//format the displayInfo
198272
displayInfoFormatter: function(displayInfo,currentWindowInfo){
199273
var index = 0,
@@ -360,6 +434,11 @@ function initResizePreferences(resize){
360434
}
361435
}
362436

437+
function disableUndoButton(resize){
438+
resize.lastTab = null;
439+
localStorage.removeItem('lastTab');
440+
}
441+
363442
function sendTracking(category, label) {
364443
var optOut = localStorage.getItem("tracking-opt-out"),
365444
deferTracking = false;
@@ -390,6 +469,15 @@ chrome.commands.onCommand.addListener(function callback(command) {
390469
return;
391470
}
392471

472+
if(command.indexOf('undo') !== -1){
473+
var resize = {};
474+
util.undoResize(resize,function(){
475+
disableUndoButton(resize);
476+
});
477+
sendTracking('undo','undo-shortcut');
478+
return;
479+
}
480+
393481
if(chrome.system && chrome.system.display){
394482
chrome.system.display.getInfo(function(displayInfo){
395483
chrome.windows.getCurrent(function(windowInfo){

js/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
layout.removeLayout(resizeType);
4242
sendTracking('resize-delete',resizeType);
4343
}).on('click','#undo-layout',function(){
44-
options.undoResize();
44+
backJs.util.undoResize(resize,options.disableUndoButton);
4545
sendTracking('undo','undo');
4646
}).on('click','#custom-layout',function(evt){
4747
evt.stopPropagation();

js/options.js

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -63,70 +63,6 @@
6363
}
6464
},
6565

66-
/*
67-
* undo previous resize option
68-
*/
69-
70-
/**
71-
* undo the previous resize that was selected
72-
*/
73-
undoResize: function() {
74-
var that = this;
75-
resize.lastTab = JSON.parse(localStorage.getItem('lastTab'));
76-
var tabIndex = resize.lastTab.lastTabIndex;
77-
var windowId = resize.lastTab.lastWindowId;
78-
var tabsArray = resize.lastTab.lastTabsArray;
79-
80-
window.chrome.windows.get(windowId, {}, function(window){
81-
if(window){
82-
that.recombineTabs(tabIndex,windowId,tabsArray);
83-
} else {
84-
chrome.tabs.query({status: "complete"}, function(tabs){
85-
var currentExistingTabs = {};
86-
var newTabsArray = [];
87-
for(var i=0; i< tabs.length; i++){
88-
currentExistingTabs[tabs[i].id] = true;
89-
}
90-
for(var j = 0; j< tabsArray.length; j++){
91-
if(currentExistingTabs[tabsArray[j]]){
92-
newTabsArray.push(tabsArray[j]);
93-
}
94-
}
95-
if(newTabsArray.length !==0){
96-
chrome.windows.create({tabId: newTabsArray[0]},function(window){
97-
that.recombineTabs(1,window.id,newTabsArray.slice(1));
98-
});
99-
} else {
100-
if(!resize.isMac){
101-
alert("Previous tabs were closed.");
102-
}
103-
that.disableUndoButton();
104-
}
105-
});
106-
}
107-
});
108-
},
109-
110-
/**
111-
* recombine the tabs into one window
112-
* @param {number} tabIndex Starting tab index in previous window of first tab
113-
* @param {number} windowId Id of final window holding recombined tabs
114-
* @param {array} tabsArray Array of tab objects to be moved back to the previous window
115-
*/
116-
recombineTabs: function(tabIndex, windowId, tabsArray) {
117-
var indexCounter = tabIndex;
118-
// for(var index=0; index<tabsArray.length; index++){
119-
// window.chrome.tabs.move(tabsArray[index],{windowId: windowId, index: indexCounter});
120-
// indexCounter++;
121-
// }
122-
window.chrome.tabs.move(tabsArray,{windowId: windowId, index: indexCounter});
123-
var updateInfo = resize.lastTab.lastWindowInfo;
124-
var updateInfoForUpdate = $.extend(true, {}, updateInfo);
125-
delete updateInfoForUpdate.incognito;
126-
window.chrome.windows.update(windowId,updateInfoForUpdate);
127-
this.disableUndoButton();
128-
},
129-
13066
/**
13167
* disabled undo button from user input
13268
*/

manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"content_security_policy": "script-src 'self' https://www.google-analytics.com; object-src 'self'",
2121
"options_page": "options.html",
2222
"commands": {
23+
"tab-resize-undo": {
24+
"description": "Undo previous resize"
25+
},
2326
"tab-resize-1-1": {
2427
"description": "Resize 1x1 layout"
2528
},

public/tabResize.js

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -479,70 +479,6 @@ if(!deferTracking) {
479479
}
480480
},
481481

482-
/*
483-
* undo previous resize option
484-
*/
485-
486-
/**
487-
* undo the previous resize that was selected
488-
*/
489-
undoResize: function() {
490-
var that = this;
491-
resize.lastTab = JSON.parse(localStorage.getItem('lastTab'));
492-
var tabIndex = resize.lastTab.lastTabIndex;
493-
var windowId = resize.lastTab.lastWindowId;
494-
var tabsArray = resize.lastTab.lastTabsArray;
495-
496-
window.chrome.windows.get(windowId, {}, function(window){
497-
if(window){
498-
that.recombineTabs(tabIndex,windowId,tabsArray);
499-
} else {
500-
chrome.tabs.query({status: "complete"}, function(tabs){
501-
var currentExistingTabs = {};
502-
var newTabsArray = [];
503-
for(var i=0; i< tabs.length; i++){
504-
currentExistingTabs[tabs[i].id] = true;
505-
}
506-
for(var j = 0; j< tabsArray.length; j++){
507-
if(currentExistingTabs[tabsArray[j]]){
508-
newTabsArray.push(tabsArray[j]);
509-
}
510-
}
511-
if(newTabsArray.length !==0){
512-
chrome.windows.create({tabId: newTabsArray[0]},function(window){
513-
that.recombineTabs(1,window.id,newTabsArray.slice(1));
514-
});
515-
} else {
516-
if(!resize.isMac){
517-
alert("Previous tabs were closed.");
518-
}
519-
that.disableUndoButton();
520-
}
521-
});
522-
}
523-
});
524-
},
525-
526-
/**
527-
* recombine the tabs into one window
528-
* @param {number} tabIndex Starting tab index in previous window of first tab
529-
* @param {number} windowId Id of final window holding recombined tabs
530-
* @param {array} tabsArray Array of tab objects to be moved back to the previous window
531-
*/
532-
recombineTabs: function(tabIndex, windowId, tabsArray) {
533-
var indexCounter = tabIndex;
534-
// for(var index=0; index<tabsArray.length; index++){
535-
// window.chrome.tabs.move(tabsArray[index],{windowId: windowId, index: indexCounter});
536-
// indexCounter++;
537-
// }
538-
window.chrome.tabs.move(tabsArray,{windowId: windowId, index: indexCounter});
539-
var updateInfo = resize.lastTab.lastWindowInfo;
540-
var updateInfoForUpdate = $.extend(true, {}, updateInfo);
541-
delete updateInfoForUpdate.incognito;
542-
window.chrome.windows.update(windowId,updateInfoForUpdate);
543-
this.disableUndoButton();
544-
},
545-
546482
/**
547483
* disabled undo button from user input
548484
*/
@@ -1125,7 +1061,7 @@ if(!deferTracking) {
11251061
layout.removeLayout(resizeType);
11261062
sendTracking('resize-delete',resizeType);
11271063
}).on('click','#undo-layout',function(){
1128-
options.undoResize();
1064+
backJs.util.undoResize(resize,options.disableUndoButton);
11291065
sendTracking('undo','undo');
11301066
}).on('click','#custom-layout',function(evt){
11311067
evt.stopPropagation();

public/tabResize.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/tabResize_background.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,80 @@ var util = {
194194
}
195195
},
196196

197+
198+
/*
199+
* undo previous resize option
200+
*/
201+
202+
/**
203+
* undo the previous resize that was selected
204+
*/
205+
undoResize: function(resize,callback) {
206+
var that = this,
207+
lastTab = localStorage.getItem('lastTab');
208+
209+
//undo not available
210+
if(!lastTab){
211+
return;
212+
}
213+
214+
resize.lastTab = JSON.parse(lastTab);
215+
var tabIndex = resize.lastTab.lastTabIndex;
216+
var windowId = resize.lastTab.lastWindowId;
217+
var tabsArray = resize.lastTab.lastTabsArray;
218+
219+
window.chrome.windows.get(windowId, {}, function(window){
220+
if(window){
221+
that.recombineTabs(resize,tabIndex,windowId,tabsArray,callback);
222+
} else {
223+
chrome.tabs.query({status: "complete"}, function(tabs){
224+
var currentExistingTabs = {};
225+
var newTabsArray = [];
226+
for(var i=0; i< tabs.length; i++){
227+
currentExistingTabs[tabs[i].id] = true;
228+
}
229+
for(var j = 0; j< tabsArray.length; j++){
230+
if(currentExistingTabs[tabsArray[j]]){
231+
newTabsArray.push(tabsArray[j]);
232+
}
233+
}
234+
if(newTabsArray.length !==0){
235+
chrome.windows.create({tabId: newTabsArray[0]},function(window){
236+
that.recombineTabs(resize,1,window.id,newTabsArray.slice(1),callback);
237+
});
238+
} else {
239+
if(!resize.isMac){
240+
alert("Previous tabs were closed.");
241+
}
242+
if(callback){
243+
callback();
244+
}
245+
}
246+
});
247+
}
248+
});
249+
},
250+
251+
/**
252+
* recombine the tabs into one window
253+
* @param {object} resize object passed in for modification
254+
* @param {number} tabIndex Starting tab index in previous window of first tab
255+
* @param {number} windowId Id of final window holding recombined tabs
256+
* @param {array} tabsArray Array of tab objects to be moved back to the previous window
257+
*/
258+
recombineTabs: function(resize,tabIndex, windowId, tabsArray, callback) {
259+
var indexCounter = tabIndex;
260+
window.chrome.tabs.move(tabsArray,{windowId: windowId, index: indexCounter});
261+
var updateInfo = resize.lastTab.lastWindowInfo;
262+
var updateInfoForUpdate = $.extend(true, {}, updateInfo);
263+
delete updateInfoForUpdate.incognito;
264+
window.chrome.windows.update(windowId,updateInfoForUpdate);
265+
if(callback){
266+
callback();
267+
}
268+
},
269+
270+
197271
//format the displayInfo
198272
displayInfoFormatter: function(displayInfo,currentWindowInfo){
199273
var index = 0,
@@ -360,6 +434,11 @@ function initResizePreferences(resize){
360434
}
361435
}
362436

437+
function disableUndoButton(resize){
438+
resize.lastTab = null;
439+
localStorage.removeItem('lastTab');
440+
}
441+
363442
function sendTracking(category, label) {
364443
var optOut = localStorage.getItem("tracking-opt-out"),
365444
deferTracking = false;
@@ -390,6 +469,15 @@ chrome.commands.onCommand.addListener(function callback(command) {
390469
return;
391470
}
392471

472+
if(command.indexOf('undo') !== -1){
473+
var resize = {};
474+
util.undoResize(resize,function(){
475+
disableUndoButton(resize);
476+
});
477+
sendTracking('undo','undo-shortcut');
478+
return;
479+
}
480+
393481
if(chrome.system && chrome.system.display){
394482
chrome.system.display.getInfo(function(displayInfo){
395483
chrome.windows.getCurrent(function(windowInfo){

0 commit comments

Comments
 (0)