@@ -10,6 +10,10 @@ export interface Diff2HtmlUIConfig extends Diff2HtmlConfig {
1010 highlight ?: boolean ;
1111 fileListToggle ?: boolean ;
1212 fileListStartVisible ?: boolean ;
13+ /**
14+ * @deprecated since version 3.1.0
15+ * Smart selection is now enabled by default with vanilla CSS
16+ */
1317 smartSelection ?: boolean ;
1418}
1519
@@ -19,6 +23,10 @@ export const defaultDiff2HtmlUIConfig = {
1923 highlight : true ,
2024 fileListToggle : true ,
2125 fileListStartVisible : false ,
26+ /**
27+ * @deprecated since version 3.1.0
28+ * Smart selection is now enabled by default with vanilla CSS
29+ */
2230 smartSelection : true ,
2331} ;
2432
@@ -44,7 +52,6 @@ export class Diff2HtmlUI {
4452
4553 draw ( ) : void {
4654 this . targetElement . innerHTML = this . diffHtml ;
47- if ( this . config . smartSelection ) this . smartSelection ( ) ;
4855 if ( this . config . synchronisedScroll ) this . synchronisedScroll ( ) ;
4956 if ( this . config . highlight ) this . highlightCode ( ) ;
5057 if ( this . config . fileListToggle ) this . fileListToggle ( this . config . fileListStartVisible ) ;
@@ -156,38 +163,11 @@ export class Diff2HtmlUI {
156163 } ) ;
157164 }
158165
166+ /**
167+ * @deprecated since version 3.1.0
168+ */
159169 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.' ) ;
191171 }
192172
193173 private instanceOfIHighlightResult ( object : IHighlightResult | IAutoHighlightResult ) : object is IHighlightResult {
@@ -206,37 +186,7 @@ export class Diff2HtmlUI {
206186 return hashTag ;
207187 }
208188
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-
235189 private isElement ( arg ?: unknown ) : arg is Element {
236190 return arg !== null && ( arg as Element ) ?. classList !== undefined ;
237191 }
238-
239- private isClipboardEvent ( arg ?: unknown ) : arg is ClipboardEvent {
240- return arg !== null && ( arg as ClipboardEvent ) ?. clipboardData !== undefined ;
241- }
242192}
0 commit comments