Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
NotesProvider.js 2.26 KiB
import _ from 'lodash'

import { TOCProvider } from 'substance'

class NotesProvider extends TOCProvider {
  computeEntries () {
    const docMain = this.getDocument()
    const nodesMain = docMain.getNodes()

    const docMini = this.config.miniEditorSession.document
    let nodesMini = ''
    if (docMini) nodesMini = docMini.getNodes()
    // if(docMini)console.log('paragraphs', nodesMini)
    // get all notes from the document
    const notesMain = _.pickBy(nodesMain, function (value, key) {
      return value.type === 'note'
    })
    const notesMini = _.pickBy(nodesMini, function (value, key) {
      return value.type === 'paragraph'
    })

    const entries = this.sortNodes(notesMain)
    const entriesMini = this.sortNodesMini(notesMini)
    // console.log(entries, entriesMini)
    return entries
  }

  sortNodes (nodes) {
    let notes = _.clone(nodes)
    const doc = this.getDocument()
    const container = doc.get('body')

    // sort notes by
    //   the index of the containing block
    //   their position within that block

    notes = _.map(notes, function (note) {
      const blockId = note.path[0]
      const blockPosition = container.getPosition(blockId)
      const nodePosition = note.start.offset

      return {
        id: note.id,
        content: note['note-content'],
        blockPosition: blockPosition,
        nodePosition: nodePosition,
        node: note
      }
    })

    return _.sortBy(notes, ['blockPosition', 'nodePosition'])
  }

  sortNodesMini (nodes) {
    let notes = _.clone(nodes)
    if (this.config.miniEditorSession === '') return

    const doc = this.config.miniEditorSession.document
    const container = doc.get('body')

    // console.log(notes)
    notes = _.map(notes, function (note) {
      // const blockId = note.path[0]
      // const blockPosition = container.getPosition(blockId)
      // const nodePosition = note.start.offset

      return {
        id: note.id,
        content: note.content
        // blockPosition: blockPosition,
        // nodePosition: nodePosition,
        // node: note
      }
    })
    return notes
    // return _.sortBy(notes, ['blockPosition', 'nodePosition'])
  }

  createShowNote (note) {
    console.log('in provider', note)
  }
}

NotesProvider.tocTypes = ['note']

export default NotesProvider