-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
JavaScriptLogViewController.js
373 lines (293 loc) · 14.5 KB
/
JavaScriptLogViewController.js
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
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
WI.JavaScriptLogViewController = class JavaScriptLogViewController extends WI.Object
{
constructor(element, scrollElement, textPrompt, delegate, historySettingIdentifier)
{
super();
console.assert(textPrompt instanceof WI.ConsolePrompt);
console.assert(historySettingIdentifier);
this._element = element;
this._scrollElement = scrollElement;
this._promptHistorySetting = new WI.Setting(historySettingIdentifier, null);
this._prompt = textPrompt;
this._prompt.delegate = this;
this._prompt.history = this._promptHistorySetting.value;
this.delegate = delegate;
this._cleared = true;
this._previousMessageView = null;
this._lastCommitted = {text: "", special: false};
this._repeatCountWasInterrupted = false;
this._sessions = [];
this._currentSessionOrGroup = null;
this.messagesAlternateClearKeyboardShortcut = new WI.KeyboardShortcut(WI.KeyboardShortcut.Modifier.Control, "L", this.requestClearMessages.bind(this), this._element);
this._messagesFindNextKeyboardShortcut = new WI.KeyboardShortcut(WI.KeyboardShortcut.Modifier.CommandOrControl, "G", this._handleFindNextShortcut.bind(this), this._element);
this._messagesFindPreviousKeyboardShortcut = new WI.KeyboardShortcut(WI.KeyboardShortcut.Modifier.CommandOrControl | WI.KeyboardShortcut.Modifier.Shift, "G", this._handleFindPreviousShortcut.bind(this), this._element);
this._promptAlternateClearKeyboardShortcut = new WI.KeyboardShortcut(WI.KeyboardShortcut.Modifier.Control, "L", this.requestClearMessages.bind(this), this._prompt.element);
this._promptFindNextKeyboardShortcut = new WI.KeyboardShortcut(WI.KeyboardShortcut.Modifier.CommandOrControl, "G", this._handleFindNextShortcut.bind(this), this._prompt.element);
this._promptFindPreviousKeyboardShortcut = new WI.KeyboardShortcut(WI.KeyboardShortcut.Modifier.CommandOrControl | WI.KeyboardShortcut.Modifier.Shift, "G", this._handleFindPreviousShortcut.bind(this), this._prompt.element);
this._pendingMessagesForSessionOrGroup = new Map;
this._scheduledRenderIdentifier = 0;
this.startNewSession();
}
// Public
clear()
{
this._cleared = true;
const clearPreviousSessions = true;
this.startNewSession(clearPreviousSessions, {newSessionReason: WI.ConsoleSession.NewSessionReason.ConsoleCleared});
}
startNewSession(clearPreviousSessions = false, data = {})
{
if (clearPreviousSessions) {
this._pendingMessagesForSessionOrGroup.clear();
if (this._sessions.length) {
for (let session of this._sessions)
session.element.remove();
this._sessions = [];
this._currentSessionOrGroup = null;
}
}
// First session shows the time when the console was opened.
if (!this._sessions.length)
data.timestamp = Date.now();
let lastSession = this._sessions.lastValue;
// Remove empty session.
if (lastSession && !lastSession.hasMessages() && !this._pendingMessagesForSessionOrGroup.has(lastSession)) {
this._sessions.pop();
lastSession.element.remove();
}
let consoleSession = new WI.ConsoleSession(data);
this._previousMessageView = null;
this._lastCommitted = {text: "", special: false};
this._repeatCountWasInterrupted = false;
this._sessions.push(consoleSession);
this._currentSessionOrGroup = consoleSession;
this._element.appendChild(consoleSession.element);
// Make sure the new session is visible.
consoleSession.element.scrollIntoView();
}
appendImmediateExecutionWithResult(text, result, {addSpecialUserLogClass, shouldRevealConsole, handleClick} = {})
{
console.assert(result instanceof WI.RemoteObject);
if (this._lastCommitted.text !== text || this._lastCommitted.special !== addSpecialUserLogClass) {
let classNames = [];
if (addSpecialUserLogClass)
classNames.push("special-user-log");
let commandMessageView = new WI.ConsoleCommandView(text, {classNames, handleClick});
this._appendConsoleMessageView(commandMessageView, true);
this._lastCommitted = {text, special: addSpecialUserLogClass};
}
function saveResultCallback(savedResultIndex)
{
let commandResultMessage = new WI.ConsoleCommandResultMessage(result.target, result, false, savedResultIndex, shouldRevealConsole);
let commandResultMessageView = new WI.ConsoleMessageView(commandResultMessage);
this._appendConsoleMessageView(commandResultMessageView, true);
}
WI.runtimeManager.saveResult(result, saveResultCallback.bind(this));
}
appendConsoleMessage(consoleMessage)
{
var consoleMessageView = new WI.ConsoleMessageView(consoleMessage);
this._appendConsoleMessageView(consoleMessageView);
return consoleMessageView;
}
updatePreviousMessageRepeatCount(count)
{
console.assert(this._previousMessageView);
if (!this._previousMessageView)
return false;
var previousIgnoredCount = this._previousMessageView[WI.JavaScriptLogViewController.IgnoredRepeatCount] || 0;
var previousVisibleCount = this._previousMessageView.repeatCount;
if (!this._repeatCountWasInterrupted) {
this._previousMessageView.repeatCount = count - previousIgnoredCount;
return true;
}
var consoleMessage = this._previousMessageView.message;
var duplicatedConsoleMessageView = new WI.ConsoleMessageView(consoleMessage);
duplicatedConsoleMessageView[WI.JavaScriptLogViewController.IgnoredRepeatCount] = previousIgnoredCount + previousVisibleCount;
duplicatedConsoleMessageView.repeatCount = 1;
this._appendConsoleMessageView(duplicatedConsoleMessageView);
return true;
}
isScrolledToBottom()
{
// Lie about being scrolled to the bottom if we have a pending request to scroll to the bottom soon.
return this._scrollToBottomTimeout || this._scrollElement.isScrolledToBottom();
}
scrollToBottom()
{
if (this._scrollToBottomTimeout)
return;
function delayedWork()
{
this._scrollToBottomTimeout = null;
this._scrollElement.scrollTop = this._scrollElement.scrollHeight;
}
// Don't scroll immediately so we are not causing excessive layouts when there
// are many messages being added at once.
this._scrollToBottomTimeout = setTimeout(delayedWork.bind(this), 0);
}
requestClearMessages()
{
WI.consoleManager.requestClearMessages();
}
// Protected
consolePromptHistoryDidChange(prompt)
{
this._promptHistorySetting.value = this._prompt.history;
}
consolePromptShouldCommitText(prompt, text, cursorIsAtLastPosition, handler)
{
// Always commit the text if we are not at the last position.
if (!cursorIsAtLastPosition) {
handler(true);
return;
}
function parseFinished(error, result, message, range)
{
handler(result !== InspectorBackend.Enum.Runtime.SyntaxErrorType.Recoverable);
}
WI.runtimeManager.activeExecutionContext.target.RuntimeAgent.parse(text, parseFinished.bind(this));
}
consolePromptTextCommitted(prompt, text)
{
console.assert(text);
if (this._lastCommitted.text !== text || this._lastCommitted.special) {
let commandMessageView = new WI.ConsoleCommandView(text);
this._appendConsoleMessageView(commandMessageView, true);
this._lastCommitted = {text, special: false};
}
function printResult(result, wasThrown, savedResultIndex)
{
if (!result || this._cleared)
return;
let shouldRevealConsole = true;
let commandResultMessage = new WI.ConsoleCommandResultMessage(result.target, result, wasThrown, savedResultIndex, shouldRevealConsole);
let commandResultMessageView = new WI.ConsoleMessageView(commandResultMessage);
this._appendConsoleMessageView(commandResultMessageView, true);
}
let options = {
objectGroup: WI.RuntimeManager.ConsoleObjectGroup,
includeCommandLineAPI: true,
doNotPauseOnExceptionsAndMuteConsole: false,
returnByValue: false,
generatePreview: true,
saveResult: true,
emulateUserGesture: WI.settings.emulateInUserGesture.value,
sourceURLAppender: appendWebInspectorConsoleEvaluationSourceURL,
};
WI.runtimeManager.evaluateInInspectedWindow(text, options, printResult.bind(this));
}
// Private
_handleFindNextShortcut()
{
this.delegate.highlightNextSearchMatch();
}
_handleFindPreviousShortcut()
{
this.delegate.highlightPreviousSearchMatch();
}
_appendConsoleMessageView(messageView, repeatCountWasInterrupted)
{
let pendingMessagesForSession = this._pendingMessagesForSessionOrGroup.get(this._currentSessionOrGroup);
if (!pendingMessagesForSession) {
pendingMessagesForSession = [];
this._pendingMessagesForSessionOrGroup.set(this._currentSessionOrGroup, pendingMessagesForSession);
}
pendingMessagesForSession.push(messageView);
this._cleared = false;
this._repeatCountWasInterrupted = repeatCountWasInterrupted || false;
if (!repeatCountWasInterrupted)
this._previousMessageView = messageView;
if (messageView.message && messageView.message.source !== WI.ConsoleMessage.MessageSource.JS)
this._lastCommitted = {test: "", special: false};
if (WI.consoleContentView.isAttached)
this.renderPendingMessagesSoon();
if (!WI.isShowingConsoleTab() && messageView.message && messageView.message.shouldRevealConsole)
WI.showSplitConsole();
}
renderPendingMessages()
{
if (this._scheduledRenderIdentifier) {
cancelAnimationFrame(this._scheduledRenderIdentifier);
this._scheduledRenderIdentifier = 0;
}
if (!this._pendingMessagesForSessionOrGroup.size)
return;
let wasScrolledToBottom = this.isScrolledToBottom();
let savedCurrentConsoleGroup = this._currentSessionOrGroup;
let lastMessageView = null;
const maxMessagesPerFrame = 100;
let renderedMessages = 0;
for (let [session, messages] of this._pendingMessagesForSessionOrGroup) {
this._currentSessionOrGroup = session;
let messagesToRender = messages.splice(0, maxMessagesPerFrame - renderedMessages);
for (let message of messagesToRender) {
message.render();
this._didRenderConsoleMessageView(message);
}
lastMessageView = messagesToRender.lastValue;
if (!messages.length)
this._pendingMessagesForSessionOrGroup.delete(session);
renderedMessages += messagesToRender.length;
if (renderedMessages >= maxMessagesPerFrame)
break;
}
this._currentSessionOrGroup = savedCurrentConsoleGroup;
if (wasScrolledToBottom || lastMessageView instanceof WI.ConsoleCommandView || lastMessageView.message.type === WI.ConsoleMessage.MessageType.Result || lastMessageView.message.type === WI.ConsoleMessage.MessageType.Image)
this.scrollToBottom();
WI.quickConsole.needsLayout();
if (this._pendingMessagesForSessionOrGroup.size)
this.renderPendingMessagesSoon();
}
renderPendingMessagesSoon()
{
if (this._scheduledRenderIdentifier)
return;
this._scheduledRenderIdentifier = requestAnimationFrame(() => this.renderPendingMessages());
}
_didRenderConsoleMessageView(messageView)
{
var type = messageView instanceof WI.ConsoleCommandView ? null : messageView.message.type;
if (type === WI.ConsoleMessage.MessageType.EndGroup) {
var parentGroup = this._currentSessionOrGroup.parentGroup;
if (parentGroup)
this._currentSessionOrGroup = parentGroup;
} else {
if (type === WI.ConsoleMessage.MessageType.StartGroup || type === WI.ConsoleMessage.MessageType.StartGroupCollapsed) {
var group = new WI.ConsoleGroup(this._currentSessionOrGroup);
var groupElement = group.render(messageView);
this._currentSessionOrGroup.append(groupElement);
this._currentSessionOrGroup = group;
} else
this._currentSessionOrGroup.addMessageView(messageView);
}
if (this.delegate && typeof this.delegate.didAppendConsoleMessageView === "function")
this.delegate.didAppendConsoleMessageView(messageView);
}
};
WI.JavaScriptLogViewController.CachedPropertiesDuration = 30_000;
WI.JavaScriptLogViewController.IgnoredRepeatCount = Symbol("ignored-repeat-count");