Skip to content
Snippets Groups Projects
Commit 56c01e2b authored by john's avatar john
Browse files

handle undo for track changes additions

parent e500027a
No related branches found
No related tags found
No related merge requests found
import { each, keys, includes } from 'lodash' import { each, keys, includes } from 'lodash'
import { import {
ContainerEditor as SubstanceContainerEditor, ContainerEditor as SubstanceContainerEditor,
// createAnnotation,
Surface, Surface,
uuid uuid
} from 'substance' } from 'substance'
...@@ -34,21 +33,42 @@ class ContainerEditor extends SubstanceContainerEditor { ...@@ -34,21 +33,42 @@ class ContainerEditor extends SubstanceContainerEditor {
// open for editing // open for editing
// TODO -- should maybe change to isEditable ? // TODO -- should maybe change to isEditable ?
// or maybe deleted? we never pass a disabled prop explicitly // or maybe delete it? we never pass a disabled prop explicitly
if (!this.props.disabled) { if (!this.props.disabled) {
el.addClass('sm-enabled') el.addClass('sm-enabled')
el.setAttribute('contenteditable', true) el.setAttribute('contenteditable', true)
} }
// editing locked, selection open (for comments)
// if (this.isReadOnlyMode()) {
// const documentSession = this.getDocumentSession()
// documentSession.on('didUpdate', this.disableToolbar, this)
// }
return el return el
} }
didMount () {
// TODO -- replace with super.didMount()
Surface.prototype.didMount.apply(this, arguments)
this.container.on('nodes:changed', this.onContainerChange, this)
// this.context.documentSession.on('didUpdate', (change, info) => {
// // console.log(this.getSelection())
// // console.log(this.context.commandManager.getCommandStates().strong.mode)
// // console.log(this.context.commandManager.getCommandStates()['track-change'].mode)
// console.log('\n did update')
// console.log('change', change)
// console.log('info', info)
//
// if (info.track) change.change.info.track = true
// }, this)
if (this.isEmpty()) this.createText()
if (this.isReadOnlyMode()) {
const documentSession = this.getDocumentSession()
documentSession.on('didUpdate', this.disableToolbar, this)
this.addTargetToLinks()
}
}
onTextInput (event) { onTextInput (event) {
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
...@@ -69,22 +89,6 @@ class ContainerEditor extends SubstanceContainerEditor { ...@@ -69,22 +89,6 @@ class ContainerEditor extends SubstanceContainerEditor {
} }
} }
didMount () {
// TODO -- replace with super.didMount()
Surface.prototype.didMount.apply(this, arguments)
this.container.on('nodes:changed', this.onContainerChange, this)
if (this.isEmpty()) this.createText()
if (this.isReadOnlyMode()) {
const documentSession = this.getDocumentSession()
documentSession.on('didUpdate', this.disableToolbar, this)
this.addTargetToLinks()
}
}
// create an empty paragraph with an empty node // create an empty paragraph with an empty node
// then select it for cursor focus // then select it for cursor focus
createText () { createText () {
......
import { keys, last } from 'lodash'
import { createAnnotation } from 'substance' import { createAnnotation } from 'substance'
class TrackChangesProvider { class TrackChangesProvider {
constructor (config) { constructor (config) {
this.config = config this.config = config
this.config.documentSession.on('didUpdate', this.handleUndoRedo, this)
} }
handleTransaction () { handleTransaction () {
const mode = this.getMode()
if (mode) return
const surface = this.getSurface() const surface = this.getSurface()
surface.transaction((tx, args) => { const transformation = (tx, args) => {
const selection = this.createSelection(args) const selection = this.createSelection(args)
// console.log(this.getTrackState())
const newNode = { const newNode = {
selection: selection, selection: selection,
node: { 'type': 'track-change' } node: { 'type': 'track-change' }
} }
const mode = this.getMode() createAnnotation(tx, newNode)
this.clearSelection(args)
// this.removeAnnotationFromHistory()
}
if (!mode) { const info = {
createAnnotation(tx, newNode) track: true
} else if (mode === 'delete') { }
// continue here to expand annotation
}
this.clearSelection(args) surface.transaction(transformation, info)
this.removeAnnotationFromHistory() }
})
handleUndoRedo (update, info) {
if (!info.replay) return
console.log(update)
console.log(info)
this.handleUndo(update)
this.handleRedo(update)
}
handleUndo (update) {
const deleted = update.change.deleted
const deletedLength = keys(deleted).length
// console.log(keys(deleted))
if (deletedLength === 0) return
if (deletedLength > 1) {
return console.warn('FIXME: Multiple operations in track changes replay!')
}
const deletedOp = deleted[keys(deleted)[0]]
if (!deletedOp.type === 'track-change') return
const documentSession = this.getDocumentSession()
documentSession.undo()
} }
removeAnnotationFromHistory () { handleRedo () {
const documentSession = this.getDocumentSession() const documentSession = this.getDocumentSession()
setTimeout(() => { const undoneChanges = documentSession.undoneChanges
documentSession.doneChanges.pop() const lastChange = last(undoneChanges)
}) // ensure that text insertion has finished const op = last(lastChange.ops)
const isTrack = op.path[0].split('-').slice(0, -1).join('-') === 'track-change'
console.log(isTrack)
} }
// removeAnnotationFromHistory () {
// const documentSession = this.getDocumentSession()
// // console.log(documentSession.doneChanges)
// setTimeout(() => {
// console.log(documentSession.doneChanges[1].toJSON())
// // documentSession.doneChanges.pop()
// }) // ensure that text insertion has finished
// }
clearSelection (args) { clearSelection (args) {
const selection = args.selection const selection = args.selection
selection.startOffset = selection.endOffset selection.startOffset = selection.endOffset
...@@ -64,13 +105,6 @@ class TrackChangesProvider { ...@@ -64,13 +105,6 @@ class TrackChangesProvider {
return state.mode return state.mode
} }
// createCommandState () {
// const commandState = this.getCommandState()
// commandState.disabled = false
// commandState.mode = 'create'
// return commandState
// }
getDocumentSession () { getDocumentSession () {
return this.config.documentSession return this.config.documentSession
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment