Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 8edfd23

Browse files
committed
Merge pull request #5207 from adobe/jasonsanjose/issue-5185
Fix live preview reload for documentSaved events
2 parents 0b31ff4 + be79158 commit 8edfd23

8 files changed

Lines changed: 204 additions & 72 deletions

File tree

src/LiveDevelopment/Documents/CSSDocument.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ define(function CSSDocumentModule(require, exports, module) {
173173
// "Instrumentation" is always enabled for CSS, we make no modifications
174174
};
175175

176+
/**
177+
* Returns true if document edits appear live in the connected browser
178+
* @return {boolean}
179+
*/
180+
CSSDocument.prototype.isLiveEditingEnabled = function () {
181+
return true;
182+
};
183+
176184
/**
177185
* Returns a JSON object with HTTP response overrides
178186
* @returns {{body: string}}

src/LiveDevelopment/Documents/HTMLDocument.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,17 @@ define(function HTMLDocumentModule(require, exports, module) {
100100
this._instrumentationEnabled = enabled;
101101
};
102102

103+
/**
104+
* Returns true if document edits appear live in the connected browser
105+
* @return {boolean}
106+
*/
107+
HTMLDocument.prototype.isLiveEditingEnabled = function () {
108+
return this._instrumentationEnabled;
109+
};
110+
103111
/**
104112
* Returns a JSON object with HTTP response overrides
105-
* @returns {{body: string}}
113+
* @return {{body: string}}
106114
*/
107115
HTMLDocument.prototype.getResponseData = function getResponseData(enabled) {
108116
var body;

src/LiveDevelopment/LiveDevelopment.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,14 @@ define(function LiveDevelopment(require, exports, module) {
151151

152152
var _liveDocument; // the document open for live editing.
153153
var _relatedDocuments; // CSS and JS documents that are used by the live HTML document
154-
var _server; // current live dev server
155154
var _openDeferred; // promise returned for each call to open()
156155

156+
/**
157+
* Current live preview server
158+
* @type {BaseServer}
159+
*/
160+
var _server;
161+
157162
function _isHtmlFileExt(ext) {
158163
return (FileUtils.isStaticHtmlFileExt(ext) ||
159164
(ProjectManager.getBaseUrl() && FileUtils.isServerHtmlFileExt(ext)));
@@ -185,23 +190,11 @@ define(function LiveDevelopment(require, exports, module) {
185190
}
186191

187192
function getLiveDocForPath(path) {
188-
var docsToSearch = [];
189-
if (_relatedDocuments) {
190-
docsToSearch = docsToSearch.concat(_relatedDocuments);
191-
}
192-
if (_liveDocument) {
193-
docsToSearch = docsToSearch.concat(_liveDocument);
193+
if (!_server) {
194+
return undefined;
194195
}
195-
var foundDoc;
196-
docsToSearch.some(function matchesPath(ele) {
197-
if (ele.doc.file.fullPath === path) {
198-
foundDoc = ele;
199-
return true;
200-
}
201-
return false;
202-
});
203-
204-
return foundDoc;
196+
197+
return _server.get(path);
205198
}
206199

207200
function getLiveDocForEditor(editor) {
@@ -1106,10 +1099,29 @@ define(function LiveDevelopment(require, exports, module) {
11061099
}
11071100
}
11081101

1109-
/** Triggered by a document saved from the DocumentManager */
1102+
/**
1103+
* Triggered by a documentSaved event from DocumentManager.
1104+
* @param {$.Event} event
1105+
* @param {Document} doc
1106+
*/
11101107
function _onDocumentSaved(event, doc) {
1111-
if (doc && Inspector.connected() && _classForDocument(doc) !== CSSDocument &&
1112-
agents.network && agents.network.wasURLRequested(doc.url)) {
1108+
if (!Inspector.connected() || !_server) {
1109+
return;
1110+
}
1111+
1112+
var absolutePath = doc.file.fullPath,
1113+
liveDocument = absolutePath && _server.get(absolutePath),
1114+
liveEditingEnabled = liveDocument && liveDocument.isLiveEditingEnabled && liveDocument.isLiveEditingEnabled();
1115+
1116+
// Skip reload if the saved document has live editing enabled
1117+
if (liveEditingEnabled) {
1118+
return;
1119+
}
1120+
1121+
var documentUrl = _server.pathToUrl(absolutePath),
1122+
wasRequested = agents.network && agents.network.wasURLRequested(documentUrl);
1123+
1124+
if (wasRequested) {
11131125
// Unload and reload agents before reloading the page
11141126
reconnect();
11151127

src/LiveDevelopment/Servers/BaseServer.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ define(function (require, exports, module) {
160160
return true;
161161
};
162162

163-
BaseServer.prototype._documentKey = function (liveDocument) {
164-
return "/" + encodeURI(this._pathResolver(liveDocument.doc.file.fullPath));
163+
BaseServer.prototype._documentKey = function (absolutePath) {
164+
return "/" + encodeURI(this._pathResolver(absolutePath));
165165
};
166166

167167
/**
@@ -170,7 +170,7 @@ define(function (require, exports, module) {
170170
*/
171171
BaseServer.prototype.add = function (liveDocument) {
172172
// use the project relative path as a key to lookup requests
173-
var key = this._documentKey(liveDocument);
173+
var key = this._documentKey(liveDocument.doc.file.fullPath);
174174

175175
this._setDocInfo(liveDocument);
176176
this._liveDocuments[key] = liveDocument;
@@ -181,13 +181,23 @@ define(function (require, exports, module) {
181181
* @param {Object} liveDocument
182182
*/
183183
BaseServer.prototype.remove = function (liveDocument) {
184-
var key = this._liveDocuments[this._documentKey(liveDocument)];
184+
var key = this._liveDocuments[this._documentKey(liveDocument.doc.file.fullPath)];
185185

186186
if (key) {
187187
delete this._liveDocuments[key];
188188
}
189189
};
190190

191+
/**
192+
* Lookup a live document using it's full path key
193+
* @param {string} path Absolute path to covert to a URL
194+
* @param {?Object} liveDocument Returns a live document or undefined if a
195+
* document does not exist for the path.
196+
*/
197+
BaseServer.prototype.get = function (path) {
198+
return this._liveDocuments[this._documentKey(path)];
199+
};
200+
191201
/**
192202
* Clears all live documents currently attached to the server
193203
*/

test/SpecRunner.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,10 @@ define(function (require, exports, module) {
382382
window.document.body.addEventListener("click", function (e) {
383383
// Check parents too, in case link has inline formatting tags
384384
var node = e.target, url;
385-
console.log(1);
385+
386386
while (node) {
387-
console.log(node.tagName);
388387
if (node.tagName === "A") {
389388
url = node.getAttribute("href");
390-
console.log(url);
391389
if (url && url.match(/^http/)) {
392390
NativeApp.openURLInDefaultBrowser(url);
393391
e.preventDefault();

test/spec/LiveDevelopment-test-files/simple1.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>Simple Test</title>
66
<link rel="stylesheet" href="simpleShared.css">
77
<link rel="stylesheet" href="simple1.css">
8+
<script type="text/javascript" src="simple1.js"></script>
89
</head>
910

1011
<body class="testClass">

test/spec/LiveDevelopment-test-files/simple1.js

Whitespace-only changes.

0 commit comments

Comments
 (0)