Skip to content

Commit 8b570ce

Browse files
authored
ACM-16850-miscellaneous-monaco-issues (stolostron#4181)
* ACM-16850-miscellaneous-monaco-issues Signed-off-by: John Swanke <jswanke@redhat.com> * fix snaps Signed-off-by: John Swanke <jswanke@redhat.com> --------- Signed-off-by: John Swanke <jswanke@redhat.com>
1 parent 94cd52d commit 8b570ce

File tree

7 files changed

+68
-24
lines changed

7 files changed

+68
-24
lines changed

frontend/src/components/SyncEditor/SyncEditor.tsx

+26-18
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export function SyncEditor(props: SyncEditorProps): JSX.Element {
179179
window.getEditorValue = () => editor.getValue()
180180
setEditor(editor)
181181
setMonaco(monaco)
182+
layoutEditor(editor)
182183
}
183184

184185
useEffect(() => {
@@ -752,25 +753,32 @@ export function SyncEditor(props: SyncEditorProps): JSX.Element {
752753
]
753754
)
754755

755-
useResizeObserver(pageRef, (entry) => {
756-
const { width } = entry.contentRect
757-
let { height } = entry.contentRect
758-
759-
if (pageRef.current) {
760-
height = window.innerHeight - pageRef.current?.getBoundingClientRect().top
761-
}
762-
763-
if (variant === 'toolbar') {
764-
height -= 36
765-
}
766-
if (editorHasErrors) {
767-
height -= 75
768-
}
769-
if (editor) {
770-
editor.layout({ width, height })
771-
setShowCondensed(width < 500)
772-
}
756+
useResizeObserver(pageRef, () => {
757+
layoutEditor(editor)
773758
})
759+
const layoutEditor = useCallback(
760+
(editor: any) => {
761+
if (pageRef.current && editor) {
762+
const rect = pageRef.current.getBoundingClientRect()
763+
const { width } = rect
764+
let { height } = rect
765+
766+
if (pageRef.current) {
767+
height = window.innerHeight - pageRef.current?.getBoundingClientRect().top
768+
}
769+
770+
if (variant === 'toolbar') {
771+
height -= 36
772+
}
773+
if (editorHasErrors) {
774+
height -= 75
775+
}
776+
editor.layout({ width, height })
777+
setShowCondensed(width < 500)
778+
}
779+
},
780+
[editorHasErrors, variant]
781+
)
774782

775783
return (
776784
<div ref={pageRef} className="sync-editor__container">

frontend/src/components/TemplateEditor/TemplateEditor.js

+8
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,16 @@ export default class TemplateEditor extends React.Component {
835835
highlightDecorations(this.editors, this.state.decorationRows, this.state.i18n)
836836
}
837837
this.layoutEditors()
838+
editor.clearedUndoRedoStack = false
838839
editor.onDidChangeModelContent(() => {
840+
const editorHasFocus = !!document.querySelector('.monaco-editor.focused')
841+
const activeId = document.activeElement?.id
842+
const formHasFocus = !editorHasFocus && ['undo-button', 'redo-button'].indexOf(activeId) === -1
839843
const model = editor.getModel()
844+
if (!editor.clearedUndoRedoStack || formHasFocus) {
845+
model._undoRedoService._editStacks.clear()
846+
editor.clearedUndoRedoStack = true
847+
}
840848
const hasUndo = model.canUndo()
841849
const hasRedo = model.canRedo()
842850
this.setState({ hasUndo, hasRedo })

frontend/src/components/TemplateEditor/components/EditorBar.js

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class EditorButton extends React.Component {
6666
role={'button'}
6767
aria-label={tooltip}
6868
title={tooltip}
69+
id={`${icon}-button`}
6970
onClick={this.handleClick}
7071
onKeyPress={this.handleKeyPress}
7172
>

frontend/src/components/TemplateEditor/components/EditorHeader.js

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class EditorHeader extends React.Component {
6969
variant="inline-compact"
7070
isBlock
7171
onCopy={() => handleEditorCommand('copyAll')}
72+
style={{ backgroundColor: '#25282c' }}
7273
/>
7374
</div>
7475
</div>

frontend/src/components/TemplateEditor/components/YamlEditor.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

44
import React from 'react'
5+
import debounce from 'lodash/debounce'
56
import PropTypes from 'prop-types'
67
import { DecorationType } from '../utils/source-utils'
78
import { global_BackgroundColor_200 as globalBackground200 } from '@patternfly/react-tokens/dist/js/global_BackgroundColor_200'
@@ -32,8 +33,8 @@ class YamlEditor extends React.Component {
3233
language: 'yaml',
3334
height: '100%',
3435
width: '100%',
36+
theme: 'console',
3537
options: {
36-
theme: 'console',
3738
wordWrap: 'wordWrapColumn',
3839
wordWrapColumn: 132,
3940
wordWrapMinified: false,
@@ -50,6 +51,7 @@ class YamlEditor extends React.Component {
5051
editorWillMount: this.editorWillMount.bind(this),
5152
onChange: onYamlChange,
5253
}),
54+
editorHasFocus: false,
5355
}
5456
}
5557

@@ -159,14 +161,31 @@ class YamlEditor extends React.Component {
159161
}
160162
}).bind(this)
161163
)
164+
165+
editor.onMouseDown(
166+
debounce(() => {
167+
const editorHasFocus = !!document.querySelector('.monaco-editor.focused')
168+
this.setState({ editorHasFocus })
169+
}, 0)
170+
)
171+
172+
editor.onDidBlurEditorWidget(() => {
173+
const editorHasFocus = !!document.querySelector('.monaco-editor.focused')
174+
const activeId = document.activeElement?.id
175+
if (!editorHasFocus && ['undo-button', 'redo-button'].indexOf(activeId) === -1) {
176+
this.setState({ editorHasFocus })
177+
}
178+
})
162179
}
163180

164181
shouldComponentUpdate(nextProps) {
182+
// if editor has focus, ignore form changes, since editor is doing all the changes
165183
return (
166-
this.props.yaml !== nextProps.yaml ||
167-
this.props.hide !== nextProps.hide ||
168-
this.props.readOnly !== nextProps.readOnly ||
169-
this.props.showCondensed !== nextProps.showCondensed
184+
!this.state.editorHasFocus &&
185+
(this.props.yaml !== nextProps.yaml ||
186+
this.props.hide !== nextProps.hide ||
187+
this.props.readOnly !== nextProps.readOnly ||
188+
this.props.showCondensed !== nextProps.showCondensed)
170189
)
171190
}
172191

@@ -193,9 +212,9 @@ class YamlEditor extends React.Component {
193212
{editor &&
194213
React.cloneElement(editor, {
195214
value: yaml,
215+
theme: 'console',
196216
options: {
197217
...this.state.editor.props.options,
198-
theme: 'console',
199218
wordWrapColumn: showCondensed ? 512 : 256,
200219
minimap: {
201220
enabled: !showCondensed,

frontend/src/components/TemplateEditor/components/__snapshots__/EditorBar.test.js.snap

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports[`EditorBar component renders as expected 1`] = `
2323
>
2424
<div
2525
class="editor-bar-button restore"
26+
id="undefined-button"
2627
role="button"
2728
tabindex="0"
2829
>
@@ -34,6 +35,7 @@ exports[`EditorBar component renders as expected 1`] = `
3435
>
3536
<div
3637
class="editor-bar-button undo disabled"
38+
id="undo-button"
3739
role="button"
3840
tabindex="0"
3941
>
@@ -53,6 +55,7 @@ exports[`EditorBar component renders as expected 1`] = `
5355
</div>
5456
<div
5557
class="editor-bar-button redo"
58+
id="redo-button"
5659
role="button"
5760
tabindex="0"
5861
>
@@ -116,6 +119,7 @@ exports[`EditorBar component renders as expected 1`] = `
116119
</div>
117120
<div
118121
class="editor-bar-button previous disabled"
122+
id="previous-button"
119123
role="button"
120124
tabindex="0"
121125
>
@@ -135,6 +139,7 @@ exports[`EditorBar component renders as expected 1`] = `
135139
</div>
136140
<div
137141
class="editor-bar-button next disabled"
142+
id="next-button"
138143
role="button"
139144
tabindex="0"
140145
>
@@ -164,6 +169,7 @@ exports[`EditorBar component renders as expected 1`] = `
164169
>
165170
<div
166171
class="editor-bar-button close"
172+
id="close-button"
167173
role="button"
168174
tabindex="0"
169175
>

frontend/src/components/TemplateEditor/components/__snapshots__/EditorHeader.test.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ exports[`EditorHeader component renders as expected 1`] = `
6262
class="pf-v5-c-clipboard-copy pf-m-inline pf-m-block"
6363
data-ouia-component-type="PF5/ClipboardCopy"
6464
data-ouia-safe="true"
65+
style="background-color: rgb(37, 40, 44);"
6566
>
6667
<span
6768
class="pf-v5-c-clipboard-copy__text"

0 commit comments

Comments
 (0)