Skip to content

Commit

Permalink
feature(bbcode-dataprocessor): Fix falsy EditorConfig types
Browse files Browse the repository at this point in the history
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")
  • Loading branch information
pkliesch committed Aug 24, 2023
1 parent 10006f5 commit d08154d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
34 changes: 24 additions & 10 deletions app/src/editors/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -133,6 +137,25 @@ const linkAttributesConfig: LinkAttributesConfig = getHashParam("skipLinkAttribu

const editorElementSelector = "#editor";

const getRichTextConfig = (
richTextCompatibility: string | true
): Partial<LatestCoreMediaRichTextConfig> | 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) {
Expand Down Expand Up @@ -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 `<mark>` as `<span class="mark">`, we must ensure,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-coremedia-richtext/src/augmentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-font-mapper/src/augmentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d08154d

Please sign in to comment.