Exceptions.js (2615B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 import { Component } from "devtools/client/shared/vendor/react"; 6 import PropTypes from "devtools/client/shared/vendor/react-prop-types"; 7 import { connect } from "devtools/client/shared/vendor/react-redux"; 8 9 import { 10 getSelectedSource, 11 getSelectedSourceExceptions, 12 } from "../../selectors/index"; 13 14 class Exceptions extends Component { 15 static get propTypes() { 16 return { 17 exceptions: PropTypes.array, 18 selectedSource: PropTypes.object, 19 editor: PropTypes.object, 20 }; 21 } 22 23 componentDidMount() { 24 this.setMarkers(); 25 } 26 27 componentDidUpdate(prevProps) { 28 this.clearMarkers(prevProps); 29 this.setMarkers(); 30 } 31 32 componentWillUnmount() { 33 this.clearMarkers(); 34 } 35 36 clearMarkers(prevProps) { 37 const { exceptions, selectedSource, editor } = this.props; 38 if (!editor) { 39 return; 40 } 41 42 if ( 43 !selectedSource || 44 !exceptions.length || 45 prevProps?.selectedSource !== selectedSource 46 ) { 47 editor.removeLineContentMarker(editor.markerTypes.LINE_EXCEPTION_MARKER); 48 editor.removePositionContentMarker( 49 editor.markerTypes.EXCEPTION_POSITION_MARKER 50 ); 51 } 52 } 53 54 setMarkers() { 55 const { exceptions, selectedSource, editor } = this.props; 56 if (!selectedSource || !editor || !exceptions.length) { 57 return; 58 } 59 60 editor.setLineContentMarker({ 61 id: editor.markerTypes.LINE_EXCEPTION_MARKER, 62 lineClassName: "line-exception", 63 lines: exceptions.map(e => ({ line: e.lineNumber })), 64 }); 65 66 editor.setPositionContentMarker({ 67 id: editor.markerTypes.EXCEPTION_POSITION_MARKER, 68 positionClassName: "mark-text-exception", 69 positions: exceptions.map(e => ({ 70 line: e.lineNumber, 71 // Exceptions are reported with column being 1-based 72 // while the frontend uses 0-based column. 73 column: e.columnNumber - 1, 74 })), 75 }); 76 } 77 78 render() { 79 return null; 80 } 81 } 82 83 export default connect(state => { 84 const selectedSource = getSelectedSource(state); 85 86 // Avoid calling getSelectedSourceExceptions when there is no source selected. 87 if (!selectedSource) { 88 return {}; 89 } 90 91 // Avoid causing any update until we start having exceptions 92 const exceptions = getSelectedSourceExceptions(state); 93 if (!exceptions.length) { 94 return {}; 95 } 96 97 return { 98 exceptions: getSelectedSourceExceptions(state), 99 selectedSource, 100 }; 101 })(Exceptions);