From b9643b05fe23d9158a933941f9ac64299db11dd5 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Sat, 15 Apr 2017 13:30:57 +0300
Subject: [PATCH] mini editor has its own importer/containerId and shares the
 same containerEditor with main plus fixes

---
 .../SimpleEditor/ContainerEditor.js           | 11 ++-
 .../isolatedNote/TextPropertyEditor.js        |  8 ++-
 .../miniEditor/MiniEditorImporter.js          | 72 +++++++++++++++++++
 .../SimpleEditor/miniEditor/miniEditor.js     |  6 +-
 .../SimpleEditor/panes/Notes/Notes.js         |  6 +-
 .../SimpleEditor/panes/Notes/NotesProvider.js | 11 ++-
 6 files changed, 101 insertions(+), 13 deletions(-)
 create mode 100644 app/components/SimpleEditor/miniEditor/MiniEditorImporter.js

diff --git a/app/components/SimpleEditor/ContainerEditor.js b/app/components/SimpleEditor/ContainerEditor.js
index 5861555..bdfd2a9 100644
--- a/app/components/SimpleEditor/ContainerEditor.js
+++ b/app/components/SimpleEditor/ContainerEditor.js
@@ -196,12 +196,13 @@ class ContainerEditor extends SubstanceContainerEditor {
       })
 
       container.show(node.id)
+      const containerId = this.getContainerId()
 
       newSel = tx.createSelection({
         type: 'property',
         // TODO -- both id's ??
-        containerId: 'body',
-        surfaceId: 'body',
+        containerId: containerId,
+        surfaceId: containerId,
         path: [ node.id, 'content' ],
         startOffset: 0,
         endOffset: 0
@@ -239,6 +240,12 @@ class ContainerEditor extends SubstanceContainerEditor {
     )
   }
 
+  getContainerId () {
+    let containerId = this.props.containerId
+    if (typeof containerId === 'undefined') containerId = 'mini'
+    return containerId
+  }
+
   getEditor () {
     return this.context.editor
   }
diff --git a/app/components/SimpleEditor/elements/isolatedNote/TextPropertyEditor.js b/app/components/SimpleEditor/elements/isolatedNote/TextPropertyEditor.js
index 085ea10..9f26b8a 100644
--- a/app/components/SimpleEditor/elements/isolatedNote/TextPropertyEditor.js
+++ b/app/components/SimpleEditor/elements/isolatedNote/TextPropertyEditor.js
@@ -48,8 +48,12 @@ class TextPropertyEditor extends SubstanceTextPropertyEditor {
   }
 
   getSurface () {
-    // TODO How to get ContainerID
-    return this.context.surfaceManager.getSurface('body')
+    const containerId = this.getContainerId()
+    return this.context.surfaceManager.getSurface(containerId)
+  }
+
+  getContainerId () {
+    return this.context.editor.props.containerId
   }
 }
 
