Skip to content

Commit 5023527

Browse files
authored
feat: format editor content on paste (#199)
1 parent 21ca03c commit 5023527

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/components/Editor.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { queries } from '@testing-library/dom';
1111

1212
import CodeMirror from 'codemirror';
1313
import debounce from 'lodash/debounce';
14-
import beautifier from '../lib/beautify';
14+
import beautify from '../lib/beautify';
1515

1616
const baseOptions = {
1717
autoCloseBrackets: true,
@@ -201,17 +201,22 @@ function Editor({ onLoad, onChange, mode, initialValue }) {
201201
}
202202
});
203203

204-
editor.current.on('blur', () => {
205-
let formattedText = editor.current.getValue();
206-
if (mode === 'htmlmixed') {
207-
formattedText = beautifier.beautifyHtml(editor.current.getValue());
208-
} else if (mode === 'javascript') {
209-
formattedText = beautifier.beautifyJs(editor.current.getValue());
204+
const format = () => {
205+
const value = editor.current.getValue();
206+
const formatted = beautify.format(mode, value);
207+
editor.current.setValue(formatted);
208+
};
209+
210+
editor.current.on('change', (_, change) => {
211+
if (change.origin !== 'paste') {
212+
return;
210213
}
211-
editor.current.setValue(formattedText);
212-
onChange(formattedText);
214+
215+
format();
213216
});
214217

218+
editor.current.on('blur', format);
219+
215220
onLoad(editor.current);
216221
}, [editor.current, onChange]);
217222

src/lib/beautify.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,28 @@ const beautifyOptions = {
66
wrap_attributes: 'force-expand-multiline',
77
};
88

9-
function beautifyHtml(html) {
9+
function html(html) {
1010
return beautify.html(html, beautifyOptions);
1111
}
1212

13-
function beautifyJs(js) {
13+
function js(js) {
1414
return beautify.js(js, beautifyOptions);
1515
}
1616

17+
function format(mode, content) {
18+
if (mode === 'htmlmixed' || mode === 'html') {
19+
return html(content);
20+
}
21+
22+
if (mode === 'javascript') {
23+
return js(content);
24+
}
25+
26+
return content;
27+
}
28+
1729
export default {
18-
beautifyHtml,
19-
beautifyJs,
30+
html,
31+
js,
32+
format,
2033
};

src/lib/state.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from 'lz-string';
55
import queryString from 'query-string';
66

7-
import beautifier from '../lib/beautify';
7+
import beautify from '../lib/beautify';
88

99
function unindent(string) {
1010
return (string || '').replace(/[ \t]*[\n][ \t]*/g, '\n');
@@ -21,12 +21,8 @@ export function compress({ markup, query }) {
2121

2222
export function decompress({ markup, query }) {
2323
const result = {
24-
markup: beautifier.beautifyHtml(
25-
decompressFromEncodedURIComponent(markup || ''),
26-
),
27-
query: beautifier.beautifyJs(
28-
decompressFromEncodedURIComponent(query || ''),
29-
),
24+
markup: beautify.html(decompressFromEncodedURIComponent(markup || '')),
25+
query: beautify.js(decompressFromEncodedURIComponent(query || '')),
3026
};
3127

3228
return result;

0 commit comments

Comments
 (0)