diff options
Diffstat (limited to 'app/assets/javascripts/lib/utils')
-rw-r--r-- | app/assets/javascripts/lib/utils/common_utils.js | 53 | ||||
-rw-r--r-- | app/assets/javascripts/lib/utils/dom_utils.js | 8 | ||||
-rw-r--r-- | app/assets/javascripts/lib/utils/keycodes.js | 1 | ||||
-rw-r--r-- | app/assets/javascripts/lib/utils/scroll_utils.js | 2 | ||||
-rw-r--r-- | app/assets/javascripts/lib/utils/text_markdown.js | 3 | ||||
-rw-r--r-- | app/assets/javascripts/lib/utils/text_utility.js | 10 |
6 files changed, 60 insertions, 17 deletions
diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js index 42a5de68cfa..f88a0433535 100644 --- a/app/assets/javascripts/lib/utils/common_utils.js +++ b/app/assets/javascripts/lib/utils/common_utils.js @@ -218,23 +218,46 @@ export const isMetaKey = e => e.metaKey || e.ctrlKey || e.altKey || e.shiftKey; export const isMetaClick = e => e.metaKey || e.ctrlKey || e.which === 2; export const contentTop = () => { - const perfBar = $('#js-peek').outerHeight() || 0; - const mrTabsHeight = $('.merge-request-tabs').outerHeight() || 0; - const headerHeight = $('.navbar-gitlab').outerHeight() || 0; - const diffFilesChanged = $('.js-diff-files-changed').outerHeight() || 0; const isDesktop = breakpointInstance.isDesktop(); - const diffFileTitleBar = - (isDesktop && $('.diff-file .file-title-flex-parent:visible').outerHeight()) || 0; - const compareVersionsHeaderHeight = (isDesktop && $('.mr-version-controls').outerHeight()) || 0; + const heightCalculators = [ + () => $('#js-peek').outerHeight(), + () => $('.navbar-gitlab').outerHeight(), + ({ desktop }) => { + const container = document.querySelector('.line-resolve-all-container'); + let size = 0; + + if (!desktop && container) { + size = container.offsetHeight; + } - return ( - perfBar + - mrTabsHeight + - headerHeight + - diffFilesChanged + - diffFileTitleBar + - compareVersionsHeaderHeight - ); + return size; + }, + () => $('.merge-request-tabs').outerHeight(), + () => $('.js-diff-files-changed').outerHeight(), + ({ desktop }) => { + const diffsTabIsActive = window.mrTabs?.currentAction === 'diffs'; + let size; + + if (desktop && diffsTabIsActive) { + size = $('.diff-file .file-title-flex-parent:visible').outerHeight(); + } + + return size; + }, + ({ desktop }) => { + let size; + + if (desktop) { + size = $('.mr-version-controls').outerHeight(); + } + + return size; + }, + ]; + + return heightCalculators.reduce((totalHeight, calculator) => { + return totalHeight + (calculator({ desktop: isDesktop }) || 0); + }, 0); }; export const scrollToElement = (element, options = {}) => { diff --git a/app/assets/javascripts/lib/utils/dom_utils.js b/app/assets/javascripts/lib/utils/dom_utils.js index 7bba7ba2f45..2f19a0c9b26 100644 --- a/app/assets/javascripts/lib/utils/dom_utils.js +++ b/app/assets/javascripts/lib/utils/dom_utils.js @@ -1,6 +1,14 @@ import { has } from 'lodash'; import { isInIssuePage, isInMRPage, isInEpicPage } from './common_utils'; +/** + * Checks whether an element's content exceeds the element's width. + * + * @param element DOM element to check + */ +export const hasHorizontalOverflow = element => + Boolean(element && element.scrollWidth > element.offsetWidth); + export const addClassIfElementExists = (element, className) => { if (element) { element.classList.add(className); diff --git a/app/assets/javascripts/lib/utils/keycodes.js b/app/assets/javascripts/lib/utils/keycodes.js index 618266f7a09..6f5cd7460f8 100644 --- a/app/assets/javascripts/lib/utils/keycodes.js +++ b/app/assets/javascripts/lib/utils/keycodes.js @@ -2,6 +2,7 @@ // See: https://gitlab.com/gitlab-org/gitlab/-/issues/216102 export const BACKSPACE_KEY_CODE = 8; +export const TAB_KEY_CODE = 9; export const ENTER_KEY_CODE = 13; export const ESC_KEY_CODE = 27; export const UP_KEY_CODE = 38; diff --git a/app/assets/javascripts/lib/utils/scroll_utils.js b/app/assets/javascripts/lib/utils/scroll_utils.js index b4da1e16f08..01e43fd3b93 100644 --- a/app/assets/javascripts/lib/utils/scroll_utils.js +++ b/app/assets/javascripts/lib/utils/scroll_utils.js @@ -49,5 +49,3 @@ export const toggleDisableButton = ($button, disable) => { if (disable && $button.prop('disabled')) return; $button.prop('disabled', disable); }; - -export default {}; diff --git a/app/assets/javascripts/lib/utils/text_markdown.js b/app/assets/javascripts/lib/utils/text_markdown.js index dfb86787788..c711c0bd163 100644 --- a/app/assets/javascripts/lib/utils/text_markdown.js +++ b/app/assets/javascripts/lib/utils/text_markdown.js @@ -339,6 +339,7 @@ export function addMarkdownListeners(form) { Shortcuts.initMarkdownEditorShortcuts($(this), updateTextForToolbarBtn); }); + // eslint-disable-next-line @gitlab/no-global-event-off const $allToolbarBtns = $('.js-md', form) .off('click') .on('click', function() { @@ -351,6 +352,7 @@ export function addMarkdownListeners(form) { } export function addEditorMarkdownListeners(editor) { + // eslint-disable-next-line @gitlab/no-global-event-off $('.js-md') .off('click') .on('click', e => { @@ -376,5 +378,6 @@ export function removeMarkdownListeners(form) { Shortcuts.removeMarkdownEditorShortcuts($(this)); }); + // eslint-disable-next-line @gitlab/no-global-event-off return $('.js-md', form).off('click'); } diff --git a/app/assets/javascripts/lib/utils/text_utility.js b/app/assets/javascripts/lib/utils/text_utility.js index a81ca3f211f..c398874db24 100644 --- a/app/assets/javascripts/lib/utils/text_utility.js +++ b/app/assets/javascripts/lib/utils/text_utility.js @@ -411,3 +411,13 @@ export const hasContent = obj => isString(obj) && obj.trim() !== ''; export const isValidSha1Hash = str => { return /^[0-9a-f]{5,40}$/.test(str); }; + +/** + * Adds a final newline to the content if it doesn't already exist + * + * @param {*} content Content + * @param {*} endOfLine Type of newline: CRLF='\r\n', LF='\n', CR='\r' + */ +export function insertFinalNewline(content, endOfLine = '\n') { + return content.slice(-endOfLine.length) !== endOfLine ? `${content}${endOfLine}` : content; +} |