({
caller,
onPrettifyQuery = DEFAULT_PRETTIFY_QUERY,
}: UsePrettifyEditorsArgs = {})
| 240 | } |
| 241 | |
| 242 | export function usePrettifyEditors({ |
| 243 | caller, |
| 244 | onPrettifyQuery = DEFAULT_PRETTIFY_QUERY, |
| 245 | }: UsePrettifyEditorsArgs = {}) { |
| 246 | const { queryEditor, headerEditor, variableEditor } = useEditorContext({ |
| 247 | nonNull: true, |
| 248 | caller: caller || _usePrettifyEditors, |
| 249 | }); |
| 250 | return async () => { |
| 251 | if (variableEditor) { |
| 252 | const variableEditorContent = variableEditor.getValue(); |
| 253 | try { |
| 254 | const prettifiedVariableEditorContent = JSON.stringify( |
| 255 | JSON.parse(variableEditorContent), |
| 256 | null, |
| 257 | 2, |
| 258 | ); |
| 259 | if (prettifiedVariableEditorContent !== variableEditorContent) { |
| 260 | variableEditor.setValue(prettifiedVariableEditorContent); |
| 261 | } |
| 262 | } catch { |
| 263 | /* Parsing JSON failed, skip prettification */ |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (headerEditor) { |
| 268 | const headerEditorContent = headerEditor.getValue(); |
| 269 | |
| 270 | try { |
| 271 | const prettifiedHeaderEditorContent = JSON.stringify( |
| 272 | JSON.parse(headerEditorContent), |
| 273 | null, |
| 274 | 2, |
| 275 | ); |
| 276 | if (prettifiedHeaderEditorContent !== headerEditorContent) { |
| 277 | headerEditor.setValue(prettifiedHeaderEditorContent); |
| 278 | } |
| 279 | } catch { |
| 280 | /* Parsing JSON failed, skip prettification */ |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (queryEditor) { |
| 285 | const editorContent = queryEditor.getValue(); |
| 286 | try { |
| 287 | const prettifiedEditorContent = await onPrettifyQuery(editorContent); |
| 288 | if (prettifiedEditorContent !== editorContent) { |
| 289 | queryEditor.setValue(prettifiedEditorContent); |
| 290 | } |
| 291 | } catch { |
| 292 | /* Parsing query failed, skip prettification */ |
| 293 | } |
| 294 | } |
| 295 | }; |
| 296 | } |
| 297 | |
| 298 | export type UseAutoCompleteLeafsArgs = { |
| 299 | /** |
no test coverage detected