import Format from "./format.js" ; import { Jsonified as JsonifiedMap } from "../map.js" ; import { Jsonified as JsonifiedItem } from "../item.js" ; export default class Plaintext extends Format { extension = "txt" ; mime = "application/vnd.mymind+txt" ; constructor ( ) { super ( "plaintext" , "Plain text" ) ; } to ( data ) { return serializeItem ( "root" in data ? data . root : data ) ; } from ( data ) { var lines = data . split ( "\n" ) . filter ( function ( line ) { return line . match ( /\S/ ) ; } ) ; var items = parseItems ( lines ) ; let result ; if ( items . length == 1 ) { result = { root : items [ 0 ] } ; } else { result = { root : { text : "" , children : items } } ; } result . root . layout = "map" ; return result ; } } function serializeItem ( item , depth = 0 ) { var lines = ( item . children || [ ] ) . map ( child => { return serializeItem ( child , depth + 1 ) ; } ) ; var prefix = new Array ( depth + 1 ) . join ( "\t" ) ; lines . unshift ( prefix + item . text . replace ( /\n/g , "" ) ) ; return lines . join ( "\n" ) + ( depth ? "" : "\n" ) ; } function parseItems ( lines ) { let items = [ ] ; if ( ! lines . length ) { return items ; } var firstPrefix = parsePrefix ( lines [ 0 ] ) ; let currentItem = null ; let childLines = [ ] ; var convertChildLinesToChildren = function ( ) { if ( ! currentItem || ! childLines . length ) { return ; } var children = parseItems ( childLines ) ; if ( children . length ) { currentItem . children = children ; } childLines = [ ] ; } ; lines . forEach ( line => { if ( parsePrefix ( line ) == firstPrefix ) { convertChildLinesToChildren ( ) ; currentItem = { text : line . match ( /^\s*(.*)/ ) ! [ 1 ] } ; items . push ( currentItem ) ; } else { childLines . push ( line ) ; } } ) ; convertChildLinesToChildren ( ) ; return items ; } function parsePrefix ( line ) { return line . match ( /^\s*/ ) ! [ 0 ] ; }