Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { FormattedMessage } from '@kbn/i18n/react';
interface Props {
getCurl: (cb: (text: string) => void) => void;
getDocumentation: () => Promise<string | null>;
autoIndent: (ev: React.MouseEvent) => void;
autoIndent: (ev?: React.MouseEvent) => void;
}

interface State {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { DevToolsSettings } from '../../services';
export type AutocompleteOptions = 'fields' | 'indices' | 'templates';

interface Props {
onSaveSettings: (newSettings: DevToolsSettings) => Promise<void>;
onSaveSettings: (newSettings: DevToolsSettings) => void;
onClose: () => void;
refreshAutocompleteSettings: (selectedSettings: any) => void;
settings: DevToolsSettings;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React, { createContext, Dispatch, useContext, useReducer } from 'react';
import { Action, reducer } from './reducer';
import { DevToolsSettings } from '../../../../services';

export interface ContextValue {
editorsReady: boolean;
settings: DevToolsSettings;
}

const EditorReadContext = createContext<ContextValue>(null as any);
const EditorActionContext = createContext<Dispatch<Action>>(null as any);

export interface EditorContextArgs {
children: any;
settings: DevToolsSettings;
}

const initialValue: ContextValue = {
editorsReady: false,
settings: null as any,
};

export function EditorContextProvider({ children, settings }: EditorContextArgs) {
const [state, dispatch] = useReducer(reducer, initialValue, value => ({
...value,
settings,
}));
return (
<EditorReadContext.Provider value={state}>
<EditorActionContext.Provider value={dispatch}>{children}</EditorActionContext.Provider>
</EditorReadContext.Provider>
);
}

export const useEditorActionContext = () => {
const context = useContext(EditorActionContext);
if (context === undefined) {
throw new Error('useEditorActionContext must be used inside EditorActionContext');
}
return context;
};

export const useEditorReadContext = () => {
const context = useContext(EditorReadContext);
if (context === undefined) {
throw new Error('useEditorReadContext must be used inside EditorContextProvider');
}
return context;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export class EditorRegistry {
inputEditor: any;
outputEditor: any;

setInputEditor(inputEditor: any) {
this.inputEditor = inputEditor;
}

setOutputEditor(outputEditor: any) {
this.outputEditor = outputEditor;
}

getInputEditor() {
return this.inputEditor;
}

getOutputEditor() {
return this.outputEditor;
}
}

// Create a single instance of this and use as private state.
export const instance = new EditorRegistry();
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export {
EditorContextProvider,
useEditorReadContext,
useEditorActionContext,
} from './editor_context';
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Reducer } from 'react';

import { instance as registry } from './editor_registry';
import { ContextValue } from './editor_context';

import { restoreRequestFromHistory } from '../legacy/console_history/restore_request_from_history';
import {
sendCurrentRequestToES,
EsRequestArgs,
} from '../legacy/console_editor/send_current_request_to_es';
import { DevToolsSettings } from '../../../../services';

export type Action =
| { type: 'setInputEditor'; value: any }
| { type: 'setOutputEditor'; value: any }
| { type: 'restoreRequest'; value: any }
| { type: 'updateSettings'; value: DevToolsSettings }
| { type: 'sendRequestToEs'; value: EsRequestArgs }
| { type: 'updateRequestHistory'; value: any };

export const reducer: Reducer<ContextValue, Action> = (state, action) => {
const nextState = { ...state };

if (action.type === 'setInputEditor') {
registry.setInputEditor(action.value);
if (registry.getOutputEditor()) {
nextState.editorsReady = true;
}
}

if (action.type === 'setOutputEditor') {
registry.setOutputEditor(action.value);
if (registry.getInputEditor()) {
nextState.editorsReady = true;
}
}

if (action.type === 'restoreRequest') {
restoreRequestFromHistory(registry.getInputEditor(), action.value);
}

if (action.type === 'updateSettings') {
nextState.settings = action.value;
}

if (action.type === 'sendRequestToEs') {
const { callback, isPolling, isUsingTripleQuotes } = action.value;
sendCurrentRequestToES({
input: registry.getInputEditor(),
output: registry.getOutputEditor(),
callback,
isUsingTripleQuotes,
isPolling,
});
}

return nextState;
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export { Editor, EditorOutput, ConsoleHistory, autoIndent, getDocumentation } from './legacy';
export { useEditorActionContext, useEditorReadContext, EditorContextProvider } from './context';
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { DevToolsSettings } from '../../../../../services';

export function applyCurrentSettings(editor: any, settings: DevToolsSettings) {
editor.getSession().setUseWrapMode(settings.wrapMode);
editor.$el.css('font-size', settings.fontSize + 'px');
}
Loading