Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config for plugin #2717

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
EditorCore,
EditorCorePlugins,
EditorOptions,
DomToModelOption,
ModelToDomOption,
} from 'roosterjs-content-model-types';

/**
Expand All @@ -18,6 +20,20 @@ import type {
*/
export function createEditorCore(contentDiv: HTMLDivElement, options: EditorOptions): EditorCore {
const corePlugins = createEditorCorePlugins(options, contentDiv);
const plugins = (options.plugins ?? []).filter(x => !!x);
const domToModelOptions: DomToModelOption[] = [];
const modelToDomOptions: ModelToDomOption[] = [];

plugins.forEach(plugin => {
const contentModelConfig = plugin.getContentModelConfig?.();
if (contentModelConfig?.domToModelOption) {
domToModelOptions.push(contentModelConfig.domToModelOption);
}

if (contentModelConfig?.modelToDomOption) {
modelToDomOptions.push(contentModelConfig.modelToDomOption);
}
});

return {
physicalRoot: contentDiv,
Expand All @@ -31,12 +47,17 @@ export function createEditorCore(contentDiv: HTMLDivElement, options: EditorOpti
corePlugins.domEvent,
corePlugins.selection,
corePlugins.entity,
...(options.plugins ?? []).filter(x => !!x),
...plugins,
corePlugins.undo,
corePlugins.contextMenu,
corePlugins.lifecycle,
],
environment: createEditorEnvironment(contentDiv, options),
environment: createEditorEnvironment(
contentDiv,
options,
domToModelOptions,
modelToDomOptions
),
darkColorHandler: createDarkColorHandler(
contentDiv,
options.getDarkColor ?? getDarkColorFallback,
Expand All @@ -53,15 +74,17 @@ export function createEditorCore(contentDiv: HTMLDivElement, options: EditorOpti

function createEditorEnvironment(
contentDiv: HTMLElement,
options: EditorOptions
options: EditorOptions,
domToModelOptionsFromPlugins: (DomToModelOption | undefined)[],
modelToDomOptionsFromPlugins: (ModelToDomOption | undefined)[]
): EditorEnvironment {
const navigator = contentDiv.ownerDocument.defaultView?.navigator;
const userAgent = navigator?.userAgent ?? '';
const appVersion = navigator?.appVersion ?? '';

return {
domToModelSettings: createDomToModelSettings(options),
modelToDomSettings: createModelToDomSettings(options),
domToModelSettings: createDomToModelSettings(options, domToModelOptionsFromPlugins),
modelToDomSettings: createModelToDomSettings(options, modelToDomOptionsFromPlugins),
isMac: appVersion.indexOf('Mac') != -1,
isAndroid: /android/i.test(userAgent),
isSafari:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import type {
* @param options The editor options
*/
export function createDomToModelSettings(
options: EditorOptions
options: EditorOptions,
additionalOptions: (DomToModelOption | undefined)[]
): ContentModelSettings<DomToModelOption, DomToModelSettings> {
const builtIn: DomToModelOption = {
processorOverride: {
Expand All @@ -31,7 +32,7 @@ export function createDomToModelSettings(
return {
builtIn,
customized,
calculated: createDomToModelConfig([builtIn, customized]),
calculated: createDomToModelConfig([builtIn, customized, ...additionalOptions]),
};
}

Expand All @@ -41,7 +42,8 @@ export function createDomToModelSettings(
* @param options The editor options
*/
export function createModelToDomSettings(
options: EditorOptions
options: EditorOptions,
additionalOptions: (ModelToDomOption | undefined)[]
): ContentModelSettings<ModelToDomOption, ModelToDomSettings> {
const builtIn: ModelToDomOption = {
metadataAppliers: {
Expand All @@ -54,6 +56,6 @@ export function createModelToDomSettings(
return {
builtIn,
customized,
calculated: createModelToDomConfig([builtIn, customized]),
calculated: createModelToDomConfig([builtIn, customized, ...additionalOptions]),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ describe('createEditorCore', () => {
options,
contentDiv
);
expect(createDefaultSettings.createDomToModelSettings).toHaveBeenCalledWith(options);
expect(createDefaultSettings.createModelToDomSettings).toHaveBeenCalledWith(options);
expect(createDefaultSettings.createDomToModelSettings).toHaveBeenCalledWith(options, []);
expect(createDefaultSettings.createModelToDomSettings).toHaveBeenCalledWith(options, []);
}

it('No options', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('createDomToModelSettings', () => {
});

it('No options', () => {
const settings = createDomToModelSettings({});
const settings = createDomToModelSettings({}, []);

expect(settings).toEqual({
builtIn: {
Expand All @@ -43,9 +43,12 @@ describe('createDomToModelSettings', () => {

it('Has options', () => {
const defaultDomToModelOptions = 'MockedOptions' as any;
const settings = createDomToModelSettings({
defaultDomToModelOptions: defaultDomToModelOptions,
});
const settings = createDomToModelSettings(
{
defaultDomToModelOptions: defaultDomToModelOptions,
},
[]
);

expect(settings).toEqual({
builtIn: {
Expand Down Expand Up @@ -77,7 +80,7 @@ describe('createModelToDomSettings', () => {
});

it('No options', () => {
const settings = createModelToDomSettings({});
const settings = createModelToDomSettings({}, []);

expect(settings).toEqual({
builtIn: {
Expand All @@ -102,9 +105,12 @@ describe('createModelToDomSettings', () => {

it('Has options', () => {
const defaultModelToDomOptions = 'MockedOptions' as any;
const settings = createModelToDomSettings({
defaultModelToDomOptions: defaultModelToDomOptions,
});
const settings = createModelToDomSettings(
{
defaultModelToDomOptions: defaultModelToDomOptions,
},
[]
);

expect(settings).toEqual({
builtIn: {
Expand Down
23 changes: 23 additions & 0 deletions packages/roosterjs-content-model-types/lib/editor/EditorPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import type { DomToModelOption } from '../context/DomToModelOption';
import type { ModelToDomOption } from '../context/ModelToDomOption';
import type { PluginEvent } from '../event/PluginEvent';
import type { IEditor } from './IEditor';

/**
* Configuration for content model of a plugin
*/
export interface PluginContentModelConfig {
/**
* The option for additional format parses
*/
domToModelOption?: DomToModelOption;

/**
* The option for additional format appliers
*/
modelToDomOption?: ModelToDomOption;
}

/**
* Interface of an editor plugin
*/
Expand Down Expand Up @@ -42,4 +59,10 @@ export interface EditorPlugin {
* @param event The event to handle:
*/
onPluginEvent?: (event: PluginEvent) => void;

/**
* This configuration will add additional format parses and applier to the editor
* @returns The content model configuration for this plugin
*/
getContentModelConfig?: () => PluginContentModelConfig;
}
2 changes: 1 addition & 1 deletion packages/roosterjs-content-model-types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export {
Announce,
} from './editor/EditorCore';
export { EditorCorePlugins } from './editor/EditorCorePlugins';
export { EditorPlugin } from './editor/EditorPlugin';
export { EditorPlugin, PluginContentModelConfig } from './editor/EditorPlugin';
export { PluginWithState } from './editor/PluginWithState';
export { ContextMenuProvider } from './editor/ContextMenuProvider';

Expand Down
Loading