Skip to content
Snippets Groups Projects
NotesEditor.js 4.31 KiB
Newer Older
import {
  ProseEditor,
  Toolbar
} from 'substance'

import ContainerEditor from '../ContainerEditor'
import Comments from '../panes/Comments/CommentBoxList'
import CommentsProvider from '../panes/Comments/CommentsProvider'
import TrackChangesProvider from '../elements/track_change/TrackChangesProvider'
chris's avatar
chris committed
import SimpleExporter from '../SimpleEditorExporter'
import { find, pickBy, isEmpty, values } from 'lodash'
chris's avatar
chris committed
class NotesEditor extends ProseEditor {
    const provider = this.getProvider()
chris's avatar
chris committed
    this.editorSession.onUpdate('document', this.findNote, this)
chris's avatar
chris committed
    provider.on('noteSelected', this.scrollTo, this)
chris's avatar
chris committed
    const el = $$('div').addClass('sc-notes-editor')
    let editor = this._renderEditor($$)
    let SplitPane = this.componentRegistry.get('split-pane')
    let ScrollPane = this.componentRegistry.get('scroll-pane')
    let Overlay = this.componentRegistry.get('overlay')
    var commentsPane = $$(Comments, {
chris's avatar
chris committed
      comments: this.props.comments,
      fragment: this.props.fragment,
      update: this.props.update,
      user: this.props.user
    }).addClass('sc-comments-pane')
    const editorWithComments = $$(SplitPane, { sizeA: '80%', splitType: 'vertical' })
      .append(
        editor,
        commentsPane
      )

    const contentPanel = $$(ScrollPane, {
chris's avatar
chris committed
      name: 'notesEditorContentPanel',
      scrollbarPosition: 'right'
    })
      .append(editorWithComments, $$(Overlay))
chris's avatar
chris committed
      .attr('id', 'notes-editor-content-panel')
      .ref('notesEditorContentPanel')
chris's avatar
chris committed
    el.append(contentPanel)
    const hasIsolatedNotes = this.getIsolatedNodes()
    const disabled = (isEmpty(hasIsolatedNotes))
    return $$(ContainerEditor, {
      book: this.props.book,
      comments: this.props.comments,
      containerId: this.props.containerId,
chris's avatar
chris committed
      configurator: this.props.configurator,
      editorSession: this.editorSession,
      disabled: disabled,
      history: this.props.history,
      fragment: this.props.fragment,
chris's avatar
chris committed
      spellcheck: 'native',
      trackChanges: this.props.trackChanges,
      trackChangesView: this.props.trackChangesView,
      user: this.props.user
chris's avatar
chris committed
    }).ref('notes_body')
chris's avatar
chris committed
    const nodes = this.getIsolatedNodes()
chris's avatar
chris committed
      return c.calloutId === nodeId
chris's avatar
chris committed
    if (note) this.refs.notesEditorContentPanel.scrollTo(note.id)
chris's avatar
chris committed
  saveNote (isolatedNote) {
chris's avatar
chris committed
    const exporter = new SimpleExporter(this.props.configurator.config)
    const convertedNode = exporter.convertNode(isolatedNote)
    this.context.editorSession.transaction(function (tx, args) {
chris's avatar
chris committed
      const path = [isolatedNote.calloutId, 'note-content']
chris's avatar
chris committed
      tx.set(path, convertedNode.innerHTML.trim())
chris's avatar
chris committed
    })
  }

  findNote () {
    const selection = this.editorSession.getSelection()
    if (!selection.end) return
    const isolatedNoteId = selection.end.path[0]
    const isolatedNote = this.editorSession.document.get(isolatedNoteId)
    return this.saveNote(isolatedNote)
  }

  getIsolatedNodes () {
    const doc = this.editorSession.document
    const nodes = doc.getNodes()
    const entries = pickBy(nodes, function (value, key) {
chris's avatar
chris committed
      return value.type === 'isolated-note'
    })
chris's avatar
chris committed
  }

  getProvider () {
    return this.context.notesProvider
  }

chris's avatar
chris committed
  getInitialState () {
    return {
      trackChangesView: this.props.trackChangesView
    }
  getChildContext () {
    const oldContext = super.getChildContext()
    const doc = this.doc

    // comments provider
    const commentsProvider = new CommentsProvider(doc, {
      commandManager: this.commandManager,
      comments: this.props.fragment.comments,
      containerId: this.props.containerId,
      controller: this,
      editorSession: this.editorSession,
      fragment: this.props.fragment,
      surfaceManager: this.surfaceManager,
      update: this.props.update
    })

    const trackChangesProvider = new TrackChangesProvider(doc, {
      commandManager: this.commandManager,
      containerId: this.props.containerId,
      controller: this,
      editorSession: this.editorSession,
      surfaceManager: this.surfaceManager,
      user: this.props.user
    })

    // attach all to context
    return {
      ...oldContext,
      commentsProvider,
      trackChangesProvider
chris's avatar
chris committed
export default NotesEditor