summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/components/Output/ConsoleOutput.js
blob: a2469b3b46a9afb8a6357fbfb4276f769089edb8 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* 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/. */
"use strict";

const {
  Component,
  createElement,
  createRef,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const {
  connect,
} = require("resource://devtools/client/shared/redux/visibility-handler-connect.js");
const {
  initialize,
} = require("resource://devtools/client/webconsole/actions/ui.js");
const LazyMessageList = require("resource://devtools/client/webconsole/components/Output/LazyMessageList.js");

const {
  getMutableMessagesById,
  getAllMessagesUiById,
  getAllDisabledMessagesById,
  getAllCssMessagesMatchingElements,
  getAllNetworkMessagesUpdateById,
  getLastMessageId,
  getVisibleMessages,
  getAllRepeatById,
  getAllWarningGroupsById,
  isMessageInWarningGroup,
} = require("resource://devtools/client/webconsole/selectors/messages.js");

loader.lazyRequireGetter(
  this,
  "PropTypes",
  "resource://devtools/client/shared/vendor/react-prop-types.js"
);
loader.lazyRequireGetter(
  this,
  "MessageContainer",
  "resource://devtools/client/webconsole/components/Output/MessageContainer.js",
  true
);
loader.lazyRequireGetter(this, "flags", "resource://devtools/shared/flags.js");

const {
  MESSAGE_TYPE,
} = require("resource://devtools/client/webconsole/constants.js");

class ConsoleOutput extends Component {
  static get propTypes() {
    return {
      initialized: PropTypes.bool.isRequired,
      mutableMessages: PropTypes.object.isRequired,
      messageCount: PropTypes.number.isRequired,
      messagesUi: PropTypes.array.isRequired,
      disabledMessages: PropTypes.array.isRequired,
      serviceContainer: PropTypes.shape({
        attachRefToWebConsoleUI: PropTypes.func.isRequired,
        openContextMenu: PropTypes.func.isRequired,
        sourceMapURLService: PropTypes.object,
      }),
      dispatch: PropTypes.func.isRequired,
      timestampsVisible: PropTypes.bool,
      cssMatchingElements: PropTypes.object.isRequired,
      messagesRepeat: PropTypes.object.isRequired,
      warningGroups: PropTypes.object.isRequired,
      networkMessagesUpdate: PropTypes.object.isRequired,
      visibleMessages: PropTypes.array.isRequired,
      networkMessageActiveTabId: PropTypes.string.isRequired,
      onFirstMeaningfulPaint: PropTypes.func.isRequired,
      editorMode: PropTypes.bool.isRequired,
      cacheGeneration: PropTypes.number.isRequired,
      disableVirtualization: PropTypes.bool,
      lastMessageId: PropTypes.string.isRequired,
    };
  }

  constructor(props) {
    super(props);
    this.onContextMenu = this.onContextMenu.bind(this);
    this.maybeScrollToBottom = this.maybeScrollToBottom.bind(this);
    this.messageIdsToKeepAlive = new Set();
    this.ref = createRef();
    this.lazyMessageListRef = createRef();

    this.resizeObserver = new ResizeObserver(entries => {
      // If we don't have the outputNode reference, or if the outputNode isn't connected
      // anymore, we disconnect the resize observer (componentWillUnmount is never called
      // on this component, so we have to do it here).
      if (!this.outputNode || !this.outputNode.isConnected) {
        this.resizeObserver.disconnect();
        return;
      }

      if (this.scrolledToBottom) {
        this.scrollToBottom();
      }
    });
  }

  componentDidMount() {
    if (this.props.disableVirtualization) {
      return;
    }

    if (this.props.visibleMessages.length) {
      this.scrollToBottom();
    }

    this.scrollDetectionIntersectionObserver = new IntersectionObserver(
      entries => {
        for (const entry of entries) {
          // Consider that we're not pinned to the bottom anymore if the bottom of the
          // scrollable area is within 10px of visible (half the typical element height.)
          this.scrolledToBottom = entry.intersectionRatio > 0;
        }
      },
      { root: this.outputNode, rootMargin: "10px" }
    );

    this.resizeObserver.observe(this.getElementToObserve());

    const { serviceContainer, onFirstMeaningfulPaint, dispatch } = this.props;
    serviceContainer.attachRefToWebConsoleUI(
      "outputScroller",
      this.ref.current
    );

    // Waiting for the next paint.
    new Promise(res => requestAnimationFrame(res)).then(() => {
      if (onFirstMeaningfulPaint) {
        onFirstMeaningfulPaint();
      }

      // Dispatching on next tick so we don't block on action execution.
      setTimeout(() => {
        dispatch(initialize());
      }, 0);
    });
  }

  // FIXME: https://bugzilla.mozilla.org/show_bug.cgi?id=1774507
  UNSAFE_componentWillUpdate(nextProps, nextState) {
    this.isUpdating = true;
    if (nextProps.cacheGeneration !== this.props.cacheGeneration) {
      this.messageIdsToKeepAlive = new Set();
    }

    if (nextProps.editorMode !== this.props.editorMode) {
      this.resizeObserver.disconnect();
    }

    const { outputNode } = this;
    if (!outputNode?.lastChild) {
      // Force a scroll to bottom when messages are added to an empty console.
      // This makes the console stay pinned to the bottom if a batch of messages
      // are added after a page refresh (Bug 1402237).
      this.shouldScrollBottom = true;
      this.scrolledToBottom = true;
      return;
    }

    const bottomBuffer = this.lazyMessageListRef.current.bottomBuffer;
    this.scrollDetectionIntersectionObserver.unobserve(bottomBuffer);

    const visibleMessagesDelta =
      nextProps.visibleMessages.length - this.props.visibleMessages.length;
    const messagesDelta = nextProps.messageCount - this.props.messageCount;
    // Evaluation results are never filtered out, so if it's in the store, it will be
    // visible in the output.
    const isNewMessageEvaluationResult =
      messagesDelta > 0 &&
      nextProps.lastMessageId &&
      nextProps.mutableMessages.get(nextProps.lastMessageId)?.type ===
        MESSAGE_TYPE.RESULT;

    // Use an inline function in order to avoid executing the expensive Array.some()
    // unless condition are meant to do this additional check.
    const isOpeningGroup = () => {
      const messagesUiDelta =
        nextProps.messagesUi.length - this.props.messagesUi.length;
      return (
        messagesUiDelta > 0 &&
        nextProps.messagesUi.some(
          id =>
            !this.props.messagesUi.includes(id) &&
            this.props.visibleMessages.includes(id) &&
            nextProps.visibleMessages.includes(id)
        )
      );
    };

    // We need to scroll to the bottom if:
    this.shouldScrollBottom =
      // - we are reacting to "initialize" action, and we are already scrolled to the bottom
      (!this.props.initialized &&
        nextProps.initialized &&
        this.scrolledToBottom) ||
      // - the number of messages in the store changed and the new message is an evaluation
      //   result.
      isNewMessageEvaluationResult ||
      // - the number of messages displayed changed and we are already scrolled to the
      //   bottom, but not if we are reacting to a group opening.
      (this.scrolledToBottom && visibleMessagesDelta > 0 && !isOpeningGroup());
  }

  componentDidUpdate(prevProps) {
    this.isUpdating = false;
    this.maybeScrollToBottom();
    if (this?.outputNode?.lastChild) {
      const bottomBuffer = this.lazyMessageListRef.current.bottomBuffer;
      this.scrollDetectionIntersectionObserver.observe(bottomBuffer);
    }

    if (prevProps.editorMode !== this.props.editorMode) {
      this.resizeObserver.observe(this.getElementToObserve());
    }
  }

  get outputNode() {
    return this.ref.current;
  }

  maybeScrollToBottom() {
    if (this.outputNode && this.shouldScrollBottom) {
      this.scrollToBottom();
    }
  }

  // The maybeScrollToBottom callback we provide to messages needs to be a little bit more
  // strict than the one we normally use, because they can potentially interrupt a user
  // scroll (between when the intersection observer registers the scroll break and when
  // a componentDidUpdate comes through to reconcile it.)
  maybeScrollToBottomMessageCallback(index) {
    if (
      this.outputNode &&
      this.shouldScrollBottom &&
      this.scrolledToBottom &&
      this.lazyMessageListRef.current?.isItemNearBottom(index)
    ) {
      this.scrollToBottom();
    }
  }

  scrollToBottom() {
    if (flags.testing && this.outputNode.hasAttribute("disable-autoscroll")) {
      return;
    }
    if (this.outputNode.scrollHeight > this.outputNode.clientHeight) {
      this.outputNode.scrollTop = this.outputNode.scrollHeight;
    }

    this.scrolledToBottom = true;
  }

  getElementToObserve() {
    // In inline mode, we need to observe the output node parent, which contains both the
    // output and the input, so we don't trigger the resizeObserver callback when only the
    // output size changes (e.g. when a network request is expanded).
    return this.props.editorMode
      ? this.outputNode
      : this.outputNode?.parentNode;
  }

  onContextMenu(e) {
    this.props.serviceContainer.openContextMenu(e);
    e.stopPropagation();
    e.preventDefault();
  }

  render() {
    const {
      cacheGeneration,
      dispatch,
      visibleMessages,
      disabledMessages,
      mutableMessages,
      messagesUi,
      cssMatchingElements,
      messagesRepeat,
      warningGroups,
      networkMessagesUpdate,
      networkMessageActiveTabId,
      serviceContainer,
      timestampsVisible,
    } = this.props;

    const renderMessage = (messageId, index) => {
      return createElement(MessageContainer, {
        dispatch,
        key: messageId,
        messageId,
        serviceContainer,
        open: messagesUi.includes(messageId),
        cssMatchingElements: cssMatchingElements.get(messageId),
        timestampsVisible,
        disabled: disabledMessages.includes(messageId),
        repeat: messagesRepeat[messageId],
        badge: warningGroups.has(messageId)
          ? warningGroups.get(messageId).length
          : null,
        inWarningGroup:
          warningGroups && warningGroups.size > 0
            ? isMessageInWarningGroup(
                mutableMessages.get(messageId),
                visibleMessages
              )
            : false,
        networkMessageUpdate: networkMessagesUpdate[messageId],
        networkMessageActiveTabId,
        getMessage: () => mutableMessages.get(messageId),
        maybeScrollToBottom: () =>
          this.maybeScrollToBottomMessageCallback(index),
        // Whenever a node is expanded, we want to make sure we keep the
        // message node alive so as to not lose the expanded state.
        setExpanded: () => this.messageIdsToKeepAlive.add(messageId),
      });
    };

    // scrollOverdrawCount tells the list to draw extra elements above and
    // below the scrollport so that we can avoid flashes of blank space
    // when scrolling. When `disableVirtualization` is passed we make it as large as the
    // number of messages to render them all and effectively disabling virtualization (this
    // should only be used for some actions that requires all the messages to be rendered
    // in the DOM, like "Copy All Messages").
    const scrollOverdrawCount = this.props.disableVirtualization
      ? visibleMessages.length
      : 20;

    const attrs = {
      className: "webconsole-output",
      role: "main",
      onContextMenu: this.onContextMenu,
      ref: this.ref,
    };
    if (flags.testing) {
      attrs["data-visible-messages"] = JSON.stringify(visibleMessages);
    }
    return dom.div(
      attrs,
      createElement(LazyMessageList, {
        viewportRef: this.ref,
        items: visibleMessages,
        itemDefaultHeight: 21,
        editorMode: this.props.editorMode,
        scrollOverdrawCount,
        ref: this.lazyMessageListRef,
        renderItem: renderMessage,
        itemsToKeepAlive: this.messageIdsToKeepAlive,
        serviceContainer,
        cacheGeneration,
        shouldScrollBottom: () => this.shouldScrollBottom && this.isUpdating,
      })
    );
  }
}

function mapStateToProps(state, props) {
  const mutableMessages = getMutableMessagesById(state);
  return {
    initialized: state.ui.initialized,
    cacheGeneration: state.ui.cacheGeneration,
    // We need to compute this so lifecycle methods can compare the global message count
    // on state change (since we can't do it with mutableMessagesById).
    messageCount: mutableMessages.size,
    mutableMessages,
    lastMessageId: getLastMessageId(state),
    visibleMessages: getVisibleMessages(state),
    disabledMessages: getAllDisabledMessagesById(state),
    messagesUi: getAllMessagesUiById(state),
    cssMatchingElements: getAllCssMessagesMatchingElements(state),
    messagesRepeat: getAllRepeatById(state),
    warningGroups: getAllWarningGroupsById(state),
    networkMessagesUpdate: getAllNetworkMessagesUpdateById(state),
    timestampsVisible: state.ui.timestampsVisible,
    networkMessageActiveTabId: state.ui.networkMessageActiveTabId,
  };
}

module.exports = connect(mapStateToProps)(ConsoleOutput);