Skip to content

Commit 72735cb

Browse files
支持点击高亮部分自动添加打回理由
1 parent b23322a commit 72735cb

File tree

8 files changed

+98
-50
lines changed

8 files changed

+98
-50
lines changed

src/app/features/article/articleViewer/getProcessor.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import rehypeHighlight from 'rehype-highlight';
44
import { visit, SKIP } from 'unist-util-visit';
55
import luoguMarkdownProcessor from 'lg-markdown-processor';
66
import { Element, ElementContent, Text } from 'hast';
7+
import * as HighlightElement from './highlightElement';
78

89
function checkClass(element: Element, target: string) {
910
let className = element.properties['className'];
@@ -16,7 +17,7 @@ function checkClass(element: Element, target: string) {
1617
}
1718

1819
const rehypeReactConfig: import('hast-util-to-jsx-runtime').Options = {
19-
Fragment: 'article',
20+
Fragment: props.Fragment,
2021
// @ts-expect-error
2122
jsx: props.jsx,
2223
// @ts-expect-error
@@ -36,32 +37,6 @@ namespace CharTest {
3637
export const Punctuation = /\p{P}/u;
3738
export const EnglishChar = /[a-zA-Z]/;
3839
}
39-
namespace HighlightElement {
40-
export const Space: Element = {
41-
type: 'element',
42-
tagName: 'span',
43-
properties: { className: ['articleHighlight', 'Space'] },
44-
children: [{ type: 'text', value: ' ' }]
45-
};
46-
export const NeedSpaceBetweenEnglishCharAndChineseChar: Element = {
47-
type: 'element',
48-
tagName: 'span',
49-
properties: {
50-
className: [
51-
'articleHighlight',
52-
'NeedSpaceBetweenPunctuationAndChineseChar'
53-
]
54-
},
55-
children: [
56-
{
57-
type: 'element',
58-
tagName: 'div',
59-
properties: {},
60-
children: []
61-
}
62-
]
63-
};
64-
}
6540
function hastHighlightSpace() {
6641
return (tree: import('hast').Root) =>
6742
visit(tree, 'element', element => {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Element } from 'hast';
2+
3+
function genTitle(title: string) {
4+
return `可能存在的问题:${title}`;
5+
}
6+
7+
export const Space: Element = {
8+
type: 'element',
9+
tagName: 'span',
10+
properties: { className: ['articleHighlight', 'Space'] },
11+
children: [{ type: 'text', value: ' ' }]
12+
};
13+
14+
export const NeedSpaceBetweenEnglishCharAndChineseCharNote =
15+
'【中文】与【英文】字符间需要用空格隔开';
16+
export const NeedSpaceBetweenEnglishCharAndChineseChar: Element = {
17+
type: 'element',
18+
tagName: 'span',
19+
properties: {
20+
className: [
21+
'articleHighlight',
22+
'NeedSpaceBetweenPunctuationAndChineseChar'
23+
],
24+
title: genTitle(NeedSpaceBetweenEnglishCharAndChineseCharNote)
25+
},
26+
children: [
27+
{
28+
type: 'element',
29+
tagName: 'div',
30+
properties: {},
31+
children: []
32+
}
33+
]
34+
};

src/app/features/article/articleViewer/highlightFormatIssue.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
height: 24px;
1414
background-image: url(assets/needSpaceBetweenPunctuationAndChineseChar.svg);
1515
background-size: 100% 100%;
16+
cursor: pointer;
1617
}
1718
}

src/app/features/article/articleViewer/index.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { createElement, memo } from 'react';
2+
import getProcessor from './getProcessor';
3+
import * as HighlightElement from './highlightElement';
4+
5+
import 'katex/dist/katex.css';
6+
import 'highlight.js/styles/base16/tomorrow.css';
7+
import './style.css';
8+
import './highlightFormatIssue.css';
9+
10+
const processor = getProcessor();
11+
12+
const ArticlerRenderer = memo(
13+
({ children: markdown }: { children: string }) =>
14+
processor.processSync(markdown).result
15+
);
16+
17+
export default function ArticleViewer({
18+
markdown,
19+
addCommit
20+
}: {
21+
markdown: string;
22+
addCommit: (content: string) => void;
23+
}) {
24+
return (
25+
<article
26+
onClick={e => {
27+
const target = e.target as HTMLElement;
28+
if (
29+
target.parentElement?.classList.contains(
30+
'NeedSpaceBetweenPunctuationAndChineseChar'
31+
)
32+
) {
33+
addCommit(
34+
HighlightElement.NeedSpaceBetweenEnglishCharAndChineseCharNote
35+
);
36+
return;
37+
}
38+
}}
39+
>
40+
<ArticlerRenderer>{markdown}</ArticlerRenderer>
41+
</article>
42+
);
43+
}

src/app/features/article/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ export default function Article() {
102102
setOtherRefuseCommit('');
103103
updateArticle();
104104
}
105+
function addCommit(s: string) {
106+
setOtherRefuseCommit(
107+
otherRefuseCommit +
108+
(otherRefuseCommit === '' || /||||$/.test(otherRefuseCommit)
109+
? ''
110+
: ';') +
111+
s
112+
);
113+
}
105114

106115
return !fetchError ? (
107116
<div className="articleFeature">
@@ -118,7 +127,10 @@ export default function Article() {
118127
className="articleViewer"
119128
style={{ display: viewSourceCode ? 'none' : 'block' }}
120129
>
121-
<ArticleViewer>{details.article.content}</ArticleViewer>
130+
<ArticleViewer
131+
markdown={details.article.content}
132+
addCommit={addCommit}
133+
/>
122134
</div>
123135
</>
124136
) : (

src/app/features/article/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ div.articleQueueEmpty > span {
8181
column-gap: 15px;
8282
justify-content: flex-end;
8383
align-items: center;
84+
flex-wrap: wrap;
8485
}
8586
.viewOperatorCommit {
8687
margin-left: 1em;

test/articleViewer/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ function MarkdownTestComponent() {
5757
}}
5858
className="articleViewer"
5959
>
60-
<ArticleViewer>{markdownText}</ArticleViewer>
60+
<ArticleViewer
61+
markdown={markdownText}
62+
addCommit={s => console.log('Add commit: ', s)}
63+
></ArticleViewer>
6164
</div>
6265
</div>
6366
</div>

0 commit comments

Comments
 (0)