actionStyles.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Action } from "./types";
  2. import { isTextElement, redrawTextBoundingBox } from "../element";
  3. import { KEYS } from "../keys";
  4. let copiedStyles: string = "{}";
  5. export const actionCopyStyles: Action = {
  6. name: "copyStyles",
  7. perform: elements => {
  8. const element = elements.find(el => el.isSelected);
  9. if (element) {
  10. copiedStyles = JSON.stringify(element);
  11. }
  12. return {};
  13. },
  14. contextItemLabel: "labels.copyStyles",
  15. keyTest: event => event[KEYS.META] && event.shiftKey && event.key === "C",
  16. contextMenuOrder: 0,
  17. };
  18. export const actionPasteStyles: Action = {
  19. name: "pasteStyles",
  20. perform: elements => {
  21. const pastedElement = JSON.parse(copiedStyles);
  22. return {
  23. elements: elements.map(element => {
  24. if (element.isSelected) {
  25. const newElement = {
  26. ...element,
  27. shape: null,
  28. backgroundColor: pastedElement?.backgroundColor,
  29. strokeWidth: pastedElement?.strokeWidth,
  30. strokeColor: pastedElement?.strokeColor,
  31. fillStyle: pastedElement?.fillStyle,
  32. opacity: pastedElement?.opacity,
  33. roughness: pastedElement?.roughness,
  34. };
  35. if (isTextElement(newElement)) {
  36. newElement.font = pastedElement?.font;
  37. redrawTextBoundingBox(newElement);
  38. }
  39. return newElement;
  40. }
  41. return element;
  42. }),
  43. };
  44. },
  45. contextItemLabel: "labels.pasteStyles",
  46. keyTest: event => event[KEYS.META] && event.shiftKey && event.key === "V",
  47. contextMenuOrder: 1,
  48. };