Alice

 view release on metacpan or  search on metacpan

share/static/alice.js  view on Meta::CPAN

    this.setValue(this.getCommand(this.index));
  },

  getCommand: function(index) {
    if (index == -1) {
      return this.buffer;
    } else {
      return this.history[index];
    }
  },

  resize: function() {
    (function() {
      var height = this.getContentHeight();
      if (height == 0) {
        this.element.setStyle({ height: null, top: 0 });
      } else if (height <= 150) {
        this.element.setStyle({ height: height + "px", top: "0px" });
      }
    }).bind(this).defer();
  },

  getContentHeight: function() {
    var element = new Element("div").setStyle({
      position:   "absolute",
      visibility: "hidden",
      left:       "-" + this.element.getWidth() + "px",
      width:      this.element.getWidth() - 7 + "px",
      fontFamily: this.element.getStyle("fontFamily"),
      fontSize:   this.element.getStyle("fontSize"),
      lineHeight: this.element.getStyle("lineHeight"),
      whiteSpace: "pre-wrap",
      wordWrap:   "break-word"
    });

    if (this.editor) element.addClassName("editor");

    var value = this.getValue();
    element.update(value.replace(/\n$/, "\n\n").replace("\n", "<br>"));
    $(document.body).insert(element);

    var height = element.getHeight();
    element.remove();
    return height;
  },

  canContentEditable: function() {
    var element = new Element("div", {contentEditable: "true"});
    return  ! (element.contentEditable == null || this.application.isMobile || Prototype.Browser.IE);
  },

  updateRange: function (e) {
    var selection = window.getSelection();
    if (selection.rangeCount > 0) {
      var range = selection.getRangeAt(0);
      this.range = range;
    }
  },

  pasteHandler: function(e) {
    if (!e.clipboardData) return;

    var items = e.clipboardData.items;
    if (items) {
      var output = "";
      for (var i=0; i < items.length; i++) {
        var blob = items[i].getAsFile();
        if (blob && blob.type.match(/image/)) {
          e.stop();
          var fd = new FormData();
          fd.append("image", blob);
          fd.append("key", "f1f60f1650a07bfe5f402f35205dffd4");
          var xhr = new XMLHttpRequest();
          xhr.open("POST", "http://api.imgur.com/2/upload.json");
          xhr.onload = function() {
            var url = xhr.responseText.evalJSON();
            this.editor.insertHTML(url.upload.links.original);
            this.updateRange();
          }.bind(this);
          xhr.send(fd);
          return;
        }
      }
    }

    var text = e.clipboardData.getData("Text");
    if (text) {
      e.preventDefault();
      text = text.escapeHTML().replace(/\n+/g, "<br>\n");
      this.editor.insertHTML(text);
      this.updateRange();
      return;
    }

    var url = e.clipboardData.getData("URL");
    if (url) {
      e.preventDefault();
      this.editor.insertHTML(url);
      this.updateRange();
      return;
    }

  }
});
Alice.Keyboard = Class.create({
  initialize: function(application) {
    this.application = application;
    this.isMac = navigator.platform.match(/mac/i);
    this.lastCycle = 0;
    this.cycleDelay = 300;
    this.enable();

    if (!this.application.isMobile) {
      this.shortcut("Cmd+C", { propagate: true });
      this.shortcut("Ctrl+C", { propagate: true });
      this.shortcut("Cmd+B");
      this.shortcut("Cmd+I");
      this.shortcut("Cmd+Shift+U");
      this.shortcut("Opt+Up");
      this.shortcut("Opt+Down");
      this.shortcut("Cmd+Shift+M");
      this.shortcut("Cmd+Shift+J");
      this.shortcut("Cmd+Shift+K");
      this.shortcut("Cmd+K");
      this.shortcut("Cmd+Shift+Left");
      this.shortcut("Cmd+Shift+Right");
      this.shortcut("Cmd+Shift+H");
      this.shortcut("Cmd+Shift+L");
      this.shortcut("Cmd+U");
      this.shortcut("Esc");
      this.shortcut("Cmd", { propagate: true });
      this.shortcut("Tab", { propagate: true });
      this.shortcut("Shift+Tab", { propagate: true });
      for (var i = 0; i < 10; i++) {
        this.shortcut("Cmd+"+i);
        if (!this.isMac) this.shortcut("Opt+"+i);
      }
    }

    this.shortcut("Enter");
  },

  shortcut: function(name, options) {

    var meta = this.isMac ? "Meta" : "Ctrl";

    var keystroke = name.replace("Cmd", meta).replace("Opt", "Alt"),
        method = "on" + name.replace(/\+/g, "");

    window.shortcut.add(keystroke, function(event) {
      if (this.enabled) {
        this.activeWindow = this.application.activeWindow();
        if (method.match(/\d$/)) {
          this.onNumeric.call(this, event, method.substr(-1));
        }



( run in 2.008 seconds using v1.01-cache-2.11-cpan-df04353d9ac )