Skip to content
Add support for Immutable.js structures to ot-json1
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.eslintrc.js
.gitignore
.npmignore
README.md
babel.config.js
package.json
yarn.lock

README.md

JSON1 + Immutable.js

This module add supports for Immutable.js structures to ot-json1.

Install

yarn add ot-json1-immutable ot-json1 immutable

Usage

Just import json from ot-json1-immutable and use it exactly like specified by ot-json1 documentation.

import json1 from 'ot-json1-immutable';
import { Map } from 'immutable';

const op = json1.moveOp(['a'], ['b']);

let doc = new Map({ a: 1 });
doc = json1.type.apply(doc, op);
// doc => new Map({ b: 1 })

Apply function also take a reviver, used by Immutable's fromJS:

function reviver(key, value) => {
  if (!isKeyed(value)) {
    return value.toList();
  }
  switch (value.get('object')) {
    case 'block':
      return Block.create(value.toJS());
    default:
      return value.toMap();
  }
}

const op = json1.insertOp(['a'], { object: 'block' });
let doc = new Map({});

doc = json1.type.apply(doc, op, reviver);

// doc.get('a') => new Block()
You can’t perform that action at this time.