Skip to content

Commit 619b43f

Browse files
committed
Implemented separated column selection through javascript clipboard hook.
1 parent 354fa22 commit 619b43f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/ui/css/diff2html.css

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,33 @@ ins.d2h-change, del.d2h-change {
274274
font-size: 10px;
275275
cursor: pointer;
276276
}
277+
278+
/*
279+
* Selection util.
280+
*/
281+
282+
.selecting-left .d2h-code-line,
283+
.selecting-left .d2h-code-line *,
284+
.selecting-right td.d2h-code-linenumber,
285+
.selecting-right td.d2h-code-linenumber *,
286+
.selecting-left .d2h-code-side-line,
287+
.selecting-left .d2h-code-side-line *,
288+
.selecting-right td.d2h-code-side-linenumber,
289+
.selecting-right td.d2h-code-side-linenumber *{
290+
-webkit-touch-callout: none;
291+
-webkit-user-select: none;
292+
-khtml-user-select: none;
293+
-moz-user-select: none;
294+
-ms-user-select: none;
295+
user-select: none;
296+
}
297+
298+
.selecting-left .d2h-code-line::selection,
299+
.selecting-left .d2h-code-line *::selection
300+
.selecting-right td.d2h-code-linenumber::selection,
301+
.selecting-left .d2h-code-side-line::selection,
302+
.selecting-left .d2h-code-side-line *::selection,
303+
.selecting-right td.d2h-code-side-linenumber::selection,
304+
.selecting-right td.d2h-code-side-linenumber *::selection {
305+
background: transparent;
306+
}

src/ui/js/diff2html-ui.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
var diffJson = null;
1616
var defaultTarget = "body";
17+
var currentSelectionColumnId = -1;
1718

1819
function Diff2HtmlUI(config) {
1920
var cfg = config || {};
@@ -23,6 +24,8 @@
2324
} else if (cfg.json) {
2425
diffJson = cfg.json;
2526
}
27+
28+
this._initSelection();
2629
}
2730

2831
Diff2HtmlUI.prototype.draw = function(targetId, config) {
@@ -130,6 +133,56 @@
130133
});
131134
};
132135

136+
Diff2HtmlUI.prototype._initSelection = function () {
137+
var body = $('body'),
138+
that = this;
139+
140+
body.on('mousedown', '.d2h-diff-table', function (event) {
141+
var target = $(event.target),
142+
table = target.closest('.d2h-diff-table');
143+
144+
if (target.closest('.d2h-code-line,.d2h-code-side-line').length) {
145+
table.removeClass('selecting-left');
146+
table.addClass('selecting-right');
147+
currentSelectionColumnId = 1;
148+
} else if (target.closest('.d2h-code-linenumber,.d2h-code-side-linenumber').length) {
149+
table.removeClass('selecting-right');
150+
table.addClass('selecting-left');
151+
currentSelectionColumnId = 0;
152+
}
153+
});
154+
155+
body.on('copy', '.d2h-diff-table', function (event) {
156+
var clipboardData = event.originalEvent.clipboardData;
157+
var text = that._getSelectedText();
158+
clipboardData.setData('text', text);
159+
event.preventDefault();
160+
});
161+
};
162+
163+
164+
Diff2HtmlUI.prototype._getSelectedText = function () {
165+
var table = $('.d2h-diff-table'),
166+
sel = window.getSelection(),
167+
range = sel.getRangeAt(0),
168+
doc = range.cloneContents(),
169+
nodes = doc.querySelectorAll('tr'),
170+
text = '';
171+
172+
var idx = currentSelectionColumnId;
173+
174+
if (nodes.length === 0) {
175+
text = doc.textContent;
176+
} else {
177+
[].forEach.call(nodes, function (tr, i) {
178+
var td = tr.cells[tr.cells.length == 1 ? 0 : idx];
179+
text += (i ? '\n' : '') + td.textContent.replace(/(?:\r\n|\r|\n)/g, '');
180+
});
181+
}
182+
183+
return text;
184+
};
185+
133186
module.exports.Diff2HtmlUI = Diff2HtmlUI;
134187

135188
// Expose diff2html in the browser

0 commit comments

Comments
 (0)