-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathLiveDevMultiBrowser.js
More file actions
829 lines (737 loc) · 29.3 KB
/
LiveDevMultiBrowser.js
File metadata and controls
829 lines (737 loc) · 29.3 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/**
* LiveDevelopment allows Brackets to launch a browser with a "live preview" that's
* connected to the current editor.
*
* # STARTING
*
* To start a session call `open`. This will read the currentDocument from brackets,
* launch it in the default browser, and connect to it for live editing.
*
* # STOPPING
*
* To stop a session call `close`. This will close the connection to the browser
* (but will not close the browser tab).
*
* # STATUS
*
* Status updates are dispatched as `statusChange` jQuery events. The status
* is passed as the first parameter and the reason for the change as the second
* parameter. Currently only the "Inactive" status supports the reason parameter.
* The status codes are:
*
* 0: Inactive
* 1: Connecting (waiting for a browser connection)
* 2: Active
* 3: Out of sync
* 4: Sync error
* 5: Reloading (after saving JS changes)
* 6: Restarting (switching context to a new HTML live doc)
*
* The reason codes are:
* - null (Unknown reason)
* - "explicit_close" (LiveDevelopment.close() was called)
* - "navigated_away" (The browser changed to a location outside of the project)
* - "detached_target_closed" (The tab or window was closed)
*/
/*global jsPromise */
define(function (require, exports, module) {
// Status Codes
const STATUS_INACTIVE = exports.STATUS_INACTIVE = 0;
const STATUS_CONNECTING = exports.STATUS_CONNECTING = 1;
const STATUS_ACTIVE = exports.STATUS_ACTIVE = 2;
const STATUS_OUT_OF_SYNC = exports.STATUS_OUT_OF_SYNC = 3;
const STATUS_SYNC_ERROR = exports.STATUS_SYNC_ERROR = 4;
const STATUS_RELOADING = exports.STATUS_RELOADING = 5;
const STATUS_RESTARTING = exports.STATUS_RESTARTING = 6;
// events
const EVENT_OPEN_PREVIEW_URL = "openPreviewURL",
EVENT_CONNECTION_CLOSE = "ConnectionClose",
EVENT_STATUS_CHANGE = "statusChange";
const Dialogs = require("widgets/Dialogs"),
DefaultDialogs = require("widgets/DefaultDialogs"),
DocumentManager = require("document/DocumentManager"),
EditorManager = require("editor/EditorManager"),
EventDispatcher = require("utils/EventDispatcher"),
MainViewManager = require("view/MainViewManager"),
ProjectManager = require("project/ProjectManager"),
Strings = require("strings"),
LiveDevelopmentUtils = require("LiveDevelopment/LiveDevelopmentUtils"),
LiveDevServerManager = require("LiveDevelopment/LiveDevServerManager"),
LivePreviewTransport = require("LiveDevelopment/MultiBrowserImpl/transports/LivePreviewTransport"),
LiveDevProtocol = require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol"),
Metrics = require("utils/Metrics"),
PageLoaderWorkerScript = require("text!LiveDevelopment/BrowserScripts/pageLoaderWorker.js");
// Documents
const LiveCSSDocument = require("LiveDevelopment/MultiBrowserImpl/documents/LiveCSSDocument"),
LiveHTMLDocument = require("LiveDevelopment/MultiBrowserImpl/documents/LiveHTMLDocument");
/**
* @private
* The live HTML document for the currently active preview.
* @type {LiveHTMLDocument}
*/
var _liveDocument;
/**
* Live preview only tracks the pinned document.
* @type {boolean}
*/
let livePreviewUrlPinned = false;
let currentPreviewFilePath;
/**
* @private
* Live documents related to the active HTML document - for example, CSS files
* that are used by the document.
* @type {Object.<string: {LiveHTMLDocument|LiveCSSDocument}>}
*/
var _relatedDocuments = {};
/**
* @private
* Protocol handler that provides the actual live development API on top of the current transport.
*/
var _protocol = LiveDevProtocol;
/**
* @private
* Current live preview server
* @type {BaseServer}
*/
var _server;
/**
* @private
* Determine which live document class should be used for a given document
* @param {Document} document The document we want to create a live document for.
* @return {function} The constructor for the live document class; will be a subclass of LiveDocument.
*/
function _classForDocument(doc) {
if (doc.getLanguage().getId() === "css") {
return LiveCSSDocument;
}
if (LiveDevelopmentUtils.isHtmlFileExt(doc.file.fullPath)) {
return LiveHTMLDocument;
}
return null;
}
/**
* Returns true if the global Live Development mode is on (might be in the middle of connecting).
* @return {boolean}
*/
function isActive() {
return exports.status > STATUS_INACTIVE;
}
/**
* Returns the live document for a given path, or null if there is no live document for it.
* @param {string} path
* @return {?LiveDocument}
*/
function getLiveDocForPath(path) {
if (!_server) {
return null;
}
return _server.get(path);
}
/**
* @private
* Close a live document.
* @param {LiveDocument}
*/
function _closeDocument(liveDocument) {
liveDocument.off(".livedev");
_protocol.off(".livedev");
liveDocument.close();
}
/**
* Removes the given CSS/JSDocument from _relatedDocuments. Signals that the
* given file is no longer associated with the HTML document that is live (e.g.
* if the related file has been deleted on disk).
* @param {string} url Absolute URL of the related document
*/
function _handleRelatedDocumentDeleted(url) {
var liveDoc = _relatedDocuments[url];
if (liveDoc) {
delete _relatedDocuments[url];
}
if (_server) {
_server.remove(liveDoc);
}
_closeDocument(liveDoc);
}
/**
* Update the status. Triggers a statusChange event.
* @param {number} status new status
* @param {?string} closeReason Optional string key suffix to display to
* user when closing the live development connection (see LIVE_DEV_* keys)
*/
function _setStatus(status, closeReason) {
// Don't send a notification when the status didn't actually change
if (status === exports.status) {
return;
}
exports.status = status;
var reason = status === STATUS_INACTIVE ? closeReason : null;
exports.trigger(EVENT_STATUS_CHANGE, status, reason);
}
/**
* @private
* Close all live documents.
*/
function _closeDocuments() {
if (_liveDocument) {
_closeDocument(_liveDocument);
_liveDocument = undefined;
}
Object.keys(_relatedDocuments).forEach(function (url) {
_closeDocument(_relatedDocuments[url]);
delete _relatedDocuments[url];
});
// Clear all documents from request filtering
if (_server) {
_server.clear();
}
}
/**
* @private
* Returns the URL that we would serve the given path at.
* @param {string} path
* @return {string}
*/
function _resolveUrl(path) {
return _server && _server.pathToUrl(path);
}
/**
* @private
* Create a LiveDocument for a Brackets editor/document to manage communication between the
* editor and the browser.
* @param {Document} doc
* @param {Editor} editor
* @param {roots} roots
* @return {?LiveDocument} The live document, or null if this type of file doesn't support live editing.
*/
function _createLiveDocument(doc, editor, roots) {
var DocClass = _classForDocument(doc),
liveDocument;
if (!DocClass) {
return null;
}
liveDocument = new DocClass(_protocol, _resolveUrl, doc, editor, roots);
liveDocument.on("errorStatusChanged.livedev", function (event, hasErrors) {
if (isActive()) {
_setStatus(hasErrors ? STATUS_SYNC_ERROR : STATUS_ACTIVE);
}
});
return liveDocument;
}
/**
* Documents are considered to be out-of-sync if they are dirty and
* do not have "update while editing" support
* @param {Document} doc
* @return {boolean}
*/
function _docIsOutOfSync(doc) {
var liveDoc = _server && _server.get(doc.file.fullPath),
isLiveEditingEnabled = liveDoc && liveDoc.isLiveEditingEnabled();
return doc.isDirty && !isLiveEditingEnabled;
}
/**
* Handles a notification from the browser that a stylesheet was loaded into
* the live HTML document. If the stylesheet maps to a file in the project, then
* creates a live document for the stylesheet and adds it to _relatedDocuments.
* @param {$.Event} event
* @param {string} url The URL of the stylesheet that was added.
* @param {array} roots The URLs of the roots of the stylesheet (the css files loaded through <link>)
*/
function _styleSheetAdded(event, url, roots) {
var path = _server && _server.urlToPath(url),
alreadyAdded = !!_relatedDocuments[url];
// path may be null if loading an external stylesheet.
// Also, the stylesheet may already exist and be reported as added twice
// due to Chrome reporting added/removed events after incremental changes
// are pushed to the browser
if (!path || alreadyAdded) {
return;
}
var docPromise = DocumentManager.getDocumentForPath(path);
docPromise.done(function (doc) {
if ((_classForDocument(doc) === LiveCSSDocument) &&
(!_liveDocument || (doc !== _liveDocument.doc))) {
var liveDoc = _createLiveDocument(doc, doc._masterEditor, roots);
if (liveDoc) {
_server.add(liveDoc);
_relatedDocuments[doc.url] = liveDoc;
liveDoc.on("updateDoc", function (event, url) {
var path = _server.urlToPath(url),
doc = getLiveDocForPath(path);
doc._updateBrowser();
});
}
}
});
}
/**
* @private
* Close the connection and the associated window
* @param {boolean} doCloseWindow Use true to close the window/tab in the browser
* @param {?string} reason Optional string key suffix to display to user (see LIVE_DEV_* keys)
*/
function _close(doCloseWindow, reason) {
if (exports.status !== STATUS_INACTIVE) {
// Close live documents
_closeDocuments();
// Close all active connections
_protocol.closeAllConnections();
if (_server) {
// Stop listening for requests when disconnected
_server.stop();
// Dispose server
_server = null;
}
}
//TODO: implement closeWindow together with launchers.
// if (doCloseWindow) {
//
// }
_setStatus(STATUS_INACTIVE, reason || "explicit_close");
}
/**
* Closes all active connections.
* Returns a resolved promise for API compatibility.
* @return {$.Promise} A resolved promise
*/
function close() {
_close(true);
return new $.Deferred().resolve().promise();
}
/**
* @private
* Displays an error when the server for live development files can't be started.
*/
function _showLiveDevServerNotReadyError() {
Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_ERROR,
Strings.LIVE_DEVELOPMENT_ERROR_TITLE,
Strings.LIVE_DEV_SERVER_NOT_READY_MESSAGE
);
}
/**
* @private
* Creates the main live document for a given HTML document and notifies the server it exists.
* TODO: we should really maintain the list of live documents, not the server.
* @param {Document} doc
*/
function _createLiveDocumentForFrame(doc) {
// create live document
doc._ensureMasterEditor();
_liveDocument = _createLiveDocument(doc, doc._masterEditor);
if(!_liveDocument){
return;
}
_server.add(_liveDocument);
_server.addVirtualContentAtPath(
`${_liveDocument.doc.file.parentPath}${LiveDevProtocol.LIVE_DEV_REMOTE_SCRIPTS_FILE_NAME}`,
_protocol.getRemoteScriptContents());
_server.addVirtualContentAtPath(
`${_liveDocument.doc.file.parentPath}${LiveDevProtocol.LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME}`,
PageLoaderWorkerScript);
}
/**
* Launches the given URL in the default browser.
* @param {string} url
* @param {string} fullPath
* TODO: launchers for multiple browsers
*/
function _launch(url, fullPath) {
exports.trigger(EVENT_OPEN_PREVIEW_URL, {
url,
fullPath
});
}
/**
* @private
* Launches the given document in the browser, given that a live document has already
* been created for it.
* @param {Document} doc
*/
function _open(doc) {
if (doc && _liveDocument && doc === _liveDocument.doc) {
if (_server) {
// Launch the URL in the browser. If it's the first one to connect back to us,
// our status will transition to ACTIVE once it does so.
if (exports.status < STATUS_ACTIVE) {
_launch(_resolveUrl(doc.file.fullPath), doc.file.fullPath);
}
_protocol
// TODO: timeout if we don't get a connection within a certain time
.on("ConnectionConnect.livedev", function (event, msg) {
if (_protocol.getConnectionIds().length >= 1) {
// check the page that connection comes from matches the current live document session
const url = new URL(msg.url);
const urlString = `${url.origin}${url.pathname}`;
if (_liveDocument && urlString === _resolveUrl(_liveDocument.doc.file.fullPath)) {
_setStatus(STATUS_ACTIVE);
resetLPEditState();
}
}
Metrics.countEvent(Metrics.EVENT_TYPE.LIVE_PREVIEW, "connect",
`${_protocol.getConnectionIds().length}-preview`);
})
.on("ConnectionClose.livedev", function (event, {clientId}) {
exports.trigger(EVENT_CONNECTION_CLOSE, {clientId});
window.logger.livePreview.log(
"Live Preview: Phoenix received ConnectionClose, live preview left: ",
_protocol.getConnectionIds().length, clientId);
})
// extract stylesheets and create related LiveCSSDocument instances
.on("DocumentRelated.livedev", function (event, msg) {
var relatedDocs = msg.related;
var docs = Object.keys(relatedDocs.stylesheets);
docs.forEach(function (url) {
_styleSheetAdded(null, url, relatedDocs.stylesheets[url]);
});
})
// create new LiveCSSDocument if a new stylesheet is added
.on("StylesheetAdded.livedev", function (event, msg) {
_styleSheetAdded(null, msg.href, msg.roots);
})
// remove LiveCSSDocument instance when stylesheet is removed
.on("StylesheetRemoved.livedev", function (event, msg) {
_handleRelatedDocumentDeleted(msg.href);
})
.on(LiveDevProtocol.EVENT_LIVE_PREVIEW_CLICKED + ".livedev", function (event, msg) {
exports.trigger(LiveDevProtocol.EVENT_LIVE_PREVIEW_CLICKED, msg);
})
.on(LiveDevProtocol.EVENT_LIVE_PREVIEW_RELOAD + ".livedev", function (event, clients) {
exports.trigger(LiveDevProtocol.EVENT_LIVE_PREVIEW_RELOAD, clients);
});
} else {
console.error("LiveDevelopment._open(): No server active");
}
} else {
// a connection is in process but there is no current
// document, Eg. A project that has only markdown or images that doesnt have a live html/css document
}
}
/**
* @private
* Create the server in preparation for opening a live preview.
* @param {Document} doc The document we want the server for. Different servers handle
* different types of project (a static server for when no app server is configured,
* vs. a user server when there is an app server set in File > Project Settings).
*/
function _prepareServer(doc) {
const deferred = new $.Deferred();
let initialServePath = doc && doc.file.fullPath;
if(!initialServePath){
initialServePath = `${ProjectManager.getProjectRoot().fullPath}index.html`;
}
_server = LiveDevServerManager.getServer(initialServePath);
// Startup the server
const readyPromise = _server.readyToServe();
if (!readyPromise) {
_showLiveDevServerNotReadyError();
deferred.reject();
} else {
readyPromise.then(deferred.resolve, function () {
_showLiveDevServerNotReadyError();
deferred.reject();
});
}
return deferred.promise();
}
/**
* @private
* MainViewManager.currentFileChange event handler.
* When switching documents, close the current preview and open a new one.
*/
function _onFileChange() {
let doc = DocumentManager.getCurrentDocument();
if (!isActive() || !doc || livePreviewUrlPinned) {
return;
}
// close the current session and begin a new session
let docUrl = _resolveUrl(doc.file.fullPath),
isViewable = _server && _server.canServe(doc.file.fullPath);
if (_liveDocument && _liveDocument.doc.url !== docUrl && isViewable) {
open();
}
}
/**
* Open a live preview on the current docuemnt.
*/
async function open() {
let doc = DocumentManager.getCurrentDocument();
if(livePreviewUrlPinned){
doc = await jsPromise(DocumentManager.getDocumentForPath(currentPreviewFilePath));
}
// wait for server (StaticServer, Base URL or file:)
_prepareServer(doc)
.done(function () {
if(!_server){
return;
}
_setStatus(STATUS_CONNECTING);
// clear live doc and related docs
_closeDocuments();
// create new live doc
doc && _createLiveDocumentForFrame(doc);
if(_server.isActive()){
doc && _open(doc);
return;
}
// start server and listen for requests
_server.start()
.then(()=>{
// open browser to the url
doc && _open(doc);
});
})
.fail(function () {
console.log("Live preview: no document to preview.");
});
}
/**
* For files that don't support as-you-type live editing, but are loaded by live HTML documents
* (e.g. JS files), we want to reload the full document when they're saved.
* @param {$.Event} event
* @param {Document} doc
*/
function _onDocumentSaved(event, doc) {
if (!isActive() || !_server || !_liveDocument) {
return;
}
var absolutePath = doc.file.fullPath,
liveDocument = absolutePath && _server.get(absolutePath),
liveEditingEnabled = liveDocument && liveDocument.isLiveEditingEnabled && liveDocument.isLiveEditingEnabled();
// Skip reload if the saved document has live editing enabled
if (liveEditingEnabled) {
return;
}
// reload the page if the given document is a JS file related
// to the current live document.
if (_liveDocument.isRelated && _liveDocument.isRelated(absolutePath)) {
if (doc.getLanguage().getId() === "javascript") {
_setStatus(STATUS_RELOADING);
_protocol.reload();
}
}
}
/**
* For files that don't support as-you-type live editing, but are loaded by live HTML documents
* (e.g. JS files), we want to show a dirty indicator on the live development icon when they
* have unsaved changes, so the user knows s/he needs to save in order to have the page reload.
* @param {$.Event} event
* @param {Document} doc
*/
function _onDirtyFlagChange(event, doc) {
if (!isActive() || !_server || !_liveDocument || !_liveDocument.isRelated) {
return;
}
var absolutePath = doc.file.fullPath;
if (_liveDocument.isRelated && _liveDocument.isRelated(absolutePath)) {
// Set status to out of sync if dirty. Otherwise, set it to active status.
_setStatus(_docIsOutOfSync(doc) ? STATUS_OUT_OF_SYNC : STATUS_ACTIVE);
}
}
/**
* Sets the current transport mechanism to be used by the live development protocol
* (e.g. socket server, iframe postMessage, etc.)
* The low-level transport. Must provide the following methods:
*
* - start(): Initiates transport (eg. creates Web Socket server).
* - send(idOrArray, string): Dispatches the given protocol message (provided as a JSON string) to the given client ID
* or array of client IDs. (See the "connect" message for an explanation of client IDs.)
* - close(id): Closes the connection to the given client ID.
* - getRemoteScript(): Returns a script that should be injected into the page's HTML in order to handle the remote side
* of the transport. Should include the "<script>" tags. Should return null if no injection is necessary.
*
* It must also dispatch the following jQuery events:
*
* - "connect": When a target browser connects back to the transport. Must provide two parameters:
* - clientID - a unique number representing this connection
* - url - the URL of the page in the target browser that's connecting to us
* - "message": When a message is received by the transport. Must provide two parameters:
* - clientID - the ID of the client sending the message
* - message - the text of the message as a JSON string
* - "close": When the remote browser closes the connection. Must provide one parameter:
* - clientID - the ID of the client closing the connection
*
* @param {{launch: function(string), send: function(number|Array.<number>, string), close: function(number), getRemoteScript: function(): ?string}} transport
*/
function setTransport(transport) {
_protocol.setTransport(transport);
}
/**
* Initialize the LiveDevelopment module.
*/
function init(config) {
exports.config = config;
MainViewManager
.on("currentFileChange", _onFileChange);
DocumentManager
.on("documentSaved", _onDocumentSaved)
.on("dirtyFlagChange", _onDirtyFlagChange);
// Default transport for live connection messages - can be changed
setTransport(LivePreviewTransport);
// Initialize exports.status
_setStatus(STATUS_INACTIVE);
}
function getLiveDocForEditor(editor) {
if (!editor) {
return null;
}
return getLiveDocForPath(editor.document.file.fullPath);
}
/**
* Enable highlighting
*/
function showHighlight() {
var doc = getLiveDocForEditor(EditorManager.getActiveEditor());
if (doc && doc.updateHighlight) {
doc.updateHighlight();
}
}
/**
* Hide any active highlighting
*/
function hideHighlight() {
if (_protocol) {
_protocol.evaluate("_LD.hideHighlight()");
}
}
/**
* Redraw highlights
*/
function redrawHighlight() {
if (_protocol) {
_protocol.evaluate("_LD.redrawHighlights()");
}
}
/**
* Check if live preview boxes are currently visible
*/
function hasVisibleLivePreviewBoxes() {
if (_protocol) {
return _protocol.evaluate("_LD.hasVisibleLivePreviewBoxes()");
}
return false;
}
/**
* Dismiss live preview boxes like info box, options box, AI box
*/
function dismissLivePreviewBoxes() {
if (_protocol) {
_protocol.evaluate("_LD.enableHoverListeners()"); // so that if hover lock is there it will get cleared
_protocol.evaluate("_LD.dismissUIAndCleanupState()");
}
}
/**
* Register event handlers in the remote browser for live preview functionality
*/
function registerHandlers() {
if (_protocol) {
_protocol.evaluate("_LD.registerHandlers()");
}
}
/**
* Update configuration in the remote browser
*/
function updateConfig(configJSON) {
if (_protocol) {
_protocol.evaluate("_LD.updateConfig(" + JSON.stringify(configJSON) + ")");
}
}
/**
* this function is to completely reset the live preview edit
* its done so that when live preview is opened/popped out, we can re-update the config so that
* there are no stale markers and edit works perfectly
*/
function resetLPEditState() {
if (_protocol) {
_protocol.evaluate("_LD.resetState()");
}
}
/**
* Originally unload and reload agents. It doesn't apply for this new implementation.
* @return {jQuery.Promise} Already resolved promise.
*/
function reconnect() {
return $.Deferred().resolve();
}
/**
* Reload current page in all connected browsers.
*/
function reload() {
if (_protocol) {
_protocol.reload();
}
}
/**
* @param urlPinned {boolean}
*/
function setLivePreviewPinned(urlPinned, currentPinnedFilePath) {
livePreviewUrlPinned = urlPinned;
currentPreviewFilePath = currentPinnedFilePath;
}
// for unit testing only
function getCurrentLiveDoc() {
return _liveDocument;
}
/**
* Returns an array of the client IDs that are being managed by this live document.
* @return {Array.<number>}
*/
function getConnectionIds() {
return _protocol.getConnectionIds();
}
function getLivePreviewDetails() {
return {
liveDocument: _liveDocument,
URL: _liveDocument ? _resolveUrl(_liveDocument.doc.file.fullPath) : null
};
}
EventDispatcher.makeEventDispatcher(exports);
// For unit testing
exports._server = _server;
// Events
exports.EVENT_OPEN_PREVIEW_URL = EVENT_OPEN_PREVIEW_URL;
exports.EVENT_CONNECTION_CLOSE = EVENT_CONNECTION_CLOSE;
exports.EVENT_STATUS_CHANGE = EVENT_STATUS_CHANGE;
exports.EVENT_LIVE_PREVIEW_CLICKED = LiveDevProtocol.EVENT_LIVE_PREVIEW_CLICKED;
exports.EVENT_LIVE_PREVIEW_RELOAD = LiveDevProtocol.EVENT_LIVE_PREVIEW_RELOAD;
// Export public functions
exports.open = open;
exports.close = close;
exports.reconnect = reconnect;
exports.reload = reload;
exports.getLiveDocForPath = getLiveDocForPath;
exports.showHighlight = showHighlight;
exports.hideHighlight = hideHighlight;
exports.redrawHighlight = redrawHighlight;
exports.hasVisibleLivePreviewBoxes = hasVisibleLivePreviewBoxes;
exports.dismissLivePreviewBoxes = dismissLivePreviewBoxes;
exports.registerHandlers = registerHandlers;
exports.updateConfig = updateConfig;
exports.init = init;
exports.isActive = isActive;
exports.setLivePreviewPinned= setLivePreviewPinned;
exports.getCurrentLiveDoc = getCurrentLiveDoc;
exports.getLivePreviewDetails = getLivePreviewDetails;
exports.getConnectionIds = getConnectionIds;
exports.setTransport = setTransport;
});