mirror of https://gitee.com/answerdev/answer.git
Merge pull request #264 from answerdev/feat/1.0.7/ui
fix: diff text optimization
This commit is contained in:
commit
a141550220
|
@ -188,20 +188,27 @@ function handleFormError(
|
|||
return data;
|
||||
}
|
||||
|
||||
function escapeHtml(str: string) {
|
||||
const tagsToReplace = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": ''',
|
||||
'`': '`',
|
||||
};
|
||||
return str.replace(/[&<>"'`]/g, (tag) => tagsToReplace[tag] || tag);
|
||||
}
|
||||
|
||||
function diffText(newText: string, oldText?: string): string {
|
||||
if (!newText) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (typeof oldText !== 'string') {
|
||||
return newText
|
||||
?.replace(/\n/gi, '<br>')
|
||||
?.replace(/<kbd/gi, '<kbd')
|
||||
?.replace(/<\/kbd>/gi, '</kbd>')
|
||||
?.replace(/<iframe/gi, '<iframe')
|
||||
?.replace(/<input/gi, '<input');
|
||||
return escapeHtml(newText?.replace(/\n/gi, '<br>'));
|
||||
}
|
||||
const diff = Diff.diffChars(oldText, newText);
|
||||
const diff = Diff.diffChars(escapeHtml(oldText), escapeHtml(newText));
|
||||
const result = diff.map((part) => {
|
||||
if (part.added) {
|
||||
if (part.value.replace(/\n/g, '').length <= 0) {
|
||||
|
@ -225,12 +232,7 @@ function diffText(newText: string, oldText?: string): string {
|
|||
return part.value;
|
||||
});
|
||||
|
||||
return result
|
||||
.join('')
|
||||
?.replace(/<iframe/gi, '<iframe')
|
||||
?.replace(/<kbd/gi, '<kbd')
|
||||
?.replace(/<\/kbd>/gi, '</kbd>')
|
||||
?.replace(/<input/gi, '<input');
|
||||
return result.join('');
|
||||
}
|
||||
|
||||
function htmlToReact(html: string) {
|
||||
|
|
Loading…
Reference in New Issue