Skip to content

Commit 3160137

Browse files
authored
feat(CodeReview): 支持传入options&暴露清除选中行样式的方法 (#1912) (#1913)
* feat(CodeReview): 支持传入options&暴露清除选中行样式的方法
1 parent 76c27dd commit 3160137

File tree

8 files changed

+46
-14
lines changed

8 files changed

+46
-14
lines changed

packages/devui-vue/devui/code-review/src/code-review-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export const codeReviewProps = {
5858
expandLoader: {
5959
type: Function as PropType<(interval: Array<number | undefined>, update: (code: string) => void) => void>,
6060
},
61+
options: {
62+
type: Object as PropType<Record<string, any>>,
63+
default: () => ({}),
64+
},
6165
};
6266
export type CodeReviewProps = ExtractPropTypes<typeof codeReviewProps>;
6367

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export default defineComponent({
2424
mouseEvent, onCommentMouseLeave,
2525
onCommentIconClick, onCommentKeyDown,
2626
unCommentKeyDown, insertComment,
27-
removeComment, updateCheckedLineClass } = useCodeReviewComment(reviewContentRef, props, ctx);
27+
removeComment, updateCheckedLineClass, clearCheckedLines } = useCodeReviewComment(reviewContentRef, props, ctx);
2828

2929
onMounted(() => {
30-
ctx.emit('afterViewInit', { toggleFold, insertComment, removeComment, updateCheckedLineClass });
30+
ctx.emit('afterViewInit', { toggleFold, insertComment, removeComment, updateCheckedLineClass, clearCheckedLines });
3131
onCommentKeyDown();
3232
});
3333
// 销毁

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@ export function useCodeReviewComment(reviewContentRef: Ref<HTMLElement>, props:
338338
}
339339
};
340340

341+
const clearCheckedLines = () => {
342+
currentLeftLineNumbers = [];
343+
currentRightLineNumbers = [];
344+
checkedLineCodeString = [];
345+
resetCommentClass();
346+
};
347+
341348
const mouseEvent = allowComment.value ? { onMousemove: onMouseMove, onMouseleave: onMouseleave } : {};
342349

