Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add onPaste prop to customise clipboard paste event #3420

Merged

Conversation

fabien0102
Copy link
Contributor

@fabien0102 fabien0102 commented Apr 8, 2021

Add onPasteFromClipboard props to @excalidraw/excalidraw.

Example

A simple example to add a custom element when the text "special" is pasted in the scene.

import React, { useCallback, useRef } from "react";
import Excalidraw from "@excalidraw/excalidraw";
import {
  ExcalidrawAPIRefValue,
  ExcalidrawProps,
} from "@excalidraw/excalidraw/types/types";

export const App = () => {
  const ref = useRef<ExcalidrawAPIRefValue>(null);
  const onPasteFromClipboard = useCallback<Required<ExcalidrawProps>["onPasteFromClipboard"]>(
    (data) => {
      if (ref.current?.ready && data.text === "special") {
        /* do something special */
        const prev = ref.current.getSceneElementsIncludingDeleted();
        ref.current.updateScene({elements: [...prev, /* my special element */]})

        return true; // Prevent default paste action
      }

      return false; // Do the default paste action
    },
    []
  );

  return <Excalidraw ref={ref} onPasteFromClipboard={onPasteFromClipboard} />;
};

@vercel
Copy link

vercel bot commented Apr 8, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/excalidraw/excalidraw/9AWJTSWVS46d7VN9KPJzL84Gm9ft
✅ Preview: https://excalidraw-git-fork-fabien0102-expose-onpastefromcli-ba604d.vercel.app

src/components/App.tsx Outdated Show resolved Hide resolved
Copy link
Member

@ad1992 ad1992 left a comment

Choose a reason for hiding this comment

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

Also can you update changelog and README_NEXT.md ?

src/components/App.tsx Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
@ad1992 ad1992 added this to the @excalidraw/excalidraw@0.7.0 milestone Apr 9, 2021
@ad1992 ad1992 changed the title feat: Expose onPasteFromClipboard props feat: Add onPaste prop to customise clipboard paste event Apr 9, 2021
@ad1992 ad1992 merged commit d91950b into excalidraw:master Apr 9, 2021
@ad1992
Copy link
Member

ad1992 commented Apr 9, 2021

Thanks for the PR @fabien0102 🎉

@fabien0102 fabien0102 deleted the expose-onpastefromclipboard-props branch April 9, 2021 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants