画布填充颜色不能更改和撤回问题
This commit is contained in:
@@ -42,13 +42,24 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
this.firstObj = null; // 用于存储组图层的原始对象
|
this.firstObj = null; // 用于存储组图层的原始对象
|
||||||
}
|
}
|
||||||
|
|
||||||
async execute() {
|
async execute(isUndo = false) {
|
||||||
const layer = this.layer;
|
const { layer, parent } = findLayerRecursively(
|
||||||
|
this.layers.value,
|
||||||
|
this.layerId
|
||||||
|
);
|
||||||
|
this.layer = layer;
|
||||||
|
this.parent = parent;
|
||||||
if (!layer) return false;
|
if (!layer) return false;
|
||||||
|
|
||||||
this.oldFill = layer.fill ?? null;
|
if(!isUndo){
|
||||||
this.oldFillColor = layer.oldFillColor ?? null;
|
this.oldFill = layer.fill ?? null;
|
||||||
|
this.oldFillColor = layer.fillColor ?? null;
|
||||||
|
if(this.oldFill){
|
||||||
|
// 移除旧的填充对象
|
||||||
|
removeCanvasObjectByObject(this.canvas, this.oldFill);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const fillColor = isUndo ? this.oldFillColor : this.fillColor;
|
||||||
// 构建填充对象
|
// 构建填充对象
|
||||||
let clippingMaskFabricObject = null;
|
let clippingMaskFabricObject = null;
|
||||||
if (layer.clippingMask) {
|
if (layer.clippingMask) {
|
||||||
@@ -63,7 +74,7 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
height: clippingMaskFabricObject.height,
|
height: clippingMaskFabricObject.height,
|
||||||
left: clippingMaskFabricObject.left || 0,
|
left: clippingMaskFabricObject.left || 0,
|
||||||
top: clippingMaskFabricObject.top || 0,
|
top: clippingMaskFabricObject.top || 0,
|
||||||
fill: this.fillColor,
|
fill: fillColor,
|
||||||
layerId: this.layerId,
|
layerId: this.layerId,
|
||||||
id: this.oldFill?.id || generateId("fill-"),
|
id: this.oldFill?.id || generateId("fill-"),
|
||||||
selectable: false,
|
selectable: false,
|
||||||
@@ -84,7 +95,7 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
height: originalInfo.height,
|
height: originalInfo.height,
|
||||||
left: originalInfo.left + originalInfo.width / 2 || 0,
|
left: originalInfo.left + originalInfo.width / 2 || 0,
|
||||||
top: originalInfo.top + originalInfo.height / 2 || 0,
|
top: originalInfo.top + originalInfo.height / 2 || 0,
|
||||||
fill: this.fillColor,
|
fill: fillColor,
|
||||||
layerId: this.layerId,
|
layerId: this.layerId,
|
||||||
id: this.oldFill?.id || generateId("fill-"),
|
id: this.oldFill?.id || generateId("fill-"),
|
||||||
selectable: false,
|
selectable: false,
|
||||||
@@ -111,7 +122,7 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
height: clippingMaskFabricObject.height,
|
height: clippingMaskFabricObject.height,
|
||||||
left: clippingMaskFabricObject.left || 0,
|
left: clippingMaskFabricObject.left || 0,
|
||||||
top: clippingMaskFabricObject.top || 0,
|
top: clippingMaskFabricObject.top || 0,
|
||||||
fill: this.fillColor,
|
fill: fillColor,
|
||||||
layerId: this.layerId,
|
layerId: this.layerId,
|
||||||
id: this.oldFill?.id || generateId("fill-"),
|
id: this.oldFill?.id || generateId("fill-"),
|
||||||
selectable: false,
|
selectable: false,
|
||||||
@@ -127,7 +138,7 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
height: this.canvasManager?.canvasHeight?.value || this.canvas.height,
|
height: this.canvasManager?.canvasHeight?.value || this.canvas.height,
|
||||||
left: this.canvas.width / 2 || 0,
|
left: this.canvas.width / 2 || 0,
|
||||||
top: this.canvas.height / 2 || 0,
|
top: this.canvas.height / 2 || 0,
|
||||||
fill: this.fillColor,
|
fill: fillColor,
|
||||||
layerId: this.layerId,
|
layerId: this.layerId,
|
||||||
id: this.oldFill?.id || generateId("fill-"),
|
id: this.oldFill?.id || generateId("fill-"),
|
||||||
selectable: false,
|
selectable: false,
|
||||||
@@ -143,8 +154,8 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
layer.fabricObjects = [
|
layer.fabricObjects = [
|
||||||
this.newFill.toObject(["id", "layerId"]) || this.newFill,
|
this.newFill.toObject(["id", "layerId"]) || this.newFill,
|
||||||
];
|
];
|
||||||
layer.fill = null; // this.newFill.toObject(["id", "layerId"]);
|
layer.fill = this.newFill; // this.newFill.toObject(["id", "layerId"]);
|
||||||
layer.fillColor = this.fillColor;
|
layer.fillColor = fillColor;
|
||||||
// 取消激活对象
|
// 取消激活对象
|
||||||
|
|
||||||
this.canvas.discardActiveObject(); // 取消当前活动对象
|
this.canvas.discardActiveObject(); // 取消当前活动对象
|
||||||
@@ -222,8 +233,8 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
// this.group?.addWithUpdate?.();
|
// this.group?.addWithUpdate?.();
|
||||||
// layer.fabricObjects = [this.group?.toObject?.(["id", "layerId"]) || this.group];
|
// layer.fabricObjects = [this.group?.toObject?.(["id", "layerId"]) || this.group];
|
||||||
// this.canvas.renderAll();
|
// this.canvas.renderAll();
|
||||||
layer.fill = null; // this.newFill.toObject(["id", "layerId"]);
|
layer.fill = this.newFill; // this.newFill.toObject(["id", "layerId"]);
|
||||||
layer.fillColor = this.fillColor;
|
layer.fillColor = fillColor;
|
||||||
// 取消激活对象
|
// 取消激活对象
|
||||||
|
|
||||||
this.canvas.discardActiveObject(); // 取消当前活动对象
|
this.canvas.discardActiveObject(); // 取消当前活动对象
|
||||||
@@ -237,14 +248,11 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
async undo() {
|
async undo() {
|
||||||
this.layer.fillColor = this.oldFillColor;
|
if (!this.originalInfo && this.layer.fill) {
|
||||||
this.layer.fill = this.oldFill;
|
|
||||||
|
|
||||||
if (!this.originalInfo && this.firstObj) {
|
|
||||||
this.canvas.discardActiveObject();
|
this.canvas.discardActiveObject();
|
||||||
this.canvas.remove(this.firstObj);
|
this.canvas.remove(this.layer.fill);
|
||||||
this.canvas.renderAll();
|
this.canvas.renderAll();
|
||||||
this.canvasManager.thumbnailManager?.generateLayerThumbnail(
|
this.parent && this.canvasManager.thumbnailManager?.generateLayerThumbnail(
|
||||||
this.parent.id
|
this.parent.id
|
||||||
);
|
);
|
||||||
this.canvasManager.thumbnailManager?.generateLayerThumbnail(
|
this.canvasManager.thumbnailManager?.generateLayerThumbnail(
|
||||||
@@ -264,7 +272,8 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
canvasObj?._objects?.length > 0
|
canvasObj?._objects?.length > 0
|
||||||
) {
|
) {
|
||||||
// 移除新添加的填充对象
|
// 移除新添加的填充对象
|
||||||
if (canvasObj._objects?.[0] === this.newFill) {
|
// if (canvasObj._objects?.[0] === this.newFill) {
|
||||||
|
if (/^fill-/.test(canvasObj._objects?.[0]?.id)) {
|
||||||
canvasObj._objects.shift();
|
canvasObj._objects.shift();
|
||||||
canvasObj.addWithUpdate();
|
canvasObj.addWithUpdate();
|
||||||
canvasObj.setCoords();
|
canvasObj.setCoords();
|
||||||
@@ -284,6 +293,14 @@ export class FillGroupLayerBackgroundCommand extends Command {
|
|||||||
this.canvas.renderAll();
|
this.canvas.renderAll();
|
||||||
this.group = null;
|
this.group = null;
|
||||||
}
|
}
|
||||||
|
if(this.oldFill){
|
||||||
|
this.layer.fill = this.oldFill;
|
||||||
|
this.layer.fillColor = this.oldFillColor;
|
||||||
|
return this.execute(true);
|
||||||
|
}else{
|
||||||
|
this.layer.fill = null;
|
||||||
|
this.layer.fillColor = null;
|
||||||
|
}
|
||||||
|
|
||||||
this.canvas.discardActiveObject(); // 取消当前活动对象
|
this.canvas.discardActiveObject(); // 取消当前活动对象
|
||||||
// 重新排序
|
// 重新排序
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { generateId, optimizeCanvasRendering } from "../utils/helper";
|
import { generateId, optimizeCanvasRendering, getLayerObjectsZIndex } from "../utils/helper";
|
||||||
import { createLayer, LayerType, OperationType } from "../utils/layerHelper";
|
import { createLayer, LayerType, OperationType } from "../utils/layerHelper";
|
||||||
import { Command } from "./Command";
|
import { Command } from "./Command";
|
||||||
import i18n from "@/lang/index.ts";
|
import i18n from "@/lang/index.ts";
|
||||||
@@ -536,6 +536,12 @@ export class CreateTextCommand extends Command {
|
|||||||
if (this.textObject && this.canvas) {
|
if (this.textObject && this.canvas) {
|
||||||
this.canvas.remove(this.textObject);
|
this.canvas.remove(this.textObject);
|
||||||
}
|
}
|
||||||
|
const layerObjects = getLayerObjectsZIndex(this.canvas, this.layerId);
|
||||||
|
layerObjects.forEach((obj) => {
|
||||||
|
if (obj.id === this.textObject?.id) {
|
||||||
|
this.canvas.remove(obj.object);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 智能移除创建的图层
|
// 智能移除创建的图层
|
||||||
if (this.layerId && this.layerManager) {
|
if (this.layerId && this.layerManager) {
|
||||||
|
|||||||
Reference in New Issue
Block a user