From c456c1e7138c2d0b8660fa6da2213d9035029f04 Mon Sep 17 00:00:00 2001 From: "Daniel J. Geiger" <1852529+DanielJGeiger@users.noreply.github.com> Date: Fri, 3 Nov 2023 19:12:46 -0500 Subject: [PATCH] fix: Only expose `registerAction` instead of `ActionManager` in the API --- src/components/App.tsx | 12 +++++------- src/element/subtypes/index.ts | 3 +++ src/tests/helpers/api.ts | 8 -------- src/types.ts | 2 +- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 2795f1b4e..49e7ab8a7 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -40,7 +40,7 @@ import { import { createRedoAction, createUndoAction } from "../actions/actionHistory"; import { ActionManager } from "../actions/manager"; import { actions } from "../actions/register"; -import { ActionResult } from "../actions/types"; +import { Action, ActionResult } from "../actions/types"; import { trackEvent } from "../analytics"; import { getDefaultAppState, @@ -601,7 +601,9 @@ class App extends React.Component { getSceneElements: this.getSceneElements, getAppState: () => this.state, getFiles: () => this.files, - actionManager: this.actionManager, + registerAction: (action: Action) => { + this.actionManager.registerAction(action); + }, addSubtype: this.addSubtype, refresh: this.refresh, setToast: this.setToast, @@ -649,11 +651,7 @@ class App extends React.Component { this.refresh(); } }; - const prep = prepareSubtype(record, subtypePrepFn, subtypeLoadedCb); - if (prep.actions) { - this.actionManager.registerAll(prep.actions); - } - return prep; + return prepareSubtype(record, subtypePrepFn, subtypeLoadedCb); } private onWindowMessage(event: MessageEvent) { diff --git a/src/element/subtypes/index.ts b/src/element/subtypes/index.ts index 97bcee2a0..3b965843b 100644 --- a/src/element/subtypes/index.ts +++ b/src/element/subtypes/index.ts @@ -484,6 +484,9 @@ export const useSubtype = ( const prep = api.addSubtype(record, subtypePrepFn); if (prep) { addSubtypeMethods(record.subtype, prep.methods); + if (prep.actions) { + prep.actions.forEach((action) => api.registerAction(action)); + } } } }, [api, record, subtypePrepFn]); diff --git a/src/tests/helpers/api.ts b/src/tests/helpers/api.ts index 3c11ac0f4..925a981d0 100644 --- a/src/tests/helpers/api.ts +++ b/src/tests/helpers/api.ts @@ -22,7 +22,6 @@ import { checkRefreshOnSubtypeLoad, prepareSubtype, selectSubtype, - subtypeActionPredicate, } from "../../element/subtypes"; import { maybeGetSubtypeProps, @@ -42,13 +41,6 @@ const readFile = util.promisify(fs.readFile); const { h } = window; export class API { - constructor() { - h.app.actionManager.registerActionPredicate(subtypeActionPredicate); - if (true) { - // Call `prepareSubtype()` here for `@excalidraw/excalidraw`-specific subtypes - } - } - static addSubtype = (record: SubtypeRecord, subtypePrepFn: SubtypePrepFn) => { const subtypeLoadedCb: SubtypeLoadedCb = (hasSubtype) => { if (checkRefreshOnSubtypeLoad(hasSubtype, h.elements)) { diff --git a/src/types.ts b/src/types.ts index 9e95d8355..0d946c98b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -634,7 +634,7 @@ export type ExcalidrawImperativeAPI = { getSceneElements: InstanceType["getSceneElements"]; getAppState: () => InstanceType["state"]; getFiles: () => InstanceType["files"]; - actionManager: InstanceType["actionManager"]; + registerAction: (action: Action) => void; addSubtype: ( record: SubtypeRecord, subtypePrepFn: SubtypePrepFn,