Skip to content

Commit 12dfec2

Browse files
committed
Add highlight component
1 parent db40888 commit 12dfec2

File tree

4 files changed

+31141
-27148
lines changed

4 files changed

+31141
-27148
lines changed

static/highlight.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
let highlight_divs = [];
21+
// First line
22+
const lhs = (highlight.column_start - 1);
23+
let rhs = 0;
24+
if (highlight.line_end === highlight.line_start && highlight.column_end > 0) {
25+
// If we're only highlighting one line, then the highlight must stop
26+
// before the end of the line.
27+
rhs = (highlight.column_end - 1);
28+
}
29+
let highlight_specs = make_highlight(src_line_prefix, highlight.line_start, lhs, rhs);
30+
if (highlight_specs) {
31+
let { top, left, width } = highlight_specs;
32+
let style = {
33+
top: top,
34+
left: left,
35+
width: width,
36+
}
37+
let highlight_div = <div className="selected floating_highlight" key={highlight.line_start} style={style}>&nbsp;</div>;
38+
highlight_divs.push(highlight_div);
39+
}
40+
41+
// Last line
42+
if (highlight.line_end > highlight.line_start) {
43+
rhs = 0;
44+
if (highlight.column_end > 0) {
45+
rhs = (highlight.column_end - 1);
46+
}
47+
highlight_specs = make_highlight(src_line_prefix, highlight.line_end, 0, rhs);
48+
if (highlight_specs) {
49+
let { top, left, width } = highlight_specs;
50+
let style = {
51+
top: top,
52+
left: left,
53+
width: width,
54+
}
55+
let highlight_div = <div className="selected floating_highlight" key={highlight.line_end} style={style}>&nbsp;</div>;
56+
highlight_divs.push(highlight_div);
57+
}
58+
}
59+
60+
return highlight_divs;
61+
}
62+
63+
function make_highlight(src_line_prefix, line_number, left, right) {
64+
let line_div = $("#" + src_line_prefix + line_number);
65+
66+
// TODO: get adjust variable as prop through diffIndent in FileResult
67+
// if Highlight component is to be used in the SearchResults component
68+
// const adjust = line_div.data('adjust');
69+
// if (adjust) {
70+
// left -= adjust;
71+
// right -= adjust;
72+
// }
73+
74+
left *= CHAR_WIDTH;
75+
right *= CHAR_WIDTH;
76+
if (right === 0) {
77+
right = line_div.width();
78+
}
79+
80+
let width = right - left;
81+
let paddingLeft = parseInt(line_div.css("padding-left"));
82+
let paddingTop = parseInt(line_div.css("padding-top"));
83+
if (left > 0) {
84+
left -= paddingLeft;
85+
} else {
86+
width += paddingLeft;
87+
}
88+
89+
let position = line_div.position();
90+
if (position) {
91+
position.left += left;
92+
position.top += paddingTop;
93+
return { top: position.top, left: position.left, width };
94+
}
95+
// If no position, don't render the highlight
96+
return null;
97+
}
98+
99+
// TODO: this could maybe be validated in app.js, at srcHighlight declaration
100+
function validate_highlight(highlight) {
101+
const required_keys = ['line_start', 'line_end', 'column_start', 'column_end'];
102+
const has_keys = required_keys.reduce((acc, k) => {
103+
return acc && highlight[k] !== undefined;
104+
}, true);
105+
106+
if (!has_keys || highlight.column_start <= 0) {
107+
return false;
108+
}
109+
return true;
110+
}

0 commit comments

Comments
 (0)