343350
window.addEventListener('scroll', resetLeftTop);
@@ -353,6 +360,7 @@ export function useCodeReviewComment(reviewContentRef: Ref<HTMLElement>, props:
353360
// currentLeftLineNumbers,
354361
// currentRightLineNumbers,
355362
updateCheckedLineClass,
363+
clearCheckedLines,
356364
onCommentMouseLeave,
357365
onCommentIconClick,
358366
onCommentKeyDown,

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ export function useCodeReviewExpand(reviewContentRef: Ref<HTMLElement>, props: C
5858
attachExpandUpDownButton(loadMoreLine.children[0] as HTMLElement, 'down');
5959
};
6060

61-
const insertIncrementCodeForDoubleColumn = (code: string, direction: 'up' | 'down', referenceDom: HTMLElement | null | undefined) => {
61+
const insertIncrementCodeForDoubleColumn = (
62+
code: string,
63+
direction: 'up' | 'down',
64+
referenceDom: HTMLElement | null | undefined,
65+
options: Record<string, any>
66+
) => {
6267
if (!referenceDom) {
6368
return;
6469
}
@@ -69,7 +74,7 @@ export function useCodeReviewExpand(reviewContentRef: Ref<HTMLElement>, props: C
6974
const prefix = '--- updated_at\tJan 1, 2019, 0:0:0 AM\n+++ updated_at\tJan 1, 2019, 0:0:0 AM\n';
7075
const container = document.createElement('div');
7176
// 解析code
72-
parseDiffCode(container, prefix + code, outputFormat.value, true);
77+
parseDiffCode(container, prefix + code, outputFormat.value, options, true);
7378

7479
const trNodes = Array.from(container.querySelectorAll('tr'));
7580
const expandLine = trNodes.find((element) => (element.children[1] as HTMLElement)?.innerText.trim().match(ExpandLineReg));
@@ -158,7 +163,12 @@ export function useCodeReviewExpand(reviewContentRef: Ref<HTMLElement>, props: C
158163
attachExpandUpDownButton(loadMoreLine.children[0] as HTMLElement, 'down');
159164
};
160165

161-
const insertIncrementCode = (code: string, direction: 'up' | 'down', referenceDom: HTMLElement | null | undefined) => {
166+
const insertIncrementCode = (
167+
code: string,
168+
direction: 'up' | 'down',
169+
referenceDom: HTMLElement | null | undefined,
170+
options: Record<string, any>
171+
) => {
162172
if (!referenceDom) {
163173
return;
164174
}
@@ -169,7 +179,7 @@ export function useCodeReviewExpand(reviewContentRef: Ref<HTMLElement>, props: C
169179
const prefix = '--- updated_at\tJan 1, 2019, 0:0:0 AM\n+++ updated_at\tJan 1, 2019, 0:0:0 AM\n';
170180
const container = document.createElement('div');
171181
// 解析code
172-
parseDiffCode(container, prefix + code, outputFormat.value, true);
182+
parseDiffCode(container, prefix + code, outputFormat.value, options, true);
173183

174184
const trNodes = Array.from(container.querySelectorAll('tr'));
175185
const expandLine = trNodes.find((element) => (element.children[1] as HTMLElement)?.innerText.trim().match(ExpandLineReg));
@@ -213,7 +223,7 @@ export function useCodeReviewExpand(reviewContentRef: Ref<HTMLElement>, props: C
213223
}
214224
};
215225

216-
const onExpandButtonClick = (e: Event) => {
226+
const onExpandButtonClick = (e: Event, options: Record<string, any>) => {
217227
const composedPath = e.composedPath() as HTMLElement[];
218228
const expandIconDom = composedPath.find((element) => element.classList?.contains('expand-icon'));
219229
if (expandIconDom) {
@@ -224,8 +234,8 @@ export function useCodeReviewExpand(reviewContentRef: Ref<HTMLElement>, props: C
224234
const [leftLineStart, leftLineEnd, rightLineStart, rightLineEnd] = getLineNumberFromDataset(expandIconDom, expandThreshold.value);
225235
expandLoader?.value?.([leftLineStart, leftLineEnd, rightLineStart, rightLineEnd], (code: string) => {
226236
outputFormat.value === 'line-by-line'
227-
? insertIncrementCode(code, direction, expandIconDom.parentElement?.parentElement)
228-
: insertIncrementCodeForDoubleColumn(code, direction, expandIconDom.parentElement?.parentElement);
237+
? insertIncrementCode(code, direction, expandIconDom.parentElement?.parentElement, options)
238+
: insertIncrementCodeForDoubleColumn(code, direction, expandIconDom.parentElement?.parentElement, options);
229239
});
230240
}
231241
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ export function useCodeReview(props: CodeReviewProps, ctx: SetupContext) {
1818
diffFile.value = Diff2Html.parse(diff.value);
1919
nextTick(() => {
2020
if (inBrowser && !showBlob.value) {
21-
parseDiffCode(reviewContentRef.value, diff.value, outputFormat.value);
21+
parseDiffCode(reviewContentRef.value, diff.value, outputFormat.value, props.options);
2222
allowExpand.value && insertExpandButton();
2323
ctx.emit('contentRefresh', JSON.parse(JSON.stringify(diffFile.value)));
2424
}
2525
});
2626
};
2727

2828
const onContentClick = (e: Event) => {
29-
onExpandButtonClick(e);
29+
onExpandButtonClick(e, props.options);
3030
};
3131

3232
watch(showBlob, initDiffContent);

packages/devui-vue/devui/code-review/src/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,20 @@ function addClassToDiffCode(codeStrArr: RegExpMatchArray | null, theClassName: s
159159
}
160160

161161
// 解析diff
162-
export function parseDiffCode(container: HTMLElement, code: string, outputFormat: OutputFormat, isAddCode = false) {
162+
export function parseDiffCode(
163+
container: HTMLElement,
164+
code: string,
165+
outputFormat: OutputFormat,
166+
options: Record<string, any>,
167+
isAddCode = false
168+
) {
163169
const diff2HtmlUi = new Diff2HtmlUI(container, code, {
164170
drawFileList: false,
165171
matching: 'lines',
166172
outputFormat: outputFormat,
167173
highlight: true,
168-
diffStyle: 'char',
169174
rawTemplates: TemplateMap[outputFormat],
175+
...options,
170176
});
171177
if (outputFormat === 'side-by-side') {
172178
let diffHtmlStr = diff2HtmlUi.diffHtml;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ export default defineComponent({
587587
| allow-expand | `boolean` | true | 可选,是否支持展开非 diff 折叠代码 |
588588
| expand-threshold | `number` | 50 | 可选,展开所有代码行的阈值,低于此阈值全部展开,高于此阈值分向上和向下两个操作展开 |
589589
| expand-loader | `(interval: Array<number>, update: (code: string) => void) => void` | -- | 可选,展开代码回调函数,interval 为展开边界,获取展开代码后,执行 update 更新视图 |
590+
|options|`object`|{}|可选,传给`diff2html`的配置项|
590591

591592
### CodeReview 事件
592593

@@ -667,5 +668,8 @@ interface CodeReviewMethods {
667668

668669
// 更新选中行样式,直接调用一般用于展开时更新选中行样式,像示例中一样使用
669670
updateCheckedLineClass: ();
671+
672+
// 清除选中行样式
673+
clearCheckedLines: () => void;
670674
}
671675
```

packages/devui-vue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-devui",
3-
"version": "1.6.22",
3+
"version": "1.6.23",
44
"license": "MIT",
55
"description": "DevUI components based on Vite and Vue3",
66
"keywords": [

0 commit comments

Comments
 (0)