From 95eaadeb854be6caf640ac9c130b8fffb498103a Mon Sep 17 00:00:00 2001 From: Kent Beck Date: Sat, 28 Mar 2020 15:43:09 -0700 Subject: [PATCH] Refactor paste code (#1102) --- src/components/App.tsx | 77 ++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 46f6e2f8..430af6c8 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -560,44 +560,20 @@ export class App extends React.Component { if ( // if no ClipboardEvent supplied, assume we're pasting via contextMenu // thus these checks don't make sense - !event || - (elementUnderCursor instanceof HTMLCanvasElement && - !isWritableElement(target)) + event && + (!(elementUnderCursor instanceof HTMLCanvasElement) || + isWritableElement(target)) ) { - const data = await getClipboardContent(event); - if (data.elements) { - this.addElementsFromPaste(data.elements); - } else if (data.text) { - const { x, y } = viewportCoordsToSceneCoords( - { clientX: cursorX, clientY: cursorY }, - this.state, - this.canvas, - window.devicePixelRatio, - ); - - const element = newTextElement({ - x: x, - y: y, - strokeColor: this.state.currentItemStrokeColor, - backgroundColor: this.state.currentItemBackgroundColor, - fillStyle: this.state.currentItemFillStyle, - strokeWidth: this.state.currentItemStrokeWidth, - roughness: this.state.currentItemRoughness, - opacity: this.state.currentItemOpacity, - text: data.text, - font: this.state.currentItemFont, - }); - - globalSceneState.replaceAllElements([ - ...globalSceneState.getAllElements(), - element, - ]); - this.setState({ selectedElementIds: { [element.id]: true } }); - history.resumeRecording(); - } - this.selectShapeTool("selection"); - event?.preventDefault(); + return; } + const data = await getClipboardContent(event); + if (data.elements) { + this.addElementsFromPaste(data.elements); + } else if (data.text) { + this.addTextFromPaste(data.text); + } + this.selectShapeTool("selection"); + event?.preventDefault(); }, ); @@ -639,6 +615,35 @@ export class App extends React.Component { }); }; + private addTextFromPaste(text: any) { + const { x, y } = viewportCoordsToSceneCoords( + { clientX: cursorX, clientY: cursorY }, + this.state, + this.canvas, + window.devicePixelRatio, + ); + + const element = newTextElement({ + x: x, + y: y, + strokeColor: this.state.currentItemStrokeColor, + backgroundColor: this.state.currentItemBackgroundColor, + fillStyle: this.state.currentItemFillStyle, + strokeWidth: this.state.currentItemStrokeWidth, + roughness: this.state.currentItemRoughness, + opacity: this.state.currentItemOpacity, + text: text, + font: this.state.currentItemFont, + }); + + globalSceneState.replaceAllElements([ + ...globalSceneState.getAllElements(), + element, + ]); + this.setState({ selectedElementIds: { [element.id]: true } }); + history.resumeRecording(); + } + // Collaboration setAppState = (obj: any) => {