From 3592333cb8c5af36d44ea54d06a8fe9e2e9af866 Mon Sep 17 00:00:00 2001 From: BurningTreeC Date: Tue, 6 Nov 2018 14:34:51 +0100 Subject: [PATCH 1/4] Add support for global keyboard shortcuts (#3493) * changes for global keyboardshortcuts * add keyboard.js startup module * remove not existing "th-opening-window" hook * correct title * use utils.addEventListeners * define platform lookup-names on startup * use the startup-lookup-names array * use the platform-specific lookupNames only * Update keyboard.js * move initializations to the constructor * move initializations to the constructor * rename hasAnyTiddlerChanged * don't explicitely create new RegExp * use $tw.utils.hopArray * match strings, no regex * remove hopArray, move to boot.js * add $tw.utils.hopArray to boot.js * style update * style updates * move more to keyboardManager module this could probably be moved to rootwidget.js * move more to keyboardManager module * add event listener for shortcuts in new windows * prevent error when opening window is blocked * add keydown listener on document in startup.js * delete startup/keyboard.js * add missing this.shortcutTiddlers * Update keyboard.js * Update boot.js * add exports.hopArray to utils.js * minor codingstyle tweak * change how lookupnames get pushed to array * Update windows.js * re-add shortcuts-listener for new windows I removed this before which I think was because I misunderstood what exactly should go to a separate PR --- core/modules/keyboard.js | 82 +++++++++++++++++++++++++++++++-- core/modules/startup/startup.js | 8 ++++ core/modules/startup/windows.js | 6 +++ core/modules/utils/utils.js | 12 +++++ 4 files changed, 104 insertions(+), 4 deletions(-) diff --git a/core/modules/keyboard.js b/core/modules/keyboard.js index bfa76d7e2..a63d48fbe 100644 --- a/core/modules/keyboard.js +++ b/core/modules/keyboard.js @@ -138,6 +138,17 @@ function KeyboardManager(options) { }); // Save the platform-specific name of the "meta" key this.metaKeyName = $tw.platform.isMac ? "cmd-" : "win-"; + this.shortcutKeysList = [], // Stores the shortcut-key descriptors + this.shortcutActionList = [], // Stores the corresponding action strings + this.shortcutParsedList = []; // Stores the parsed key descriptors + this.lookupNames = ["shortcuts"]; + this.lookupNames.push($tw.platform.isMac ? "shortcuts-mac" : "shortcuts-not-mac") + this.lookupNames.push($tw.platform.isWindows ? "shortcuts-windows" : "shortcuts-not-windows"); + this.lookupNames.push($tw.platform.isLinux ? "shortcuts-linux" : "shortcuts-not-linux"); + this.updateShortcutLists(this.getShortcutTiddlerList()); + $tw.wiki.addEventListener("change",function(changes) { + self.handleShortcutChanges(changes); + }); } /* @@ -229,10 +240,9 @@ KeyboardManager.prototype.parseKeyDescriptors = function(keyDescriptors,options) result.push.apply(result,self.parseKeyDescriptors(keyDescriptors,options)); } }; - lookupName("shortcuts"); - lookupName($tw.platform.isMac ? "shortcuts-mac" : "shortcuts-not-mac"); - lookupName($tw.platform.isWindows ? "shortcuts-windows" : "shortcuts-not-windows"); - lookupName($tw.platform.isLinux ? "shortcuts-linux" : "shortcuts-not-linux"); + $tw.utils.each(self.lookupNames,function(platformDescriptor) { + lookupName(platformDescriptor); + }); } } else { result.push(self.parseKeyDescriptor(keyDescriptor)); @@ -274,6 +284,70 @@ KeyboardManager.prototype.checkKeyDescriptors = function(event,keyInfoArray) { return false; }; +KeyboardManager.prototype.getShortcutTiddlerList = function() { + return $tw.wiki.getTiddlersWithTag("$:/tags/KeyboardShortcut"); +}; + +KeyboardManager.prototype.updateShortcutLists = function(tiddlerList) { + this.shortcutTiddlers = tiddlerList; + for(var i=0; i Date: Tue, 6 Nov 2018 14:54:00 +0100 Subject: [PATCH 2/4] Fix error opening new windows with popup-blocker (#3515) this prevents errors when a popup blocker blocks opening a new window from within a running TW --- core/modules/startup/windows.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/modules/startup/windows.js b/core/modules/startup/windows.js index fbaccccc3..591f5d5fa 100644 --- a/core/modules/startup/windows.js +++ b/core/modules/startup/windows.js @@ -33,8 +33,16 @@ exports.startup = function() { height = paramObject.height || "600", variables = $tw.utils.extend({},paramObject,{currentTiddler: title}); // Open the window - var srcWindow = window.open("","external-" + title,"scrollbars,width=" + width + ",height=" + height), + var srcWindow, + srcDocument; + // In case that popup blockers deny opening a new window + try { + srcWindow = window.open("","external-" + title,"scrollbars,width=" + width + ",height=" + height), srcDocument = srcWindow.document; + } + catch(e) { + return; + } windows[title] = srcWindow; // Check for reopening the same window if(srcWindow.haveInitialisedWindow) { From 97b098b059aa75e9c5965cafa5973ea6d98951d7 Mon Sep 17 00:00:00 2001 From: BurningTreeC Date: Tue, 6 Nov 2018 14:55:18 +0100 Subject: [PATCH 3/4] Observe openLinkFromOutsideRiver when creating new tiddlers (#3514) --- core/modules/widgets/navigator.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index 3014978f0..728d83e7e 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -468,6 +468,9 @@ NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) { // Update the story to insert the new draft at the top and remove any existing tiddler if(storyList.indexOf(draftTitle) === -1) { var slot = storyList.indexOf(event.navigateFromTitle); + if(slot === -1) { + slot = this.openLinkFromOutsideRiver === "bottom" ? storyList.length - 1 : slot; + } storyList.splice(slot + 1,0,draftTitle); } if(storyList.indexOf(title) !== -1) { From aeaf5ee5b623fce19af8d25134b60cdebc459682 Mon Sep 17 00:00:00 2001 From: BurningTreeC Date: Tue, 6 Nov 2018 15:33:41 +0100 Subject: [PATCH 4/4] FIX "Observe openLinkFromOutsideRiver ..." (#3516) I assumed the attribute to be available but it's not --- core/modules/widgets/navigator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index 728d83e7e..2bf0ac594 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -469,7 +469,7 @@ NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) { if(storyList.indexOf(draftTitle) === -1) { var slot = storyList.indexOf(event.navigateFromTitle); if(slot === -1) { - slot = this.openLinkFromOutsideRiver === "bottom" ? storyList.length - 1 : slot; + slot = this.getAttribute("openLinkFromOutsideRiver","top") === "bottom" ? storyList.length - 1 : slot; } storyList.splice(slot + 1,0,draftTitle); }