From: Go MAEDA Date: Thu, 7 Jan 2021 05:39:36 +0000 (+0000) Subject: Add keyboard shortcut to toggle between Edit/Preview tabs (#30459). X-Git-Tag: 4.2.0~123 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=81efbcaa2f592234ffa22a289f650a3aeb288e3c;p=redmine.git Add keyboard shortcut to toggle between Edit/Preview tabs (#30459). Patch by Marius BALTEANU. git-svn-id: http://svn.redmine.org/redmine/trunk@20706 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/public/javascripts/jstoolbar/jstoolbar.js b/public/javascripts/jstoolbar/jstoolbar.js index 376698f9a..de4eb997a 100644 --- a/public/javascripts/jstoolbar/jstoolbar.js +++ b/public/javascripts/jstoolbar/jstoolbar.js @@ -21,6 +21,7 @@ */ /* Modified by JP LANG for textile formatting */ +let lastJstPreviewed = null; function jsToolBar(textarea) { if (!document.createElement) { return; } @@ -53,6 +54,8 @@ function jsToolBar(textarea) { var This = this; + this.textarea.onkeydown = function(event) { This.keyboardShortcuts.call(This, event); }; + this.editTab = new jsTab('Edit', true); this.editTab.onclick = function(event) { This.hidePreview.call(This, event); return false; }; @@ -401,22 +404,33 @@ jsToolBar.prototype = { }, showPreview: function(event) { if (event.target.classList.contains('selected')) { return; } + lastJstPreviewed = this.toolbarBlock; this.preview.setAttribute('style', 'min-height: ' + this.textarea.clientHeight + 'px;') this.toolbar.classList.add('hidden'); this.textarea.classList.add('hidden'); this.preview.classList.remove('hidden'); this.tabsBlock.getElementsByClassName('tab-edit')[0].classList.remove('selected'); event.target.classList.add('selected'); - }, hidePreview: function(event) { if (event.target.classList.contains('selected')) { return; } this.toolbar.classList.remove('hidden'); this.textarea.classList.remove('hidden'); + this.textarea.focus(); this.preview.classList.add('hidden'); this.tabsBlock.getElementsByClassName('tab-preview')[0].classList.remove('selected'); event.target.classList.add('selected'); }, + keyboardShortcuts: function(e) { + if (isToogleEditPreviewShortcut(e)) { + // Switch to preview only if tab edit is selected when the event triggered. + if (this.tabsBlock.querySelector('.tab-edit.selected')) { + e.stopPropagation(); + e.preventDefault(); + this.tabsBlock.getElementsByClassName('tab-preview')[0].click(); + } + } + }, stripBaseURL: function(url) { if (this.base_url != '') { var pos = url.indexOf(this.base_url); @@ -507,3 +521,22 @@ jsToolBar.prototype.tableMenu = function(fn){ }); return false; }; + +$(document).keydown(function(e) { + if (isToogleEditPreviewShortcut(e)) { + if (lastJstPreviewed !== null) { + e.preventDefault(); + e.stopPropagation(); + lastJstPreviewed.querySelector('.tab-edit').click(); + lastJstPreviewed = null; + } + } +}); + +function isToogleEditPreviewShortcut(e) { + if ((e.metaKey || e.ctrlKey) && e.shiftKey && e.key.toLowerCase() === 'p') { + return true; + } else { + return false; + } +} \ No newline at end of file