11import type { DocAndNode } from '@spyglassmc/core'
22import { fileUtil } from '@spyglassmc/core'
3- import { useCallback , useEffect , useRef , useState } from 'preact/hooks'
3+ import { useCallback , useEffect , useMemo , useRef , useState } from 'preact/hooks'
44import { useLocale } from '../../contexts/index.js'
55import { useDocAndNode , useSpyglass } from '../../contexts/Spyglass.jsx'
66import { useLocalStorage } from '../../hooks/index.js'
@@ -12,7 +12,7 @@ import { Btn, BtnMenu } from '../index.js'
1212interface Editor {
1313 getValue ( ) : string
1414 setValue ( value : string ) : void
15- configure ( indent : string , format : string ) : void
15+ configure ( indent : string , format : string , wrap : boolean ) : void
1616 select ( ) : void
1717}
1818
@@ -27,9 +27,10 @@ type SourcePanelProps = {
2727export function SourcePanel ( { docAndNode, doCopy, doDownload, doImport, copySuccess, onError } : SourcePanelProps ) {
2828 const { locale } = useLocale ( )
2929 const { service } = useSpyglass ( )
30- const [ indent , setIndent ] = useState ( Store . getIndent ( ) )
31- const [ format , setFormat ] = useState ( Store . getFormat ( ) )
32- const [ sort , setSort ] = useLocalStorage ( 'misode_output_sort' , 'schema' )
30+ const [ cIndent , setIndent ] = useState ( Store . getIndent ( ) )
31+ const [ cFormat , setFormat ] = useState ( Store . getFormat ( ) )
32+ const [ inline , setInline ] = useLocalStorage ( 'misode_output_inline' , false , ( s ) => s === 'true' , ( b ) => b ? 'true' : 'false' )
33+ // const [sort, setSort] = useLocalStorage('misode_output_sort', 'schema')
3334 const [ highlighting , setHighlighting ] = useState ( Store . getHighlighting ( ) )
3435 const [ braceLoaded , setBraceLoaded ] = useState ( false )
3536 const download = useRef < HTMLAnchorElement > ( null )
@@ -40,13 +41,14 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
4041 const textarea = useRef < HTMLTextAreaElement > ( null )
4142 const editor = useRef < Editor > ( )
4243
44+ const { indent, format } = useMemo ( ( ) => {
45+ return inline ? { indent : 'minified' , format : 'snbt' } : { indent : cIndent , format : cFormat }
46+ } , [ cIndent , cFormat , inline ] )
47+
4348 const getSerializedOutput = useCallback ( ( text : string ) => {
4449 // TODO: implement sort
45- // if (sort === 'alphabetically') {
46- // data = sortData(data)
47- // }
4850 return stringifySource ( text , format , indent )
49- } , [ indent , format , sort ] )
51+ } , [ indent , format ] )
5052
5153 const text = useDocAndNode ( docAndNode ) ?. doc . getText ( )
5254
@@ -89,7 +91,7 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
8991 console . error ( e )
9092 }
9193 }
92- } , [ service , docAndNode , text , indent , format , sort , highlighting ] )
94+ } , [ service , docAndNode , text , indent , format , highlighting ] )
9395
9496 useEffect ( ( ) => {
9597 if ( highlighting ) {
@@ -111,6 +113,7 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
111113 showFoldWidgets : false ,
112114 highlightSelectedWord : false ,
113115 scrollPastEnd : 0.5 ,
116+ indentedSoftWrap : false ,
114117 } )
115118 braceEditor . $blockScrolling = Infinity
116119 braceEditor . on ( 'blur' , ( ) => onImport . current ( ) )
@@ -123,7 +126,8 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
123126 setValue ( value ) {
124127 braceEditor . getSession ( ) . setValue ( value )
125128 } ,
126- configure ( indent , format ) {
129+ configure ( indent , format , wrap ) {
130+ braceEditor . setOption ( 'wrap' , wrap )
127131 braceEditor . setOption ( 'useSoftTabs' , indent !== 'tabs' )
128132 braceEditor . setOption ( 'tabSize' , indent === 'tabs' ? 4 : getSourceIndent ( indent ) )
129133 braceEditor . getSession ( ) . setMode ( `ace/mode/${ format } ` )
@@ -144,7 +148,11 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
144148 if ( ! textarea . current ) return
145149 textarea . current . value = value
146150 } ,
147- configure ( ) { } ,
151+ configure ( _indent , _format , wrap ) {
152+ if ( ! textarea . current ) return
153+ textarea . current . style . wordBreak = wrap ? 'break-all' : 'unset'
154+ textarea . current . style . whiteSpace = wrap ? 'wrap' : 'pre'
155+ } ,
148156 select ( ) { } ,
149157 }
150158 }
@@ -159,10 +167,10 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
159167 useEffect ( ( ) => {
160168 if ( ! editor . current || ! retransform . current ) return
161169 if ( ! highlighting || braceLoaded ) {
162- editor . current . configure ( indent , format === 'snbt' ? 'yaml' : format )
170+ editor . current . configure ( indent , format === 'snbt' ? 'yaml' : format , inline )
163171 retransform . current ( )
164172 }
165- } , [ indent , format , sort , highlighting , braceLoaded ] )
173+ } , [ indent , format , inline , highlighting , braceLoaded ] )
166174
167175 useEffect ( ( ) => {
168176 if ( doCopy && outputRef . current ) {
@@ -217,18 +225,19 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
217225 { window . matchMedia ( '(pointer: coarse)' ) . matches && < >
218226 < Btn icon = "paste" onClick = { importFromClipboard } />
219227 </ > }
228+ < Btn label = { locale ( 'inline' ) } active = { inline } onClick = { ( ) => setInline ( ! inline ) } />
220229 < BtnMenu icon = "gear" tooltip = { locale ( 'output_settings' ) } >
221230 { getSourceIndents ( ) . map ( key =>
222- < Btn label = { locale ( `indentation.${ key } ` ) } active = { indent === key }
231+ < Btn label = { locale ( `indentation.${ key } ` ) } active = { cIndent === key }
223232 onClick = { ( ) => changeIndent ( key ) } />
224233 ) }
225234 < hr />
226235 { getSourceFormats ( ) . map ( key =>
227- < Btn label = { locale ( `format.${ key } ` ) } active = { format === key }
236+ < Btn label = { locale ( `format.${ key } ` ) } active = { cFormat === key }
228237 onClick = { ( ) => changeFormat ( key ) } /> ) }
229238 < hr />
230- < Btn icon = { sort === 'alphabetically' ? 'square_fill' : 'square' } label = { locale ( 'sort_alphabetically' ) }
231- onClick = { ( ) => setSort ( sort === 'alphabetically' ? 'schema' : 'alphabetically' ) } />
239+ { /* <Btn icon={sort === 'alphabetically' ? 'square_fill' : 'square'} label={locale('sort_alphabetically')}
240+ onClick={() => setSort(sort === 'alphabetically' ? 'schema' : 'alphabetically')} /> */ }
232241 < Btn icon = { highlighting ? 'square_fill' : 'square' } label = { locale ( 'highlighting' ) }
233242 onClick = { ( ) => changeHighlighting ( ! highlighting ) } />
234243 </ BtnMenu >
0 commit comments