Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: add diff function

shuaib97f0fe9d310
diff --git a/ui/package.json b/ui/package.json
index 4dd40263..a26e51bb 100644
--- a/ui/package.json
+++ b/ui/package.json
@@ -19,6 +19,7 @@
"codemirror": "5.65.0",
"copy-to-clipboard": "^3.3.2",
"dayjs": "^1.11.5",
+ "diff": "^5.1.0",
"i18next": "^21.9.0",
"katex": "^0.16.2",
"lodash": "^4.17.21",
diff --git a/ui/pnpm-lock.yaml b/ui/pnpm-lock.yaml
index 78e83835..6f91fd63 100644
--- a/ui/pnpm-lock.yaml
+++ b/ui/pnpm-lock.yaml
@@ -25,6 +25,7 @@ specifiers:
copy-to-clipboard: ^3.3.2
customize-cra: ^1.0.0
dayjs: ^1.11.5
+ diff: ^5.1.0
eslint: ^8.0.1
eslint-config-airbnb: ^19.0.4
eslint-config-airbnb-typescript: ^17.0.0
@@ -72,6 +73,7 @@ dependencies:
codemirror: 5.65.0
copy-to-clipboard: 3.3.2
dayjs: 1.11.5
+ diff: 5.1.0
i18next: 21.9.2
katex: 0.16.2
lodash: 4.17.21
@@ -4742,6 +4744,11 @@ packages:
resolution: {integrity: sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==}
engines: {node: '>=0.3.1'}
+ /diff/5.1.0:
+ resolution: {integrity: sha512-D+mk+qE8VC/PAUrlAU34N+VfXev0ghe5ywmpqrawphmVZc1bEfn56uo9qpyGp1p4xpzOHkSW4ztBd6L7Xx4ACw==}
+ engines: {node: '>=0.3.1'}
+ dev: false
+
/dir-glob/3.0.1:
resolution: {integrity: sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA==}
engines: {node: '>=8'}
diff --git a/ui/src/index.scss b/ui/src/index.scss
index a767e0a0..66b3c299 100644
--- a/ui/src/index.scss
+++ b/ui/src/index.scss
@@ -253,3 +253,18 @@ a {
padding: 1px 0.5rem 2px;
height: 24px;
}
+
+.review-text-delete {
+ color: #842029;
+ background-color: #F8D7DA;
+ text-decoration: line-through;
+ .review-text-add {
+ text-decoration: none;
+ }
+}
+
+.review-text-add {
+ color: #0F5132;
+ background-color: #D1E7DD;
+ text-decoration: none !important;
+}
diff --git a/ui/src/router/routes.ts b/ui/src/router/routes.ts
index be7e61ac..3454738a 100644
--- a/ui/src/router/routes.ts
+++ b/ui/src/router/routes.ts
@@ -201,6 +201,10 @@ const routes: RouteNode[] = [
return guard.forbidden();
},
},
+ {
+ path: '/revision',
+ page: 'pages/Revision',
+ },
// for admin
{
path: 'admin',
diff --git a/ui/src/utils/common.ts b/ui/src/utils/common.ts
index 41622307..46d43ba0 100644
--- a/ui/src/utils/common.ts
+++ b/ui/src/utils/common.ts
@@ -1,5 +1,7 @@
import i18next from 'i18next';
+const Diff = require('diff');
+
function thousandthDivision(num) {
const reg = /\d{1,3}(?=(\d{3})+$)/g;
return `${num}`.replace(reg, '$&,');
@@ -163,6 +165,24 @@ function handleFormError(
return data;
}
+function diffText(newText: string, oldText: string): string {
+ const diff = Diff.diffChars(newText, oldText);
+ const result = diff.map((part) => {
+ if (part.added) {
+ return `<span class="review-text-add">${part.value}</span>`;
+ }
+ if (part.removed) {
+ return `<span class="review-text-delete">${part.value}</span>`;
+ }
+ return part.value;
+ });
+ return result
+ .join('')
+ ?.replace(/\n/gi, '<br>')
+ ?.replace(/<iframe/gi, '&lt;iframe')
+ ?.replace(/<input/gi, '&lt;input');
+}
+
export {
thousandthDivision,
formatCount,
@@ -175,4 +195,5 @@ export {
colorRgb,
labelStyle,
handleFormError,
+ diffText,
};