2023-10-12 05:40:11 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import { fabric } from "fabric";
|
2023-11-25 07:05:45 +01:00
|
|
|
import { opacityStateStore, textEditingState } from "image-occlusion/store";
|
2023-11-24 05:06:40 +01:00
|
|
|
import { get } from "svelte/store";
|
2023-10-12 05:40:11 +02:00
|
|
|
|
2023-10-25 01:23:47 +02:00
|
|
|
import { enableUniformScaling, stopDraw, TEXT_BACKGROUND_COLOR, TEXT_FONT_FAMILY, TEXT_PADDING } from "./lib";
|
2023-10-12 05:40:11 +02:00
|
|
|
import { undoStack } from "./tool-undo-redo";
|
|
|
|
|
|
|
|
export const drawText = (canvas: fabric.Canvas): void => {
|
|
|
|
canvas.selectionColor = "rgba(0, 0, 0, 0)";
|
|
|
|
stopDraw(canvas);
|
|
|
|
|
|
|
|
canvas.on("mouse:down", function(o) {
|
|
|
|
if (o.target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const pointer = canvas.getPointer(o.e);
|
|
|
|
const text = new fabric.IText("text", {
|
|
|
|
id: "text-" + new Date().getTime(),
|
|
|
|
left: pointer.x,
|
|
|
|
top: pointer.y,
|
|
|
|
originX: "left",
|
|
|
|
originY: "top",
|
|
|
|
selectable: true,
|
|
|
|
strokeWidth: 1,
|
|
|
|
noScaleCache: false,
|
|
|
|
fontFamily: TEXT_FONT_FAMILY,
|
2023-10-13 00:49:31 +02:00
|
|
|
backgroundColor: TEXT_BACKGROUND_COLOR,
|
2023-10-12 05:40:11 +02:00
|
|
|
padding: TEXT_PADDING,
|
2023-11-24 05:06:40 +01:00
|
|
|
opacity: get(opacityStateStore) ? 0.4 : 1,
|
2023-10-12 05:40:11 +02:00
|
|
|
});
|
|
|
|
enableUniformScaling(canvas, text);
|
|
|
|
canvas.add(text);
|
2023-10-13 01:58:43 +02:00
|
|
|
canvas.setActiveObject(text);
|
2023-10-12 05:40:11 +02:00
|
|
|
undoStack.onObjectAdded(text.id);
|
2023-10-28 02:25:56 +02:00
|
|
|
text.enterEditing();
|
|
|
|
text.selectAll();
|
2023-10-12 05:40:11 +02:00
|
|
|
});
|
2023-11-25 07:05:45 +01:00
|
|
|
|
|
|
|
canvas.on("text:editing:entered", function() {
|
|
|
|
textEditingState.set(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
canvas.on("text:editing:exited", function() {
|
|
|
|
textEditingState.set(false);
|
|
|
|
});
|
2023-10-12 05:40:11 +02:00
|
|
|
};
|