diff --git a/package-lock.json b/package-lock.json index 0a3c2cf..d5fc188 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "graphiql-explorer", - "version": "0.4.6", + "version": "0.5.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a2f9a16..19036ed 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiql-explorer", - "version": "0.4.6", + "version": "0.5.1", "homepage": "https://github.com/onegraph/graphiql-explorer", "bugs": { "url": "https://github.com/onegraph/graphiql-explorer/issues" diff --git a/src/Explorer.js b/src/Explorer.js index dd8c09f..ea2ed42 100644 --- a/src/Explorer.js +++ b/src/Explorer.js @@ -126,6 +126,7 @@ type NewOperationType = 'query' | 'mutation' | 'subscription'; type State = {| operation: ?OperationDefinitionNode, newOperationType: NewOperationType, + operationToScrollTo: ?string, |}; type Selections = $ReadOnlyArray; @@ -1445,6 +1446,7 @@ type RootViewProps = {| ) => void, onOperationRename: (query: string) => void, onRunOperation: (name: ?string) => void, + onMount: (rootViewElId: string) => void, getDefaultFieldNames: (type: GraphQLObjectType) => Array, getDefaultScalarArgValue: GetDefaultScalarArgValue, makeDefaultArg: ?MakeDefaultArg, @@ -1516,15 +1518,28 @@ class RootView extends React.PureComponent { } }; + _rootViewElId = () => { + const {operation, name} = this.props; + const rootViewElId = `${operation}-${name || 'unknown'}`; + return rootViewElId; + }; + + componentDidMount() { + const rootViewElId = this._rootViewElId(); + + this.props.onMount(rootViewElId); + } + render() { const { operation, - name, definition, schema, getDefaultFieldNames, styleConfig, } = this.props; + const rootViewElId = this._rootViewElId(); + const fields = this.props.fields || {}; const operationDef = definition; const selections = operationDef.selectionSet.selections; @@ -1534,7 +1549,7 @@ class RootView extends React.PureComponent { return (
{ getDefaultScalarArgValue: defaultGetDefaultScalarArgValue, }; - state = {newOperationType: 'query', operation: null}; + state = { + newOperationType: 'query', + operation: null, + operationToScrollTo: null, + }; _ref: ?any; _resetScroll = () => { @@ -1645,6 +1664,18 @@ class Explorer extends React.PureComponent { this.setState({newOperationType: value}); }; + _handleRootViewMount = (rootViewElId: string) => { + if ( + !!this.state.operationToScrollTo && + this.state.operationToScrollTo === rootViewElId + ) { + var selector = `.graphiql-explorer-root #${rootViewElId}`; + + var el = document.querySelector(selector); + el && el.scrollIntoView(); + } + }; + render() { const {schema, query, makeDefaultArg} = this.props; @@ -1795,35 +1826,40 @@ class Explorer extends React.PureComponent { definitions: newDefinitions, }; + this.setState({operationToScrollTo: `${kind}-${newOperationName}`}); + this.props.onEdit(print(newOperationDef)); }; const actionsOptions = [ !!queryFields ? ( ) : null, !!mutationFields ? ( ) : null, !!subscriptionFields ? ( ) : null, ].filter(Boolean); @@ -1846,6 +1882,14 @@ class Explorer extends React.PureComponent { borderTop: '1px solid rgb(214, 214, 214)', }} onSubmit={event => event.preventDefault()}> + + Add new{' '} +