Skip to content

Commit fc423e6

Browse files
committed
Merge pull request #71 from lantian/separate-selection
Implemented separated column selection through javascript clipboard h…
2 parents 354fa22 + 8c50a59 commit fc423e6

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-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: 51 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,54 @@
130133
});
131134
};
132135

136+
Diff2HtmlUI.prototype._initSelection = function() {
137+
var body = $('body');
138+
var that = this;
139+
140+
body.on('mousedown', '.d2h-diff-table', function(event) {
141+
var target = $(event.target);
142+
var 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 sel = window.getSelection();
166+
var range = sel.getRangeAt(0);
167+
var doc = range.cloneContents();
168+
var nodes = doc.querySelectorAll('tr');
169+
var text = '';
170+
var idx = currentSelectionColumnId;
171+
172+
if (nodes.length === 0) {
173+
text = doc.textContent;
174+
} else {
175+
[].forEach.call(nodes, function(tr, i) {
176+
var td = tr.cells[tr.cells.length === 1 ? 0 : idx];
177+
text += (i ? '\n' : '') + td.textContent.replace(/(?:\r\n|\r|\n)/g, '');
178+
});
179+
}
180+
181+
return text;
182+
};
183+
133184
module.exports.Diff2HtmlUI = Diff2HtmlUI;
134185

135186
// Expose diff2html in the browser

0 commit comments

Comments
 (0)