-
-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
286 additions
and
489 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/react-settings-form/src/components/MonacoInput/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { loader } from '@monaco-editor/react' | ||
import chromeTheme from './themes/chrome' | ||
import monokaiTheme from './themes/monokai' | ||
import { format } from './format' | ||
|
||
loader.init().then((monaco) => { | ||
monaco.editor.defineTheme('monokai', monokaiTheme as any) | ||
monaco.editor.defineTheme('chrome-devtools', chromeTheme as any) | ||
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ | ||
target: monaco.languages.typescript.ScriptTarget.Latest, | ||
allowNonTsExtensions: true, | ||
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs, | ||
module: monaco.languages.typescript.ModuleKind.CommonJS, | ||
noEmit: true, | ||
esModuleInterop: true, | ||
jsx: monaco.languages.typescript.JsxEmit.React, | ||
reactNamespace: 'React', | ||
allowJs: true, | ||
}) | ||
|
||
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({ | ||
noSemanticValidation: false, | ||
noSyntaxValidation: true, | ||
}) | ||
monaco.languages.registerDocumentFormattingEditProvider('typescript', { | ||
async provideDocumentFormattingEdits(model) { | ||
return [ | ||
{ | ||
text: await format( | ||
model['getLanguage']?.() || 'typescript', | ||
model.getValue() | ||
), | ||
range: model.getFullModelRange(), | ||
}, | ||
] | ||
}, | ||
}) | ||
}) |
68 changes: 0 additions & 68 deletions
68
packages/react-settings-form/src/components/MonacoInput/expression.ts
This file was deleted.
Oops, something went wrong.
67 changes: 34 additions & 33 deletions
67
packages/react-settings-form/src/components/MonacoInput/format.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,49 @@ | ||
import { parseExpression, parse } from '@babel/parser' | ||
import { loadScript } from '../../shared/loadScript' | ||
|
||
loadScript({ | ||
package: 'prettier@2.3.2', | ||
entry: 'standalone.min.js', | ||
root: 'prettier', | ||
}) | ||
interface IPrettierModule { | ||
default: { | ||
format( | ||
source: string, | ||
options: { | ||
semi?: boolean | ||
parser?: (code: string) => any | ||
} | ||
): string | ||
} | ||
} | ||
|
||
export const loadPrettier = () => | ||
loadScript({ | ||
package: 'prettier@2.3.2', | ||
entry: 'standalone.min.js', | ||
root: 'prettier', | ||
}) | ||
const prettier: Promise<IPrettierModule> = new Function( | ||
'return import("https://cdn.jsdelivr.net/npm/prettier@2.3.2/esm/standalone.mjs")' | ||
)() | ||
|
||
export const format = async (code: string, language: string) => { | ||
const lang = String(language).toLocaleLowerCase() | ||
if (lang === 'json') { | ||
return JSON.stringify(JSON.parse(code), null, 2) | ||
} | ||
if (lang === 'javascript' || lang === 'typescript') { | ||
return loadPrettier().then(({ format }) => { | ||
return format(code, { | ||
export const format = async (language: string, source: string) => { | ||
return prettier.then((module) => { | ||
if ( | ||
language === 'javascript.expression' || | ||
language === 'typescript.expression' | ||
) { | ||
return module.default.format(source, { | ||
semi: false, | ||
parser(text) { | ||
return parse(text, { | ||
sourceType: 'module', | ||
plugins: ['jsx'], | ||
return parseExpression(text, { | ||
plugins: ['typescript', 'jsx'], | ||
}) | ||
}, | ||
}) | ||
}) | ||
} | ||
if (lang === 'javascript.expression' || lang === 'typescript.expression') { | ||
return loadPrettier().then(({ format }) => { | ||
return format(code, { | ||
} | ||
if (/(?:javascript|typescript)/gi.test(language)) { | ||
return module.default.format(source, { | ||
semi: false, | ||
parser(text) { | ||
return parseExpression(text, { | ||
plugins: ['jsx'], | ||
return parse(text, { | ||
plugins: ['typescript', 'jsx'], | ||
}) | ||
}, | ||
}) | ||
}) | ||
} | ||
return code | ||
} | ||
if (language === 'json') { | ||
return JSON.stringify(source, null, 2) | ||
} | ||
return source | ||
}) | ||
} |
Oops, something went wrong.