{"size": 1548, "ext": "js", "max_stars_repo_path": "duna-web/www/js/components/codemirror.js", "max_stars_repo_name": "noloerino/mars", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "duna-web/www/js/components/codemirror.js", "max_issues_repo_name": "noloerino/mars", "max_issues_repo_issues_event_min_datetime": "2020-12-04T01:26:02.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T11:08:00.000Z", "max_forks_repo_path": "duna-web/www/js/components/codemirror.js", "max_forks_repo_name": "noloerino/mars", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 8, "max_forks_count": null, "avg_line_length": 35.1818181818, "max_line_length": 94, "alphanum_fraction": 0.7131782946}
// These extra CSS imports probably would go over poorly with typescript, so we expose
// a smaller interface
// https://github.com/codemirror/CodeMirror/issues/5484
import CodeMirror from "codemirror/lib/codemirror.js";
// https://github.com/survivejs-demos/react-demos/blob/master/demos/index.js
// https://stackoverflow.com/questions/55685029/codemirror-with-vanilla-typescript-and-webpack
import "codemirror/lib/codemirror.css";
import "codemirror/mode/gas/gas";
import "codemirror/theme/elegant.css";
// import "codemirror/theme/monokai.css";

const STORAGE_MAIN_FILE_KEY = "mainfile";

export class CodeMirrorWrapper {
  constructor() {
    // TODO define custom types for RISC-V etc.
    // GAS supports ARM and x86-64
    this.localStorage = window.localStorage;
    let textArea = document.getElementById("code");
    // This needs to be set instead of the "value" parameter of the cm constructor
    textArea.value = this.localStorage.getItem(STORAGE_MAIN_FILE_KEY);
    let cm = CodeMirror.fromTextArea(textArea, {
      mode: "gas",
      lineNumbers: "true",
      theme: "elegant",
    });
    CodeMirror.commands.save = () => {
      console.log("Saved file at " + new Date().toTimeString());
      this.save();
    };
    this.codeMirror = cm;
  }

  getProgram() {
    return this.codeMirror.getValue();
  }

  // Saves the text of the main file in local storage for the time being
  // this will change when multiple files become supported
  save() {
    this.localStorage.setItem(STORAGE_MAIN_FILE_KEY, this.getProgram());
  }
}
