Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions frontend/web/components/ValueEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Highlight from './Highlight'
import ConfigProvider from 'common/providers/ConfigProvider'
import { Clipboard } from 'polyfill-react-native'
import Icon from './icons/Icon'
import BareButton from './base/forms/BareButton'
import { IonIcon } from '@ionic/react'
import { checkmarkCircle, warning } from 'ionicons/icons'

Expand Down Expand Up @@ -141,8 +142,25 @@ class ValueEditor extends Component {
/>
)

formatJson = () => {
if (this.props.disabled || !this.props.onChange) return
try {
const formatted = JSON.stringify(JSON.parse(this.props.value), null, 2)
// Don't emit a no-op change; the parent flags dirty on any onChange.
if (formatted !== this.props.value) {
this.props.onChange(formatted)
}
} catch (e) {
toast('Cannot format invalid JSON', 'danger')
}
}

render() {
const { ...rest } = this.props
const showFormat =
this.state.language === 'json' &&
!this.props.disabled &&
!!this.props.value
return (
<div
className={cx(
Expand Down Expand Up @@ -207,6 +225,19 @@ class ValueEditor extends Component {
>
.yaml {this.state.language === 'yaml' && this.renderValidation()}
</span>
<BareButton
disabled={!showFormat}
// Don't blur the editor (which would fire its onBlur commit);
// onClick still runs the format.
onMouseDown={(e) => e.preventDefault()}
onClick={this.formatJson}
className={cx('primary format-action', {
'is-visible': showFormat,
})}
>
<Icon name='code' />
format
</BareButton>
<span
onMouseDown={() => {
const res = Clipboard.setString(this.props.value)
Expand Down
21 changes: 20 additions & 1 deletion frontend/web/styles/3rdParty/_hljs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
color: var(--color-text-default);
}
}
span {
span,
.bare-btn {
display: flex;
align-items: center;
gap: 2px;
Expand All @@ -81,6 +82,24 @@
color: var(--color-text-action);
}
}
// Slides and fades in when JSON is selected, collapses out otherwise.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add the required blank line before the new comment.

Stylelint reports scss/double-slash-comment-empty-line-before because the comment on Line 85 follows the closing brace on Line 84 without an empty line. Insert one blank line so the changed stylesheet passes the configured lint rule.

🧰 Tools
🪛 Stylelint (17.14.0)

[error] 85-85: Expected empty line before comment (scss/double-slash-comment-empty-line-before)

(scss/double-slash-comment-empty-line-before)

Source: Linters/SAST tools

.format-action {
max-width: 0;
overflow: hidden;
opacity: 0;
white-space: nowrap;
pointer-events: none;
transition: max-width var(--duration-normal, 200ms) ease,
opacity var(--duration-normal, 200ms) ease;
@media (prefers-reduced-motion: reduce) {
transition: none;
}
&.is-visible {
max-width: 6rem;
opacity: 1;
pointer-events: auto;
}
}
}
}
.hljs {
Expand Down
Loading