feat: Support subtypes for ExcalidrawTextElement

This commit is contained in:
Daniel J. Geiger
2023-10-14 17:46:57 -05:00
parent 44d9d5fcac
commit 7958b7144a
12 changed files with 830 additions and 74 deletions
+25
View File
@@ -16,6 +16,14 @@ import util from "util";
import path from "path";
import { getMimeType } from "../../data/blob";
import {
SubtypeLoadedCb,
SubtypePrepFn,
SubtypeRecord,
checkRefreshOnSubtypeLoad,
prepareSubtype,
} from "../../element/subtypes";
import {
maybeGetSubtypeProps,
newEmbeddableElement,
newFrameElement,
newFreeDrawElement,
@@ -32,6 +40,16 @@ const readFile = util.promisify(fs.readFile);
const { h } = window;
export class API {
static addSubtype = (record: SubtypeRecord, subtypePrepFn: SubtypePrepFn) => {
const subtypeLoadedCb: SubtypeLoadedCb = (hasSubtype) => {
if (checkRefreshOnSubtypeLoad(hasSubtype, h.elements)) {
h.app.refresh();
}
};
const prep = prepareSubtype(record, subtypePrepFn, subtypeLoadedCb);
return prep;
};
static setSelectedElements = (elements: ExcalidrawElement[]) => {
h.setState({
selectedElementIds: elements.reduce((acc, element) => {
@@ -113,6 +131,8 @@ export class API {
verticalAlign?: T extends "text"
? ExcalidrawTextElement["verticalAlign"]
: never;
subtype?: ExcalidrawElement["subtype"];
customData?: ExcalidrawElement["customData"];
boundElements?: ExcalidrawGenericElement["boundElements"];
containerId?: T extends "text"
? ExcalidrawTextElement["containerId"]
@@ -141,6 +161,10 @@ export class API {
const appState = h?.state || getDefaultAppState();
const custom = maybeGetSubtypeProps({
subtype: rest.subtype,
customData: rest.customData,
});
const base: Omit<
ExcalidrawGenericElement,
| "id"
@@ -155,6 +179,7 @@ export class API {
| "link"
| "updated"
> = {
...custom,
x,
y,
frameId: rest.frameId ?? null,