<commit_before>// This file is the friendlycode half of a proxy layer between Thimble functionality
// (like publishing) and bramble's codemirror instance. It was designed to map to existing
// friendlycode use of codemirror, with the exception of duplicate functionality
// between bramble and friendlycode that bramble already provides (or will)
/*global define */
define(["backbone-events", "fc/prefs", "fc/bramble-ui-bridge"],
  function(BackboneEvents, Preferences, BrambleUIBridge) {
  "use strict";

  var eventCBs = {
    "change": [],
    "reparse": [],
    "loaded": [],
    "viewlink": []
  };
  var telegraph;
  var iframe;

  function BrambleProxy(place, options) {
    var latestSource;
    var prefSize = Preferences.get("textSize");
    var that = this;
    var lastLine = 0;
    var scrollInfo;

    var editorHost = this.editorHost = options.editorHost;

    function communicateEditMessage(fn) {
      var argLen = arguments.length;
      var callback = typeof arguments[argLen - 1] === "function" ? arguments[argLen - 1] : undefined;
      var params = Array.prototype.slice.call(arguments, 1, callback ? argLen - 1 : argLen);

      if(callback) {
        window.addEventListener("message", function editReceiver(e) {
          var message = JSON.parse(e.data);

          // Make sure we only take postMessages seriously
          // when they come from our editor
          if (e.origin !== editorHost) {
            return;
          }

          if(message.type !== "bramble:edit" || message.fn !== fn) {
            return;
          }

          window.removeEventListener("message", editReceiver);

          callback(message.value);
        });
      }

      telegraph.postMessage(JSON.stringify({
        type: "bramble:edit",
        fn: fn,
        params: params
      }), editorHost);
    }

    // Event listening for proxied event messages from our editor iframe.
    window.addEventListener("message", function(evt) {
      return;



      // Set the communication channel to our iframe
      // now that it's signaled that it has content
      // by sending a postMessage
      telegraph = iframe.contentWindow;
      var message = JSON.parse(evt.data);

      // Make sure we only take postMessages seriously
      // when they come from our editor
      if (evt.origin !== editorHost) {
        return;
      }

      if (typeof message.type !== "string" || message.type.indexOf("bramble") === -1) {
        return;
      }

      if (message.type === "bramble:change") {
        latestSource = message.sourceCode;
        lastLine = message.lastLine;
        if(message.scrollInfo) scrollInfo = message.scrollInfo;

        eventCBs["change"].forEach(function(cb) {
          cb();
        });
        return;
      }

      if (message.type === "bramble:init") {
        telegraph.postMessage(JSON.stringify({
          type: "bramble:init",
          source: latestSource
        }), editorHost);
        return;
      }

      if (message.type === "bramble:loaded") {
        eventCBs["loaded"].forEach(function(cb) {
          cb();
        });

        that.executeCommand("_fontSize", { data : prefSize });
        that.executeCommand("_spaceUnits", { data : 2 });
        return;
      }

      if (message.type === "bramble:viewportChange") {
        scrollInfo = message.scrollInfo;
      }
    });

    // Create CodeMirror-like interface for friendlycode to use
    this.getValue = function() {
      return latestSource;
    };

    // Stub for CodeMirror's method to get the last line in the editor
    this.lastLine = function() {
      return lastLine;
    };

    this.getScrollInfo = function() {
      return scrollInfo;
    };

    this.lineAtHeight = function(height, mode, callback) {
      communicateEditMessage("lineAtHeight", height, mode, callback);
    };

    this.setGutterMarker = function(line, gutterID, element, callback) {
      communicateEditMessage("setGutterMarker", line, gutterID, element, callback);
    };

    this.addLineClass = function(line, where, cssClass, callback) {
      communicateEditMessage("addLineClass", line, where, cssClass, callback);
    };

    this.removeLineClass = function(line, where, cssClass, callback) {
      communicateEditMessage("removeLineClass", line, where, cssClass, callback);
    };

    this.heightAtLine = function(line, mode, callback) {
      communicateEditMessage("heightAtLine", line, mode, callback);
    };

    this.getLineHeight = function(selector, callback) {
      communicateEditMessage("getLineHeight", selector, callback);
    };

    this.scrollTo = function(x, y) {
      communicateEditMessage("scrollTo", x, y);
    };

    this.init = function(make) {
      // Start loading Bramble
      Bramble.load("#webmaker-bramble",{
        url: "http://localhost:8000/src/index.html",
        hideUntilReady: false,
        debug: true
      });

      // Setup the filesystem while Bramble is loading
      var fs = Bramble.getFileSystem();
      fs.mkdir("/project", function(err) {
        // If we run this multiple times, the dir will exist
        if (err & err.code !== "EEXIST") {
          throw err;
        }

        var html = "<html>\n"                     +
                   "  <head>\n"                   +
                   "    <title>Bramble</title>\n" +
                   "  </head>\n"                  +
                   "  <body>\n"                   +<commit_msg>Add event listeners for Bramble<commit_after>// This file is the friendlycode half of a proxy layer between Thimble functionality
// (like publishing) and bramble's codemirror instance. It was designed to map to existing
// friendlycode use of codemirror, with the exception of duplicate functionality
// between bramble and friendlycode that bramble already provides (or will)
/*global define */
define(["backbone-events", "fc/prefs", "fc/bramble-ui-bridge"],
  function(BackboneEvents, Preferences, BrambleUIBridge) {
  "use strict";

  var eventCBs = {
    "change": [],
    "reparse": [],
    "loaded": [],
    "viewlink": []
  };
  var telegraph;
  var iframe;

  function BrambleProxy(place, options) {
    var latestSource;
    var prefSize = Preferences.get("textSize");
    var that = this;
    var lastLine = 0;
    var scrollInfo;

    var editorHost = this.editorHost = options.editorHost;

    function communicateEditMessage(fn) {
      var argLen = arguments.length;
      var callback = typeof arguments[argLen - 1] === "function" ? arguments[argLen - 1] : undefined;
      var params = Array.prototype.slice.call(arguments, 1, callback ? argLen - 1 : argLen);

      if(callback) {
        window.addEventListener("message", function editReceiver(e) {
          var message = JSON.parse(e.data);

          // Make sure we only take postMessages seriously
          // when they come from our editor
          if (e.origin !== editorHost) {
            return;
          }

          if(message.type !== "bramble:edit" || message.fn !== fn) {
            return;
          }

          window.removeEventListener("message", editReceiver);

          callback(message.value);
        });
      }

      telegraph.postMessage(JSON.stringify({
        type: "bramble:edit",
        fn: fn,
        params: params
      }), editorHost);
    }

    // Event listening for proxied event messages from our editor iframe.
    window.addEventListener("message", function(evt) {
      return;



      // Set the communication channel to our iframe
      // now that it's signaled that it has content
      // by sending a postMessage
      telegraph = iframe.contentWindow;
      var message = JSON.parse(evt.data);

      // Make sure we only take postMessages seriously
      // when they come from our editor
      if (evt.origin !== editorHost) {
        return;
      }

      if (typeof message.type !== "string" || message.type.indexOf("bramble") === -1) {
        return;
      }

      if (message.type === "bramble:change") {
        latestSource = message.sourceCode;
        lastLine = message.lastLine;
        if(message.scrollInfo) scrollInfo = message.scrollInfo;

        eventCBs["change"].forEach(function(cb) {
          cb();
        });
        return;
      }

      if (message.type === "bramble:init") {
        telegraph.postMessage(JSON.stringify({
          type: "bramble:init",
          source: latestSource
        }), editorHost);
        return;
      }

      if (message.type === "bramble:loaded") {
        eventCBs["loaded"].forEach(function(cb) {
          cb();
        });

        that.executeCommand("_fontSize", { data : prefSize });
        that.executeCommand("_spaceUnits", { data : 2 });
        return;
      }

      if (message.type === "bramble:viewportChange") {
        scrollInfo = message.scrollInfo;
      }
    });

    // Create CodeMirror-like interface for friendlycode to use
    this.getValue = function() {
      return latestSource;
    };

    // Stub for CodeMirror's method to get the last line in the editor
    this.lastLine = function() {
      return lastLine;
    };

    this.getScrollInfo = function() {
      return scrollInfo;
    };

    this.lineAtHeight = function(height, mode, callback) {
      communicateEditMessage("lineAtHeight", height, mode, callback);
    };

    this.setGutterMarker = function(line, gutterID, element, callback) {
      communicateEditMessage("setGutterMarker", line, gutterID, element, callback);
    };

    this.addLineClass = function(line, where, cssClass, callback) {
      communicateEditMessage("addLineClass", line, where, cssClass, callback);
    };

    this.removeLineClass = function(line, where, cssClass, callback) {
      communicateEditMessage("removeLineClass", line, where, cssClass, callback);
    };

    this.heightAtLine = function(line, mode, callback) {
      communicateEditMessage("heightAtLine", line, mode, callback);
    };

    this.getLineHeight = function(selector, callback) {
      communicateEditMessage("getLineHeight", selector, callback);
    };

    this.scrollTo = function(x, y) {
      communicateEditMessage("scrollTo", x, y);
    };

    this.init = function(make) {
      // Start loading Bramble
      Bramble.load("#webmaker-bramble",{
        url: "http://localhost:8000/src/index.html",
      });

      // Event listeners
      Bramble.on("ready", function(bramble) {
        console.log("ready");
        // For debugging, attach to window.
        window.bramble = bramble;
      });

      Bramble.on("error", function(e, err) {
        console.log("error", err);
      })

      Bramble.on("readyStateChange", function(previous, current) {
        console.log("readyStateChange", previous, current);
      });

      // Setup the filesystem while Bramble is loading
      var fs = Bramble.getFileSystem();
      fs.mkdir("/project", function(err) {
        // If we run this multiple times, the dir will exist
        if (err & err.code !== "EEXIST") {
          throw err;
        }

        var html = "<html>\n"                     +
                   "  <head>\n"                   +
                   "    <title>Bramble</title>\n" +
                   "  </head>\n"                  +
                   "  <body>\n"                   +
{'fraction_non_alphanumeric': 0.12227656736327257, 'fraction_numerical': 0.001956425077812361, 'mean_word_length': 2.278717201166181, 'pattern_counts': {'":': 8, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '816057', 'n_tokens_mistral': 3209, 'n_tokens_neox': 3055, 'n_words': 795}