Modules (180)

EditAgent

Description

EditAgent propagates changes from the in-document editor to the source document.

Dependencies

Functions

Private

_findChangedCharacters

Find changed characters

old string
value
changed string
value
Returns: from,to,text
    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);
    }
Public API

load

Initialize the agent

    function load() {
        RemoteAgent.on("edit.EditAgent", _onRemoteEdit);
    }
Public API

unload

Initialize the agent

    function unload() {
        RemoteAgent.off(".EditAgent");
    }

    // Export public functions
    exports.load = load;
    exports.unload = unload;
});