Newer
Older
import React from 'react'
import ReactDOM from 'react-dom'
import { Alert } from 'react-bootstrap'
import {
ProseEditorConfigurator as Configurator,
DocumentSession
} from 'substance'
import config from './config'
import Editor from './Editor'
import Importer from './SimpleEditorImporter'
import SimpleExporter from './SimpleEditorExporter'
import './SimpleEditor.scss'
export default class SimpleEditor extends React.Component {
constructor (props) {
super(props)
this.save = this.save.bind(this)
this.updateTrackChangesStatus = this.updateTrackChangesStatus.bind(this)
this._checkRights = this._checkRights.bind(this)
this.state = {
canEdit: false
}
}
componentWillMount () {
const { fragment } = this.props
if (fragment) {
this._checkRights()
// this._acquireLock()
}
}
createSession () {
const { fragment } = this.props
let source
if (fragment) { source = fragment.source }
const configurator = new Configurator().import(config)
configurator.addImporter('html', Importer)
const importer = configurator.createImporter('html')
const doc = importer.importDocument(source)
const documentSession = new DocumentSession(doc)
documentSession.setSaveHandler({
saveDocument: this.save
})
configurator: configurator,
documentSession: documentSession
const { fragment, update } = this.props
fragment.trackChanges = !fragment.trackChanges
update(fragment)
}
save (source, changes, callback) {
const { onSave } = this.props
const config = this.state.config
const exporter = new SimpleExporter(config)
const convertedSource = exporter.exportDocument(source)
onSave(convertedSource, callback)
_checkRights () {
const { fragment, user } = this.props
if (user.admin) return this.setState({ canEdit: true })
if ((fragment.progress['review'] === 1 && user.teams[0].teamType.name === 'Author') ||
(fragment.progress['edit'] === 1 && user.teams[0].teamType.name === 'Copy Editor')) {
this.setState({ canEdit: true })
} else {
this.setState({ canEdit: false })
}
if (user.teams[0].teamType.name === 'Production Editor') {
this.setState({ canEdit: true })
}
}
componentWillReceiveProps (nextProps) {
const { fragment } = this.props
if (fragment) return
var self = this
if (nextProps.fragment) {
setTimeout(function () {
self._checkRights()
self.componentDidMount()
}, 0)
}
}
const { onSave } = this.props
const el = ReactDOM.findDOMNode(this)
const session = this.createSession()
const documentSession = session.documentSession
const configurator = session.configurator
this.setState({
config: configurator.config
})
book: this.props.book,
configurator: configurator,
containerId: 'body',
disabled: !this.state.canEdit, // set to true read only mode
fileUpload: this.props.fileUpload,
trackChanges: this.props.fragment.trackChanges,
updateComments: this.props.updateComments,
updateTrackChangesStatus: this.updateTrackChangesStatus,
user: this.props.user
}, el)
}
// New props arrived, update the editor
// componentDidUpdate () {
// console.log('did update')
//
// var session = this.createSession()
// var documentSession = session.documentSession
// var configurator = session.configurator
//
// this.writer.extendProps({
// documentSession: documentSession,
// configurator: configurator
// })
//
// console.log(this.writer)
// }
//
// componentWillUnmount () {
// this.writer.dispose()
// }
render () {
let viewMode = !this.state.canEdit
<Alert bsStyle='warning' className='view-mode'>
<span>Editor is in Read-Only Mode</span>
</Alert>
)
: null
return (
<div className='editor-wrapper'>
</div>
)
}
}
SimpleEditor.propTypes = {
book: React.PropTypes.object.isRequired,
canEdit: React.PropTypes.bool,
onSave: React.PropTypes.func.isRequired,
update: React.PropTypes.func.isRequired,
updateComments: React.PropTypes.func.isRequired,
fileUpload: React.PropTypes.func.isRequired,
user: React.PropTypes.object.isRequired
}