Skip to content
Snippets Groups Projects
Notes.js 3.9 KiB
Newer Older
/* eslint react/prop-types: 0 */

import { Component, EditorSession,
  ProseEditorConfigurator as Configurator } from 'substance'
import NotesEditor from '../../notesEditor/NotesEditor'
import config from '../../notesEditor/config'
import Importer from '../../notesEditor/NotesEditorImporter'
chris's avatar
chris committed
import {isEmpty, find} from 'lodash'
john's avatar
john committed
class Notes extends Component {
  constructor (props) {
    super(props)

    this.resize = this.resize.bind(this)
    this.stopResize = this.stopResize.bind(this)
chris's avatar
chris committed

john's avatar
john committed
  didMount () {
    this.context.editorSession.onUpdate('document', this.onNotesUpdated, this)
chris's avatar
chris committed
    this.context.editor.emit('ui:updated')
chris's avatar
chris committed
  didUpdate () {
    this.el.el.style.height = this.notesPaneHeight + 'px'
chris's avatar
chris committed
    this.computeMainPane()
  render ($$) {
    const { book, comments, containerId,
            history, disabled, fragment,
            trackChanges, trackChangesView,
            update, user } = this.props

    const { editorSession, configurator } = this._initNotesEditor()

    const resizer = $$('div').addClass('resize-area')
    const el = $$('div')
         .addClass('notes-container').append(resizer)
    if (!fragment) return el
chris's avatar
chris committed

    el.append($$(NotesEditor, {
      book,
      editorSession,
      comments,
      configurator,
      containerId,
      history,
      disabled,
      fragment,
      trackChanges,
      trackChangesView,
      update,
      user
    resizer.addEventListener('mousedown', this.initResize, false)
chris's avatar
chris committed
  _initNotesEditor () {
    const configurator = new Configurator().import(config)
    configurator.addImporter('html', Importer)
    const importer = configurator.createImporter('html')
chris's avatar
chris committed
    const provider = this.getProvider()

    const notes = provider.computeEntries()
chris's avatar
chris committed
    let noteContent = ''

    for (var i = 0; i < notes.length; i++) {
      let isolatedNoteElement = exporter.createElement('isolated-note')
      isolatedNoteElement.setAttribute('data-parent-id', notes[i].id)
      isolatedNoteElement.innerHTML = notes[i]['note-content']
      noteContent += isolatedNoteElement.outerHTML
chris's avatar
chris committed
    }

    const doc = importer.importDocument(noteContent)
chris's avatar
chris committed

    const editorSession = new EditorSession(doc, {
      configurator: configurator
    })
chris's avatar
chris committed
    return {
      editorSession: editorSession,
      configurator: configurator
    }
chris's avatar
chris committed
  initResize (e) {
    window.addEventListener('mousemove', this.resize, false)
    window.addEventListener('mouseup', this.stopResize, false)
chris's avatar
chris committed

chris's avatar
chris committed
  resize (e) {
    this.notesPaneHeight = (this.el.el.offsetHeight + this.el.el.offsetTop - e.clientY)
chris's avatar
chris committed
    const scrollPane = document.getElementById('notes-editor-content-panel').children
    scrollPane[0].style.minHeight = this.notesPaneHeight - 40 + 'px'
    this.el.el.style.height = this.notesPaneHeight + 'px'
chris's avatar
chris committed

    this.computeMainPane()
chris's avatar
chris committed
  }

chris's avatar
chris committed
  stopResize (e) {
    window.removeEventListener('mousemove', this.resize, false)
    window.removeEventListener('mouseup', this.stopResize, false)
chris's avatar
chris committed
  computeMainPane () {
    const mainPane = document.getElementById('content-panel')
chris's avatar
chris committed
    mainPane.style.height = this.el.el.offsetTop - 162 + 'px'
  onNotesUpdated (change) {
    const notesProvider = this.getProvider()
    notesProvider.handleDocumentChange(change)
chris's avatar
chris committed
    const noteCreated = find(change.created, function (value, key) {
      return value.type === 'note'
    })

    const noteDeleted = find(change.deleted, function (value, key) {
      return value.type === 'note'
    if (!isEmpty(noteCreated) || !isEmpty(noteDeleted)) {
      this.rerender()
      if (!isEmpty(noteCreated)) notesProvider.calloutSelected(noteCreated.id)
chris's avatar
chris committed
      return false
    }
chris's avatar
chris committed
  getProvider () {
    return this.context.notesProvider
  }

john's avatar
john committed
  dispose () {
    const provider = this.getProvider()
john's avatar
john committed
export default Notes