Browse Source

fix: onPaste should return false to prevent paste action (#3974)

* Update App.tsx

* Update README_NEXT.md

* Update CHANGELOG.md

* Update App.tsx

* Update App.tsx

* Update src/packages/excalidraw/CHANGELOG.md

* fix lint

Co-authored-by: Aakansha Doshi <aakansha1216@gmail.com>
zsviczian 3 years ago
parent
commit
7d1fddc144

+ 6 - 2
src/components/App.tsx

@@ -1193,8 +1193,12 @@ class App extends React.Component<AppProps, AppState> {
       }
       const data = await parseClipboard(event);
       if (this.props.onPaste) {
-        if (await this.props.onPaste(data, event)) {
-          return;
+        try {
+          if ((await this.props.onPaste(data, event)) === false) {
+            return;
+          }
+        } catch (e) {
+          console.error(e);
         }
       }
       if (data.errorMessage) {

+ 8 - 0
src/packages/excalidraw/CHANGELOG.md

@@ -21,6 +21,14 @@ Please add the latest change on the top under the correct section.
 
 ## Excalidraw API
 
+### Fixes
+
+- [`onPaste`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#onPaste) prop should return false to prevent the native excalidraw paste action.
+
+#### BREAKING CHANGE
+
+- Earlier the paste action was prevented when the prop [`onPaste`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#onPaste) returned true, but now it should return false to prevent the paste action. This was done to make it semantically more correct and intuitive.
+
 ### Docs
 
 - Correct exportToBackend in README to onExportToBackend [#3952](https://github.com/excalidraw/excalidraw/pull/3952)

+ 1 - 1
src/packages/excalidraw/README_NEXT.md

@@ -610,7 +610,7 @@ This callback is triggered if passed when something is pasted into the scene. Yo
 
 This callback must return a `boolean` value or a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) which resolves to a boolean value.
 
-In case you want to prevent the excalidraw paste action you must return `true`, it will stop the native excalidraw clipboard management flow (nothing will be pasted into the scene).
+In case you want to prevent the excalidraw paste action you must return `false`, it will stop the native excalidraw clipboard management flow (nothing will be pasted into the scene).
 
 ### Does it support collaboration ?