Skip to content

Commit ac2f86a

Browse files
committed
Add highlight component
1 parent db40888 commit ac2f86a

File tree

4 files changed

+31062
-27146
lines changed

4 files changed

+31062
-27146
lines changed

static/highlight.js

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

0 commit comments

Comments
 (0)