From 59737edfb4b8d7e9bc373e63dd74a28d2b3ff374 Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Thu, 16 Apr 2020 11:51:18 +0200 Subject: [PATCH] Always focus editor after pressing buttons in Publish sidebar component --- app/src/components/Sidebar/Publish/Editor.tsx | 2 + .../Sidebar/Publish/EditorModeSelect.tsx | 2 + .../components/Sidebar/Publish/Publish.tsx | 70 +++++++++++-------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/app/src/components/Sidebar/Publish/Editor.tsx b/app/src/components/Sidebar/Publish/Editor.tsx index 99141b5..2bef531 100644 --- a/app/src/components/Sidebar/Publish/Editor.tsx +++ b/app/src/components/Sidebar/Publish/Editor.tsx @@ -16,6 +16,7 @@ function Editor(props: { theme: Theme value: string | undefined onChange: (value: string) => void + editorRef: React.Ref }) { const editorOptions = { showLineNumbers: false, @@ -24,6 +25,7 @@ function Editor(props: { return ( , value: string) => void + focusEditor: () => void } export function EditorModeSelect(props: Props) { const labelStyle = { margin: '0 8px 0 8px' } @@ -11,6 +12,7 @@ export function EditorModeSelect(props: Props) { diff --git a/app/src/components/Sidebar/Publish/Publish.tsx b/app/src/components/Sidebar/Publish/Publish.tsx index 3f29d27..3392ca9 100644 --- a/app/src/components/Sidebar/Publish/Publish.tsx +++ b/app/src/components/Sidebar/Publish/Publish.tsx @@ -3,7 +3,7 @@ import FormatAlignLeft from '@material-ui/icons/FormatAlignLeft' import Message from './Model/Message' import Navigation from '@material-ui/icons/Navigation' import PublishHistory from './PublishHistory' -import React, { useCallback, useMemo, useState } from 'react' +import React, { useCallback, useMemo, useState, useRef, memo } from 'react' import RetainSwitch from './RetainSwitch' import TopicInput from './TopicInput' import { AppState } from '../../../reducers' @@ -13,6 +13,7 @@ import { connect } from 'react-redux' import { EditorModeSelect } from './EditorModeSelect' import { globalActions, publishActions } from '../../../actions' import { KeyCodes } from '../../../utils/KeyCodes' +import { default as AceEditor } from 'react-ace' interface Props { connectionId?: string @@ -41,8 +42,13 @@ function useHistory(): [Array, (topic: string, payload?: string) => voi } function Publish(props: Props) { + const editorRef = useRef() const [history, amendToHistory] = useHistory() + const focusEditor = useCallback(() => { + editorRef.current?.editor.focus() + }, [editorRef]) + const publish = useCallback(() => { if (!props.connectionId) { return @@ -74,13 +80,19 @@ function Publish(props: Props) {
- +
@@ -93,6 +105,7 @@ function Publish(props: Props) { function EditorMode(props: { payload?: string editorMode: string + focusEditor: () => void actions: typeof publishActions globalActions: typeof globalActions publish: () => void @@ -114,41 +127,42 @@ function EditorMode(props: { } }, [props.payload]) - const renderFormatJson = useCallback(() => { - if (props.editorMode !== 'json') { - return null - } - - return ( - - - - - - ) - }, [formatJson, props.editorMode, props.publish]) - return useMemo( () => (
- - {renderFormatJson()} + +
- +
), - [props.editorMode, renderFormatJson] + [props.editorMode] ) } -const PublishButton = (props: { publish: () => void }) => { +const FormatJsonButton = React.memo(function FormatJsonButton(props: { editorMode: string, focusEditor: () => void, formatJson: () => void }) { + if (props.editorMode !== 'json') { + return null + } + + return ( + + + + + + ) +}) + +const PublishButton = (props: { publish: () => void, focusEditor: () => void }) => { const handleClickPublish = useCallback( (e: React.MouseEvent) => { e.stopPropagation() @@ -157,10 +171,6 @@ const PublishButton = (props: { publish: () => void }) => { [props.publish] ) - const onFocus = useCallback((e: React.FocusEvent) => { - ;(e.relatedTarget as HTMLElement | null)?.focus() - }, []) - return useMemo( () => (