From 06034a8fc4f9e949450b1de7a62015495fe6aadf Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 6 Nov 2023 07:20:47 -0800 Subject: [PATCH] Save after formatting GraphQL (Closes #9) --- src-web/components/core/Editor/Editor.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src-web/components/core/Editor/Editor.tsx b/src-web/components/core/Editor/Editor.tsx index 8ea03bd7..3a50d090 100644 --- a/src-web/components/core/Editor/Editor.tsx +++ b/src-web/components/core/Editor/Editor.tsx @@ -6,14 +6,14 @@ import classNames from 'classnames'; import { EditorView } from 'codemirror'; import type { MutableRefObject, ReactNode } from 'react'; import { forwardRef, memo, useCallback, useEffect, useImperativeHandle, useRef } from 'react'; +import { useActiveEnvironment } from '../../../hooks/useActiveEnvironment'; +import { useActiveWorkspace } from '../../../hooks/useActiveWorkspace'; import { IconButton } from '../IconButton'; import { HStack } from '../Stacks'; import './Editor.css'; import { baseExtensions, getLanguageExtension, multiLineExtensions } from './extensions'; import type { GenericCompletionConfig } from './genericCompletion'; import { singleLineExt } from './singleLine'; -import { useActiveEnvironment } from '../../../hooks/useActiveEnvironment'; -import { useActiveWorkspace } from '../../../hooks/useActiveWorkspace'; // Export some things so all the code-split parts are in this file export { buildClientSchema, getIntrospectionQuery } from 'graphql/utilities'; @@ -231,10 +231,14 @@ const _Editor = forwardRef(function Editor( onClick={() => { if (cm.current === null) return; const { doc } = cm.current.view.state; - const insert = format(doc.toString()); + const formatted = format(doc.toString()); // Update editor and blur because the cursor will reset anyway - cm.current.view.dispatch({ changes: { from: 0, to: doc.length, insert } }); + cm.current.view.dispatch({ + changes: { from: 0, to: doc.length, insert: formatted }, + }); cm.current.view.contentDOM.blur(); + // Fire change event + onChange?.(formatted); }} />