Skip to content

Commit

Permalink
create type Pluginterface locally as it is not exposed anymore from c…
Browse files Browse the repository at this point in the history
…keditor package
  • Loading branch information
jens-meisner committed Nov 20, 2024
1 parent 9025a3a commit c47a0d9
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/ckeditor5-core-common/src/Plugins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Logger, LoggerProvider } from "@coremedia/ckeditor5-logging";
import { Editor, Plugin, PluginConstructor, PluginsMap } from "ckeditor5";
import { Editor, Plugin, PluginCollection, PluginConstructor, PluginsMap } from "ckeditor5";

const pluginsLogger: Logger = LoggerProvider.getLogger("Plugins");

Expand All @@ -12,8 +12,14 @@ const pluginsLogger: Logger = LoggerProvider.getLogger("Plugins");
*/
export type OnMissingPlugin = (pluginName: string) => void;

type PluginInterface = ReturnType<PluginCollection<Editor>["get"]>;

type PluginClassConstructor = typeof Plugin;

const hasPluginName = (obj: unknown): obj is { pluginName: string } =>
// @ts-expect-error PluginInterface is not exported from ckeditor package anymore
obj.pluginName !== undefined;

export function getOptionalPlugin<TConstructor extends PluginClassConstructor, TContext extends Editor = Editor>(
editor: TContext,
key: TConstructor,
Expand All @@ -38,25 +44,24 @@ export function getOptionalPlugin<TName extends string, TContext extends Editor
* is missing. Defaults to some generic message on not found plugin at debug
* level.
*/
export function getOptionalPlugin(
editor: Editor,
key: PluginConstructor<Editor> | string,
export function getOptionalPlugin<TKey, TContext extends Editor = Editor>(
editor: TContext,
key: TKey extends PluginClassConstructor ? TKey : TKey extends string ? TKey : never,
onMissing?: OnMissingPlugin,
) {
): PluginInterface | undefined {
const { plugins } = editor;
if (plugins.has(key)) {
if (typeof key === "string") {
return plugins.get(key);
} else {
// @ts-expect-error maybe we should change the type of key to string only
return plugins.get(key);
}
}
let pluginName: string;
if (typeof key === "string") {
pluginName = key;
} else {
pluginName = key.pluginName ?? key.name;
pluginName = hasPluginName(key) ? key.pluginName : key.name;
}
if (onMissing) {
onMissing(pluginName);
Expand Down

0 comments on commit c47a0d9

Please sign in to comment.