Created
October 17, 2009 01:14
-
-
Save satyr/212188 to your computer and use it in GitHub Desktop.
Insertion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CmdUtils.CreateCommand({ | |
names: ['insert HTML', 'Zen Coding'], | |
description: ( | |
'Inserts HTML, using pseudo '+ | |
'Zen Coding'.link('http://code.google.com/p/zen-coding/') +'.'), | |
help: ('Try: <code>dl#id.cla.ss > dt{selection: ~}'+ | |
' + dd > input[name=foo][disabled]*2</code>'), | |
icon: 'chrome://ubiquity/skin/icons/html_go.png', | |
argument: {object_ZC: noun_arb_text}, | |
execute: function ihtm_execute({object: {text}}){ | |
if(!text) return; | |
var h = this._htm(text); | |
(CmdUtils.setSelection(h, {text: h}) || | |
CmdUtils.copyToClipboard(h)); | |
}, | |
preview: function ihtm_preview(pb, {object: {text}}){ | |
pb.innerHTML = ( | |
text | |
? '<code>'+ Utils.escapeHtml(this._htm(text)) +'</code>' | |
: this.previewDefault()); | |
}, | |
_htm: function ihtm_htm(zc) { | |
var x = zen4x(zc).toXMLString(); | |
if(~zc.indexOf('~')){ | |
let s = CmdUtils.getSelection(); | |
x = x.replace(/~~?/g, function(m) m[1] || s); | |
} | |
return ($('body')[0].parentNode.ownerDocument.createRange() | |
.createContextualFragment('<div>'+ x).lastChild.innerHTML); | |
}, | |
}); | |
function zen4x(zc){ | |
var word = /[-\w$]+/, char = /^[^]/, digits = /\d+/; | |
var kv = /([-\w$]+)(?:=([^\]]*))?\]?/, content = /{([^\}]*)}?/; | |
var zs = zc.trim().split(/\s{0,}([>+])\s{0,}/); | |
var root = <_/>, curs = [root], sep; | |
for(let i = -1, l = zs.length; ++i < l; sep = zs[++i]){ | |
let [name] = word(zs[i]) || ''; | |
if(!name) continue; | |
let lm = <{name}/>, n = 1; | |
for(let _, m; char.test(_ = RegExp.rightContext);) switch(_[0]){ | |
case '#': lm.@id = word(_) || ''; | |
break; | |
case '.': lm.@class += (lm.@class +'' && ' ') + (word(_) || ''); | |
break; | |
case '[': if((m = kv(_))) lm['@'+ m[1]] = m[2] || ''; | |
break; | |
case '{': if((m = content(_))) lm.appendChild(m[1]); | |
break; | |
case '*': n *= +digits(_) || 1; | |
} | |
let _curs = [], p = sep === '+'; | |
let sl = n > 1 && ~zc.indexOf('$') && lm.toXMLString(); | |
for each(let cur in curs){ | |
if(p) cur = cur.parent() || cur; | |
for(let i = 1; i <= n; ++i){ | |
let clm = sl ? XML(sl.replace(/\$/g, i)) : lm.copy(); | |
cur.appendChild(clm); | |
_curs.push(clm); | |
} | |
} | |
curs = _curs; | |
} | |
return root.*; | |
} | |
CmdUtils.CreateCommand({ | |
name: 'insert link', | |
description: 'Inserts a link, enclosing selection.', | |
arguments: { | |
object: noun_type_url, | |
alias: noun_arb_text, | |
}, | |
execute: function ilnk_execute({object: {text: url}, alias: {text}}){ | |
if(!url) return; | |
var tag = this._link(url, text); | |
(CmdUtils.setSelection(tag, {text: tag}) || | |
CmdUtils.copyToClipboard(tag)); | |
}, | |
preview: function ilnk_preview(pb, {object: {text: url}, alias: {text}}){ | |
pb.innerHTML = ( | |
url | |
? this._link(url, text) | |
: this.previewDefault()); | |
}, | |
_link: function ilnk__link(url, txt) | |
<a href={url}>{txt || url}</a>.toXMLString(), | |
}); | |
CmdUtils.CreateCommand({ | |
name: 'insert character', | |
description: 'Inserts a Unicode character by hex code point.', | |
help: 'Chosen at random by default.', | |
argument: { | |
name: 'unicode', | |
label: 'U+', | |
default: function ichar_default() this._make(Math.random() * 0x10000), | |
suggest: function ichar_suggest(txt){ | |
var c = txt && +('0x'+ txt); | |
return c ? [this._make(c)] : []; | |
}, | |
_make: function ichar_make(c){ | |
var hex = (0x10000 | c).toString(16).slice(1).toUpperCase(); | |
return { | |
text: String.fromCharCode(c), data: hex, summary: this.label + hex}; | |
}, | |
}, | |
execute: function ichar_execute({object: {text}}){ | |
CmdUtils.setSelection(Utils.escapeHtml(text), {text: text}); | |
CmdUtils.copyToClipboard(text); | |
}, | |
preview: function ichar_preview(pb, {object: {text, data, summary}}){ | |
pb.innerHTML = <h1 align="center" title={summary}><a href={ | |
'http://www.fileformat.info/info/unicode/char/'+ data +'/index.htm' | |
} accesskey="/"><code style="font-size:9em">{text}</code></a></h1>; | |
}, | |
}); | |
const DateFormats = ['yyyy-MM-dd', 'hh:mm:ss', 'yyyy-MM-dd hh:mm:ss', '']; | |
var dfIndex = Bin.dfIndex() || 0; | |
CmdUtils.CreateCommand({ | |
name: 'insert datetime', | |
description: 'Inserts date and/or time.', | |
argument: noun_type_date_time, | |
execute: function idt_execute({object: {data}}){ | |
var t = data.toString(DateFormats[dfIndex]); | |
CmdUtils.setSelection(t); | |
CmdUtils.copyToClipboard(t); | |
}, | |
preview: function idt_preview(pb, {object: {data}}){ | |
CmdUtils.previewList( | |
pb, | |
[data.toString(f).bold() for each(f in DateFormats)], | |
idt_select, | |
'li:not(.active) b {opacity:0.7}'); | |
pb.getElementsByTagName('li')[dfIndex].className = 'active'; | |
}, | |
}); | |
function idt_select(i, {target}){ | |
Bin.dfIndex(dfIndex = +i); | |
target.ownerDocument.querySelector('li.active').className = ''; | |
$(target).closest('li')[0].className = 'active'; | |
return true; | |
} | |
$.extend(feed, { | |
author: {name: 'satyr', email: 'murky.satyr\100gmail.com'}, | |
license: 'MIT', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment