all files / model/ createDocumentFactory.js

7.69% Statements 1/13
100% Branches 0/0
0% Functions 0/4
7.69% Lines 1/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                                                                                                 
import { EditingInterface, DocumentChange } from '../model'
import TransactionDocument from './TransactionDocument'
 
/*
  Creates a factory for documents and the correspondent initial changeset
 
  @param {String} name schema identifier
  @param {String} schema schema version
 
  @example
 
  var myDocFactory = createDocumentFactory(ProseArticle, function(tx) {
    var body = tx.get('body');
    tx.create({
      id: 'p1',
      type: 'paragraph',
      content: '0123456789'
    });
    body.show('p1');
  });
 
  myDocFactory.ArticleClass;
  myDocFactory.createEmptyArticle();
  myDocFactory.createArticle();
  myDocFactory.createChangeset();
*/
function createDocumentFactory(ArticleClass, create) {
  return {
    ArticleClass: ArticleClass,
    createEmptyArticle: function() {
      const doc = new ArticleClass()
      return doc
    },
    createArticle: function() {
      const doc = new ArticleClass()
      create(doc)
      return doc
    },
    createChangeset: function() {
      const doc = new ArticleClass()
      const txDoc = new TransactionDocument(doc)
      const tx = new EditingInterface(txDoc)
      create(tx)
      const change = new DocumentChange(txDoc.ops)
      return [change.toJSON()]
    }
  }
}
 
export default createDocumentFactory