diff --git a/app/components/SimpleEditor/ContainerEditor.js b/app/components/SimpleEditor/ContainerEditor.js index c9e603ae38ee06fe57abf08fe0ba79fd9a4ae91c..d195af86b2158b536292c5905fdd59e46f931956 100644 --- a/app/components/SimpleEditor/ContainerEditor.js +++ b/app/components/SimpleEditor/ContainerEditor.js @@ -36,7 +36,6 @@ class ContainerEditor extends SubstanceContainerEditor { super.didMount() if (this.isEmpty() && this.props.containerId !== 'mini') this.createText() - if (this.isEmpty() && this.props.containerId === 'mini') this.createTextMini() // TODO -- why this and not this.focus ? this.el.focus() @@ -210,28 +209,6 @@ class ContainerEditor extends SubstanceContainerEditor { this.editorSession.setSelection(newSel) } - createTextMini () { - const notes = this.context.notesProvider.computeEntries() - - this.editorSession.transaction(function (tx) { - var container = tx.get(this.props.containerId) - var textType = tx.getSchema().getDefaultTextType() - - forEach(notes, function (note, index) { - let node = tx.create({ - 'content': note['note-content'], - 'parentNoteId': note.id, - index: index, - type: textType - }) - container.show(node.id) - }) - }.bind(this)) - - this.rerender() - // this.editorSession.setSelection(newSel) - } - // only runs if editor is in read-only mode // disables all tools, apart from comments disableToolbar () { diff --git a/app/components/SimpleEditor/elements/note/NoteHTMLConverter.js b/app/components/SimpleEditor/elements/note/NoteHTMLConverter.js index 0beb642ac79e573231c5f03102f22049919a5733..2ce72eab5137194ebf425db8035f10fe59f465df 100644 --- a/app/components/SimpleEditor/elements/note/NoteHTMLConverter.js +++ b/app/components/SimpleEditor/elements/note/NoteHTMLConverter.js @@ -3,9 +3,7 @@ export default { tagName: 'note', import: function (element, node, converter) { - // node['note-content'] = converter.annotatedText(element.attr('note-content')) node['note-content'] = element.attr('note-content') - // console.log('converter', converter.annotatedText(element, [node.id, node['note-content']])) }, export: function (node, element, converter) { diff --git a/app/components/SimpleEditor/miniEditor/config.js b/app/components/SimpleEditor/miniEditor/config.js index 222a72ef9bda7c2cbfcc9b40b4df6d767fe49b7b..9db582758811096a8458837ab1a6d37ac05ab48d 100644 --- a/app/components/SimpleEditor/miniEditor/config.js +++ b/app/components/SimpleEditor/miniEditor/config.js @@ -19,7 +19,7 @@ let config = { config.defineSchema({ name: 'prose-article', ArticleClass: ProseArticle, - defaultTextType: 'isolated-note' + defaultTextType: 'paragraph' }) config.import(BasePackage, { diff --git a/app/components/SimpleEditor/miniEditor/miniEditor.js b/app/components/SimpleEditor/miniEditor/miniEditor.js index 81c0315bcc401a925fb5b5a13ca9a516cbe9b664..e26b1389971b99321b487d2c4576ad3b479dcb65 100644 --- a/app/components/SimpleEditor/miniEditor/miniEditor.js +++ b/app/components/SimpleEditor/miniEditor/miniEditor.js @@ -8,16 +8,17 @@ import Comments from '../panes/Comments/CommentBoxList' import CommentsProvider from '../panes/Comments/CommentsProvider' import TrackChangesProvider from '../elements/track_change/TrackChangesProvider' import SimpleExporter from '../SimpleEditorExporter' -import {forEach, values} from 'lodash' +import _ from 'lodash' class MiniEditor extends ProseEditor { didMount () { const provider = this.getProvider() provider.config.miniEditorContext = this.getChildContext() provider.config.miniEditorSession = this.editorSession - this.editorSession.onUpdate('document', this.saveNote, this) + this.editorSession.onRender('document', this.findNote, this) this.on('noteSelected', this.scrollTo, this) - this.on('notes:updated', this.createNotes, this) + this.on('notes:inserted', this.createNote, this) + this.on('notes:deleted', this.removeNote, this) } render ($$) { @@ -62,7 +63,7 @@ class MiniEditor extends ProseEditor { return $$('div').addClass('se-toolbar-wrapper').append( $$(Toolbar, { commandStates: commandStates, - toolGroups: ['document', 'annotations'] + toolGroups: ['annotations'] }).ref('mini_toolbar') ) } @@ -84,65 +85,123 @@ class MiniEditor extends ProseEditor { } scrollTo (nodeId) { - this.refs.miniEditorContentPanel.scrollTo(nodeId) + const nodes = this.getIsolatedNodes() + const note = _.find(nodes, function (c) { + return c.parentNoteId === nodeId + }) + + this.refs.miniEditorContentPanel.scrollTo(note.id) } - createNodeData (note, index) { + createNodeData (note) { return { 'type': 'isolated-note', 'content': note['note-content'], - 'parentNoteId': note.id, - index: index + 'parentNoteId': note.id } } - createNotes () { + createNote (noteId) { const provider = this.getProvider() const notes = provider.computeEntries() const surface = this.getSurface() const container = surface.getContainer() - this.removeNotes() - - forEach(notes, function (node, key) { - let nodeData = this.createNodeData(node, key) - this.editorSession.transaction(function (tx) { - if (!tx.get(nodeData.id)) { - let blockNode = tx.create(nodeData) - tx.update(container.getContentPath(), { type: 'insert', pos: key, value: blockNode.id }) - } - }) - }.bind(this)) + + let note = _.find(notes, function (c) { + return c.id === noteId + }) + + let findIndex = _.findIndex(notes, ['id', note.id]) + let nodeData = this.createNodeData(note) + + this.editorSession.transaction(function (tx) { + let blockNode = tx.create(nodeData) + tx.update(container.getContentPath(), { type: 'insert', pos: findIndex, value: blockNode.id }) + }) + + const existingNodes = this.getIsolatedNodes() + let isolatedNote = _.find(existingNodes, function (c) { + return c.parentNoteId === noteId + }) + this.saveNote(isolatedNote) } - removeNotes () { - let existingNodes = this.editorSession.document.data.nodes + removeNote (noteId) { + const existingNodes = this.getIsolatedNodes() const surface = this.getSurface() const container = surface.getContainer() - console.log(this.editorSession.getSelection()) - forEach(values(existingNodes), function (node, key) { - if (node.type === 'isolated-note') { - let position = container.getPosition(node.id, 'strict') - this.editorSession.transaction(function (tx) { - tx.update(container.getContentPath(), { type: 'delete', pos: position }) - tx.delete(node.id) - }) - } - }.bind(this)) + + let note = _.find(existingNodes, function (c) { + return c.parentNoteId === noteId + }) + + // if Isolated note is focused cannot be deleted. Remove focus + // before deleting callout + this.editorSession.setSelection(null) + + // Delete comments from the isolated note before + // removing the note + this.removeCommentsFirst(note.id) + + let findIndex = _.findIndex(existingNodes, ['parentNoteId', noteId]) + + this.editorSession.transaction(function (tx) { + tx.update(container.getContentPath(), { type: 'delete', pos: findIndex }) + tx.delete(note.id) + }) } - saveNote () { + saveNote (isolatedNote) { + // If isloated note has no content and you keep pressing backspace, + // it gets deleted. Set selection to null to prevent that const selection = this.editorSession.getSelection() - if (!selection.end) return - const isolatedNoteId = selection.end.path[0] - const isolatedNote = this.editorSession.document.get(isolatedNoteId) + if (selection.start.offset === 0 && selection.end.offset === 0) { + this.editorSession.setSelection(null) + } + const exporter = new SimpleExporter(this.props.configurator.config) const convertedNode = exporter.convertNode(isolatedNote) - this.context.editorSession.transaction(function (tx, args) { const path = [isolatedNote.parentNoteId, 'note-content'] - tx.set(path, convertedNode.innerHTML) + tx.set(path, convertedNode.outerHTML) + }) + } + + 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) { + return value.type === 'isolated-note' + }) + return _.values(entries) + } + + removeCommentsFirst (noteId) { + const doc = this.editorSession.document + const nodes = doc.getNodes() + + const entries = _.pickBy(nodes, function (value, key) { + return value.type === 'comment' }) - // console.log(isolatedNote) + + if (!entries) return + + _.forEach(entries, function (node, key) { + if (node.path[0] === noteId) { + this.editorSession.transaction(function (tx) { + tx.delete(node.id) + }) + } + }.bind(this)) } getProvider () { diff --git a/app/components/SimpleEditor/panes/Notes/Notes.js b/app/components/SimpleEditor/panes/Notes/Notes.js index a26aad30092cb3fc2e50dd9605fa773144247133..5879e890e64e6925089841c264689492f88e2411 100644 --- a/app/components/SimpleEditor/panes/Notes/Notes.js +++ b/app/components/SimpleEditor/panes/Notes/Notes.js @@ -3,7 +3,7 @@ import { Component, EditorSession, import MiniEditor from '../../miniEditor/miniEditor' import config from '../../miniEditor/config' import Importer from '../../miniEditor/MiniEditorImporter' -import {pickBy, isEmpty} from 'lodash' +import {isEmpty, find} from 'lodash' class Notes extends Component { constructor (props) { @@ -67,17 +67,17 @@ class Notes extends Component { configurator.addImporter('html', Importer) const importer = configurator.createImporter('html') - // const provider = this.getProvider() - // - // const notes = provider.computeEntries() - // let noteContent = '' - // - // for (var i = 0; i < notes.length; i++) { - // noteContent += notes[i]['note-content'] - // } - // - // const doc = importer.importDocument(noteContent) - const doc = importer.importDocument() + const provider = this.getProvider() + + const notes = provider.computeEntries() + let noteContent = '' + + for (var i = 0; i < notes.length; i++) { + noteContent += notes[i]['note-content'] + } + + const doc = importer.importDocument(noteContent) + // const doc = importer.importDocument() const editorSession = new EditorSession(doc, { configurator: configurator }) @@ -107,20 +107,25 @@ class Notes extends Component { onNotesUpdated (change) { const notesProvider = this.getProvider() notesProvider.handleDocumentChange(change) + const miniEditor = this.getMiniEditor() - const isNoteCreated = pickBy(change.created, function (value, key) { + const NoteCreated = find(change.created, function (value, key) { return value.type === 'note' }) - const isNoteDeleted = pickBy(change.deleted, function (value, key) { + const NoteDeleted = find(change.deleted, function (value, key) { return value.type === 'note' }) - if (isEmpty(isNoteCreated) && isEmpty(isNoteDeleted)) return + if (!isEmpty(NoteCreated)) { + miniEditor.emit('notes:inserted', NoteCreated.id) + return false + } - const miniEditor = this.getMiniEditor() - miniEditor.emit('notes:updated') - // this.el.el.style.height = '95px' + if (!isEmpty(NoteDeleted)) { + miniEditor.emit('notes:deleted', NoteDeleted.id) + return false + } } getParentProps () { diff --git a/app/components/SimpleEditor/panes/Notes/NotesProvider.js b/app/components/SimpleEditor/panes/Notes/NotesProvider.js index 9c7211c0a0f3ba419992c234ac9c5a576f5304b2..44c655a1d5cdf94fcab66a8b2950148cadba5226 100644 --- a/app/components/SimpleEditor/panes/Notes/NotesProvider.js +++ b/app/components/SimpleEditor/panes/Notes/NotesProvider.js @@ -45,9 +45,7 @@ class NotesProvider extends TOCProvider { } showNote (note) { - const notes = this.computeEntries() - const miniNote = _.find(notes.mini, ['parentNoteId', note.id]) - if (miniNote) this.config.miniEditorContext.editor.emit('noteSelected', miniNote.id) + this.config.miniEditorContext.editor.emit('noteSelected', note.id) } }