From 65cde5e1376041606cb48c837a4a752a526009ae Mon Sep 17 00:00:00 2001 From: Morten Olsen Date: Fri, 5 Jul 2019 14:24:54 +0200 Subject: [PATCH] update --- demo/App.js | 10 +++-- lib/src/components/DevTool/Console/Input.js | 46 +++++++++++++-------- lib/src/components/DevTool/Console/index.js | 3 +- lib/src/components/DevTool/Modal.js | 2 +- lib/src/components/DevTool/index.js | 2 + lib/src/console.js | 5 ++- lib/src/log.js | 3 +- 7 files changed, 46 insertions(+), 25 deletions(-) diff --git a/demo/App.js b/demo/App.js index 890fe0f..47a2272 100644 --- a/demo/App.js +++ b/demo/App.js @@ -1,7 +1,6 @@ import React from 'react'; import { StyleSheet, - Text, View, Button, KeyboardAvoidingView, @@ -17,7 +16,7 @@ import { } from 'react-native-debug-console'; network.attach(); -log.attach(); +log.attach(true); console.log('fooo'); let xhr = new XMLHttpRequest(); @@ -117,8 +116,13 @@ export default class App extends React.Component { style={{ flex: 1, }} + storageProvider={AsyncStorage} + context={global} + /> + - ); diff --git a/lib/src/components/DevTool/Console/Input.js b/lib/src/components/DevTool/Console/Input.js index ecb9862..0d7a072 100644 --- a/lib/src/components/DevTool/Console/Input.js +++ b/lib/src/components/DevTool/Console/Input.js @@ -3,6 +3,7 @@ import { StyleSheet, View, TextInput, + Platform, } from 'react-native'; import styled from 'styled-components/native'; import { createContext } from '../../../console'; @@ -33,11 +34,29 @@ const styles = StyleSheet.create({ const Input = ({ provider, - logs, + context: baseContext, }) => { const [text, setText] = useState(''); const [history, setHistory] = useState([]); const [historyOffset, setHistoryOffset] = useState(); + + const send = () => { + const newHistory = [...history, text]; + const context = createContext({ + logProvider: provider, + }, baseContext); + const contextKeys = Object.keys(context); + const contextValues = Object.values(context); + const fn = new Function(...contextKeys, text); + try { + fn(...contextValues); + setText(''); + setHistoryOffset(undefined); + setHistory(newHistory); + } catch (err) { + provider.error([err]); + } + }; return ( diff --git a/lib/src/components/DevTool/Console/index.js b/lib/src/components/DevTool/Console/index.js index 551e1ca..c4c5f67 100644 --- a/lib/src/components/DevTool/Console/index.js +++ b/lib/src/components/DevTool/Console/index.js @@ -32,6 +32,7 @@ const initFilters = [ const Console = ({ includeStackTrace, provider, + context, }) => { const logs = useLog(provider); const [filters, setFilters] = useState(initFilters); @@ -55,7 +56,7 @@ const Console = ({ /> f.selected).map(f => f.name)} logs={logs} includeStackTrace={includeStackTrace} /> - + ); }; diff --git a/lib/src/components/DevTool/Modal.js b/lib/src/components/DevTool/Modal.js index 7dd463f..ea993ff 100644 --- a/lib/src/components/DevTool/Modal.js +++ b/lib/src/components/DevTool/Modal.js @@ -53,7 +53,7 @@ class Events extends Component { }} > { @@ -36,6 +37,7 @@ const DevTool = ({ ), getData: log.get, diff --git a/lib/src/console.js b/lib/src/console.js index d40565b..1dea6e0 100644 --- a/lib/src/console.js +++ b/lib/src/console.js @@ -1,6 +1,9 @@ export const createContext = ({ logProvider, -}) => ({ +}, baseContext = {}) => ({ log: (...args) => logProvider.log(...args), clear: logProvider.clear, + window: baseContext, + global: baseContext, + context: baseContext, }); \ No newline at end of file diff --git a/lib/src/log.js b/lib/src/log.js index 545e5a0..b4db42c 100644 --- a/lib/src/log.js +++ b/lib/src/log.js @@ -60,11 +60,12 @@ class Log { handleError(err) { if (err && err.error) { - this.error([err.error], true); + this.error([err.error]); } } attach(keep) { + global.proxyConsole = proxyConsole; const redirected = Object.keys(proxyConsole).reduce((output, key) => ({ ...output, [key]: keep ? (...args) => proxyConsole[key](...args) : () => {},