diff options
author | Mike Greiling <mike@pixelcog.com> | 2018-09-18 23:34:14 +0300 |
---|---|---|
committer | Mike Greiling <mike@pixelcog.com> | 2018-09-19 01:26:15 +0300 |
commit | 196dfd2627fec6c0ede1d7df0dd49881b2bbd898 (patch) | |
tree | 530ba14b7ab2560f11335b1bfe9daa2bb3079efd /app/assets/javascripts/behaviors/shortcuts | |
parent | 08c3920ce14628e90244fba5f580ca4d7bdabdbd (diff) |
Move shortcuts classes into behaviors/shortcuts
Diffstat (limited to 'app/assets/javascripts/behaviors/shortcuts')
7 files changed, 315 insertions, 0 deletions
diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts.js new file mode 100644 index 00000000000..6719bfd6d22 --- /dev/null +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts.js @@ -0,0 +1,124 @@ +import $ from 'jquery'; +import Cookies from 'js-cookie'; +import Mousetrap from 'mousetrap'; +import axios from '../../lib/utils/axios_utils'; +import { refreshCurrentPage, visitUrl } from '../../lib/utils/url_utility'; +import findAndFollowLink from '../../lib/utils/navigation_utility'; + +const defaultStopCallback = Mousetrap.stopCallback; +Mousetrap.stopCallback = (e, element, combo) => { + if (['ctrl+shift+p', 'command+shift+p'].indexOf(combo) !== -1) { + return false; + } + + return defaultStopCallback(e, element, combo); +}; + +export default class Shortcuts { + constructor() { + this.onToggleHelp = this.onToggleHelp.bind(this); + this.enabledHelp = []; + + Mousetrap.bind('?', this.onToggleHelp); + Mousetrap.bind('s', Shortcuts.focusSearch); + Mousetrap.bind('f', this.focusFilter.bind(this)); + Mousetrap.bind('p b', Shortcuts.onTogglePerfBar); + + const findFileURL = document.body.dataset.findFile; + + Mousetrap.bind('shift+t', () => findAndFollowLink('.shortcuts-todos')); + Mousetrap.bind('shift+a', () => findAndFollowLink('.dashboard-shortcuts-activity')); + Mousetrap.bind('shift+i', () => findAndFollowLink('.dashboard-shortcuts-issues')); + Mousetrap.bind('shift+m', () => findAndFollowLink('.dashboard-shortcuts-merge_requests')); + Mousetrap.bind('shift+p', () => findAndFollowLink('.dashboard-shortcuts-projects')); + Mousetrap.bind('shift+g', () => findAndFollowLink('.dashboard-shortcuts-groups')); + Mousetrap.bind('shift+l', () => findAndFollowLink('.dashboard-shortcuts-milestones')); + Mousetrap.bind('shift+s', () => findAndFollowLink('.dashboard-shortcuts-snippets')); + + Mousetrap.bind(['ctrl+shift+p', 'command+shift+p'], Shortcuts.toggleMarkdownPreview); + + if (typeof findFileURL !== 'undefined' && findFileURL !== null) { + Mousetrap.bind('t', () => { + visitUrl(findFileURL); + }); + } + + $(document).on('click.more_help', '.js-more-help-button', function clickMoreHelp(e) { + $(this).remove(); + $('.hidden-shortcut').show(); + e.preventDefault(); + }); + } + + onToggleHelp(e) { + if (e.preventDefault) { + e.preventDefault(); + } + + Shortcuts.toggleHelp(this.enabledHelp); + } + + static onTogglePerfBar(e) { + e.preventDefault(); + const performanceBarCookieName = 'perf_bar_enabled'; + if (Cookies.get(performanceBarCookieName) === 'true') { + Cookies.set(performanceBarCookieName, 'false', { path: '/' }); + } else { + Cookies.set(performanceBarCookieName, 'true', { path: '/' }); + } + refreshCurrentPage(); + } + + static toggleMarkdownPreview(e) { + // Check if short-cut was triggered while in Write Mode + const $target = $(e.target); + const $form = $target.closest('form'); + + if ($target.hasClass('js-note-text')) { + $('.js-md-preview-button', $form).focus(); + } + $(document).triggerHandler('markdown-preview:toggle', [e]); + } + + static toggleHelp(location) { + const $modal = $('#modal-shortcuts'); + + if ($modal.length) { + $modal.modal('toggle'); + return null; + } + + return axios.get(gon.shortcuts_path, { + responseType: 'text', + }).then(({ data }) => { + $.globalEval(data); + + if (location && location.length > 0) { + const results = []; + for (let i = 0, len = location.length; i < len; i += 1) { + results.push($(location[i]).show()); + } + return results; + } + + $('.hidden-shortcut').show(); + return $('.js-more-help-button').remove(); + }); + } + + focusFilter(e) { + if (!this.filterInput) { + this.filterInput = $('input[type=search]', '.nav-controls'); + } + this.filterInput.focus(); + e.preventDefault(); + } + + static focusSearch(e) { + $('#search').focus(); + + if (e.preventDefault) { + e.preventDefault(); + } + } +} diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js new file mode 100644 index 00000000000..052e33b4a2b --- /dev/null +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js @@ -0,0 +1,26 @@ +import Mousetrap from 'mousetrap'; +import { getLocationHash, visitUrl } from '../../lib/utils/url_utility'; +import Shortcuts from './shortcuts'; + +const defaults = { + skipResetBindings: false, + fileBlobPermalinkUrl: null, +}; + +export default class ShortcutsBlob extends Shortcuts { + constructor(opts) { + const options = Object.assign({}, defaults, opts); + super(options.skipResetBindings); + this.options = options; + + Mousetrap.bind('y', this.moveToFilePermalink.bind(this)); + } + + moveToFilePermalink() { + if (this.options.fileBlobPermalinkUrl) { + const hash = getLocationHash(); + const hashUrlString = hash ? `#${hash}` : ''; + visitUrl(`${this.options.fileBlobPermalinkUrl}${hashUrlString}`); + } + } +} diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_find_file.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_find_file.js new file mode 100644 index 00000000000..8658081c6c2 --- /dev/null +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_find_file.js @@ -0,0 +1,29 @@ +import Mousetrap from 'mousetrap'; +import ShortcutsNavigation from './shortcuts_navigation'; + +export default class ShortcutsFindFile extends ShortcutsNavigation { + constructor(projectFindFile) { + super(); + + const oldStopCallback = Mousetrap.stopCallback; + this.projectFindFile = projectFindFile; + + Mousetrap.stopCallback = (e, element, combo) => { + if ( + element === this.projectFindFile.inputElement[0] && + (combo === 'up' || combo === 'down' || combo === 'esc' || combo === 'enter') + ) { + // when press up/down key in textbox, cursor prevent to move to home/end + e.preventDefault(); + return false; + } + + return oldStopCallback(e, element, combo); + }; + + Mousetrap.bind('up', this.projectFindFile.selectRowUp); + Mousetrap.bind('down', this.projectFindFile.selectRowDown); + Mousetrap.bind('esc', this.projectFindFile.goToTree); + Mousetrap.bind('enter', this.projectFindFile.goToBlob); + } +} diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js new file mode 100644 index 00000000000..5e48bf5a35c --- /dev/null +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js @@ -0,0 +1,77 @@ +import $ from 'jquery'; +import Mousetrap from 'mousetrap'; +import _ from 'underscore'; +import Sidebar from '../../right_sidebar'; +import Shortcuts from './shortcuts'; +import { CopyAsGFM } from '../markdown/copy_as_gfm'; + +export default class ShortcutsIssuable extends Shortcuts { + constructor(isMergeRequest) { + super(); + + Mousetrap.bind('a', () => ShortcutsIssuable.openSidebarDropdown('assignee')); + Mousetrap.bind('m', () => ShortcutsIssuable.openSidebarDropdown('milestone')); + Mousetrap.bind('l', () => ShortcutsIssuable.openSidebarDropdown('labels')); + Mousetrap.bind('r', ShortcutsIssuable.replyWithSelectedText); + Mousetrap.bind('e', ShortcutsIssuable.editIssue); + + if (isMergeRequest) { + this.enabledHelp.push('.hidden-shortcut.merge_requests'); + } else { + this.enabledHelp.push('.hidden-shortcut.issues'); + } + } + + static replyWithSelectedText() { + const $replyField = $('.js-main-target-form .js-vue-comment-form'); + const documentFragment = window.gl.utils.getSelectedFragment(); + + if (!$replyField.length) { + return false; + } + + if (!documentFragment) { + $replyField.focus(); + return false; + } + + const el = CopyAsGFM.transformGFMSelection(documentFragment.cloneNode(true)); + const selected = CopyAsGFM.nodeToGFM(el); + + if (selected.trim() === '') { + return false; + } + + const quote = _.map(selected.split('\n'), val => `${`> ${val}`.trim()}\n`); + + // If replyField already has some content, add a newline before our quote + const separator = ($replyField.val().trim() !== '' && '\n\n') || ''; + $replyField + .val((a, current) => `${current}${separator}${quote.join('')}\n`) + .trigger('input') + .trigger('change'); + + // Trigger autosize + const event = document.createEvent('Event'); + event.initEvent('autosize:update', true, false); + $replyField.get(0).dispatchEvent(event); + + // Focus the input field + $replyField.focus(); + + return false; + } + + static editIssue() { + // Need to click the element as on issues, editing is inline + // on merge request, editing is on a different page + document.querySelector('.js-issuable-edit').click(); + + return false; + } + + static openSidebarDropdown(name) { + Sidebar.instance.openDropdown(name); + return false; + } +} diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_navigation.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_navigation.js new file mode 100644 index 00000000000..fa9b2c9f755 --- /dev/null +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_navigation.js @@ -0,0 +1,28 @@ +import Mousetrap from 'mousetrap'; +import findAndFollowLink from '../../lib/utils/navigation_utility'; +import Shortcuts from './shortcuts'; + +export default class ShortcutsNavigation extends Shortcuts { + constructor() { + super(); + + Mousetrap.bind('g p', () => findAndFollowLink('.shortcuts-project')); + Mousetrap.bind('g v', () => findAndFollowLink('.shortcuts-project-activity')); + Mousetrap.bind('g f', () => findAndFollowLink('.shortcuts-tree')); + Mousetrap.bind('g c', () => findAndFollowLink('.shortcuts-commits')); + Mousetrap.bind('g j', () => findAndFollowLink('.shortcuts-builds')); + Mousetrap.bind('g n', () => findAndFollowLink('.shortcuts-network')); + Mousetrap.bind('g d', () => findAndFollowLink('.shortcuts-repository-charts')); + Mousetrap.bind('g i', () => findAndFollowLink('.shortcuts-issues')); + Mousetrap.bind('g b', () => findAndFollowLink('.shortcuts-issue-boards')); + Mousetrap.bind('g m', () => findAndFollowLink('.shortcuts-merge_requests')); + Mousetrap.bind('g w', () => findAndFollowLink('.shortcuts-wiki')); + Mousetrap.bind('g s', () => findAndFollowLink('.shortcuts-snippets')); + Mousetrap.bind('g k', () => findAndFollowLink('.shortcuts-kubernetes')); + Mousetrap.bind('g e', () => findAndFollowLink('.shortcuts-environments')); + Mousetrap.bind('g l', () => findAndFollowLink('.shortcuts-metrics')); + Mousetrap.bind('i', () => findAndFollowLink('.shortcuts-new-issue')); + + this.enabledHelp.push('.hidden-shortcut.project'); + } +} diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_network.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_network.js new file mode 100644 index 00000000000..a88c280fa3b --- /dev/null +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_network.js @@ -0,0 +1,17 @@ +import Mousetrap from 'mousetrap'; +import ShortcutsNavigation from './shortcuts_navigation'; + +export default class ShortcutsNetwork extends ShortcutsNavigation { + constructor(graph) { + super(); + + Mousetrap.bind(['left', 'h'], graph.scrollLeft); + Mousetrap.bind(['right', 'l'], graph.scrollRight); + Mousetrap.bind(['up', 'k'], graph.scrollUp); + Mousetrap.bind(['down', 'j'], graph.scrollDown); + Mousetrap.bind(['shift+up', 'shift+k'], graph.scrollTop); + Mousetrap.bind(['shift+down', 'shift+j'], graph.scrollBottom); + + this.enabledHelp.push('.hidden-shortcut.network'); + } +} diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_wiki.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_wiki.js new file mode 100644 index 00000000000..8b7e6a56d25 --- /dev/null +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_wiki.js @@ -0,0 +1,14 @@ +import Mousetrap from 'mousetrap'; +import ShortcutsNavigation from './shortcuts_navigation'; +import findAndFollowLink from '../../lib/utils/navigation_utility'; + +export default class ShortcutsWiki extends ShortcutsNavigation { + constructor() { + super(); + Mousetrap.bind('e', ShortcutsWiki.editWiki); + } + + static editWiki() { + findAndFollowLink('.js-wiki-edit'); + } +} |