Skip to content

Commit 152107b

Browse files
committed
feat(CodeReview): 抛出选中事件
1 parent c673ccf commit 152107b

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

packages/devui-vue/devui/code-review/src/code-review.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import './code-review.scss';
1414
export default defineComponent({
1515
name: 'DCodeReview',
1616
props: codeReviewProps,
17-
emits: ['foldChange', 'addComment', 'afterViewInit', 'contentRefresh'],
17+
emits: ['foldChange', 'addComment', 'afterViewInit', 'contentRefresh', 'afterCheckLines'],
1818
setup(props: CodeReviewProps, ctx: SetupContext) {
1919
const ns = useNamespace('code-review');
2020
const { diffType } = toRefs(props);

packages/devui-vue/devui/code-review/src/composables/use-code-review-comment.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
export function useCodeReviewComment(reviewContentRef: Ref<HTMLElement>, props: CodeReviewProps, ctx: SetupContext) {
1515
const { outputFormat, allowComment, allowChecked } = toRefs(props);
1616
const ns = useNamespace('code-review');
17-
const { onMousedown } = useCodeReviewLineSelection(reviewContentRef, props, updateLineNumbers, updateLineNumbers);
17+
const { onMousedown } = useCodeReviewLineSelection(reviewContentRef, props, updateLineNumbers, updateLineNumbers, afterCheckLines);
1818
const commentLeft = ref(-100);
1919
const commentTop = ref(-100);
2020
let currentLeftLineNumber = -1;
@@ -24,6 +24,8 @@ export function useCodeReviewComment(reviewContentRef: Ref<HTMLElement>, props:
2424
let currentLeftLineNumbers: Array<number> = [];
2525
let currentRightLineNumbers: Array<number> = [];
2626
let checkedLineCodeString: Array<string> | Record<string, Array<string>> = {};
27+
let allTrNodes: NodeListOf<Element> = [];
28+
let afterCheckLinesEmitData: Record<string, any>;
2729
watch(
2830
() => outputFormat.value,
2931
() => {
@@ -137,11 +139,9 @@ export function useCodeReviewComment(reviewContentRef: Ref<HTMLElement>, props:
137139
};
138140
// 获取一些公共类和判断
139141
const getCommonClassAndJudge = () => {
140-
const lineClassName = props.outputFormat === 'line-by-line' ? '.d2h-code-linenumber' : '.d2h-code-side-linenumber';
141-
const linenumberDom = reviewContentRef.value.querySelectorAll(lineClassName);
142142
const checkedLine = [currentLeftLineNumbers, currentRightLineNumbers];
143143
return {
144-
linenumberDom,
144+
linenumberDom: allTrNodes,
145145
checkedLine,
146146
};
147147
};
@@ -217,6 +217,15 @@ export function useCodeReviewComment(reviewContentRef: Ref<HTMLElement>, props:
217217
currentRightLineNumbers =
218218
currentRightLineNumber === -1 ? currentRightLineNumbers : getLineNumbers(currentRightLineNumber, currentRightLineNumbers);
219219
getCheckedLineCode(false);
220+
afterCheckLinesEmitData = {
221+
left: currentLeftLineNumber,
222+
right: currentRightLineNumber,
223+
details: {
224+
lefts: currentLeftLineNumbers,
225+
rights: currentRightLineNumbers,
226+
codes: checkedLineCodeString,
227+
},
228+
};
220229
}
221230
const updateCheckedLineClass = () => {
222231
getCheckedLineCode(true);
@@ -266,6 +275,9 @@ export function useCodeReviewComment(reviewContentRef: Ref<HTMLElement>, props:
266275
// 点击添加评论图标触发的事件
267276
ctx.emit('addComment', obj);
268277
};
278+
function afterCheckLines() {
279+
ctx.emit('afterCheckLines', afterCheckLinesEmitData);
280+
}
269281
// 图标或者单行的点击
270282
const onCommentIconClick = (e: Event) => {
271283
if (e) {
@@ -327,13 +339,19 @@ export function useCodeReviewComment(reviewContentRef: Ref<HTMLElement>, props:
327339
resetCommentClass();
328340
};
329341

342+
const handleMouseDown = (e: MouseEvent) => {
343+
const lineClassName = props.outputFormat === 'line-by-line' ? '.d2h-code-linenumber' : '.d2h-code-side-linenumber';
344+
allTrNodes = reviewContentRef.value.querySelectorAll(lineClassName);
345+
onMousedown(e);
346+
};
347+
330348
const mouseEvent: Record<string, (e: MouseEvent) => void> = {};
331349
if (allowComment.value) {
332350
mouseEvent.onMousemove = onMouseMove;
333351
mouseEvent.onMouseleave = onMouseleave;
334352
}
335353
if (props.allowChecked) {
336-
mouseEvent.onMousedown = onMousedown;
354+
mouseEvent.onMousedown = handleMouseDown;
337355
}
338356

339357
window.addEventListener('scroll', resetLeftTop);

packages/devui-vue/devui/code-review/src/composables/use-code-review-line-selection.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ export function useCodeReviewLineSelection(
77
reviewContentRef: Ref<HTMLElement>,
88
props: CodeReviewProps,
99
mouseDownCb: () => void,
10+
mouseMoveCb: () => void,
1011
mouseupCb: () => void
1112
) {
1213
const ns = useNamespace('code-review');
1314
let dragging = false;
1415
let startTrNode: HTMLElement;
1516
let trNodes: HTMLElement[];
1617
let isClickedLeft: boolean | undefined;
18+
let shouldClear: boolean;
1719
let isMouseMoved: boolean;
1820

1921
const onMousedown = (e: MouseEvent) => {
@@ -41,6 +43,7 @@ export function useCodeReviewLineSelection(
4143
}
4244

4345
dragging = true;
46+
shouldClear = true;
4447
isMouseMoved = false;
4548
mouseDownCb();
4649
e.preventDefault();
@@ -55,6 +58,10 @@ export function useCodeReviewLineSelection(
5558
return;
5659
}
5760
isMouseMoved = true;
61+
if (shouldClear) {
62+
clearCommentChecked();
63+
shouldClear = false;
64+
}
5865
const composedPath = e.composedPath() as HTMLElement[];
5966
const inReviewContent = composedPath.some((item) => item.classList?.contains(ns.e('content')));
6067
if (!inReviewContent) {
@@ -69,6 +76,7 @@ export function useCodeReviewLineSelection(
6976
if (endIndex === -1) {
7077
return;
7178
}
79+
mouseMoveCb();
7280
if (startIndex > endIndex) {
7381
[startIndex, endIndex] = [endIndex, startIndex];
7482
}
@@ -99,6 +107,13 @@ export function useCodeReviewLineSelection(
99107
document.removeEventListener('mousemove', onMousemove);
100108
}
101109

110+
// 清除上次的选中
111+
function clearCommentChecked() {
112+
for (let i = 0; i < trNodes.length; i++) {
113+
toggleCommentCheckedClass(trNodes[i], false, 'all');
114+
}
115+
}
116+
102117
function toggleCommentCheckedClass(trNode: HTMLElement, isAddClass: boolean, position: 'left' | 'right' | 'all') {
103118
const tdNodes = Array.from(trNode.children);
104119
let toDoNodes;
@@ -109,7 +124,7 @@ export function useCodeReviewLineSelection(
109124
} else {
110125
toDoNodes = tdNodes.slice(2);
111126
}
112-
if ((position === 'left' || position === 'right') && isNaN(parseInt(toDoNodes[0].innerHTML))) {
127+
if ((position === 'left' || position === 'right') && isNaN(parseInt(toDoNodes[0]?.innerHTML))) {
113128
return;
114129
}
115130
toDoNodes.forEach((item) => {

packages/devui-vue/docs/components/code-review/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ export default defineComponent({
597597
| add-comment | `Function(position: CommentPosition)` | 点击添加评论图标时触发的事件,参数内容详见[CommentPosition](#commentposition) |
598598
| after-view-init | `Function(methods: CodeReviewMethods)` | 初始化完成后触发的事件,返回相关操作方法,参数内容详见[CodeReviewMethods](#codereviewmethods) |
599599
| content-refresh | `Function(diffFile: DiffFile)` | 内容刷新后触发的事件,返回解析后的相关文件信息,参数内容详见[DiffFile](https://github.com/rtfpessoa/diff2html/blob/master/src/types.ts#L49) |
600+
|after-check-lines|`Function(position: CommentPosition)`|多行选中后触发的事件,参数内容详见[CommentPosition](#commentposition)|
600601

601602
### CodeReview 插槽
602603

0 commit comments

Comments
 (0)