diff --git a/app/components/SimpleEditor/miniEditor/MiniEditorImporter.js b/app/components/SimpleEditor/miniEditor/MiniEditorImporter.js
new file mode 100644
index 0000000..8770c6f
--- /dev/null
+++ b/app/components/SimpleEditor/miniEditor/MiniEditorImporter.js
@@ -0,0 +1,72 @@
+import { HTMLImporter } from 'substance'
+import SimpleArticle from '../SimpleArticle'
+
+class SimpleImporter extends HTMLImporter {
+  constructor (config) {
+    super({
+      schema: config.schema,
+      converters: config.converters,
+      DocumentClass: SimpleArticle
+    })
+  }
+
+  convertDocument (bodyEls) {
+    if (!bodyEls.length) bodyEls = [bodyEls]
+
+    this.convertContainer(bodyEls, 'mini')
+  }
+
+  // TODO -- check substance's implementation of overlapping annotations
+
+  // override substance's internal function to allow for overlapping
+  // annotations, without adhering to an expand / fuse mode
+  _createInlineNodes () {
+    var state = this.state
+    var doc = state.doc
+
+    /*
+      substance will break overlapping annotations of the same type into
+      pieces like this:
+
+      <anno id=1>
+          <anno id=2></anno>
+      </anno>
+      <anno id=2>
+      </anno>
+
+      when the importer finds the duplicate annotation id, it will remove
+      the first one altogether as a node from the doc
+
+      here, we are forcing it to keep these duplicates and merge them
+    */
+
+    state.inlineNodes.forEach(function (node) {
+      if (doc.get(node.id)) {
+        const existing = doc.get(node.id)
+        const newOne = node
+
+        doc.delete(node.id)
+
+        // only for comments
+        if (node.id.split('-')[0] === 'comment') {
+          // if they are adjacent and have the same id, merge the offsets
+          if (
+            existing.startOffset === newOne.endOffset ||
+            existing.endOffset === newOne.startOffset
+          ) {
+            node.startOffset = Math.min(existing.startOffset, newOne.startOffset)
+            node.endOffset = Math.max(existing.endOffset, newOne.endOffset)
+
+            doc.create(node)
+          }
+        } else {
+          doc.create(node) // TODO -- refactor
+        }
+      } else {
+        doc.create(node)
+      }
+    })
+  }
+}
+
+export default SimpleImporter
diff --git a/app/components/SimpleEditor/miniEditor/miniEditor.js b/app/components/SimpleEditor/miniEditor/miniEditor.js
index f63ecf0..14629db 100644
--- a/app/components/SimpleEditor/miniEditor/miniEditor.js
+++ b/app/components/SimpleEditor/miniEditor/miniEditor.js
@@ -72,7 +72,7 @@ class MiniEditor extends ProseEditor {
   _renderEditor ($$) {
     return $$(ContainerEditor, {
       comments: this.props.comments,
-      containerId: 'body',
+      containerId: this.props.containerId,
       editorSession: this.editorSession,
       fragment: this.props.fragment,
       spellcheck: 'native',
@@ -93,7 +93,7 @@ class MiniEditor extends ProseEditor {
     const commentsProvider = new CommentsProvider(doc, {
       commandManager: this.commandManager,
       comments: this.props.fragment.comments,
-      containerId: 'body',
+      containerId: this.props.containerId,
       controller: this,
       editorSession: this.editorSession,
       fragment: this.props.fragment,
@@ -104,7 +104,7 @@ class MiniEditor extends ProseEditor {
 
     const trackChangesProvider = new TrackChangesProvider(doc, {
       commandManager: this.commandManager,
-      containerId: 'body',
+      containerId: this.props.containerId,
       controller: this,
       editorSession: this.editorSession,
       surfaceManager: this.surfaceManager,
diff --git a/app/components/SimpleEditor/panes/Notes/Notes.js b/app/components/SimpleEditor/panes/Notes/Notes.js
index a9ecff7..29ec535 100644
--- a/app/components/SimpleEditor/panes/Notes/Notes.js
+++ b/app/components/SimpleEditor/panes/Notes/Notes.js
@@ -2,9 +2,8 @@ import { Component, EditorSession,
   ProseEditorConfigurator as Configurator } from 'substance'
 import MiniEditor from '../../miniEditor/miniEditor'
 import config from '../../miniEditor/config'
-import Importer from '../../SimpleEditorImporter'
+import Importer from '../../miniEditor/MiniEditorImporter'
 import {forEach} from 'lodash'
-// import SimpleExporter from '../../SimpleEditorExporter'
 
 class Notes extends Component {
   constructor (props) {
@@ -60,6 +59,7 @@ class Notes extends Component {
 
     el.append($$(MiniEditor, {
       comments: comments,
+      containerId: 'mini',
       editorSession: miniEditorSession,
       fragment: fragment,
       trackChanges: trackChanges,
@@ -72,7 +72,7 @@ class Notes extends Component {
     return el
   }
 
-  _initMiniEditor (tx) {
+  _initMiniEditor () {
     const configurator = new Configurator().import(config)
     configurator.addImporter('html', Importer)
 
diff --git a/app/components/SimpleEditor/panes/Notes/NotesProvider.js b/app/components/SimpleEditor/panes/Notes/NotesProvider.js
index dd0f5c3..b3f1cfe 100644
--- a/app/components/SimpleEditor/panes/Notes/NotesProvider.js
+++ b/app/components/SimpleEditor/panes/Notes/NotesProvider.js
@@ -99,10 +99,11 @@ class NotesProvider extends TOCProvider {
     const notes = this.computeEntries()
 
     if (notes.mini && notes.mini.length > 0) {
-      let findIndex = _.findIndex(notes.main, ['id', note.id])
-      let surface = this.config.miniEditorContext.surfaceManager.getSurface('body')
+      const containerId = this.getMiniContainerId()
+      let surface = this.config.miniEditorContext.surfaceManager.getSurface(containerId)
       let container = surface.getContainer()
-      // let nodePos = container.getPosition(lastNote.id, 'strict')
+
+      let findIndex = _.findIndex(notes.main, ['id', note.id])
 
       let nodeData = this.createNodeData(note)
       this.config.miniEditorSession.transaction(function (tx) {
@@ -118,6 +119,10 @@ class NotesProvider extends TOCProvider {
       }.bind(this))
     }
   }
+
+  getMiniContainerId () {
+    return this.config.miniEditorContext.editor.props.containerId
+  }
 }
 
 NotesProvider.tocTypes = ['note']
-- 
GitLab