import { Editing } from 'substance'

/*
  EXPERIMENTAL: an 'Editing' interface that takes the XML schema into account.
  TODO: try to generalize this and add it to the 'app dev kit'
*/
export default class TextureEditing extends Editing {
  /*
    2.0 API suggestion (pass only id, not data)
  */
  insertInlineNode (tx, node) {
    let text = '\uFEFF'
    this.insertText(tx, text)
    let sel = tx.selection
    let endOffset = tx.selection.end.offset
    let startOffset = endOffset - text.length
    // TODO: introduce a coordinate operation for that
    tx.set([node.id, 'start', 'path'], sel.path)
    tx.set([node.id, 'start', 'offset'], startOffset)
    tx.set([node.id, 'end', 'path'], sel.path)
    tx.set([node.id, 'end', 'offset'], endOffset)
    return node
  }

  createListNode (tx, containerPath, params) {
    let prop = tx.getProperty(containerPath)
    if (prop.targetTypes.indexOf('list') >= 0) {
      return tx.create({ type: 'list', listType: params.listType })
    } else {
      throw new Error(`'list' is not a valid child node for ${containerPath}`)
    }
  }
}

@@ -1079,11 +1079,363 @@
    %7D%0A  %7D
+%0A%0A  insertBlockNode (tx, node) %7B%0A    // HACK: deviating from the current implementation%0A    // to replace selected node, because it happens quite often%0A    let sel = tx.selection%0A    if (sel.isNodeSelection() && sel.mode !== 'before') %7B%0A      tx.setSelection(Object.assign(sel.toJSON(), %7B mode: 'after' %7D))%0A    %7D%0A    super.insertBlockNode(tx, node)%0A  %7D
 %0A%7D%0A

Insert block node after current node.