Anki/ts/image-occlusion/tools/api.ts
Aristotelis 56f7d54900
Add APIs for IO mask editing (#2758)
* Add simple mask editor add-on API

* Signal completed mask editor image loading to Python

* Add API methods for querying mask editor state, fix formatting

* Use event forwarding to propagate image loaded event

Should fix mobile support by moving all bridgeCommand calls to `NoteEditor.svelte`

* Add shape classes to mask editor API

---------

Co-authored-by: Glutanimate <glutanimate@users.noreply.github.com>
2023-10-22 10:40:40 +10:00

54 lines
1.4 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import type { fabric } from "fabric";
import { baseShapesFromFabric, exportShapesToClozeDeletions } from "image-occlusion/shapes/to-cloze";
import type { ShapeOrShapes } from "../shapes";
import { Ellipse, Polygon, Rectangle, Shape, Text } from "../shapes";
import { addShape, addShapeGroup } from "./from-shapes";
import { clear, redraw } from "./lib";
interface ClozeExportResult {
clozes: string;
cardCount: number;
}
export class MaskEditorAPI {
readonly Shape = Shape;
readonly Rectangle = Rectangle;
readonly Ellipse = Ellipse;
readonly Polygon = Polygon;
readonly Text = Text;
readonly canvas: fabric.Canvas;
constructor(canvas) {
this.canvas = canvas;
}
addShape(shape: Shape): void {
addShape(this.canvas, shape);
}
addShapeGroup(shapes: Shape[]): void {
addShapeGroup(this.canvas, shapes);
}
getClozes(occludeInactive: boolean): ClozeExportResult {
const { clozes, noteCount: cardCount } = exportShapesToClozeDeletions(occludeInactive);
return { clozes, cardCount };
}
getShapes(occludeInactive: boolean): ShapeOrShapes[] {
return baseShapesFromFabric(occludeInactive);
}
redraw(): void {
redraw(this.canvas);
}
clear(): void {
clear(this.canvas);
}
}