Skip to content

Commit 26d729c

Browse files
authored
make it possible to use Escape key to escape editor (#1849)
1 parent 17dfb6b commit 26d729c

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/neo4j-arc/cypher-language-support/cypher-editor/CypherEditor.tsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const shouldCheckForHints = (code: string) =>
3737
const MonacoStyleWrapper = styled.div`
3838
height: 100%;
3939
width: 100%;
40+
overflow-x: auto;
4041
4142
.margin .margin-view-overlays {
4243
margin-left: 10px;
@@ -91,7 +92,11 @@ const cypherEditorDefaultProps: CypherEditorDefaultProps = {
9192
value: ''
9293
}
9394

94-
type CypherEditorState = { currentHistoryIndex: number; draft: string }
95+
type CypherEditorState = {
96+
currentHistoryIndex: number
97+
draft: string
98+
isEditorFocusable: boolean
99+
}
95100
const UNRUN_CMD_HISTORY_INDEX = -1
96101

97102
export class CypherEditor extends React.Component<
@@ -100,15 +105,17 @@ export class CypherEditor extends React.Component<
100105
> {
101106
state: CypherEditorState = {
102107
currentHistoryIndex: UNRUN_CMD_HISTORY_INDEX,
108+
isEditorFocusable: true,
103109
draft: ''
104110
}
105111
resizeObserver: ResizeObserver
106112
editor?: monaco.editor.IStandaloneCodeEditor
107113
container?: HTMLElement
114+
wrapperRef = React.createRef<HTMLDivElement>()
108115

109116
constructor(props: CypherEditorProps) {
110117
super(props)
111-
118+
this.wrapperRef = React.createRef()
112119
// Wrapped in requestAnimationFrame to avoid the error "ResizeObserver loop limit exceeded"
113120
this.resizeObserver = new ResizeObserver(() => {
114121
window.requestAnimationFrame(() => {
@@ -391,6 +398,10 @@ export class CypherEditor extends React.Component<
391398
this.props.onDisplayHelpKeys
392399
)
393400

401+
this.editor.addCommand(KeyCode.Escape, () => {
402+
this.wrapperRef.current?.focus()
403+
})
404+
394405
keys(this.props.additionalCommands).forEach(key => {
395406
const command = this.props.additionalCommands[key]
396407
if (!command) {
@@ -435,6 +446,14 @@ export class CypherEditor extends React.Component<
435446
<MonacoStyleWrapper
436447
id={this.getMonacoId()}
437448
className={this.props.className}
449+
ref={this.wrapperRef}
450+
tabIndex={-1}
451+
onFocus={() => {
452+
this.setState({ isEditorFocusable: false })
453+
}}
454+
onBlur={() => {
455+
this.setState({ isEditorFocusable: true })
456+
}}
438457
/>
439458
)
440459
}
@@ -458,9 +477,9 @@ export class CypherEditor extends React.Component<
458477
this.onContentUpdate()
459478
}
460479

461-
if (tabIndex !== prevProps.tabIndex) {
462-
this.editor?.updateOptions({ tabIndex: this.props.tabIndex })
463-
}
480+
this.editor?.updateOptions({
481+
tabIndex: this.state.isEditorFocusable ? this.props.tabIndex : -1
482+
})
464483
}
465484

466485
componentWillUnmount = (): void => {

src/neo4j-arc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@neo4j-devtools/arc",
3-
"version": "0.0.39",
3+
"version": "0.0.40",
44
"main": "dist/neo4j-arc.js",
55
"author": "Neo4j Inc.",
66
"license": "GPL-3.0",

0 commit comments

Comments
 (0)