Skip to content

Commit ff3866f

Browse files
authored
Merge pull request #116 from devtron-labs/yaml-stringify-code-editor
feat: Generalize yaml stringify
2 parents 2f4fef0 + cdf2a8e commit ff3866f

File tree

4 files changed

+60
-32
lines changed

4 files changed

+60
-32
lines changed

package-lock.json

Lines changed: 46 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.0.72",
3+
"version": "0.0.73",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",
@@ -76,7 +76,8 @@
7676
"react-dom": "^17.0.2",
7777
"react-router": "^5.3.0",
7878
"react-router-dom": "^5.3.0",
79-
"react-draggable": "^4.4.5"
79+
"react-draggable": "^4.4.5",
80+
"yaml": "^2.4.1"
8081
},
8182
"dependencies": {
8283
"react-monaco-editor": "^0.54.0",

src/Common/CodeEditor/CodeEditor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import 'monaco-editor'
2121
// import YamlWorker from 'web-worker:monaco-yaml/lib/esm/yaml.worker';
2222
import { Progressing } from '../Progressing'
2323
import { useWindowSize } from '../Hooks/UseWindowSize/UseWindowSize'
24-
import { cleanKubeManifest, copyToClipboard, useJsonYaml } from '../Helper'
24+
import { YAMLStringify, cleanKubeManifest, copyToClipboard, useJsonYaml } from '../Helper'
2525
import Select from '../Select/Select'
2626
import RadioGroup from '../RadioGroup/RadioGroup'
2727

@@ -300,7 +300,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
300300
}
301301
let final = value
302302
if (obj) {
303-
final = state.mode === 'json' ? JSON.stringify(obj, null, tabSize) : YAML.stringify(obj, { indent: 2 })
303+
final = state.mode === 'json' ? JSON.stringify(obj, null, tabSize) : YAMLStringify(obj, { indent: 2 })
304304
}
305305
dispatch({ type: 'setCode', value: final })
306306
}, [value, noParsing])

src/Common/Helper.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export function useJsonYaml(value, tabSize = 4, language = 'json', shouldRun = f
369369
}
370370
if (obj && typeof obj === 'object') {
371371
setJson(JSON.stringify(obj, null, tabSize))
372-
setYaml(YAML.stringify(obj, { indent: 2 }))
372+
setYaml(YAML.stringify(obj, { indent: 2, lineWidth: 0 }))
373373
setNativeObject(obj)
374374
setError('')
375375
} else {
@@ -711,3 +711,11 @@ export const handleRelativeDateSorting = (dateStringA, dateStringB, sortOrder) =
711711
return sortOrder === SortingOrder.ASC ? dateB - dateA : dateA - dateB
712712
}
713713
}
714+
715+
/**
716+
* Returns a stringified YAML with default indentation & line width
717+
*/
718+
719+
export const YAMLStringify = (obj: object | unknown, option?: object) => (
720+
YAML.stringify(obj, { indent: 2, lineWidth: 0, ...option })
721+
)

0 commit comments

Comments
 (0)