Changing doc.attrs?

Hi johanneswilm - I’m happy to share, but I haven’t implemented this in a generic way, so it’s not particularly useful verbatim. It’s pretty simple though:

class AddTodo extends Step {
  constructor(todo) {
    super();

    this.todo = todo;
  }

  apply(doc) {
    doc.attrs.todos.push(this.todo);

    return StepResult.ok(doc);
  }

  invert() {
    return new RemoveTodo(this.todo);
  }

  map(mapping) {
    return this;
  }

  toJSON() {
    return {
      stepType: "addTodo",
      todo: this.todo
    };
  }

  static fromJSON(json) {
    return new AddTodo(json.todo);
  }
}
Step.jsonID("addTodo", AddTodo);

which is used as:

state.tr.step(new AddTodo(todo));

Modifying the doc.attrs value directly seems a bit wrong, but appears to work as desired. Note that I’m only ever using this in conjunction with a step that actually modifies the document content (in a standard way), so I’m not sure how well this would work if it’s the only step being applied.

6 Likes