@@ -157,6 +157,7 @@ export default function PlaygroundNew() {
157157 const sandpackClientRef = useRef < any > ( null ) ;
158158 const [ error , setError ] = useState < Error | null > ( null ) ;
159159 const monaco = useMonaco ( ) ;
160+ const editorRef = useRef < any > ( null ) ;
160161
161162 const setInputFiles = useCallback (
162163 ( updatedInputFiles : Record < string , string > , replace : boolean = true ) => {
@@ -431,17 +432,22 @@ export const vars = stylex.defineVars({
431432 } ;
432433 } , [ ] ) ;
433434
434- const handleFormat = useCallback ( async ( ) => {
435- const formatted = await prettier . format ( inputFiles [ activeInputFile ] ! , {
436- parser : 'babel' ,
437- plugins : [ estreePlugin as any , babelPlugin ] ,
438- } ) ;
439- const updatedInputFiles = {
440- ...inputFiles ,
441- [ activeInputFile ] : formatted ,
442- } ;
443- setInputFiles ( updatedInputFiles , updateSandpack ( updatedInputFiles ) ) ;
444- } , [ inputFiles , activeInputFile , setInputFiles ] ) ;
435+ const handleFormat = async ( ) => {
436+ if ( editorRef . current ) {
437+ const model = editorRef . current . getModel ( ) ;
438+ const formatted = await prettier . format ( model . getValue ( ) , {
439+ parser : 'babel' ,
440+ plugins : [ estreePlugin as any , babelPlugin ] ,
441+ } ) ;
442+
443+ editorRef . current . executeEdits ( 'format' , [
444+ {
445+ range : model . getFullModelRange ( ) ,
446+ text : formatted ,
447+ } ,
448+ ] ) ;
449+ }
450+ } ;
445451
446452 return (
447453 < >
@@ -503,6 +509,7 @@ export const vars = stylex.defineVars({
503509 defaultLanguage = "typescript"
504510 onChange = { handleEditorChange }
505511 onMount = { ( editor : any ) => {
512+ editorRef . current = editor ;
506513 editor . onKeyDown ( ( e : any ) => {
507514 if ( e . browserEvent . key === '/' ) {
508515 e . browserEvent . stopPropagation ( ) ;
0 commit comments