Skip to content

Commit 0cc9550

Browse files
authored
Merge pull request #304 from rtfpessoa/implement-smart-selection-in-css
feature: Implement smart selection in CSS
2 parents 4e28c5c + ee58e2d commit 0cc9550

File tree

4 files changed

+16
-102
lines changed

4 files changed

+16
-102
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ draw(): void
308308
synchronisedScroll(): void
309309
fileListToggle(startVisible: boolean): void
310310
highlightCode(): void
311-
smartSelection(): void
312311
```
313312

314313
> Check out the [docs/demo.html](./docs/demo.html) for a demo example.
@@ -319,8 +318,6 @@ smartSelection(): void
319318
- `highlight`: syntax highlight the code on the diff: `true` or `false`, default is `true`
320319
- `fileListToggle`: allow the file summary list to be toggled: `true` or `false`, default is `true`
321320
- `fileListStartVisible`: choose if the file summary list starts visible: `true` or `false`, default is `false`
322-
- `smartSelection`: allow selection of the code without including line numbers of line prefixes: `true` or `false`,
323-
default is `true`
324321

325322
> NOTE: All the options from Diff2Html are also valid configurations in Diff2HtmlUI
326323

src/ui/css/diff2html.css

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -335,41 +335,8 @@
335335
border: #3572b0 1px solid;
336336
}
337337

338-
/*
339-
* Selection util.
340-
*/
341-
342-
.selecting-left .d2h-code-line,
343-
.selecting-left .d2h-code-line *,
344-
.selecting-right td.d2h-code-linenumber,
345-
.selecting-right td.d2h-code-linenumber *,
346-
.selecting-left .d2h-code-side-line,
347-
.selecting-left .d2h-code-side-line *,
348-
.selecting-right td.d2h-code-side-linenumber,
349-
.selecting-right td.d2h-code-side-linenumber * {
350-
-webkit-touch-callout: none;
351-
-webkit-user-select: none;
352-
-moz-user-select: none;
353-
-ms-user-select: none;
338+
.d2h-code-linenumber,
339+
.d2h-code-side-linenumber,
340+
.d2h-code-line-prefix {
354341
user-select: none;
355342
}
356-
357-
.selecting-left .d2h-code-line::-moz-selection,
358-
.selecting-left .d2h-code-line *::-moz-selection,
359-
.selecting-right td.d2h-code-linenumber::-moz-selection,
360-
.selecting-left .d2h-code-side-line::-moz-selection,
361-
.selecting-left .d2h-code-side-line *::-moz-selection,
362-
.selecting-right td.d2h-code-side-linenumber::-moz-selection,
363-
.selecting-right td.d2h-code-side-linenumber *::-moz-selection {
364-
background: transparent;
365-
}
366-
367-
.selecting-left .d2h-code-line::selection,
368-
.selecting-left .d2h-code-line *::selection,
369-
.selecting-right td.d2h-code-linenumber::selection,
370-
.selecting-left .d2h-code-side-line::selection,
371-
.selecting-left .d2h-code-side-line *::selection,
372-
.selecting-right td.d2h-code-side-linenumber::selection,
373-
.selecting-right td.d2h-code-side-linenumber *::selection {
374-
background: transparent;
375-
}

src/ui/js/diff2html-ui-base.ts

Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

website/templates/pages/demo/content.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
<p>Just copy the url from the page, which should contain all the customizations and reference for the diff you
113113
introduced.</p>
114114
<p>ex: <a
115-
href="demo.html?matching=none&matchWordsThreshold=0.25&maxLineLengthHighlight=10000&diffStyle=word&renderNothingWhenEmpty=0&matchingMaxComparisons=2500&maxLineSizeInBlockForComparison=200&outputFormat=line-by-line&drawFileList=1&synchronisedScroll=1&highlight=1&fileListToggle=1&fileListStartVisible=0&smartSelection=1&diff=https://github.com/rtfpessoa/diff2html/pull/106">https://diff2html.xyz/demo.html?matching=none&matchWordsThreshold=0.25&maxLineLengthHighlight=10000&diffStyle=word&renderNothingWhenEmpty=0&matchingMaxComparisons=2500&maxLineSizeInBlockForComparison=200&outputFormat=line-by-line&drawFileList=1&synchronisedScroll=1&highlight=1&fileListToggle=1&fileListStartVisible=0&smartSelection=1&diff=https://github.com/rtfpessoa/diff2html/pull/106</a>
115+
href="demo.html?matching=none&matchWordsThreshold=0.25&maxLineLengthHighlight=10000&diffStyle=word&renderNothingWhenEmpty=0&matchingMaxComparisons=2500&maxLineSizeInBlockForComparison=200&outputFormat=line-by-line&drawFileList=1&synchronisedScroll=1&highlight=1&fileListToggle=1&fileListStartVisible=0&diff=https://github.com/rtfpessoa/diff2html/pull/106">https://diff2html.xyz/demo.html?matching=none&matchWordsThreshold=0.25&maxLineLengthHighlight=10000&diffStyle=word&renderNothingWhenEmpty=0&matchingMaxComparisons=2500&maxLineSizeInBlockForComparison=200&outputFormat=line-by-line&drawFileList=1&synchronisedScroll=1&highlight=1&fileListToggle=1&fileListStartVisible=0&diff=https://github.com/rtfpessoa/diff2html/pull/106</a>
116116
</p>
117117
</li>
118118
<li class="block">

0 commit comments

Comments
 (0)