summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/components/Editor/EditorMenu.js
blob: a865fcc9bdb9b1b71cbca0a1d0f966593e29e119 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

import { Component } from "react";
import PropTypes from "prop-types";

import { connect } from "../../utils/connect";
import { showMenu } from "../../context-menu/menu";

import { getSourceLocationFromMouseEvent } from "../../utils/editor";
import { isPretty } from "../../utils/source";
import {
  getPrettySource,
  getIsCurrentThreadPaused,
  getThreadContext,
  isSourceWithMap,
  getBlackBoxRanges,
  isSourceOnSourceMapIgnoreList,
  isSourceMapIgnoreListEnabled,
} from "../../selectors";

import { editorMenuItems, editorItemActions } from "./menus/editor";

class EditorMenu extends Component {
  static get propTypes() {
    return {
      clearContextMenu: PropTypes.func.isRequired,
      contextMenu: PropTypes.object,
      isSourceOnIgnoreList: PropTypes.bool,
    };
  }

  // FIXME: https://bugzilla.mozilla.org/show_bug.cgi?id=1774507
  UNSAFE_componentWillUpdate(nextProps) {
    this.props.clearContextMenu();
    if (nextProps.contextMenu) {
      this.showMenu(nextProps);
    }
  }

  showMenu(props) {
    const {
      cx,
      editor,
      selectedSource,
      blackboxedRanges,
      editorActions,
      hasMappedLocation,
      isPaused,
      editorWrappingEnabled,
      contextMenu: event,
      isSourceOnIgnoreList,
    } = props;

    const location = getSourceLocationFromMouseEvent(
      editor,
      selectedSource,
      // Use a coercion, as contextMenu is optional
      event
    );

    showMenu(
      event,
      editorMenuItems({
        cx,
        editorActions,
        selectedSource,
        blackboxedRanges,
        hasMappedLocation,
        location,
        isPaused,
        editorWrappingEnabled,
        selectionText: editor.codeMirror.getSelection().trim(),
        isTextSelected: editor.codeMirror.somethingSelected(),
        editor,
        isSourceOnIgnoreList,
      })
    );
  }

  render() {
    return null;
  }
}

const mapStateToProps = (state, props) => {
  // This component is a no-op when contextmenu is false
  if (!props.contextMenu) {
    return {};
  }
  return {
    cx: getThreadContext(state),
    blackboxedRanges: getBlackBoxRanges(state),
    isPaused: getIsCurrentThreadPaused(state),
    hasMappedLocation:
      (props.selectedSource.isOriginal ||
        isSourceWithMap(state, props.selectedSource.id) ||
        isPretty(props.selectedSource)) &&
      !getPrettySource(state, props.selectedSource.id),
    isSourceOnIgnoreList:
      isSourceMapIgnoreListEnabled(state) &&
      isSourceOnSourceMapIgnoreList(state, props.selectedSource),
  };
};

const mapDispatchToProps = dispatch => ({
  editorActions: editorItemActions(dispatch),
});

export default connect(mapStateToProps, mapDispatchToProps)(EditorMenu);