1
1
import { DataModel } from '@mcschema/core'
2
2
import { useCallback , useEffect , useRef , useState } from 'preact/hooks'
3
3
import { useLocale } from '../../contexts/index.js'
4
- import { useModel } from '../../hooks/index.js'
4
+ import { useLocalStorage , useModel } from '../../hooks/index.js'
5
5
import { getOutput } from '../../schema/transformOutput.js'
6
6
import type { BlockStateRegistry } from '../../services/index.js'
7
- import { getSourceFormats , getSourceIndent , getSourceIndents , parseSource , stringifySource } from '../../services/index.js'
7
+ import { getSourceFormats , getSourceIndent , getSourceIndents , parseSource , sortData , stringifySource } from '../../services/index.js'
8
8
import { Store } from '../../Store.js'
9
9
import { message } from '../../Utils.js'
10
10
import { Btn , BtnMenu } from '../index.js'
@@ -30,6 +30,7 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm
30
30
const { locale } = useLocale ( )
31
31
const [ indent , setIndent ] = useState ( Store . getIndent ( ) )
32
32
const [ format , setFormat ] = useState ( Store . getFormat ( ) )
33
+ const [ sort , setSort ] = useLocalStorage ( 'misode_output_sort' , 'schema' )
33
34
const [ highlighting , setHighlighting ] = useState ( Store . getHighlighting ( ) )
34
35
const [ braceLoaded , setBraceLoaded ] = useState ( false )
35
36
const download = useRef < HTMLAnchorElement > ( null )
@@ -40,9 +41,12 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm
40
41
const editor = useRef < Editor > ( )
41
42
42
43
const getSerializedOutput = useCallback ( ( model : DataModel , blockStates : BlockStateRegistry ) => {
43
- const data = getOutput ( model , blockStates )
44
+ let data = getOutput ( model , blockStates )
45
+ if ( sort === 'alphabetically' ) {
46
+ data = sortData ( data )
47
+ }
44
48
return stringifySource ( data , format , indent )
45
- } , [ indent , format ] )
49
+ } , [ indent , format , sort ] )
46
50
47
51
useEffect ( ( ) => {
48
52
retransform . current = ( ) => {
@@ -80,7 +84,7 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm
80
84
console . error ( e )
81
85
}
82
86
}
83
- } , [ model , blockStates , indent , format , highlighting ] )
87
+ } , [ model , blockStates , indent , format , sort , highlighting ] )
84
88
85
89
useEffect ( ( ) => {
86
90
if ( highlighting ) {
@@ -156,7 +160,7 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm
156
160
editor . current . configure ( indent , format === 'snbt' ? 'yaml' : format )
157
161
retransform . current ( )
158
162
}
159
- } , [ indent , format , highlighting , braceLoaded ] )
163
+ } , [ indent , format , sort , highlighting , braceLoaded ] )
160
164
161
165
useEffect ( ( ) => {
162
166
if ( doCopy && model && blockStates ) {
@@ -210,6 +214,8 @@ export function SourcePanel({ name, model, blockStates, doCopy, doDownload, doIm
210
214
< Btn label = { locale ( `format.${ key } ` ) } active = { format === key }
211
215
onClick = { ( ) => changeFormat ( key ) } /> ) }
212
216
< hr />
217
+ < Btn icon = { sort === 'alphabetically' ? 'square_fill' : 'square' } label = { locale ( 'sort_alphabetically' ) }
218
+ onClick = { ( ) => setSort ( sort === 'alphabetically' ? 'schema' : 'alphabetically' ) } />
213
219
< Btn icon = { highlighting ? 'square_fill' : 'square' } label = { locale ( 'highlighting' ) }
214
220
onClick = { ( ) => changeHighlighting ( ! highlighting ) } />
215
221
</ BtnMenu >
0 commit comments