Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

feat(src): pasting clause uses new uuid - I88 #89

Merged
merged 3 commits into from
Sep 4, 2019

Conversation

irmerk
Copy link
Member

@irmerk irmerk commented Aug 30, 2019

Issue #88

Create new uuid for a clause that was copy/pasted
Handle html on a copy/paste and abstract out clause paste logic to Cicero-UI

Changes

  • On a paste event, we take the Document on the event and make it mutable in order to change the clauseid to a uuid and revert everything to immutable
  • On a paste event, we handle html type in the paste effect

Flags

  • NOTE: This functionality may need to be moved to @accordproject/cicero-ui to abstract out clause template specific logic.
    • This could take the form of replicating the logic in cicero-ui, passing it down, and running props.onPaste if it has been passed down
      • This has the issue of onPaste requiring PluginManager and FromHTML from markdown-editor, which means they will need to be imported into cicero-ui only for them to be passed down into markdown-editor from which they came from originally
    • Another form this could take is a higher order function, where the clause specific logic is passed down and used in onPaste if it exists and then carries on to the rest of the plugin process.
    • if (transfer.type !== 'html') return undefined; may need to be adjusted with next() or something similar? Unsure what next() really does here.
  • Slate uses IMMUTABLE.js, so we need to augment multiple layers to be mutable in order to then change the clauseid and then augment everything back to immutable.
  • Further work is needed in the redux store of TSv2, showcased here where the new uuid's placed onto the clauses are not represented in the store, and moreover the extra pasted clause does not appear at all.

DOM:
Screen Shot 2019-08-30 at 3 32 49 PM
Store:
Screen Shot 2019-08-30 at 3 33 08 PM

Related Issues

Signed-off-by: irmerk <jolenelanglinais@gmail.com>
Signed-off-by: irmerk <jolenelanglinais@gmail.com>
@irmerk irmerk added Type: Feature Request 🛍️ New feature or request Help Wanted 🆘 Extra attention is needed Difficulty: Challenging labels Aug 30, 2019
@irmerk irmerk added this to the 0.6.0 milestone Aug 30, 2019
@irmerk irmerk self-assigned this Aug 30, 2019
@irmerk irmerk added this to In Progress PR in Template Studio V2 via automation Aug 30, 2019
@irmerk irmerk added this to In Progress PR in Full Contract Support via automation Aug 30, 2019
@dselman
Copy link
Contributor

dselman commented Sep 1, 2019

Yes, the clause specific logic should be in Cicero-ui not here. I think we can register multiple onPaste handlers on the underlying slate editor, they are chained by the call to next() I believe. We also need to test what happens on paste of html that contains an existing clause div.

Copy link
Contributor

@dselman dselman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move any logic specific to clauses into cicero-ui.

@irmerk
Copy link
Member Author

irmerk commented Sep 3, 2019

Yes, the clause specific logic should be in Cicero-ui not here. I think we can register multiple onPaste handlers on the underlying slate editor, they are chained by the call to next() I believe. We also need to test what happens on paste of html that contains an existing clause div.

Not sure I understand how to go about this. This is the direction I am currently going in:

markdown-editor inside SlateAsInputEditor:

const SlateAsInputEditor = React.forwardRef((props, ref) => {
  /**
   * Destructure props for efficiency
   */
  const {
    onChange, plugins, value, lockText, onPaste
  } = props;

...

const onPasteHandler = (event, editor, next) => {
    const pluginManager = new PluginManager(props.plugins);

    if (isEditable(editor, 'paste')) {
      const transfer = getEventTransfer(event);
      const mutableFragment = transfer.fragment.asMutable();
      const mutableNodes = mutableFragment.nodes.asMutable();
      const pastedMutableNodes = onPaste(mutableNodes);

      mutableFragment.nodes = pastedMutableNodes.asImmutable();
      transfer.fragment = mutableFragment.asImmutable();
      editor.insertFragment(transfer.fragment);

      if (transfer.type !== 'html') return undefined;

      const fromHtml = new FromHTML(pluginManager);
      const { document } = fromHtml.convert(editor, transfer.html);
      editor.insertFragment(document);
      return undefined;
    }

    return false;
  };

cicero-ui in ClausePlugin:

const onPaste = (nodes) => {
   // nodes is a Class, and errors when trying to run map on the following line
    nodes.map((node) => {
      if (isHeadingClause(node)) {
        const mutableNode = node.asMutable();
        const mutableDataMap = mutableNode.data.asMutable();
        const mutableAttributesMap = mutableDataMap.get('attributes');

        mutableAttributesMap.clauseid = uuidv4(); // unique identifier for a clause instance

        mutableDataMap.set('attributes', mutableAttributesMap);
        mutableNode.data = mutableDataMap.asImmutable();
        return mutableNode;
      }
      return node;
    });
  };

Signed-off-by: irmerk <jolenelanglinais@gmail.com>
@irmerk
Copy link
Member Author

irmerk commented Sep 4, 2019

Further issues should consider:

  • Handling different transfer.type situations

    • Editor instance (Slate) to another editor instance (Slate)
    • Raw HTML (Chrome to VSCode to Chrome)
    • Editor instance (Slate) to Google Docs and back (recognize <clause> formatting)
  • Ensure onPaste handlers are not doubling up work for any transfer.type cases

    • @accordproject/cicero-ui/src/plugins/ClausePlugin
    • @accordproject/markdown-editor/src/SlateAsInputEditor
  • What fragment.type results when copying pasting from one editor to another instance of an editor?

    • fragment as a transfer.type is a Slate DOM block fragment, not HTML anymore, so copying and pasting from the same editor into itself should be a fragment
    • However, copying pasting from one editor to another instance of an editor may result in HTML or fragment, needs experimentation
  • Preserve formatting, if H1 in HTML, we should be preserving the formatting

    • Slate has a mechanism for mapping from HTML tags to Slate blocks here

@irmerk irmerk merged commit 2dc1098 into accordproject:master Sep 4, 2019
Template Studio V2 automation moved this from In Progress PR to Finished PR Sep 4, 2019
Full Contract Support automation moved this from In Progress PR to Finished PR Sep 4, 2019
@irmerk irmerk deleted the irmerk/i88/copy-paste-clause branch September 4, 2019 19:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Full Contract Support
  
Finished PR
Template Studio V2
  
Finished PR
Development

Successfully merging this pull request may close these issues.

None yet

3 participants