@@ -10,6 +10,10 @@ export interface Diff2HtmlUIConfig extends Diff2HtmlConfig {
10
10
highlight ?: boolean ;
11
11
fileListToggle ?: boolean ;
12
12
fileListStartVisible ?: boolean ;
13
+ /**
14
+ * @deprecated since version 3.1.0
15
+ * Smart selection is now enabled by default with vanilla CSS
16
+ */
13
17
smartSelection ?: boolean ;
14
18
}
15
19
@@ -19,6 +23,10 @@ export const defaultDiff2HtmlUIConfig = {
19
23
highlight : true ,
20
24
fileListToggle : true ,
21
25
fileListStartVisible : false ,
26
+ /**
27
+ * @deprecated since version 3.1.0
28
+ * Smart selection is now enabled by default with vanilla CSS
29
+ */
22
30
smartSelection : true ,
23
31
} ;
24
32
@@ -44,7 +52,6 @@ export class Diff2HtmlUI {
44
52
45
53
draw ( ) : void {
46
54
this . targetElement . innerHTML = this . diffHtml ;
47
- if ( this . config . smartSelection ) this . smartSelection ( ) ;
48
55
if ( this . config . synchronisedScroll ) this . synchronisedScroll ( ) ;
49
56
if ( this . config . highlight ) this . highlightCode ( ) ;
50
57
if ( this . config . fileListToggle ) this . fileListToggle ( this . config . fileListStartVisible ) ;
@@ -156,38 +163,11 @@ export class Diff2HtmlUI {
156
163
} ) ;
157
164
}
158
165
166
+ /**
167
+ * @deprecated since version 3.1.0
168
+ */
159
169
smartSelection ( ) : void {
160
- const body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
161
- const diffTable = body . getElementsByClassName ( 'd2h-diff-table' ) [ 0 ] ;
162
-
163
- diffTable . addEventListener ( 'mousedown' , event => {
164
- if ( event === null || ! this . isElement ( event . target ) ) return ;
165
-
166
- const table = event . target . closest ( '.d2h-diff-table' ) ;
167
- if ( table !== null ) {
168
- if ( event . target . closest ( '.d2h-code-line,.d2h-code-side-line' ) !== null ) {
169
- table . classList . remove ( 'selecting-left' ) ;
170
- table . classList . add ( 'selecting-right' ) ;
171
- this . currentSelectionColumnId = 1 ;
172
- } else if ( event . target . closest ( '.d2h-code-linenumber,.d2h-code-side-linenumber' ) !== null ) {
173
- table . classList . remove ( 'selecting-right' ) ;
174
- table . classList . add ( 'selecting-left' ) ;
175
- this . currentSelectionColumnId = 0 ;
176
- }
177
- }
178
- } ) ;
179
-
180
- diffTable . addEventListener ( 'copy' , event => {
181
- if ( ! this . isClipboardEvent ( event ) ) return ;
182
-
183
- const clipboardData = event . clipboardData ;
184
- const text = this . getSelectedText ( ) ;
185
-
186
- if ( clipboardData === null || text === undefined ) return ;
187
-
188
- clipboardData . setData ( 'text' , text ) ;
189
- event . preventDefault ( ) ;
190
- } ) ;
170
+ console . warn ( 'Smart selection is now enabled by default with CSS. No need to call this method anymore.' ) ;
191
171
}
192
172
193
173
private instanceOfIHighlightResult ( object : IHighlightResult | IAutoHighlightResult ) : object is IHighlightResult {
@@ -206,37 +186,7 @@ export class Diff2HtmlUI {
206
186
return hashTag ;
207
187
}
208
188
209
- private getSelectedText ( ) : string | undefined {
210
- const sel = window . getSelection ( ) ;
211
-
212
- if ( sel === null ) return ;
213
-
214
- const range = sel . getRangeAt ( 0 ) ;
215
- const doc = range . cloneContents ( ) ;
216
- const nodes = doc . querySelectorAll ( 'tr' ) ;
217
- const idx = this . currentSelectionColumnId ;
218
-
219
- let text = '' ;
220
- if ( nodes . length === 0 ) {
221
- text = doc . textContent || '' ;
222
- } else {
223
- nodes . forEach ( ( tr , i ) => {
224
- const td = tr . cells [ tr . cells . length === 1 ? 0 : idx ] ;
225
-
226
- if ( td === undefined || td . textContent === null ) return ;
227
-
228
- text += ( i ? '\n' : '' ) + td . textContent . replace ( / \r \n | \r | \n / g, '' ) ;
229
- } ) ;
230
- }
231
-
232
- return text ;
233
- }
234
-
235
189
private isElement ( arg ?: unknown ) : arg is Element {
236
190
return arg !== null && ( arg as Element ) ?. classList !== undefined ;
237
191
}
238
-
239
- private isClipboardEvent ( arg ?: unknown ) : arg is ClipboardEvent {
240
- return arg !== null && ( arg as ClipboardEvent ) ?. clipboardData !== undefined ;
241
- }
242
192
}
0 commit comments