This repository has been archived by the owner on May 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (83 loc) · 1.66 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Module dependencies.
*/
var Emitter = require('emitter')
, classes = require('classes')
/**
* expose Editable
*/
module.exports = Editable;
/**
* Initialize a given `el` as a
* contenteditable element
*
* @param {Element} el
* @return {self}
* @api public
*/
function Editable(el){
if (!(this instanceof Editable)) return new Editable(el);
this.el = el;
this.classes = classes(el);
this.classes.add('editable');
this.el.setAttribute('contenteditable', true);
}
/**
* Mixin Emitter
*/
Emitter(Editable.prototype);
/**
* Convenience method to hook in plugins.
*
* @param {Function} fn
* @api private
*/
Editable.prototype.use = function(fn){
fn(this);
return this;
}
/**
* Removes event listeners, not before emitting
* `'unbind'` to which plugins can bind to it,
* and programatically unbind there callbacks.
*
* @return {self}
* @api public
*/
Editable.prototype.unbind = function(){
this.emit('unbind');
this.off();
return this;
}
/**
* `.innerHTML` getter/setter
*
* @param {String} html
* @return {String}
* @api public
*/
Editable.prototype.html = function(html){
if (typeof(html) !== 'undefined') {
this.el.innerHTML = html;
this.emit('html', html);
}
return this.el.innerHTML;
}
/**
* `.textContent` getter/setter. Falls back
* to `innerText`.
*
* @param {String} text
* @return {String}
* @api public
*/
Editable.prototype.text = function(text){
var el = this.el
, defined = (typeof(text) !== 'undefined')
, fn = 'textContent' in el ? 'textContent' : 'innerText';
if (defined) {
el[fn] = text.trim();
this.emit('text', text.trim());
}
return el[fn].trim();
}