diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 12:09:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 12:09:06 +0300 |
commit | 7e8278c0f46cf6058efad5afd0aef177977bd663 (patch) | |
tree | 7ac46710921145bb782bcb208ea896e1548b168b /app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js | |
parent | bbf6581214128ae12a6ff32f66a0d03ee57a2e91 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js')
-rw-r--r-- | app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js index 052e33b4a2b..d5d8edd5ac0 100644 --- a/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js @@ -1,26 +1,67 @@ import Mousetrap from 'mousetrap'; -import { getLocationHash, visitUrl } from '../../lib/utils/url_utility'; +import { + getLocationHash, + updateHistory, + urlIsDifferent, + urlContainsSha, + getShaFromUrl, +} from '~/lib/utils/url_utility'; +import { updateRefPortionOfTitle } from '~/repository/utils/title'; import Shortcuts from './shortcuts'; const defaults = { skipResetBindings: false, fileBlobPermalinkUrl: null, + fileBlobPermalinkUrlElement: null, }; +function eventHasModifierKeys(event) { + // We ignore alt because I don't think alt clicks normally do anything special? + return event.ctrlKey || event.metaKey || event.shiftKey; +} + export default class ShortcutsBlob extends Shortcuts { constructor(opts) { const options = Object.assign({}, defaults, opts); super(options.skipResetBindings); this.options = options; + this.shortcircuitPermalinkButton(); + Mousetrap.bind('y', this.moveToFilePermalink.bind(this)); } moveToFilePermalink() { - if (this.options.fileBlobPermalinkUrl) { + const permalink = this.options.fileBlobPermalinkUrl; + + if (permalink) { const hash = getLocationHash(); const hashUrlString = hash ? `#${hash}` : ''; - visitUrl(`${this.options.fileBlobPermalinkUrl}${hashUrlString}`); + + if (urlIsDifferent(permalink)) { + updateHistory({ + url: `${permalink}${hashUrlString}`, + title: document.title, + }); + } + + if (urlContainsSha({ url: permalink })) { + updateRefPortionOfTitle(getShaFromUrl({ url: permalink })); + } + } + } + + shortcircuitPermalinkButton() { + const button = this.options.fileBlobPermalinkUrlElement; + const handleButton = e => { + if (!eventHasModifierKeys(e)) { + e.preventDefault(); + this.moveToFilePermalink(); + } + }; + + if (button) { + button.addEventListener('click', handleButton); } } } |