index.js (view raw)
1const blob = new Blob(['importScripts("https://cdn.jsdelivr.net/npm/lzma@2.3.2/src/lzma_worker.min.js");']);
2const lzma = new LZMA(window.URL.createObjectURL(blob));
3
4let editor = null;
5let select = null;
6let clipboard = null;
7let statsEl = null;
8
9const initCodeEditor = (initialValue) => {
10 CodeMirror.modeURL = 'https://cdn.jsdelivr.net/npm/codemirror@5.52.0/mode/%N/%N.js';
11 editor = new CodeMirror(byId('editor'), {
12 lineNumbers: true,
13 theme: 'dracula',
14 readOnly: readOnly,
15 lineWrapping: false,
16 scrollbarStyle: 'simple',
17 value: initialValue,
18 });
19 if (readOnly) {
20 document.body.classList.add('readonly');
21 }
22
23 statsEl = byId('stats');
24 statsEl.innerHTML = `Length: ${initialValue.length} | Lines: ${editor['doc'].size}`;
25 editor.on('change', () => {
26 statsEl.innerHTML = `Length: ${editor.getValue().length} | Lines: ${editor['doc'].size}`;
27 });
28 initLangSelector();
29};
30
31const initLangSelector = () => {
32 select = new SlimSelect({
33 select: '#language',
34 data: CodeMirror.modeInfo.map((e) => ({
35 text: e.name,
36 value: slugify(e.name),
37 data: { mime: e.mime, mode: e.mode },
38 })),
39 showContent: 'down',
40 onChange: (e) => {
41 const language = e.data || { mime: null, mode: null };
42 editor.setOption('mode', language.mime);
43 CodeMirror.autoLoadMode(editor, language.mode);
44 },
45 });
46
47 select.set(decodeURIComponent(new URLSearchParams(window.location.search).get('lang') || 'plain-text'));
48};
49
50const initCode = () => {
51 const base64 = location.pathname.substr(1) || location.hash.substr(1);
52 if (base64.length === 0) {
53 initCodeEditor('');
54 return;
55 }
56 decompress(base64, (code, err) => {
57 if (err) {
58 alert('Failed to decompress data: ' + err);
59 initCodeEditor('');
60 return;
61 }
62 initCodeEditor(code);
63 });
64};
65
66const initClipboard = () => {
67 clipboard = new ClipboardJS('.clipboard');
68 clipboard.on('success', () => {
69 hideCopyBar(true);
70 });
71};
72
73const generateLink = (mode) => {
74 const data = editor.getValue();
75 compress(data, (base64, err) => {
76 if (err) {
77 alert('Failed to compress data: ' + err);
78 return;
79 }
80 const url = buildUrl(base64, mode);
81 statsEl.innerHTML = `Data length: ${data.length} | Link length: ${
82 url.length
83 } | Compression ratio: ${Math.round((100 * url.length) / data.length)}%`;
84
85 showCopyBar(url);
86 });
87};
88
89// Open the "Copy" bar and select the content
90const showCopyBar = (dataToCopy) => {
91 byId('copy').classList.remove('hidden');
92 const linkInput = byId('copy-link');
93 linkInput.value = dataToCopy;
94 linkInput.focus();
95 linkInput.setSelectionRange(0, dataToCopy.length);
96};
97
98// Close the "Copy" bar
99const hideCopyBar = (success) => {
100 const copyButton = byId('copy-btn');
101 const copyBar = byId('copy');
102 if (!success) {
103 copyBar.classList.add('hidden');
104 return;
105 }
106 copyButton.innerText = 'Copied !';
107 setTimeout(() => {
108 copyBar.classList.add('hidden');
109 copyButton.innerText = 'Copy';
110 }, 800);
111};
112
113const disableLineWrapping = () => {
114 byId('disable-line-wrapping').classList.add('hidden');
115 byId('enable-line-wrapping').classList.remove('hidden');
116 editor.setOption('lineWrapping', false);
117};
118
119const enableLineWrapping = () => {
120 byId('enable-line-wrapping').classList.add('hidden');
121 byId('disable-line-wrapping').classList.remove('hidden');
122 editor.setOption('lineWrapping', true);
123};
124
125const openInNewTab = () => {
126 window.open(location.href.replace('&readonly', ''));
127};
128
129// Build a shareable URL
130const buildUrl = (rawData, mode) => {
131 const url = `${location.protocol}//${location.host}/` + rawData + `?lang=${encodeURIComponent(select.selected())}`;
132 if (mode === 'markdown') {
133 return `[NoPaste snippet](${url})`;
134 }
135 if (mode === 'iframe') {
136 const height = Math.min(editor['doc'].height + 45, 800);
137 return `<iframe width="100%" height="${height}" frameborder="0" src="${url}"></iframe>`;
138 }
139 return url;
140};
141
142// Transform a compressed base64 string into a plain text string
143const decompress = (base64, cb) => {
144 const progressBar = byId('progress');
145
146 const req = new XMLHttpRequest();
147 req.open('GET', 'data:application/octet;base64,' + base64);
148 req.responseType = 'arraybuffer';
149 req.onload = (e) => {
150 lzma.decompress(
151 new Uint8Array(e.target.response),
152 (result, err) => {
153 progressBar.style.width = '0';
154 cb(result, err);
155 },
156 (progress) => {
157 progressBar.style.width = 100 * progress + '%';
158 }
159 );
160 };
161 req.send();
162};
163
164// Transform a plain text string into a compressed base64 string
165const compress = (str, cb) => {
166 const progressBar = byId('progress');
167
168 lzma.compress(
169 str,
170 1,
171 (compressed, err) => {
172 if (err) {
173 progressBar.style.width = '0';
174 cb(compressed, err);
175 return;
176 }
177 const reader = new FileReader();
178 reader.onload = () => {
179 progressBar.style.width = '0';
180 cb(reader.result.substr(reader.result.indexOf(',') + 1));
181 };
182 reader.readAsDataURL(new Blob([new Uint8Array(compressed)]));
183 },
184 (progress) => {
185 progressBar.style.width = 100 * progress + '%';
186 }
187 );
188};
189
190const slugify = (str) =>
191 str
192 .toString()
193 .toLowerCase()
194 .replace(/\s+/g, '-')
195 .replace(/\+/g, '-p')
196 .replace(/#/g, '-sharp')
197 .replace(/[^\w\-]+/g, '');
198
199const byId = (id) => document.getElementById(id);
200
201/* Only for tests purposes */
202const testAllModes = () => {
203 for (const [index, language] of Object.entries(CodeMirror.modeInfo)) {
204 setTimeout(() => {
205 console.info(language.name);
206 select.set(slugify(language.name));
207 }, 1000 * index);
208 }
209};
210
211initCode(); // Will decode URL, create code editor, and language selector
212initClipboard();