text: document.addEventListener("DOMContentLoaded", function(event) {
  
    var componentManager;
    var workingNote, clientData;
    var lastValue, lastUUID;
    var editor;
    var ignoreTextChange = false;
    var initialLoad = true;
  
    function loadComponentManager() {
      var permissions = [{name: "stream-context-item"}]
      componentManager = new ComponentManager(permissions, function(){
        // on ready
        var platform = componentManager.platform;
        if (platform) {
          document.body.classList.add(platform);
        }
  
        // only use CodeMirror selection color if we're not on mobile.
        editor.setOption("styleSelectedText", !componentManager.isMobile);
      });
  
      componentManager.streamContextItem((note) => {
        onReceivedNote(note);
      });
    }
  
    function save() {
      if(workingNote) {
        // Be sure to capture this object as a variable, as this.note may be reassigned in `streamContextItem`, so by the time
        // you modify it in the presave block, it may not be the same object anymore, so the presave values will not be applied to
        // the right object, and it will save incorrectly.
        let note = workingNote;
  
        componentManager.saveItemWithPresave(note, () => {
          lastValue = editor.getValue();
          note.content.text = lastValue;
          note.clientData = clientData;
  
          // clear previews
          note.content.preview_plain = null;
          note.content.preview_html = null;
        });
      }
    }
  
    function onReceivedNote(note) {
      if(note.uuid !== lastUUID) {
        // Note changed, reset last values
        lastValue = null;
        initialLoad = true;
        lastUUID = note.uuid;
      }
  
      workingNote = note;
  
      // Only update UI on non-metadata updates.
      if(note.isMetadataUpdate) {
        return;
      }
  
      clientData = note.clientData;
  
      if(editor) {
        if(note.content.text !== lastValue) {
          ignoreTextChange = true;
          editor.getDoc().setValue(workingNote.content.text);
          ignoreTextChange = false;
        }
  
        if(initialLoad) {
          initialLoad = false;
          editor.getDoc().clearHistory();
        }
      }
    }
  
    function loadEditor() {
      editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        mode: "markdown",
        lineWrapping: true,
        extraKeys: {"Alt-F": "findPersistent"}
      });
      editor.setSize("100%", "100%");
  
      editor.on("change", function(){
        if(ignoreTextChange) {return;}
        save();
      });
    }
  
    loadEditor();
    loadComponentManager();
  });
  