Fix new/reset button
This commit is contained in:
15
src/App.tsx
15
src/App.tsx
@@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import Editor from './components/Editor';
|
import Editor from './components/Editor';
|
||||||
import { loadFromBytebin } from './util/storage';
|
import { loadFromBytebin } from './util/storage';
|
||||||
|
|
||||||
@@ -15,18 +15,16 @@ export default function App() {
|
|||||||
const [actualContent, setActualContent] = useState<string>('');
|
const [actualContent, setActualContent] = useState<string>('');
|
||||||
const [contentType, setContentType] = useState<string>();
|
const [contentType, setContentType] = useState<string>();
|
||||||
|
|
||||||
const setContent = useCallback(
|
function setContent(content: string) {
|
||||||
(content: string) => {
|
|
||||||
setActualContent(content);
|
setActualContent(content);
|
||||||
setForcedContent(content);
|
setForcedContent(content);
|
||||||
},
|
}
|
||||||
[setActualContent, setForcedContent]
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (pasteId && state === INITIAL) {
|
if (pasteId && state === INITIAL) {
|
||||||
setState(LOADING);
|
setState(LOADING);
|
||||||
setForcedContent('Loading...');
|
setContent('Loading...');
|
||||||
|
|
||||||
loadFromBytebin(pasteId).then(({ ok, content, type }) => {
|
loadFromBytebin(pasteId).then(({ ok, content, type }) => {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
setContent(content);
|
setContent(content);
|
||||||
@@ -39,12 +37,11 @@ export default function App() {
|
|||||||
setState(LOADED);
|
setState(LOADED);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [pasteId, state, setContent]);
|
}, [pasteId, state]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
forcedContent={forcedContent}
|
forcedContent={forcedContent}
|
||||||
setForcedContent={setContent}
|
|
||||||
actualContent={actualContent}
|
actualContent={actualContent}
|
||||||
setActualContent={setActualContent}
|
setActualContent={setActualContent}
|
||||||
contentType={contentType}
|
contentType={contentType}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { isMobile } from 'react-device-detect';
|
import { isMobile } from 'react-device-detect';
|
||||||
import { ThemeProvider } from 'styled-components';
|
import { ThemeProvider } from 'styled-components';
|
||||||
|
|
||||||
@@ -10,16 +10,16 @@ import EditorTextArea from './EditorTextArea';
|
|||||||
|
|
||||||
export interface EditorProps {
|
export interface EditorProps {
|
||||||
forcedContent: string;
|
forcedContent: string;
|
||||||
setForcedContent: (value: string) => void;
|
|
||||||
actualContent: string;
|
actualContent: string;
|
||||||
setActualContent: (value: string) => void;
|
setActualContent: (value: string) => void;
|
||||||
contentType?: string;
|
contentType?: string;
|
||||||
pasteId?: string;
|
pasteId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ResetFunction = () => void;
|
||||||
|
|
||||||
export default function Editor({
|
export default function Editor({
|
||||||
forcedContent,
|
forcedContent,
|
||||||
setForcedContent,
|
|
||||||
actualContent,
|
actualContent,
|
||||||
setActualContent,
|
setActualContent,
|
||||||
contentType,
|
contentType,
|
||||||
@@ -27,6 +27,7 @@ export default function Editor({
|
|||||||
}: EditorProps) {
|
}: EditorProps) {
|
||||||
const [language, setLanguage] = useState<string>('plain');
|
const [language, setLanguage] = useState<string>('plain');
|
||||||
const [readOnly, setReadOnly] = useState<boolean>(isMobile && !!pasteId);
|
const [readOnly, setReadOnly] = useState<boolean>(isMobile && !!pasteId);
|
||||||
|
const resetFunction = useRef<ResetFunction>();
|
||||||
|
|
||||||
const [theme, setTheme] = usePreference<keyof Themes>(
|
const [theme, setTheme] = usePreference<keyof Themes>(
|
||||||
'theme',
|
'theme',
|
||||||
@@ -58,7 +59,7 @@ export default function Editor({
|
|||||||
<EditorGlobalStyle />
|
<EditorGlobalStyle />
|
||||||
<EditorControls
|
<EditorControls
|
||||||
actualContent={actualContent}
|
actualContent={actualContent}
|
||||||
setForcedContent={setForcedContent}
|
resetFunction={resetFunction}
|
||||||
language={language}
|
language={language}
|
||||||
setLanguage={setLanguage}
|
setLanguage={setLanguage}
|
||||||
readOnly={readOnly}
|
readOnly={readOnly}
|
||||||
@@ -75,6 +76,7 @@ export default function Editor({
|
|||||||
language={language}
|
language={language}
|
||||||
fontSize={fontSize}
|
fontSize={fontSize}
|
||||||
readOnly={readOnly}
|
readOnly={readOnly}
|
||||||
|
resetFunction={resetFunction}
|
||||||
/>
|
/>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
import copy from 'copy-to-clipboard';
|
import copy from 'copy-to-clipboard';
|
||||||
import history from 'history/browser';
|
import history from 'history/browser';
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { MutableRefObject, useCallback, useEffect, useState } from 'react';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
|
||||||
import themes, { Themes } from '../style/themes';
|
import themes, { Themes } from '../style/themes';
|
||||||
import { languages } from '../util/highlighting';
|
import { languages } from '../util/highlighting';
|
||||||
import { saveToBytebin } from '../util/storage';
|
import { saveToBytebin } from '../util/storage';
|
||||||
import Button from './Button';
|
import Button from './Button';
|
||||||
|
import { ResetFunction } from './Editor';
|
||||||
import MenuButton from './MenuButton';
|
import MenuButton from './MenuButton';
|
||||||
|
|
||||||
export interface EditorControlsProps {
|
export interface EditorControlsProps {
|
||||||
actualContent: string;
|
actualContent: string;
|
||||||
setForcedContent: (value: string) => void;
|
resetFunction: MutableRefObject<ResetFunction | undefined>;
|
||||||
language: string;
|
language: string;
|
||||||
setLanguage: (value: string) => void;
|
setLanguage: (value: string) => void;
|
||||||
readOnly: boolean;
|
readOnly: boolean;
|
||||||
@@ -23,7 +24,7 @@ export interface EditorControlsProps {
|
|||||||
|
|
||||||
export default function EditorControls({
|
export default function EditorControls({
|
||||||
actualContent,
|
actualContent,
|
||||||
setForcedContent,
|
resetFunction,
|
||||||
language,
|
language,
|
||||||
setLanguage,
|
setLanguage,
|
||||||
readOnly,
|
readOnly,
|
||||||
@@ -77,7 +78,11 @@ export default function EditorControls({
|
|||||||
}, [save, zoom]);
|
}, [save, zoom]);
|
||||||
|
|
||||||
function reset() {
|
function reset() {
|
||||||
setForcedContent('');
|
if (!resetFunction.current) {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
|
||||||
|
resetFunction.current();
|
||||||
setLanguage('plain');
|
setLanguage('plain');
|
||||||
history.replace({
|
history.replace({
|
||||||
pathname: '/',
|
pathname: '/',
|
||||||
|
|||||||
@@ -5,11 +5,18 @@ import Editor, {
|
|||||||
OnMount,
|
OnMount,
|
||||||
} from '@monaco-editor/react';
|
} from '@monaco-editor/react';
|
||||||
import history from 'history/browser';
|
import history from 'history/browser';
|
||||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
import React, {
|
||||||
|
MutableRefObject,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from 'react';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import themes, { makeMonacoTheme, Theme } from '../style/themes';
|
import themes, { makeMonacoTheme, Theme } from '../style/themes';
|
||||||
|
|
||||||
import type { editor } from 'monaco-editor';
|
import type { editor } from 'monaco-editor';
|
||||||
|
import { ResetFunction } from './Editor';
|
||||||
|
|
||||||
export interface EditorTextAreaProps {
|
export interface EditorTextAreaProps {
|
||||||
forcedContent: string;
|
forcedContent: string;
|
||||||
@@ -19,6 +26,7 @@ export interface EditorTextAreaProps {
|
|||||||
language: string;
|
language: string;
|
||||||
fontSize: number;
|
fontSize: number;
|
||||||
readOnly: boolean;
|
readOnly: boolean;
|
||||||
|
resetFunction: MutableRefObject<ResetFunction | undefined>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function EditorTextArea({
|
export default function EditorTextArea({
|
||||||
@@ -29,6 +37,7 @@ export default function EditorTextArea({
|
|||||||
language,
|
language,
|
||||||
fontSize,
|
fontSize,
|
||||||
readOnly,
|
readOnly,
|
||||||
|
resetFunction,
|
||||||
}: EditorTextAreaProps) {
|
}: EditorTextAreaProps) {
|
||||||
const [editor, setEditor] = useState<editor.IStandaloneCodeEditor>();
|
const [editor, setEditor] = useState<editor.IStandaloneCodeEditor>();
|
||||||
const [monaco, setMonaco] = useState<Monaco>();
|
const [monaco, setMonaco] = useState<Monaco>();
|
||||||
@@ -78,6 +87,11 @@ export default function EditorTextArea({
|
|||||||
setEditor(editor);
|
setEditor(editor);
|
||||||
setMonaco(monaco);
|
setMonaco(monaco);
|
||||||
|
|
||||||
|
resetFunction.current = () => {
|
||||||
|
editor.setValue('');
|
||||||
|
editor.focus();
|
||||||
|
};
|
||||||
|
|
||||||
editor.focus();
|
editor.focus();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user