Skip to content

Commit

Permalink
feat: 解决activeObj无法被监听的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhcc365 committed Jul 19, 2024
1 parent 91e6c3f commit ac60bc1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/pages/EditPage/components/Tools/Tools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ export const TextTools = observer(() => {
onChange={handleFontChange}
size="small"
/>

<div>{(store.activeObj as any)?.fontFamily}</div>
<Select
style={{ width: 75 }}
value={(store.activeObj as any)?.fontSize}
Expand Down Expand Up @@ -537,7 +535,7 @@ export enum ToolType {
delete = "delete",
layer = "layer",
fillcolor = "fillcolor",
text = "text",
text = "textbox",
stokeStyle = "stokeStyle",
setGroup = "setGroup",
}
Expand Down
22 changes: 18 additions & 4 deletions src/store/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { createContext } from "react";
import { makeObservable } from "mobx";

Expand Down Expand Up @@ -84,7 +85,7 @@ export class CanvasStore {
elementHover: string = "";

canvas!: Canvas;
activeObj: FabricObject | null = null;
activeObj: { [key: string]: any } | null = null;

showSafeLine: boolean = true;
showClipLine: boolean = true;
Expand All @@ -106,12 +107,25 @@ export class CanvasStore {
}

setActiveObj = (activeObj: FabricObject | FabricObject[] | null) => {
this.activeObj = Array.isArray(activeObj) ? activeObj[0] : activeObj;
if (!activeObj) {
this.activeObj = null;
} else {
const obj = Array.isArray(activeObj) ? activeObj[0] : activeObj;
const nextObj = {};
Object.keys(obj).map((key) => {
if (typeof obj[key] !== "function" && typeof obj[key] !== "object") {
nextObj[key] = obj[key];
}
});
nextObj.type = obj?.type;

this.activeObj = nextObj;
}
};

setActiveObjParam = (key: string | Record<string, any>, value?: any) => {
setActiveObjParam = (key: string, value?: any) => {
this.canvas?.getActiveObject()?.set({ [key as any]: value });
this.activeObj?.set({ [key as any]: value });
this.activeObj[key] = value;
this.canvas?.renderAll();
};

Expand Down

0 comments on commit ac60bc1

Please sign in to comment.