mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-16 19:46:19 +02:00
more prettier to worker, migrating change from master branch
This commit is contained in:
@@ -103,9 +103,10 @@ export default class UserCodeMirror extends Component {
|
||||
},
|
||||
'Shift-Tab': function(editor) {
|
||||
if (options.prettier) {
|
||||
prettify(editor.getValue(), options.prettierParser).then(
|
||||
formattedCode => editor.setValue(formattedCode)
|
||||
);
|
||||
prettify({
|
||||
content: editor.getValue(),
|
||||
type: options.prettierParser
|
||||
}).then(formattedCode => editor.setValue(formattedCode));
|
||||
} else {
|
||||
CodeMirror.commands.indentAuto(editor);
|
||||
}
|
||||
|
@@ -1326,17 +1326,18 @@ export default class App extends Component {
|
||||
return classes.join(' ');
|
||||
}
|
||||
|
||||
prettify(selectedFile) {
|
||||
prettifyHandler(selectedFile) {
|
||||
const currentItem = {
|
||||
...this.state.currentItem,
|
||||
files: [...this.state.currentItem.files]
|
||||
};
|
||||
const formattedContent = prettify(selectedFile);
|
||||
if (formattedContent !== selectedFile.content) {
|
||||
selectedFile.content = formattedContent;
|
||||
this.incrementUnsavedChanges();
|
||||
this.setState({ currentItem });
|
||||
}
|
||||
prettify({ file: selectedFile }).then(formattedContent => {
|
||||
if (formattedContent !== selectedFile.content) {
|
||||
selectedFile.content = formattedContent;
|
||||
this.incrementUnsavedChanges();
|
||||
this.setState({ currentItem });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
@@ -1375,7 +1376,7 @@ export default class App extends Component {
|
||||
onRenameFile={this.renameFileHandler.bind(this)}
|
||||
onFileDrop={this.fileDropHandler.bind(this)}
|
||||
onFolderSelect={this.folderSelectHandler.bind(this)}
|
||||
onPrettifyBtnClick={this.prettify.bind(this)}
|
||||
onPrettifyBtnClick={this.prettifyHandler.bind(this)}
|
||||
/>
|
||||
) : (
|
||||
<ContentWrap
|
||||
|
@@ -1,13 +1,28 @@
|
||||
importScripts('./prettier/standalone.js');
|
||||
|
||||
function prettify({ content, type }) {
|
||||
let plugins, parser;
|
||||
if (type === 'js') {
|
||||
parser = 'babylon';
|
||||
importScripts('./prettier/parser-babylon.js');
|
||||
} else if (type === 'css') {
|
||||
parser = 'css';
|
||||
importScripts('./prettier/parser-postcss.js');
|
||||
let parser;
|
||||
switch (type) {
|
||||
case 'js':
|
||||
parser = 'babylon';
|
||||
importScripts('./prettier/parser-babylon.js')
|
||||
break;
|
||||
case 'json':
|
||||
parser = 'json';
|
||||
importScripts('./prettier/parser-babylon.js')
|
||||
break;
|
||||
case 'css':
|
||||
case 'scss':
|
||||
case 'sass':
|
||||
case 'less':
|
||||
parser = 'css';
|
||||
importScripts('./prettier/parser-postcss.js')
|
||||
break;
|
||||
case 'md':
|
||||
case 'markdown':
|
||||
parser = 'markdown';
|
||||
importScripts('./prettier/parser-markdown.js')
|
||||
break;
|
||||
}
|
||||
|
||||
if (!parser) {
|
||||
|
1
src/lib/prettier/parser-markdown.js
Normal file
1
src/lib/prettier/parser-markdown.js
Normal file
File diff suppressed because one or more lines are too long
46
src/utils.js
46
src/utils.js
@@ -461,12 +461,16 @@ export function getFilenameFromUrl(url) {
|
||||
return url.match(/\/([^/]*)$/)[1];
|
||||
}
|
||||
|
||||
export function prettify(content, type = 'js') {
|
||||
export function prettify({ file, content, type }) {
|
||||
const d = deferred();
|
||||
if (file) {
|
||||
type = getExtensionFromFileName(file.name);
|
||||
content = file.content;
|
||||
}
|
||||
const worker = new Worker(
|
||||
chrome.extension
|
||||
? chrome.extension.getURL('lib/prettier-worker.js')
|
||||
: `${BASE_PATH}/lib/prettier-worker.js`
|
||||
: `${BASE_PATH !== '/' ? BASE_PATH : ''}/lib/prettier-worker.js`
|
||||
);
|
||||
worker.postMessage({ content, type });
|
||||
worker.addEventListener('message', e => {
|
||||
@@ -481,41 +485,3 @@ if (window.IS_EXTENSION) {
|
||||
} else {
|
||||
document.body.classList.add('is-app');
|
||||
}
|
||||
|
||||
export function prettify(file) {
|
||||
const prettier = require('prettier/standalone');
|
||||
const fileExtension = getExtensionFromFileName(file.name);
|
||||
|
||||
let plugins, parser;
|
||||
switch (fileExtension) {
|
||||
case 'js':
|
||||
parser = 'babylon';
|
||||
plugins = [require('prettier/parser-babylon')];
|
||||
break;
|
||||
case 'json':
|
||||
parser = 'json';
|
||||
plugins = [require('prettier/parser-babylon')];
|
||||
break;
|
||||
case 'css':
|
||||
case 'scss':
|
||||
case 'sass':
|
||||
case 'less':
|
||||
parser = 'css';
|
||||
plugins = [require('prettier/parser-postcss')];
|
||||
break;
|
||||
case 'md':
|
||||
case 'markdown':
|
||||
parser = 'markdown';
|
||||
plugins = [require('prettier/parser-markdown')];
|
||||
break;
|
||||
}
|
||||
|
||||
if (!parser) {
|
||||
return file.content;
|
||||
}
|
||||
|
||||
return prettier.format(file.content, {
|
||||
parser,
|
||||
plugins
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user