ianstormtaylor / slate

A completely customizable framework for building rich text editors. (Currently in beta.)
http://slatejs.org
MIT License
29.93k stars 3.25k forks source link

TypeError: Cannot read property 'marks' of undefined #270

Closed aeneasr closed 8 years ago

aeneasr commented 8 years ago

I am using redux and redux-undo. Every onChange in slate triggers an action change in redux and is undoable using redux-undo. I disabled slate's internal undo completely.

Sometimes, when un-/ or redoing the state, I receive the following exception: TypeError: Cannot read property 'marks' of undefined.

The stacktrace is:

state.js

    /**
     * Get the marks of the current selection.
     *
     * @return {Set} marks
     */

  }, {
    key: 'marks',
    get: function get() {
      return this.cursorMarks || this.document.getMarksAtRange(this.selection); // ############## FAILS HERE ####################

    }

memoize.js

/**
 * Memoize all of the `properties` on a `object`.
 *
 * @param {Object} object
 * @param {Array} properties
 * @return {Record}
 */

function memoize(object, properties) {
  var _iteratorNormalCompletion = true;
  var _didIteratorError = false;
  var _iteratorError = undefined;

  try {
    var _loop = function _loop() {
      var property = _step.value;

      var original = object[property];

      if (!original) {
        throw new Error('Object does not have a property named "' + property + '".');
      }

      object[property] = function () {
        for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        var keys = [property].concat(args, [LEAF]);
        var cache = this.__cache = this.__cache || new _immutable.Map();

        if (cache.hasIn(keys)) return cache.getIn(keys);

        var value = original.apply(this, args); // ############## FAILS HERE ####################
        this.__cache = cache.setIn(keys, value);
        return value;
      };
    };

    for (var _iterator = properties[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
      _loop();
    }
  } catch (err) {
    _didIteratorError = true;
    _iteratorError = err;
  } finally {
    try {
      if (!_iteratorNormalCompletion && _iterator.return) {
        _iterator.return();
      }
    } finally {
      if (_didIteratorError) {
        throw _iteratorError;
      }
    }
  }
}

node.js

  getMarksAtRange: function getMarksAtRange(range) {
    range = range.normalize(this);
    var _range = range;
    var startKey = _range.startKey;
    var startOffset = _range.startOffset;

    var marks = _mark2.default.createSet();

    // If the range is collapsed at the start of the node, check the previous.
    if (range.isCollapsed && startOffset == 0) {
      var text = this.getDescendant(startKey);
      var previous = this.getPreviousText(startKey);
      if (!previous || !previous.length) return marks;
      var char = previous.characters.get(previous.length - 1);
      return char.marks;
    }

    // If the range is collapsed, check the character before the start.
    if (range.isCollapsed) {
      var _text = this.getDescendant(startKey);
      var _char = _text.characters.get(range.startOffset - 1);

      return _char.marks; // ############## FAILS HERE ####################
    }

    // Otherwise, get a set of the marks for each character in the range.
    return this.getCharactersAtRange(range).reduce(function (memo, char) {
      return memo.union(char.marks);
    }, new _immutable.Set());
  },

I am still looking for an easy way to reproduce this, but maybe someone has an idea how to resolve this.

aeneasr commented 8 years ago

This could, maybe, be linked to #271

ianstormtaylor commented 8 years ago

@arekkas I'm going to close this for now with 0.14.0 since the internals have been changed a lot for transforms and history, but if you notice it again feel free to re-open!

aeneasr commented 8 years ago

Thanks, I will check once I'm back from vacation :)

Am 11.09.2016 um 03:38 schrieb Ian Storm Taylor notifications@github.com:

@arekkas https://github.com/arekkas I'm going to close this for now with 0.14.0 since the internals have been changed a lot for transforms and history, but if you notice it again feel free to re-open!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ianstormtaylor/slate/issues/270#issuecomment-246156265, or mute the thread https://github.com/notifications/unsubscribe-auth/ADN1epqK9qPzJVA7tihEyu_XqTBAJcV0ks5qo1t7gaJpZM4JveAq.