Skip to content

Commit

Permalink
Refactor paste code (excalidraw#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
KentBeck authored Mar 28, 2020
1 parent 6056170 commit 95eaade
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -560,44 +560,20 @@ export class App extends React.Component<any, AppState> {
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();
},
);

Expand Down Expand Up @@ -639,6 +615,35 @@ export class App extends React.Component<any, AppState> {
});
};

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) => {
Expand Down

0 comments on commit 95eaade

Please sign in to comment.