accordproject / markdown-transform

Parse and transform markdown text, including TemplateMark markdown templates
Apache License 2.0
72 stars 50 forks source link

Functional Transformations #203

Open jolanglinais opened 4 years ago

jolanglinais commented 4 years ago

Describe the bug In the slateToCiceroMarkDom I’m running into this error where node.children is read only and errors because it cannot be reassigned.

The error is Uncaught TypeError: Cannot assign to read only property 'children' of object '#<Object>' which occurs at that line I linked above.

To Reproduce Steps to reproduce the behavior:

  1. Link slate-0.50.x branch in this editor to cicero-ui slate57 branch.
  2. Highlight an entire clause template in the demo
  3. Copy
  4. See error

Expected behavior Currently this works: JSON.parse(JSON.stringify(Node.fragment(editor, editor.selection));, but I expect this to not be required.

Screenshots

Screen Shot 2020-04-02 at 11 36 06 AM

Additional context

const onCopy = (event) => {
    const slateTransformer = new SlateTransformer();
    const htmlTransformer = new HtmlTransformer();
    const ciceroMarkTransformer = new CiceroMarkTransformer();

    const SLATE_CHILDREN = JSON.parse(JSON.stringify(Node.fragment(editor, editor.selection)));
    console.log('SLATE_CHILDREN ', SLATE_CHILDREN);
    const SLATE_DOM = {
      object: 'value',
      document: {
        object: 'document',
        data: {},
        children: SLATE_CHILDREN
      }
    };

    console.log('SLATE_DOM ', SLATE_DOM);
    const CICERO_MARK_DOM = slateTransformer.toCiceroMark(SLATE_DOM);
    console.log('CICERO_MARK_DOM ', CICERO_MARK_DOM);
    const HTML_DOM = htmlTransformer.toHtml(CICERO_MARK_DOM);
    console.log('HTML_DOM ', HTML_DOM);
    const MARKDOWN_TEXT = ciceroMarkTransformer.toMarkdown(CICERO_MARK_DOM);
    console.log('MARKDOWN_TEXT ', MARKDOWN_TEXT);

    event.preventDefault();
    event.clipboardData.setData('text/html', HTML_DOM);
    event.clipboardData.setData('text/plain', MARKDOWN_TEXT);
  };
jeromesimeon commented 4 years ago

The markdown transform expects a plain JSON object. Can you try with that? I'm suspecting that what you are passing isn't proper JSON.

(Note: I believe that's why it's working with JSON.parse(JSON.stringify(...)))

jolanglinais commented 4 years ago

Tracking in https://github.com/ianstormtaylor/slate/issues/3577

jolanglinais commented 4 years ago

Changing title. We need to migrate to a functional approach to transformations and avoid mutations.