This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.smarttext.js
223 lines (192 loc) · 7.44 KB
/
jquery.smarttext.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
(function ($) {
// Use underscore.js html escaper
// http://underscorejs.org/#escape
var _escape;
(function () {
var entityMap = {
escape: {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
}
};
var escapeKeys = [];
for (var k in entityMap.escape) {
if (entityMap.escape.hasOwnProperty(k)) {
escapeKeys.push(k);
}
}
escapeKeys = escapeKeys.join('');
var entityRegexes = {
escape: new RegExp('[' + escapeKeys + ']', 'g')
};
_escape = function (string) {
if (string == null) { return ''; }
return ('' + string).replace(entityRegexes['escape'], function (match) {
return entityMap['escape'][match];
});
};
})();
var _defaultOptions = {
linkAttributes: {
title: 'Click outside of link to edit',
contenteditable: 'false'
},
parseLinks: true,
newlines: true,
editable: true
};
var _options = null;
var _methods = {
destroy: function () {
// remove listeners
this.off('change keydown keypress input', _placeholderUpdate);
this.off('blur', this.data('onBlurListener'));
this.off('focus', this.data('onFocusListener'));
this.find('a').off('mousedown', this.data('onMouseDownListener'));
},
value: function (val) {
if (typeof val === 'string') {
return _methods.setValue.call(this, val);
}
return _methods.getValue.call(this);
},
getValue: function () {
var text, inner;
var textEl = this.clone();
var children = textEl.children('div');
textEl.find('br').replaceWith('\n');
if (children) {
children.detach();
text = textEl.text();
children.each(function (indx, child) {
inner = $(child).text();
if (inner === '\n') {
text += inner;
} else {
text += ('\n' + inner);
}
});
} else {
text = textEl.text();
}
return text;
},
setValue: function (val) {
if (_options && _options.parseLinks) {
this.html(_parseLinks(val));
} else {
this.html(val);
}
_placeholderUpdate.call(this);
return this;
}
};
var _parseLinks = function (text, options) {
if (!text) { return ''; }
options || (options = {});
var linkMaps = [];
var escaped = [];
// use String.prototype.replace because its a crossbrowser way to iterate over links
text.replace(_linkDetectionRegex, function () {
var url = arguments[0];
var index = arguments[arguments.length - 2];
// Map the positions of link and non-link text
linkMaps.push({
linkTag: _makeLink(url, options.linkAttributes),
linkStart: index,
linkEnd: index + url.length
});
// don't actually change the text yet
return url;
});
var lastI = 0;
// Go though each link, and escape the text between it and the last link
for (var i = 0; i < linkMaps.length; i++) {
escaped.push(_escape(text.slice(lastI, linkMaps[i].linkStart)));
escaped.push(linkMaps[i].linkTag);
lastI = linkMaps[i].linkEnd;
};
// Escape everything after the last link
if (lastI) {
escaped.push(_escape(text.slice(lastI)));
} else {
// or if there where no links just escape the whole string
escaped.push(_escape(text));
}
escaped = escaped.join('');
if (options.newlines) { escaped = escaped.replace(/\n/g, '<br>'); }
return escaped;
};
var _makeLink = function (url, attrs) {
var linkStart = "<a href='" + (url.substr(0,4) == 'http' ? url : 'http://' + url) + "' ";
var linkEnd = ">" + url + "</a>";
var linkAttributes = '';
for (var a in attrs) {
if (attrs.hasOwnProperty(a) && a !== 'href' && a !== 'src') {
linkAttributes += (a + "='" + attrs[a] + "' ");
}
}
return linkStart + linkAttributes + linkEnd;
};
var _linkDetectionRegex = /(([a-z]+:\/\/)?(([a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal))(:[0-9]{1,5})?(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@/?]*)?)(?=(\)|\(|\<|\>|\s|$))/gi;
// emulate placeholder text in contenteditable HTML
// this was inspired by https://github.com/sprucemedia/jQuery.divPlaceholder.js
var _placeholderUpdate = function () {
$(this).each(function () {
if (this.textContent) {
this.setAttribute('data-div-placeholder-content', 'true');
}
else {
this.removeAttribute('data-div-placeholder-content');
}
});
};
var _smarttext = function ($el, options) {
if (options.parseLinks) {
$el.html(_parseLinks(_methods['value'].call($el), options));
} else {
$el.html(_methods['value'].call($el));
}
// This ensures (cross-browser) that if the user clicks the link before
// the element gets focus we follow the hyperlink as usual
$el.find('a').one('mousedown', $el.data('onMouseDownListener'));
return $el;
};
$.fn.smarttext = function () {
var args = Array.prototype.slice.apply(arguments);
var isMethod = typeof args[0] === 'string';
if (isMethod) {
return _methods[args[0]].apply(this, args.slice(1));
}
_options = $.extend(true, {}, _defaultOptions, args[0]);
return this.each(function (indx, el) {
var $el = $(el);
$el.attr('contenteditable', _options.editable);
var onFocusListener = function () {
if ($el.data('follow-link')) { return; }
$el.find('a').attr('contenteditable', _options.editable);
};
var onBlurListener = function () {
_smarttext($el, _options);
$el.find('a').attr('contenteditable', _options.linkAttributes.contenteditable);
$el.data('follow-link', false);
};
var onMouseDownListener = function () {
// we are relying on the mousedown event firing before focus
if ($(this).attr('contenteditable') === 'false') {
$el.data('follow-link', true);
}
}
$el.data('onFocusListener', onFocusListener);
$el.data('onBlurListener', onBlurListener);
$el.data('onMouseDownListener', onMouseDownListener);
_smarttext($el, _options);
_placeholderUpdate.call($el);
$el.on('focus', onFocusListener).on('blur', onBlurListener);
$el.on('change keydown keypress input', _placeholderUpdate);
});
};
})(jQuery);