/** * Copyright (c) Tiny Technologies, Inc. All rights reserved. * Licensed under the LGPL or a commercial license. * For LGPL see License.txt in the project root for license information. * For commercial licenses see https://www.tiny.cloud/ */ import { AlloyComponent, AlloyEvents } from '@ephox/alloy'; import { Cell } from '@ephox/katamari'; export interface GetApiType { getApi: (comp: AlloyComponent) => T; } export type OnDestroy = (controlApi: T) => void; export interface OnControlAttachedType extends GetApiType { onSetup: (controlApi: T) => OnDestroy; // TODO: check: no change here? } const runWithApi = (info: GetApiType, comp: AlloyComponent) => { const api = info.getApi(comp); return (f: OnDestroy) => { f(api); }; }; const onControlAttached = (info: OnControlAttachedType, editorOffCell: Cell>) => { return AlloyEvents.runOnAttached((comp) => { const run = runWithApi(info, comp); run((api) => { const onDestroy = info.onSetup(api); if (onDestroy !== null && onDestroy !== undefined) { editorOffCell.set(onDestroy); } }); }); }; const onControlDetached = (getApi: GetApiType, editorOffCell: Cell>) => { return AlloyEvents.runOnDetached((comp) => runWithApi(getApi, comp)(editorOffCell.get())); }; export { runWithApi, onControlAttached, onControlDetached };