Iván Ovejero 2023-08-17 12:28:32 +02:00 committed by GitHub
parent 914dd1f046
commit 832d08776c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 16 deletions

View file

@ -42,8 +42,9 @@
<script lang="ts">
import { defineComponent } from 'vue';
import type { PropType } from 'vue';
import jsParser from 'prettier/parser-babel';
import prettier from 'prettier/standalone';
import jsParser from 'prettier/plugins/babel';
import { format } from 'prettier';
import * as estree from 'prettier/plugins/estree';
import { mapStores } from 'pinia';
import type { LanguageSupport } from '@codemirror/language';
import type { Extension, Line } from '@codemirror/state';
@ -203,9 +204,9 @@ export default defineComponent({
return true;
},
async onReplaceCode(code: string) {
const formattedCode = prettier.format(code, {
const formattedCode = await format(code, {
parser: 'babel',
plugins: [jsParser],
plugins: [jsParser, estree],
});
this.editor?.dispatch({

View file

@ -4,10 +4,11 @@
<script lang="ts">
import { defineComponent } from 'vue';
import prettier from 'prettier/standalone';
import htmlParser from 'prettier/parser-html';
import cssParser from 'prettier/parser-postcss';
import jsParser from 'prettier/parser-babel';
import { format } from 'prettier';
import htmlParser from 'prettier/plugins/html';
import cssParser from 'prettier/plugins/postcss';
import jsParser from 'prettier/plugins/babel';
import * as estree from 'prettier/plugins/estree';
import { htmlLanguage, autoCloseTags, html } from 'codemirror-lang-html-n8n';
import { autocompletion } from '@codemirror/autocomplete';
import { indentWithTab, insertNewlineAndIndent, history, redo } from '@codemirror/commands';
@ -191,16 +192,16 @@ export default defineComponent({
);
},
format() {
async format() {
if (this.sections.length === 1 && this.isMissingHtmlTags()) {
const zerothSection = this.sections.at(0) as Section;
const formatted = prettier
.format(zerothSection.content, {
const formatted = (
await format(zerothSection.content, {
parser: 'html',
plugins: [htmlParser],
})
.trim();
).trim();
return this.editor.dispatch({
changes: { from: 0, to: this.doc.length, insert: formatted },
@ -211,7 +212,7 @@ export default defineComponent({
for (const { kind, content } of this.sections) {
if (kind === 'style') {
const formattedStyle = prettier.format(content, {
const formattedStyle = await format(content, {
parser: 'css',
plugins: [cssParser],
});
@ -220,9 +221,9 @@ export default defineComponent({
}
if (kind === 'script') {
const formattedScript = prettier.format(content, {
const formattedScript = await format(content, {
parser: 'babel',
plugins: [jsParser],
plugins: [jsParser, estree],
});
formatted.push(`<script>\n${formattedScript}<` + '/script>');
@ -238,7 +239,7 @@ export default defineComponent({
const { pre, rest } = match.groups;
const formattedRest = prettier.format(rest, {
const formattedRest = await format(rest, {
parser: 'html',
plugins: [htmlParser],
});