Skip to content

Instantly share code, notes, and snippets.

@tkf

tkf/x

Created May 16, 2009 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkf/112651 to your computer and use it in GitHub Desktop.
Save tkf/112651 to your computer and use it in GitHub Desktop.
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");
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment