forked from steveathon/bootstrap-wysiwyg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-wysiwyg.js
153 lines (153 loc) · 4.85 KB
/
bootstrap-wysiwyg.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
/* http://github.com/mindmup/bootstrap-wysiwyg */
/*global $, FileReader*/
/*jslint browser:true*/
$(function () {
'use strict';
var readFileIntoDataUrl = function (fileInfo) {
var loader = $.Deferred(),
fReader = new FileReader();
fReader.onload = function (e) {
loader.resolve(e.target.result);
};
fReader.onerror = loader.reject;
fReader.onprogress = loader.notify;
fReader.readAsDataURL(fileInfo);
return loader.promise();
};
$.fn.cleanHtml = function () {
var html = $(this).html();
return html && html.replace(/(<br>|\s|<div><br><\/div>| )*$/, '');
};
$.fn.wysiwyg = function (options) {
var editor = this,
selectedRange,
defaultOptions = {
hotKeys: {
'ctrl+b meta+b': 'bold',
'ctrl+i meta+i': 'italic',
'ctrl+u meta+u': 'underline',
'ctrl+z meta+z': 'undo',
'ctrl+y meta+y meta+shift+z': 'redo',
'ctrl+l meta+l': 'justifyleft',
'ctrl+r meta+r': 'justifyright',
'ctrl+e meta+e': 'justifycenter',
'ctrl+j meta+j': 'justifyfull',
'shift+tab': 'outdent',
'tab': 'indent'
},
toolbarRole: 'editor-toolbar',
commandRole: 'edit'
},
execCommand = function (commandWithArgs, valueArg) {
var commandArr = commandWithArgs.split(' '),
command = commandArr.shift(),
args = commandArr.join(' ') + (valueArg || '');
document.execCommand(command, 0, args);
},
bindHotkeys = function (hotKeys) {
$.each(hotKeys, function (hotkey, command) {
editor.keydown(hotkey, function (e) {
if (editor.attr('contenteditable') && editor.is(':visible')) {
e.preventDefault();
e.stopPropagation();
execCommand(command);
}
}).keyup(hotkey, function (e) {
if (editor.attr('contenteditable') && editor.is(':visible')) {
e.preventDefault();
e.stopPropagation();
}
});
});
},
getCurrentRange = function () {
var sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
return sel.getRangeAt(0);
}
},
saveSelectionRange = function () {
selectedRange = getCurrentRange();
},
restoreSelectionRange = function () {
var selection = window.getSelection();
if (selectedRange) {
selection.removeAllRanges();
selection.addRange(selectedRange);
}
},
insertFiles = function (files) {
editor.focus();
$.each(files, function (idx, fileInfo) {
if (/^image\//.test(fileInfo.type)) {
$.when(readFileIntoDataUrl(fileInfo)).done(function (dataUrl) {
execCommand('insertimage', dataUrl);
});
}
});
},
bindToolbar = function (toolbar, options) {
toolbar.find('a[data-' + options.commandRole + ']').click(function () {
restoreSelectionRange();
execCommand($(this).data(options.commandRole));
saveSelectionRange();
});
$('input[type=text][data-' + options.commandRole + ']', toolbar).on('webkitspeechchange change', function () {
var newValue = this.value; /* ugly but prevents fake double-calls due to selection restoration */
this.value = '';
restoreSelectionRange();
if (newValue) {
editor.focus();
execCommand($(this).data(options.commandRole), newValue);
}
saveSelectionRange();
});
toolbar.find('input[type=file][data-' + options.commandRole + ']').change(function () {
restoreSelectionRange();
if (this.type === 'file' && this.files && this.files.length > 0) {
insertFiles(this.files);
}
saveSelectionRange();
});
},
initFileDrops = function () {
$.event.props.push('dataTransfer');
editor.bind('dragenter dragover', false)
.bind('drop', function (e) {
e.stopPropagation();
e.preventDefault();
if (e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files.length > 0) {
insertFiles(e.dataTransfer.files);
}
});
};
options = $.extend({}, defaultOptions, options);
bindHotkeys(options.hotKeys);
initFileDrops();
$.each($.find('[data-role=' + options.toolbarRole + ']'), function () { bindToolbar($(this), options); });
$.each(this, function () {
var before,
element = $(this);
element.attr('contenteditable', true)
.on('focus', function () {
before = element.html();
})
.on('mouseup keyup mouseout', saveSelectionRange)
.on('input blur keyup paste', function () {
if (before !== element.html()) {
before = element.html();
element.trigger('change');
}
});
$(window).bind('touchend', function (e) {
var isInside = (e.target === element[0] || $(element).children().index($(e.target)) !== -1 || $(element).has(e.target).length > 0),
currentRange = getCurrentRange(),
clear = currentRange && (currentRange.startContainer === currentRange.endContainer && currentRange.startOffset === currentRange.endOffset);
if (!clear || isInside) {
saveSelectionRange();
}
});
});
return this;
};
});