@ -36,12 +36,11 @@ import {
import { getQueryStringState , updateQueryString , serializeState } from '../lib/routing'
import { getState , saveState } from '../lib/util'
const removeQueryString = str => {
const qI = str . indexOf ( '?' )
return ( qI >= 0 ? str . substr ( 0 , qI ) : str )
. replace ( /</g , '<' )
. replace ( />/g , '>' )
. replace ( /\//g , '/' )
const saveButtonOptions = {
button : true ,
color : '#c198fb' ,
selected : { id : 'SAVE_IMAGE' , name : 'Save Image' } ,
list : [ 'png' , 'svg' ] . map ( id => ( { id , name : id . toUpperCase ( ) } ) )
}
class Editor extends React . Component {
@ -75,11 +74,16 @@ class Editor extends React.Component {
this . save = this . save . bind ( this )
this . upload = this . upload . bind ( this )
this . updateCode = this . updateCode . bind ( this )
this . updateTitleBar = this . updateTitleBar . bind ( this )
this . updateAspectRatio = this . updateAspectRatio . bind ( this )
this . updateSetting = this . updateSetting . bind ( this )
this . updateCode = this . updateSetting . bind ( this , 'code' )
this . updateAspectRatio = this . updateSetting . bind ( this , 'aspectRatio' )
this . updateTitleBar = this . updateSetting . bind ( this , 'titleBar' )
this . updateTheme = this . updateTheme . bind ( this )
this . updateLanguage = this . updateLanguage . bind ( this )
this . updateBackground = this . updateBackground . bind ( this )
this . resetDefaultSettings = this . resetDefaultSettings . bind ( this )
this . getCarbonImage = this . getCarbonImage . bind ( this )
this . onDrop = this . onDrop . bind ( this )
}
componentDidMount ( ) {
@ -133,19 +137,11 @@ class Editor extends React.Component {
: domtoimage . toPng ( node , config )
}
updateCode ( code ) {
this . setState ( { code } )
}
updateAspectRatio ( aspectRatio ) {
this . setState ( { aspectRatio } )
}
updateTitleBar ( titleBar ) {
this . setState ( { titleBar } )
updateSetting ( key , value ) {
this . setState ( { [ key ] : value } )
}
save ( { format } = { format : 'png' } ) {
save ( { id : format = 'png' } ) {
this . getCarbonImage ( { format } ) . then ( dataUrl => {
if ( format === 'svg' ) {
dataUrl = dataUrl . split ( ' ' ) . join ( ' ' )
@ -176,19 +172,39 @@ class Editor extends React.Component {
} )
}
isImage ( file ) {
return file . type . split ( '/' ) [ 0 ] === 'image'
onDrop ( [ file ] ) {
if ( isImage ( file ) ) {
this . setState ( {
backgroundImage : file . content ,
backgroundImageSelection : null ,
backgroundMode : 'image'
} )
} else {
this . setState ( { code : file . content , language : 'auto' } )
}
}
updateTheme ( theme ) {
this . updateSetting ( 'theme' , theme . id )
}
updateLanguage ( language ) {
this . updateSetting ( 'language' , language . mime || language . mode )
}
updateBackground ( changes , cb ) {
this . setState ( changes , cb )
}
render ( ) {
return (
< Page enableHeroText >
< Page enableHeroText ={ true } >
< div id = "editor" >
< Toolbar >
< Dropdown
selected = { THEMES _HASH [ this . state . theme ] || DEFAULT _THEME }
list = { THEMES }
onChange = { theme => this . setState ( { theme : theme . id } ) }
onChange = { this . updateTheme }
/ >
< Dropdown
selected = {
@ -197,12 +213,18 @@ class Editor extends React.Component {
LANGUAGE _MODE _HASH [ this . state . language ]
}
list = { LANGUAGES }
onChange = { language => this . setState ( { language : language . mime || language . mode } ) }
onChange = { this . updateLanguage }
/ >
< BackgroundSelect
onChange = { this . updateBackground }
mode = { this . state . backgroundMode }
color = { this . state . backgroundColor }
image = { this . state . backgroundImage }
aspectRatio = { this . state . aspectRatio }
/ >
< BackgroundSelect onChange = { changes => this . setState ( changes ) } config = { this . state } / >
< Settings
onChange = { ( key , value ) => this . setState ( { [ key ] : value } ) }
enabled = { this . state }
{ ... this . state }
onChange= { this . updateSetting }
resetDefaultSettings = { this . resetDefaultSettings }
/ >
< div className = "buttons" >
@ -213,35 +235,11 @@ class Editor extends React.Component {
color = "#57b5f9"
style = { { marginRight : '8px' } }
/ >
< Dropdown
button
color = "#c198fb"
selected = { { id : 'SAVE_IMAGE' , name : 'Save Image' } }
list = { [ 'png' , 'svg' ] . map ( id => ( { id , name : id . toUpperCase ( ) } ) ) }
onChange = { saveAs => this . save ( { format : saveAs . id } ) }
/ >
< Dropdown { ... saveButtonOptions } onChange = { this . save } / >
< / d i v >
< / T o o l b a r >
< ReadFileDropContainer
readAs = { file => {
if ( this . isImage ( file ) ) {
return DATA _URL
}
return TEXT
} }
onDrop = { ( [ file ] ) => {
if ( this . isImage ( file ) ) {
this . setState ( {
backgroundImage : file . content ,
backgroundImageSelection : null ,
backgroundMode : 'image'
} )
} else {
this . setState ( { code : file . content , language : 'auto' } )
}
} }
>
< ReadFileDropContainer readAs = { readAs } onDrop = { this . onDrop } >
{ ( { isOver , canDrop } ) => (
< Overlay
isOver = { isOver || canDrop }
@ -249,8 +247,9 @@ class Editor extends React.Component {
>
< Carbon
config = { this . state }
updateCode = { code => this . updateCode ( code ) }
updateCode = { this . updateCode }
onAspectRatioChange = { this . updateAspectRatio }
titleBar = { this . state . titleBar }
updateTitleBar = { this . updateTitleBar }
>
{ this . state . code != null ? this . state . code : DEFAULT _CODE }
@ -277,4 +276,23 @@ class Editor extends React.Component {
}
}
function removeQueryString ( str ) {
const qI = str . indexOf ( '?' )
return ( qI >= 0 ? str . substr ( 0 , qI ) : str )
. replace ( /</g , '<' )
. replace ( />/g , '>' )
. replace ( /\//g , '/' )
}
function isImage ( file ) {
return file . type . split ( '/' ) [ 0 ] === 'image'
}
function readAs ( file ) {
if ( isImage ( file ) ) {
return DATA _URL
}
return TEXT
}
export default DragDropContext ( HTML5Backend ) ( Editor )