Always focus editor after pressing buttons in Publish sidebar component
This commit is contained in:
@@ -16,6 +16,7 @@ function Editor(props: {
|
|||||||
theme: Theme
|
theme: Theme
|
||||||
value: string | undefined
|
value: string | undefined
|
||||||
onChange: (value: string) => void
|
onChange: (value: string) => void
|
||||||
|
editorRef: React.Ref<AceEditor>
|
||||||
}) {
|
}) {
|
||||||
const editorOptions = {
|
const editorOptions = {
|
||||||
showLineNumbers: false,
|
showLineNumbers: false,
|
||||||
@@ -24,6 +25,7 @@ function Editor(props: {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<AceEditor
|
<AceEditor
|
||||||
|
ref={props.editorRef}
|
||||||
style={{}}
|
style={{}}
|
||||||
mode={props.editorMode}
|
mode={props.editorMode}
|
||||||
theme={props.theme.palette.type === 'dark' ? 'monokai' : 'dawn'}
|
theme={props.theme.palette.type === 'dark' ? 'monokai' : 'dawn'}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { FormControlLabel, Radio, RadioGroup } from '@material-ui/core'
|
|||||||
interface Props {
|
interface Props {
|
||||||
value: string
|
value: string
|
||||||
onChange: (event: React.ChangeEvent<{}>, value: string) => void
|
onChange: (event: React.ChangeEvent<{}>, value: string) => void
|
||||||
|
focusEditor: () => void
|
||||||
}
|
}
|
||||||
export function EditorModeSelect(props: Props) {
|
export function EditorModeSelect(props: Props) {
|
||||||
const labelStyle = { margin: '0 8px 0 8px' }
|
const labelStyle = { margin: '0 8px 0 8px' }
|
||||||
@@ -11,6 +12,7 @@ export function EditorModeSelect(props: Props) {
|
|||||||
<RadioGroup
|
<RadioGroup
|
||||||
style={{ display: 'inline-block', float: 'left' }}
|
style={{ display: 'inline-block', float: 'left' }}
|
||||||
value={props.value}
|
value={props.value}
|
||||||
|
onFocus={props.focusEditor}
|
||||||
onChange={props.onChange}
|
onChange={props.onChange}
|
||||||
row={true}
|
row={true}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import FormatAlignLeft from '@material-ui/icons/FormatAlignLeft'
|
|||||||
import Message from './Model/Message'
|
import Message from './Model/Message'
|
||||||
import Navigation from '@material-ui/icons/Navigation'
|
import Navigation from '@material-ui/icons/Navigation'
|
||||||
import PublishHistory from './PublishHistory'
|
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 RetainSwitch from './RetainSwitch'
|
||||||
import TopicInput from './TopicInput'
|
import TopicInput from './TopicInput'
|
||||||
import { AppState } from '../../../reducers'
|
import { AppState } from '../../../reducers'
|
||||||
@@ -13,6 +13,7 @@ import { connect } from 'react-redux'
|
|||||||
import { EditorModeSelect } from './EditorModeSelect'
|
import { EditorModeSelect } from './EditorModeSelect'
|
||||||
import { globalActions, publishActions } from '../../../actions'
|
import { globalActions, publishActions } from '../../../actions'
|
||||||
import { KeyCodes } from '../../../utils/KeyCodes'
|
import { KeyCodes } from '../../../utils/KeyCodes'
|
||||||
|
import { default as AceEditor } from 'react-ace'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
connectionId?: string
|
connectionId?: string
|
||||||
@@ -41,8 +42,13 @@ function useHistory(): [Array<Message>, (topic: string, payload?: string) => voi
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Publish(props: Props) {
|
function Publish(props: Props) {
|
||||||
|
const editorRef = useRef<AceEditor>()
|
||||||
const [history, amendToHistory] = useHistory()
|
const [history, amendToHistory] = useHistory()
|
||||||
|
|
||||||
|
const focusEditor = useCallback(() => {
|
||||||
|
editorRef.current?.editor.focus()
|
||||||
|
}, [editorRef])
|
||||||
|
|
||||||
const publish = useCallback(() => {
|
const publish = useCallback(() => {
|
||||||
if (!props.connectionId) {
|
if (!props.connectionId) {
|
||||||
return
|
return
|
||||||
@@ -74,13 +80,19 @@ function Publish(props: Props) {
|
|||||||
<TopicInput />
|
<TopicInput />
|
||||||
<div style={{ width: '100%', display: 'block' }}>
|
<div style={{ width: '100%', display: 'block' }}>
|
||||||
<EditorMode
|
<EditorMode
|
||||||
|
focusEditor={focusEditor}
|
||||||
actions={props.actions}
|
actions={props.actions}
|
||||||
globalActions={props.globalActions}
|
globalActions={props.globalActions}
|
||||||
payload={props.payload}
|
payload={props.payload}
|
||||||
editorMode={props.editorMode}
|
editorMode={props.editorMode}
|
||||||
publish={publish}
|
publish={publish}
|
||||||
/>
|
/>
|
||||||
<Editor value={props.payload} editorMode={props.editorMode} onChange={props.actions.setPayload} />
|
<Editor
|
||||||
|
value={props.payload}
|
||||||
|
editorMode={props.editorMode}
|
||||||
|
onChange={props.actions.setPayload}
|
||||||
|
editorRef={editorRef as any}
|
||||||
|
/>
|
||||||
<RetainSwitch />
|
<RetainSwitch />
|
||||||
</div>
|
</div>
|
||||||
<PublishHistory history={history} />
|
<PublishHistory history={history} />
|
||||||
@@ -93,6 +105,7 @@ function Publish(props: Props) {
|
|||||||
function EditorMode(props: {
|
function EditorMode(props: {
|
||||||
payload?: string
|
payload?: string
|
||||||
editorMode: string
|
editorMode: string
|
||||||
|
focusEditor: () => void
|
||||||
actions: typeof publishActions
|
actions: typeof publishActions
|
||||||
globalActions: typeof globalActions
|
globalActions: typeof globalActions
|
||||||
publish: () => void
|
publish: () => void
|
||||||
@@ -114,41 +127,42 @@ function EditorMode(props: {
|
|||||||
}
|
}
|
||||||
}, [props.payload])
|
}, [props.payload])
|
||||||
|
|
||||||
const renderFormatJson = useCallback(() => {
|
|
||||||
if (props.editorMode !== 'json') {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Tooltip title="Format JSON">
|
|
||||||
<Fab
|
|
||||||
style={{ width: '36px', height: '36px', margin: '0 8px' }}
|
|
||||||
onClick={formatJson}
|
|
||||||
id="sidebar-publish-format-json"
|
|
||||||
>
|
|
||||||
<FormatAlignLeft style={{ fontSize: '20px' }} />
|
|
||||||
</Fab>
|
|
||||||
</Tooltip>
|
|
||||||
)
|
|
||||||
}, [formatJson, props.editorMode, props.publish])
|
|
||||||
|
|
||||||
return useMemo(
|
return useMemo(
|
||||||
() => (
|
() => (
|
||||||
<div style={{ marginTop: '16px' }}>
|
<div style={{ marginTop: '16px' }}>
|
||||||
<div style={{ width: '100%', lineHeight: '64px', textAlign: 'center' }}>
|
<div style={{ width: '100%', lineHeight: '64px', textAlign: 'center' }}>
|
||||||
<EditorModeSelect value={props.editorMode} onChange={updateMode} />
|
<EditorModeSelect value={props.editorMode} onChange={updateMode} focusEditor={props.focusEditor} />
|
||||||
{renderFormatJson()}
|
<FormatJsonButton editorMode={props.editorMode} focusEditor={props.focusEditor} formatJson={formatJson} />
|
||||||
<div style={{ float: 'right' }}>
|
<div style={{ float: 'right' }}>
|
||||||
<PublishButton publish={props.publish} />
|
<PublishButton publish={props.publish} focusEditor={props.focusEditor} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
),
|
),
|
||||||
[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 (
|
||||||
|
<Tooltip title="Format JSON">
|
||||||
|
<Fab
|
||||||
|
style={{ width: '36px', height: '36px', margin: '0 8px' }}
|
||||||
|
onClick={props.formatJson}
|
||||||
|
onFocus={props.focusEditor}
|
||||||
|
id="sidebar-publish-format-json"
|
||||||
|
>
|
||||||
|
<FormatAlignLeft style={{ fontSize: '20px' }} />
|
||||||
|
</Fab>
|
||||||
|
</Tooltip>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const PublishButton = (props: { publish: () => void, focusEditor: () => void }) => {
|
||||||
const handleClickPublish = useCallback(
|
const handleClickPublish = useCallback(
|
||||||
(e: React.MouseEvent) => {
|
(e: React.MouseEvent) => {
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
@@ -157,10 +171,6 @@ const PublishButton = (props: { publish: () => void }) => {
|
|||||||
[props.publish]
|
[props.publish]
|
||||||
)
|
)
|
||||||
|
|
||||||
const onFocus = useCallback((e: React.FocusEvent<HTMLElement>) => {
|
|
||||||
;(e.relatedTarget as HTMLElement | null)?.focus()
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
return useMemo(
|
return useMemo(
|
||||||
() => (
|
() => (
|
||||||
<Button
|
<Button
|
||||||
@@ -168,7 +178,7 @@ const PublishButton = (props: { publish: () => void }) => {
|
|||||||
size="small"
|
size="small"
|
||||||
color="primary"
|
color="primary"
|
||||||
onClick={handleClickPublish}
|
onClick={handleClickPublish}
|
||||||
onFocusCapture={onFocus}
|
onFocus={props.focusEditor}
|
||||||
id="publish-button"
|
id="publish-button"
|
||||||
>
|
>
|
||||||
<Navigation style={{ marginRight: '8px' }} /> Publish
|
<Navigation style={{ marginRight: '8px' }} /> Publish
|
||||||
|
|||||||
Reference in New Issue
Block a user