EditAgent propagates changes from the in-document editor to the source document.
Find changed characters
function _findChangedCharacters(oldValue, value) {
if (oldValue === value) {
return undefined;
}
var length = oldValue.length;
var index = 0;
// find the first character that changed
var i;
for (i = 0; i < length; i++) {
if (value[i] !== oldValue[i]) {
break;
}
}
index += i;
value = value.substr(i);
length -= i;
// find the last character that changed
for (i = 0; i < length; i++) {
if (value[value.length - 1 - i] !== oldValue[oldValue.length - 1 - i]) {
break;
}
}
length -= i;
value = value.substr(0, value.length - i);
return { from: index, to: index + length, text: value };
}
// 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(_editedNode.value, res.characterData);
if (change) {
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(_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);
}