mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[Console Monaco Migration] Add autosave to localStorage (#180620)
Closes https://github.com/elastic/kibana/issues/180211 ## Summary This PR adds support for autosaving the current Console editor value to local storage. **How to test:** 1. Create a `config/kibana.dev.yml` file (if one doesn't exist already) and add the line: `console.dev.enableMonaco: true` 2. Start Es and Kibana 3. Go to Dev Tools -> Console 4. Type in some input in Console 5. Navigate to another page and come back to Console. Verify that the input is the same as the one you last typed.
This commit is contained in:
parent
173c96fa6f
commit
46111c9436
2 changed files with 52 additions and 0 deletions
|
@ -11,6 +11,7 @@ import { CodeEditor } from '@kbn/code-editor';
|
|||
import { css } from '@emotion/react';
|
||||
import { CONSOLE_LANG_ID, CONSOLE_THEME_ID } from '@kbn/monaco';
|
||||
import { useSetInitialValue } from './use_set_initial_value';
|
||||
import { useSetupAutosave } from './use_setup_autosave';
|
||||
import { useServicesContext, useEditorReadContext } from '../../../contexts';
|
||||
|
||||
export interface EditorProps {
|
||||
|
@ -33,6 +34,8 @@ export const MonacoEditor = ({ initialTextValue }: EditorProps) => {
|
|||
setInitialValue({ initialTextValue, setValue, toasts });
|
||||
}, [initialTextValue, setInitialValue, toasts]);
|
||||
|
||||
useSetupAutosave({ value });
|
||||
|
||||
return (
|
||||
<div
|
||||
css={css`
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0 and the Server Side Public License, v 1; you may not use this file except
|
||||
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
||||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useSaveCurrentTextObject } from '../../../hooks';
|
||||
|
||||
interface SetupAutosaveParams {
|
||||
/** The current input value in the Console editor. */
|
||||
value: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook that sets up autosaving the Console editor input to localStorage.
|
||||
*
|
||||
* @param params The {@link SetupAutosaveParams} to use.
|
||||
*/
|
||||
export const useSetupAutosave = (params: SetupAutosaveParams) => {
|
||||
const { value } = params;
|
||||
const saveCurrentTextObject = useSaveCurrentTextObject();
|
||||
const timerRef = useRef<number | undefined>();
|
||||
|
||||
useEffect(() => {
|
||||
function saveCurrentState() {
|
||||
try {
|
||||
saveCurrentTextObject(value);
|
||||
} catch (e) {
|
||||
// Ignoring saving error
|
||||
}
|
||||
}
|
||||
|
||||
const saveDelay = 500;
|
||||
|
||||
if (timerRef.current) {
|
||||
clearTimeout(timerRef.current);
|
||||
}
|
||||
timerRef.current = window.setTimeout(saveCurrentState, saveDelay);
|
||||
|
||||
return () => {
|
||||
if (timerRef.current) {
|
||||
clearTimeout(timerRef.current);
|
||||
}
|
||||
};
|
||||
}, [saveCurrentTextObject, value]);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue