bustle / mobiledoc-kit

A toolkit for building WYSIWYG editors with Mobiledoc
https://bustle.github.io/mobiledoc-kit/demo/
MIT License
1.55k stars 151 forks source link

implement card rendering refactor #235

Closed bantic closed 8 years ago

bantic commented 8 years ago

This is a meta-issue that tracks implementing the changes explicated in #234. The notes below are copied from that PR.

Next steps:

Every card is an object that must define the following properties:

Additionally a card may define the following property:

The render method (and the edit method if applicable) receives a single argument that is an object with the following properties:

The return value of the render method is the rendered result of the card. This should either be a dom node (for cards with type 'dom') or a string (for cards with type 'html' or 'text'). Renderers should validate that the return value is the correct type when given. A render method may also return undefined (this is the case for the ember-mobiledoc-dom-renderer, for instance, since it uses hooks from options to defer card rendering to ember components), which should also be considered a valid return value.

Tearing down a card

The 80% use case is likely that most cards will not need to clean up after themselves. The editor-dom renderer will clear child nodes, which should account for most use cases. When cards need to manage their own cleanup, however, they can register a teardown callback by calling the onTeardown method with a function to be called on teardown, e.g.:

card = {
  name: ...,
  type: ...,
  render({env, options, payload}) {
    let { name, onTeardown } = env;
    onTeardown(() => {
      console.log('tearing down ' + name + ' card');
    });
  }
};

Renderer

Change Renderer constructor signature

The renderer constructor will change to accept an options object with the following properties:

let renderer = new Renderer({cards, cardOptions, unknownCardHandler, atoms});

Change Renderer render signature and return value

The render method will accept a mobiledoc and return an object with result property and teardown method:

let rendered = render(mobiledoc);
// renderered.result === DOM node or string, depending on the renderer

rendered.teardown() // calls teardown hooks for any rendered cards, removes the rendered.result if it is attached

Renderer instance is reusable, render is reentrant (stateless)

Ensure that render is stateless for the dom, html and text renderers (this is already the case for the dom and html renderers).

An instance of a renderer should be reusable for rendering multiple mobiledocs (this simplifies "inception" cards that also render mobiledoc), which means its render method must also be stateless.

unknownCardHandler method

This method is called with the same arguments as a normal card's render method.

bantic commented 8 years ago

Next steps will be to release a non-beta version of mobiledoc-kit and ember-mobiledoc-editor.