tkf (owner)

Revisions

  • 0b0ee0 Sat May 16 04:14:32 -0700 2009
gist: 112651 Download_button fork
public
Public Clone URL: git://gist.github.com/112651.git
Embed All Files: show embed
x #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
CmdUtils.CreateCommand({
  name: "insert-formula",
  takes: {"expression in LaTeX": noun_arb_text},
  
  homepage: "http://arataka.wordpress.com",
  author: {name: "Takafumi Arakaki", homepage: "http://arataka.wordpress.com"},
  license: "MPL",
  
  preview: function( pblock, what ) {
    var base = "https://secure.s21g.com/formula/formulae/preview_with_auth";
    var img_url = [base, '?expression=', encodeURIComponent(what.text)].join('');
    jQuery.get(img_url, null, function(){
      pblock.innerHTML = [
        '<div style="height:200px"><img src="', img_url,
        '" style="display:block;background:#FFF;-moz-border-radius:5px;border:5px solid #FFF" />',
        'powered by <a href="http://formula.s21g.com/" style="text-decoration:underline">formula.s21g.com</a></div>'].join('');
    });
  },
  
  execute: function( what, mods ) {
    jQuery.ajax({
      url: ['https://secure.s21g.com/formula/formulae/create_with_auth?formula[expression]=',
            encodeURIComponent(what.text), '&base=http%3A%2F%2Fformula.s21g.com%2F'].join(''),
      cache: false,
      success: function(html){
      var ehtml = jQuery(html).find("#embed_html").val();
      var fsrc = jQuery(ehtml).find("img").attr("src");
      //var furl = jQuery(ehtml).parent().find("a").attr("href");
      var furl = "http://formula.s21g.com" + jQuery(html).find("#formula .formula a").attr("href");
      
      var doc = context.focusedWindow.document;
      var focused = context.focusedElement;
      
      if (doc.designMode == "on")
      //rich text case
      doc.execCommand("insertHTML", false, "<img src='" + fsrc + "'/>");
      
      else if (focused != null && ((focused.type == "textarea") || (focused.type == "text")))
      // simple text
      CmdUtils.setSelection(furl);
      
      else
      displayMessage("Sorry, this function can be used in an editable box only");
    }
                });
  }
});