Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/LiveDevelopment/Agents/DOMAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ define(function DOMAgent(require, exports, module) {

var Inspector = require("LiveDevelopment/Inspector/Inspector");
var RemoteAgent = require("LiveDevelopment/Agents/RemoteAgent");
var EditAgent = require("LiveDevelopment/Agents/EditAgent");
var DOMNode = require("LiveDevelopment/Agents/DOMNode");
var DOMHelpers = require("LiveDevelopment/Agents/DOMHelpers");

Expand Down Expand Up @@ -269,7 +270,10 @@ define(function DOMAgent(require, exports, module) {
value += text;
value += node.value.substr(to - node.location);
node.value = value;
Inspector.DOM.setNodeValue(node.nodeId, node.value);
if (!EditAgent.isEditing) {
// only update the DOM if the change was not caused by the edit agent
Inspector.DOM.setNodeValue(node.nodeId, node.value);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid changes that occur from the editor on the remote site to propagate back there, the DOMAgent must not push any changed node values if they have come from there (through the EditAgent).

} else {
console.warn("Changing non-text nodes not supported.");
}
Expand Down
45 changes: 35 additions & 10 deletions src/LiveDevelopment/Agents/EditAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ define(function EditAgent(require, exports, module) {

var EditorManager = require("editor/EditorManager");

var _editedNode;

/** Find changed characters
* @param {string} old value
* @param {string} changed value
Expand Down Expand Up @@ -74,28 +76,51 @@ define(function EditAgent(require, exports, module) {
return { from: index, to: index + length, text: value };
}

// Remote Event: Go to the given source node
function _onRemoteEdit(event, res) {
// res = {nodeId, name, value}
var node = DOMAgent.nodeWithId(res.nodeId);
node = node.children[0];
if (!node.location) {
// WebInspector Event: DOM.characterDataModified
function _onCharacterDataModified(event, res) {
// res = {nodeId, characterData}
if (_editedNode.nodeId !== res.nodeId) {
return;
}

GotoAgent.open(DOMAgent.url);
var editor = EditorManager.getCurrentFullEditor();
var codeMirror = editor._codeMirror;
var change = _findChangedCharacters(node.value, res.value);
var change = _findChangedCharacters(_editedNode.value, res.characterData);
if (change) {
var from = codeMirror.posFromIndex(node.location + change.from);
var to = codeMirror.posFromIndex(node.location + change.to);
var from = codeMirror.posFromIndex(_editedNode.location + change.from);
var to = codeMirror.posFromIndex(_editedNode.location + change.to);
exports.isEditing = true;
editor.document.replaceRange(change.text, from, to);
exports.isEditing = false;

var newPos = codeMirror.posFromIndex(node.location + change.from + change.text.length);
var newPos = codeMirror.posFromIndex(_editedNode.location + change.from + change.text.length);
editor.setCursorPos(newPos.line, newPos.ch);
}
}

// Remote Event: Go to the given source node
function _onRemoteEdit(event, res) {
// res = {nodeId, name, value}

// detach from DOM change events
if (res.value === "0") {
$(Inspector.DOM).off(".EditAgent");
return;
}

// find and store the edited node
var node = DOMAgent.nodeWithId(res.nodeId);
node = node.children[0];
if (!node.location) {
return;
}
_editedNode = node;

// attach to character data modified events
$(Inspector.DOM).on("characterDataModified.EditAgent", _onCharacterDataModified);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EditAgent now relies on a data-ld-edit event to enable/disable the editor + on DOM. characterDataModified to realize the actual changes.

/** Initialize the agent */
function load() {
$(RemoteAgent).on("edit.EditAgent", _onRemoteEdit);
Expand Down
4 changes: 1 addition & 3 deletions src/LiveDevelopment/Agents/GotoAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ define(function GotoAgent(require, exports, module) {
}
for (i in node.events) {
var trace = node.events[i];
if (trace.children.length > 0) {
_makeJSTarget(targets, trace.children[0].callFrames[0]);
}
_makeJSTarget(targets, trace.callFrames[0]);
}
for (i in res.matchedCSSRules.reverse()) {
_makeCSSTarget(targets, res.matchedCSSRules[i]);
Expand Down
2 changes: 1 addition & 1 deletion src/LiveDevelopment/Agents/HighlightAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ define(function HighlightAgent(require, exports, module) {
if (res.value === "1") {
node = DOMAgent.nodeWithId(res.nodeId);
}
$(exports).triggerHandler("highlight", node);
$(exports).triggerHandler("highlight", [node]);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix: jQuery event parameters must be given as an array or bad things happen!

}

/** Hide in-browser highlighting */
Expand Down
3 changes: 2 additions & 1 deletion src/LiveDevelopment/Agents/RemoteAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ define(function RemoteAgent(require, exports, module) {

var $exports = $(exports);

var LiveDevelopment = require("LiveDevelopment/LiveDevelopment");
var Inspector = require("LiveDevelopment/Inspector/Inspector");

var _load; // deferred load
Expand All @@ -48,7 +49,7 @@ define(function RemoteAgent(require, exports, module) {
var request = new XMLHttpRequest();
request.open("GET", "LiveDevelopment/Agents/RemoteFunctions.js");
request.onload = function onLoad() {
var run = "window._LD=" + request.response + "()";
var run = "window._LD=" + request.response + "(" + LiveDevelopment.config.experimental + ")";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By passing the experimental to RemoteFunctions.js, we can avoid commenting out experimental stuff there.

Inspector.Runtime.evaluate(run, function onEvaluate(res) {
console.assert(!res.wasThrown, res.result.description);
_objectId = res.result.objectId;
Expand Down
Loading