-
Notifications
You must be signed in to change notification settings - Fork 9
/
htmlpaste.js
172 lines (140 loc) · 4.23 KB
/
htmlpaste.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
// contenteditable selectText function; via http://stackoverflow.com/questions/12243898/how-to-select-all-text-in-contenteditable-div
jQuery.fn.selectText = function(){
var doc = document;
var element = this[0];
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
};
// shim to make j2m.js (and other npm libs) not crash
function require(str) {
return window[str];
}
(function () {
var input, output;
function updateOutput() {
var html = input.data('redactor').getCode();
var outData = getSelectedType().converter(html);
output.val(outData);
}
function updateOutputUI(type) {
$('#output-header a').removeClass('active');
$(type.buttonSelector).addClass('active');
$('#output-container .output-toolbar').removeClass('active');
$(type.toolbarSelector).addClass('active');
}
function setSelectedType(type) {
window.localStorage.setItem("selectedType", type);
}
function getSelectedType() {
// By default it's set to markdown.
var typeKey = window.localStorage.getItem("selectedType") || "markdown";
return types[typeKey];
}
// Work-around bug in md.html markdown converter,
// which ignores that in HTML \n is just a space.
function fixExtraSpaces(html) {
return html.replace(/[\n ]+/g, " ");
}
function toMarkdown(html) {
html = fixExtraSpaces(html);
return md(html, {'absolute': true, 'inline': true});
}
function toHTML(html) {
if (jQuery('#setting-html-tidy').prop('checked')) {
var mdText = toMarkdown(html);
return markdown.toHTML(mdText);
}
return html;
}
function toTextile(html) {
html = fixExtraSpaces(html);
return textile(html, {'absolute': true, 'inline': true});
}
function toJira(html) {
var mdText = toMarkdown(html);
return J2M.prototype.to_jira(mdText);
}
function setupClipboard() {
var clipboard = new Clipboard('.copy-clipboard');
clipboard.on('success', function(e) {
e.clearSelection();
jQuery(e.trigger).find('span').html("Copied");
setTimeout(function () {
jQuery(e.trigger).find('span').html("Copy to clipboard");
}, 1000);
event.preventDefault();
});
}
var types = {
'markdown': {
'converter': toMarkdown,
'buttonSelector': '#output-header-markdown',
'toolbarSelector': '#output-toolbar-markdown',
},
'html': {
'converter': toHTML,
'buttonSelector': '#output-header-html',
'toolbarSelector': '#output-toolbar-html',
},
'textile': {
'converter': toTextile,
'buttonSelector': '#output-header-textile',
'toolbarSelector': '#output-toolbar-textile',
},
'jira': {
'converter': toJira,
'buttonSelector': '#output-header-jira',
'toolbarSelector': '#output-toolbar-jira',
}
}
jQuery(function($) {
// initialize on load
input = jQuery('#input');
output = jQuery('#output');
setupClipboard();
input.redactor({
keyupCallback: updateOutput,
execCommandCallback: updateOutput,
});
updateOutput();
updateOutputUI(getSelectedType());
var setClick = function(typeKey) {
var type = types[typeKey];
$('#output-header').on('click', type.buttonSelector, function() {
setSelectedType(typeKey);
updateOutput();
updateOutputUI(type);
return false;
});
}
// associate click handler with all buttons for each type
jQuery.each(types, function( typeKey, type ) {
if (type.buttonSelector) {
setClick(typeKey);
}
});
$('#setting-html-tidy').change(function() {
updateOutput();
});
// focus select all
$('#output').focus(function() {
var $this = $(this);
$this.select();
// Work around Chrome's little problem
$this.mouseup(function() {
// Prevent further mouseup intervention
$this.unbind("mouseup");
return false;
});
})
});
})();