satyr (owner)

Revisions

gist: 212188 Download_button fork
public
Description:
Insertion
Public Clone URL: git://gist.github.com/212188.git
Embed All Files: show embed
insert.ubiq.js #
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
CmdUtils.CreateCommand({
  name: 'insert link',
  description: 'Inserts a link, enclosing selection.',
  icon: 'chrome://ubiquity/skin/icons/html_go.png',
  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 HTML',
  description: 'Inserts HTML tag, enclosing selection.',
  help: 'Separate attributes by spaces.',
  icon: 'chrome://ubiquity/skin/icons/html_go.png',
  arguments: {'object name [key[=value] ...]': noun_arb_text},
  execute: function ihtm_execute({object: {text}}){
    if(!text) return;
    var tag = this._tag(text);
    (CmdUtils.setSelection(tag, {text: tag}) ||
     CmdUtils.copyToClipboard(tag));
  },
  preview: function ihtm_preview(pb, {object: {text}}){
    pb.innerHTML = (
      text
      ? '<code>'+ Utils.escapeHtml(this._tag(text)) +'</code>'
      : this.previewDefault());
  },
  _tag: function ihtm__tag(text){
    var items = text.split(/\s+/), name = items.shift().toLowerCase();
    var tag = '<'+ name;
    for each(let attr in items){
      let [k, v] = /^[^=]+(?==?(.*))/(attr);
      k = k.toLowerCase();
      if(~['disabled', 'checked', 'readonly'].indexOf(k))
        v = '"'+ k +'"';
      else {
        let q = /\"/.test(v) ? "'" : '"';
        v = q + v + q;
      }
      tag += ' '+ k +'='+ v;
    }
    return tag + (
      ~['area', 'base', 'basefont', 'br', 'col', 'embed', 'frame', 'hr',
        'img', 'input', 'isindex', 'link', 'meta', 'param', 'source',
        ].indexOf(name)
      ? '/>'
      : ('>'+ (CmdUtils.getHtmlSelection() || CmdUtils.getSelection()) +
         '</'+ name +'>'));
  },
});
const DateFormats = ['yy-MM-dd', 'hh:mm:ss', 'yy-MM-dd hh:mm:ss', ''];
var dfIndex = Bin.dfIndex() || 0;
CmdUtils.CreateCommand({
  name: 'insert date-time',
  description: 'Inserts date and/or time.',
  icon: 'chrome://ubiquity/skin/icons/favicon.png',
  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(commands[0], {
  author: {name: 'satyr', email: 'murky.satyr@gmail.com'},
  license: 'MIT',
});