-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhighlight.js
More file actions
83 lines (71 loc) · 2.55 KB
/
highlight.js
File metadata and controls
83 lines (71 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2018 The Rustw Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
import React from 'react';
export const Highlight = (props) => {
const src_line_prefix = 'src_line_';
const highlight = props.highlight;
const is_valid_highlight = validate_highlight(highlight);
if (!is_valid_highlight) {
return null;
}
const lhs = (highlight.column_start - 1);
const rhs = (highlight.column_end - 1);
const highlight_specs = make_highlight(src_line_prefix, highlight.line_start, lhs, rhs);
if (highlight_specs) {
const { top, left, width } = highlight_specs;
const style = {
top: top,
left: left,
width: width,
}
return <div className="selected floating_highlight" key={highlight.line_start} style={style}> </div>;
}
return null;
}
function make_highlight(src_line_prefix, line_number, left, right) {
const line_div = $("#" + src_line_prefix + line_number);
// TODO: get adjust variable as prop through diffIndent in FileResult
// if Highlight component is to be used in the SearchResults component
// const adjust = line_div.data('adjust');
// if (adjust) {
// left -= adjust;
// right -= adjust;
// }
left *= CHAR_WIDTH;
right *= CHAR_WIDTH;
if (right === 0) {
right = line_div.width();
}
let width = right - left;
const paddingLeft = parseInt(line_div.css("padding-left"));
const paddingTop = parseInt(line_div.css("padding-top"));
if (left >= 0) {
left -= paddingLeft;
} else {
width += paddingLeft;
}
const position = line_div.position();
if (position) {
position.left += left;
position.top += paddingTop;
return { top: position.top, left: position.left, width };
}
// If no position, don't render the highlight
return null;
}
// TODO: this could maybe be validated in app.js, at srcHighlight declaration
function validate_highlight(highlight) {
const required_keys = ['line_start', 'line_end', 'column_start', 'column_end'];
const has_keys = required_keys.reduce((acc, k) => {
return acc && highlight[k] !== undefined;
}, true);
if (!has_keys || highlight.column_start <= 0) {
return false;
}
return true;
}