@@ -19,7 +19,7 @@ export const getPlainDiffTemplate = ({
1919 rawLine : string ;
2020 operator : "add" | "del" ;
2121} ) => {
22- if ( diffLine . plainTemplate ) return ;
22+ if ( diffLine . plainTemplate && diffLine . plainTemplateMode === "relative" ) return ;
2323
2424 const changes = diffLine . changes ;
2525
@@ -28,22 +28,78 @@ export const getPlainDiffTemplate = ({
2828 const transform = isTransformEnabled ( ) ? processTransformTemplateContent : defaultTransform ;
2929
3030 const range = changes . range ;
31+
3132 const str1 = rawLine . slice ( 0 , range . location ) ;
33+
3234 const str2 = rawLine . slice ( range . location , range . location + range . length ) ;
35+
3336 const str3 = rawLine . slice ( range . location + range . length ) ;
37+
3438 const isLast = str2 . includes ( "\n" ) ;
35- const _str2 = isLast ? str2 . replace ( "\n" , "" ) . replace ( "\r" , "" ) : str2 ;
39+
3640 const isNewLineSymbolChanged = changes . newLineSymbol ;
3741
38- const template = `<span data-range-start="${ range . location } " data-range-end="${
39- range . location + range . length
40- } ">${ transform ( str1 ) } <span data-diff-highlight style="background-color: var(${ operator === "add" ? addContentHighlightBGName : delContentHighlightBGName } );border-radius: 0.2em;">${
41- isLast
42- ? `${ transform ( _str2 ) } <span data-newline-symbol>${ getSymbol ( isNewLineSymbolChanged ) } </span>`
43- : transform ( str2 )
44- } </span>${ transform ( str3 ) } </span>`;
42+ let template = `<span data-range-start="${ range . location } " data-range-end="${ range . location + range . length } ">` ;
43+
44+ template += transform ( str1 ) ;
45+
46+ template += `<span data-diff-highlight style="background-color: var(${ operator === "add" ? addContentHighlightBGName : delContentHighlightBGName } );border-radius: 0.2em;">` ;
47+
48+ template += isLast
49+ ? `${ transform ( str2 ) } <span data-newline-symbol>${ getSymbol ( isNewLineSymbolChanged ) } </span>`
50+ : transform ( str2 ) ;
51+
52+ template += `</span>` ;
53+
54+ template += transform ( str3 ) ;
55+
56+ template += `</span>` ;
57+
58+ diffLine . plainTemplate = template ;
59+
60+ diffLine . plainTemplateMode = "relative" ;
61+ } ;
62+
63+ export const getPlainDiffTemplateByFastDiff = ( {
64+ diffLine,
65+ rawLine,
66+ operator,
67+ } : {
68+ diffLine : DiffLine ;
69+ rawLine : string ;
70+ operator : "add" | "del" ;
71+ } ) => {
72+ const changes = diffLine . diffChanges ;
73+
74+ if ( ! changes || ! changes . hasLineChange || ! rawLine ) return ;
75+
76+ const transform = isTransformEnabled ( ) ? processTransformTemplateContent : defaultTransform ;
77+
78+ let template = `` ;
79+
80+ changes . range . forEach ( ( { type, str, location, length } , index , array ) => {
81+ const isLatest = index === array . length - 1 ;
82+ if ( type === 0 ) {
83+ template += `<span>${ transform ( str ) } ` ;
84+ template +=
85+ isLatest && changes . newLineSymbol
86+ ? `<span data-newline-symbol data-diff-highlight style="background-color: var(${ operator === "add" ? addContentHighlightBGName : delContentHighlightBGName } );border-radius: 0.2em;">${ getSymbol ( changes . newLineSymbol ) } </span>`
87+ : "" ;
88+ template += `</span>` ;
89+ } else {
90+ template += `<span data-range-start="${ location } " data-range-end="${ location + length } ">` ;
91+ template += `<span data-diff-highlight style="background-color: var(${ operator === "add" ? addContentHighlightBGName : delContentHighlightBGName } );border-radius: 0.2em;">${ transform ( str ) } ` ;
92+ template +=
93+ isLatest && changes . newLineSymbol
94+ ? `<span data-newline-symbol data-diff-highlight>${ getSymbol ( changes . newLineSymbol ) } </span>`
95+ : "" ;
96+ template += `</span></span>` ;
97+ }
98+ } ) ;
4599
46100 diffLine . plainTemplate = template ;
101+
102+ diffLine . plainTemplateMode = "fast-diff" ;
47103} ;
48104
49105export const getSyntaxDiffTemplate = ( {
@@ -55,7 +111,9 @@ export const getSyntaxDiffTemplate = ({
55111 syntaxLine : SyntaxLineWithTemplate ;
56112 operator : "add" | "del" ;
57113} ) => {
58- if ( diffLine . syntaxTemplate || ! syntaxLine ) return ;
114+ if ( ! syntaxLine ) return ;
115+
116+ if ( diffLine . syntaxTemplate && diffLine . syntaxTemplateMode === "relative" ) return ;
59117
60118 const changes = diffLine . changes ;
61119
@@ -74,21 +132,28 @@ export const getSyntaxDiffTemplate = ({
74132 ) ?. join ( " " ) } " style="${ wrapper ?. properties ?. style || "" } ">${ transform ( node . value ) } </span>`;
75133 } else {
76134 const index1 = range . location - node . startIndex ;
135+
77136 const index2 = index1 < 0 ? 0 : index1 ;
137+
78138 const str1 = node . value . slice ( 0 , index2 ) ;
139+
79140 const str2 = node . value . slice ( index2 , index1 + range . length ) ;
141+
80142 const str3 = node . value . slice ( index1 + range . length ) ;
143+
81144 const isStart = str1 . length || range . location === node . startIndex ;
145+
82146 const isEnd = str3 . length || node . endIndex === range . location + range . length - 1 ;
147+
83148 const isLast = str2 . includes ( "\n" ) ;
84- const _str2 = isLast ? str2 . replace ( "\n" , "" ) . replace ( "\r" , "" ) : str2 ;
149+
85150 template += `<span data-start="${ node . startIndex } " data-end="${ node . endIndex } " class="${ (
86151 wrapper ?. properties ?. className || [ ]
87152 ) ?. join (
88153 " "
89154 ) } " style="${ wrapper ?. properties ?. style || "" } ">${ transform ( str1 ) } <span data-diff-highlight style="background-color: var(${ operator === "add" ? addContentHighlightBGName : delContentHighlightBGName } );border-top-left-radius: ${ isStart ? "0.2em" : "0" } ;border-bottom-left-radius: ${ isStart ? "0.2em" : "0" } ;border-top-right-radius: ${ isEnd || isLast ? "0.2em" : "0" } ;border-bottom-right-radius: ${ isEnd || isLast ? "0.2em" : "0" } ">${
90155 isLast
91- ? `${ transform ( _str2 ) } <span data-newline-symbol>${ getSymbol ( changes . newLineSymbol ) } </span>`
156+ ? `${ transform ( str2 ) } <span data-newline-symbol>${ getSymbol ( changes . newLineSymbol ) } </span>`
92157 : transform ( str2 )
93158 } </span>${ transform ( str3 ) } </span>`;
94159 }
@@ -97,6 +162,8 @@ export const getSyntaxDiffTemplate = ({
97162 template += "</span>" ;
98163
99164 diffLine . syntaxTemplate = template ;
165+
166+ diffLine . syntaxTemplateMode = "relative" ;
100167} ;
101168
102169export const getSyntaxLineTemplate = ( line : SyntaxLine ) => {
0 commit comments