From d08154df2426cef415dfc4b8a17f43037ff1418a Mon Sep 17 00:00:00 2001 From: pkliesch Date: Thu, 24 Aug 2023 10:13:39 +0200 Subject: [PATCH] feature(bbcode-dataprocessor): Fix falsy EditorConfig types The EditorConfig type extensions in some of our augmentation files were marked as mandatory. This is false. TypeScript would complain about insufficient configs if these plugins were not configured. (Which is totally fine) Therefore, these config extensions are now marked as optional. Also, the configuration of the CoreMedia RichText plugin was now moved into a separate function. TypeScript was not happy with compatibility and rules being set independently because these values have to match in order to represent a certain config type (v10 or latest). Compatibility is now also set explicitly to make sure the value matches the given type. (type expects "v10" or "latest" and the value was of type "string | true") --- app/src/editors/default.ts | 34 +++++++++++++------ .../src/augmentation.ts | 2 +- .../src/augmentation.ts | 2 +- .../ckeditor5-font-mapper/src/augmentation.ts | 2 +- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/app/src/editors/default.ts b/app/src/editors/default.ts index 93fa0f3328..abdab30089 100644 --- a/app/src/editors/default.ts +++ b/app/src/editors/default.ts @@ -55,6 +55,10 @@ import { COREMEDIA_LINK_CONFIG_KEY } from "@coremedia/ckeditor5-coremedia-link/s import { LinkAttributesConfig } from "@coremedia/ckeditor5-link-common/src/LinkAttributesConfig"; import { LinkAttributes } from "@coremedia/ckeditor5-link-common/src/LinkAttributes"; import { Differencing } from "@coremedia/ckeditor5-coremedia-differencing"; +import type { + LatestCoreMediaRichTextConfig, + V10CoreMediaRichTextConfig, +} from "@coremedia/ckeditor5-coremedia-richtext"; /** * Typings for CKEditorInspector, as it does not ship with typings yet. */ @@ -133,6 +137,25 @@ const linkAttributesConfig: LinkAttributesConfig = getHashParam("skipLinkAttribu const editorElementSelector = "#editor"; +const getRichTextConfig = ( + richTextCompatibility: string | true +): Partial | V10CoreMediaRichTextConfig => { + // Use v10 for first data-processor architecture, for example. + if (richTextCompatibility === "v10") { + return { + // Defaults to: Loose + strictness: Strictness.STRICT, + compatibility: "v10", + rules: v10RichTextRuleConfigurations, + }; + } + return { + strictness: Strictness.STRICT, + compatibility: "latest", + rules: richTextRuleConfigurations, + }; +}; + export const createDefaultEditor = (language = "en") => { const sourceElement = document.querySelector(editorElementSelector) as HTMLElement; if (!sourceElement) { @@ -331,16 +354,7 @@ export const createDefaultEditor = (language = "en") => { }); }, }, - [COREMEDIA_RICHTEXT_CONFIG_KEY]: { - // Defaults to: Loose - strictness: Strictness.STRICT, - // The Latest is the default. Use v10 for first data-processor architecture, - // for example. - // @ts-expect-error - TODO[cke] 37.x Fix Typings - compatibility: richTextCompatibility, - //@ts-expect-error the types do not match here rules may not be RuleConfig[] TODO - rules: richTextCompatibility === "v10" ? v10RichTextRuleConfigurations : richTextRuleConfigurations, - }, + [COREMEDIA_RICHTEXT_CONFIG_KEY]: getRichTextConfig(richTextCompatibility), [COREMEDIA_RICHTEXT_SUPPORT_CONFIG_KEY]: { aliases: [ // As we represent `` as ``, we must ensure, diff --git a/packages/ckeditor5-coremedia-richtext-support/src/augmentation.ts b/packages/ckeditor5-coremedia-richtext-support/src/augmentation.ts index 7d950ed806..f445a33293 100644 --- a/packages/ckeditor5-coremedia-richtext-support/src/augmentation.ts +++ b/packages/ckeditor5-coremedia-richtext-support/src/augmentation.ts @@ -12,7 +12,7 @@ declare module "@ckeditor/ckeditor5-core" { * if you do not want to get Markup cleaned up from elements or attributes * that are not supported in the editor instance. */ - [COREMEDIA_RICHTEXT_SUPPORT_CONFIG_KEY]: CoreMediaRichTextSupportConfig; + [COREMEDIA_RICHTEXT_SUPPORT_CONFIG_KEY]?: CoreMediaRichTextSupportConfig; } interface PluginsMap { diff --git a/packages/ckeditor5-coremedia-richtext/src/augmentation.ts b/packages/ckeditor5-coremedia-richtext/src/augmentation.ts index b4cd631b01..f1b27bd603 100644 --- a/packages/ckeditor5-coremedia-richtext/src/augmentation.ts +++ b/packages/ckeditor5-coremedia-richtext/src/augmentation.ts @@ -7,7 +7,7 @@ declare module "@ckeditor/ckeditor5-core" { * if you do not want to get Markup cleaned up from elements or attributes * that are not supported in the editor instance. */ - [COREMEDIA_RICHTEXT_CONFIG_KEY]: CoreMediaRichTextConfig; + [COREMEDIA_RICHTEXT_CONFIG_KEY]?: CoreMediaRichTextConfig; } interface PluginsMap { diff --git a/packages/ckeditor5-font-mapper/src/augmentation.ts b/packages/ckeditor5-font-mapper/src/augmentation.ts index 3021f7d418..089524f4eb 100644 --- a/packages/ckeditor5-font-mapper/src/augmentation.ts +++ b/packages/ckeditor5-font-mapper/src/augmentation.ts @@ -2,7 +2,7 @@ import { type FontMapper, type FontMapperConfig, COREMEDIA_FONT_MAPPER_CONFIG_KE declare module "@ckeditor/ckeditor5-core" { interface EditorConfig { - [COREMEDIA_FONT_MAPPER_CONFIG_KEY]: FontMapperConfig; + [COREMEDIA_FONT_MAPPER_CONFIG_KEY]?: FontMapperConfig; } interface PluginsMap {