2021-01-03 14:25:43 +05:30
|
|
|
import { __ } from '~/locale';
|
|
|
|
import {
|
|
|
|
MATCH_LINE_TYPE,
|
|
|
|
CONTEXT_LINE_TYPE,
|
|
|
|
LINE_HOVER_CLASS_NAME,
|
|
|
|
OLD_NO_NEW_LINE_TYPE,
|
|
|
|
NEW_NO_NEW_LINE_TYPE,
|
|
|
|
EMPTY_CELL_TYPE,
|
2021-09-30 23:02:18 +05:30
|
|
|
CONFLICT_MARKER_OUR,
|
|
|
|
CONFLICT_MARKER_THEIR,
|
|
|
|
CONFLICT_THEIR,
|
|
|
|
CONFLICT_OUR,
|
2022-07-16 23:28:13 +05:30
|
|
|
EXPANDED_LINE_TYPE,
|
2021-01-03 14:25:43 +05:30
|
|
|
} from '../constants';
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
export const isHighlighted = (highlightedRow, line, isCommented) => {
|
2021-01-03 14:25:43 +05:30
|
|
|
if (isCommented) return true;
|
|
|
|
|
|
|
|
const lineCode = line?.line_code;
|
2021-09-30 23:02:18 +05:30
|
|
|
return lineCode ? lineCode === highlightedRow : false;
|
2021-01-03 14:25:43 +05:30
|
|
|
};
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const isContextLine = (type) => type === CONTEXT_LINE_TYPE;
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const isMatchLine = (type) => type === MATCH_LINE_TYPE;
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const isMetaLine = (type) =>
|
2021-01-03 14:25:43 +05:30
|
|
|
[OLD_NO_NEW_LINE_TYPE, NEW_NO_NEW_LINE_TYPE, EMPTY_CELL_TYPE].includes(type);
|
|
|
|
|
|
|
|
export const shouldRenderCommentButton = (isLoggedIn, isCommentButtonRendered) => {
|
|
|
|
return isCommentButtonRendered && isLoggedIn;
|
|
|
|
};
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const hasDiscussions = (line) => line?.discussions?.length > 0;
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const lineHref = (line) => `#${line?.line_code || ''}`;
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const lineCode = (line) => {
|
2021-01-03 14:25:43 +05:30
|
|
|
if (!line) return undefined;
|
|
|
|
return line.line_code || line.left?.line_code || line.right?.line_code;
|
|
|
|
};
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
export const classNameMapCell = ({
|
|
|
|
line,
|
|
|
|
highlighted,
|
|
|
|
commented,
|
|
|
|
selectionStart,
|
|
|
|
selectionEnd,
|
|
|
|
isLoggedIn,
|
|
|
|
isHover,
|
|
|
|
}) => {
|
|
|
|
const classes = {
|
|
|
|
'highlight-top': highlighted || selectionStart,
|
|
|
|
'highlight-bottom': highlighted || selectionEnd,
|
|
|
|
hll: highlighted,
|
|
|
|
commented,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (line) {
|
|
|
|
const { type } = line;
|
|
|
|
Object.assign(classes, {
|
|
|
|
[type]: true,
|
2021-01-03 14:25:43 +05:30
|
|
|
[LINE_HOVER_CLASS_NAME]: isLoggedIn && isHover && !isContextLine(type) && !isMetaLine(type),
|
2023-04-23 21:23:45 +05:30
|
|
|
old_line: type === 'old',
|
|
|
|
new_line: type === 'new',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return [classes];
|
2021-01-03 14:25:43 +05:30
|
|
|
};
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
export const addCommentTooltip = (line) => {
|
2021-01-03 14:25:43 +05:30
|
|
|
let tooltip;
|
2023-01-13 00:05:48 +05:30
|
|
|
if (!line) {
|
|
|
|
return tooltip;
|
|
|
|
}
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
tooltip = __('Add a comment to this line or drag for multiple lines');
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
if (!line.problems) {
|
|
|
|
return tooltip;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { brokenSymlink, brokenLineCode, fileOnlyMoved } = line.problems;
|
|
|
|
|
|
|
|
if (brokenSymlink) {
|
|
|
|
if (brokenSymlink.wasSymbolic || brokenSymlink.isSymbolic) {
|
2021-01-03 14:25:43 +05:30
|
|
|
tooltip = __(
|
2023-01-13 00:05:48 +05:30
|
|
|
'Commenting on symbolic links that replace or are replaced by files is not supported',
|
2021-01-03 14:25:43 +05:30
|
|
|
);
|
2023-01-13 00:05:48 +05:30
|
|
|
} else if (brokenSymlink.wasReal || brokenSymlink.isReal) {
|
2021-01-03 14:25:43 +05:30
|
|
|
tooltip = __(
|
2023-01-13 00:05:48 +05:30
|
|
|
'Commenting on files that replace or are replaced by symbolic links is not supported',
|
2021-01-03 14:25:43 +05:30
|
|
|
);
|
|
|
|
}
|
2023-01-13 00:05:48 +05:30
|
|
|
} else if (fileOnlyMoved) {
|
|
|
|
tooltip = __('Commenting on files that are only moved or renamed is not supported');
|
|
|
|
} else if (brokenLineCode) {
|
|
|
|
tooltip = __('Commenting on this line is not supported');
|
2021-01-03 14:25:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return tooltip;
|
|
|
|
};
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
export const parallelViewLeftLineType = ({
|
|
|
|
line,
|
|
|
|
highlighted,
|
|
|
|
commented,
|
|
|
|
selectionStart,
|
|
|
|
selectionEnd,
|
|
|
|
}) => {
|
2021-01-03 14:25:43 +05:30
|
|
|
if (line?.right?.type === NEW_NO_NEW_LINE_TYPE) {
|
|
|
|
return OLD_NO_NEW_LINE_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const lineTypeClass = line?.left ? line.left.type : EMPTY_CELL_TYPE;
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
return [
|
|
|
|
lineTypeClass,
|
|
|
|
{
|
|
|
|
hll: highlighted,
|
|
|
|
commented,
|
|
|
|
'highlight-top': highlighted || selectionStart,
|
|
|
|
'highlight-bottom': highlighted || selectionEnd,
|
|
|
|
},
|
|
|
|
];
|
2021-01-03 14:25:43 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export const shouldShowCommentButton = (hover, context, meta, discussions) => {
|
|
|
|
return hover && !context && !meta && !discussions;
|
|
|
|
};
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const mapParallel = (content) => (line) => {
|
2021-01-29 00:20:46 +05:30
|
|
|
let { left, right } = line;
|
|
|
|
|
|
|
|
// Dicussions/Comments
|
|
|
|
const hasExpandedDiscussionOnLeft =
|
|
|
|
left?.discussions?.length > 0 ? left?.discussionsExpanded : false;
|
|
|
|
const hasExpandedDiscussionOnRight =
|
|
|
|
right?.discussions?.length > 0 ? right?.discussionsExpanded : false;
|
|
|
|
|
|
|
|
const renderCommentRow =
|
|
|
|
hasExpandedDiscussionOnLeft || hasExpandedDiscussionOnRight || left?.hasForm || right?.hasForm;
|
|
|
|
|
|
|
|
if (left) {
|
|
|
|
left = {
|
|
|
|
...left,
|
|
|
|
renderDiscussion: hasExpandedDiscussionOnLeft,
|
|
|
|
hasDraft: content.hasParallelDraftLeft(content.diffFile.file_hash, line),
|
2022-11-25 23:54:43 +05:30
|
|
|
lineDrafts: content.draftsForLine(content.diffFile.file_hash, line, 'left'),
|
2021-01-29 00:20:46 +05:30
|
|
|
hasCommentForm: left.hasForm,
|
2021-09-30 23:02:18 +05:30
|
|
|
isConflictMarker:
|
|
|
|
line.left.type === CONFLICT_MARKER_OUR || line.left.type === CONFLICT_MARKER_THEIR,
|
|
|
|
emptyCellClassMap: { conflict_our: line.right?.type === CONFLICT_THEIR },
|
|
|
|
addCommentTooltip: addCommentTooltip(line.left),
|
2021-01-29 00:20:46 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
if (right) {
|
|
|
|
right = {
|
|
|
|
...right,
|
2022-07-16 23:28:13 +05:30
|
|
|
renderDiscussion: Boolean(
|
|
|
|
hasExpandedDiscussionOnRight && right.type && right.type !== EXPANDED_LINE_TYPE,
|
|
|
|
),
|
2021-01-29 00:20:46 +05:30
|
|
|
hasDraft: content.hasParallelDraftRight(content.diffFile.file_hash, line),
|
2022-11-25 23:54:43 +05:30
|
|
|
lineDrafts: content.draftsForLine(content.diffFile.file_hash, line, 'right'),
|
2022-07-16 23:28:13 +05:30
|
|
|
hasCommentForm: Boolean(right.hasForm && right.type && right.type !== EXPANDED_LINE_TYPE),
|
2021-09-30 23:02:18 +05:30
|
|
|
emptyCellClassMap: { conflict_their: line.left?.type === CONFLICT_OUR },
|
|
|
|
addCommentTooltip: addCommentTooltip(line.right),
|
2021-01-29 00:20:46 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...line,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
isMatchLineLeft: isMatchLine(left?.type),
|
|
|
|
isMatchLineRight: isMatchLine(right?.type),
|
|
|
|
isContextLineLeft: isContextLine(left?.type),
|
|
|
|
isContextLineRight: isContextLine(right?.type),
|
|
|
|
hasDiscussionsLeft: hasDiscussions(left),
|
|
|
|
hasDiscussionsRight: hasDiscussions(right),
|
|
|
|
lineHrefOld: lineHref(left),
|
|
|
|
lineHrefNew: lineHref(right),
|
|
|
|
lineCode: lineCode(line),
|
|
|
|
isMetaLineLeft: isMetaLine(left?.type),
|
|
|
|
isMetaLineRight: isMetaLine(right?.type),
|
2022-11-25 23:54:43 +05:30
|
|
|
draftRowClasses: left?.hasDraft || right?.hasDraft ? '' : 'js-temp-notes-holder',
|
2021-01-29 00:20:46 +05:30
|
|
|
renderCommentRow,
|
|
|
|
commentRowClasses: hasDiscussions(left) || hasDiscussions(right) ? '' : 'js-temp-notes-holder',
|
|
|
|
};
|
|
|
|
};
|