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