Skip to content
Snippets Groups Projects
EditNoteTool.js 3.68 KiB
Newer Older
john's avatar
john committed
import { each, includes, keys } from 'lodash'
import {
  documentHelpers,
  EditorSession,
  ProseEditorConfigurator as Configurator,
  Tool
} from 'substance'
import MiniEditor from '../../miniEditor/miniEditor'
import config from '../../miniEditor/config'
import Importer from '../../SimpleEditorImporter'
import SimpleExporter from '../../SimpleEditorExporter'
john's avatar
john committed
class EditNoteTool extends Tool {
  constructor (props) {
    super(props)
    this.saveNote = this.saveNote.bind(this)
  }

john's avatar
john committed
  render ($$) {
    const miniEditorSession = this._initMiniEditor()
john's avatar
john committed
    const selected = this.getSelection()
    const provider = this.getProvider()

    let el = $$('div').addClass('sc-edit-note-tool')

    if (!selected.node) return el
    el.append($$(MiniEditor, {
      editorSession: miniEditorSession
    }))
    provider.config.miniEditorContext.editor.emit('noteSelected', 'paragraph-71bf75436a5f9b56700064c6ee2116ab')
  didMount () {
john's avatar
john committed
    this.context.editorSession.onUpdate('', this.disableTools, this)
    const selected = this.getSelection()
    if (!selected.node) return

    const configurator = new Configurator().import(config)
    configurator.addImporter('html', Importer)

    const importer = configurator.createImporter('html')
    const doc = importer.importDocument(selected.node['note-content'])

    const editorSession = new EditorSession(doc, {
      configurator: configurator
    })

    editorSession.setSaveHandler({
      saveDocument: this.saveNote
    })

john's avatar
john committed
  disableTools () {
    const selected = this.getSelection()
    if (!selected.node) return
    const commandStates = this.context.commandManager.commandStates
    each(keys(commandStates), (key) => {
      const allowed = ['comment', 'redo', 'save', 'switch-text-type', 'undo', 'note']
      if (!includes(allowed, key)) commandStates[key].disabled = true
    })
john's avatar
john committed
    const selected = this.getSelection()
    const config = this.context.editorSession.configurator.config
    const exporter = new SimpleExporter(config)
    const convertedSource = exporter.exportDocument(source)
    const editorSession = this.context.editorSession
    editorSession.transaction(function (tx, args) {
john's avatar
john committed
      const path = [selected.node.id, 'note-content']
      tx.set(path, convertedSource)
    })

    // Return dummy Promise needed in saveDocument
    return new Promise(function (resolve, reject) {
      resolve()
john's avatar
john committed
    })
  }

  getSelection () {
    // TODO -- write cleaner
    const surface = this.context.surfaceManager.getFocusedSurface()
    if (!surface) return {}

    const session = this.context.editorSession
john's avatar
john committed
    const sel = session.getSelection()
chris's avatar
chris committed

    const notes = documentHelpers.getPropertyAnnotationsForSelection(
john's avatar
john committed
      session.getDocument(),
      sel,
      { type: 'note' }
    )

    const note = notes[0]
    let show = false

    if (typeof note !== 'undefined') {
      if ((sel.start.offset === note.start.offset &&
        sel.end.offset === note.end.offset)) {
        show = true
      }
    }

john's avatar
john committed
    // disable when larger selection that just includes a note as well
    // const selectionLength = (sel.end.offset - sel.start.offset === 1)
    // if (sel.end.offset - sel.)
  getProvider () {
    return this.context.notesProvider
  }

  getSurface () {
    const surfaceManager = this.context.surfaceManager
    return surfaceManager.getFocusedSurface()
  }

  isEditorReadOnly () {
    const surface = this.getSurface()
    return surface.isReadOnlyMode()
  }
john's avatar
john committed
EditNoteTool.type = 'edit-note'
john's avatar
john committed
export default EditNoteTool