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