Skip to content

Commit

Permalink
Allow to update pasteDomToModelOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanValverdeU committed Dec 10, 2024
1 parent 0d6f734 commit 5d0c2ad
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import type {
IEditor,
} from 'roosterjs-content-model-types';

const emptyDomToModelOption: Readonly<DomToModelOptionForSanitizing> = {
additionalAllowedTags: [],
additionalDisallowedTags: [],
additionalFormatParsers: {},
formatParserOverride: {},
processorOverride: {},
styleSanitizers: {},
attributeSanitizers: {},
};

/**
* @internal
*/
Expand All @@ -15,18 +25,9 @@ export function generatePasteOptionFromPlugins(
clipboardData: ClipboardData,
fragment: DocumentFragment,
htmlFromClipboard: HtmlFromClipboard,
pasteType: PasteType
pasteType: PasteType,
domToModelOption: Readonly<Partial<DomToModelOptionForSanitizing>>
): BeforePasteEvent {
const domToModelOption: DomToModelOptionForSanitizing = {
additionalAllowedTags: [],
additionalDisallowedTags: [],
additionalFormatParsers: {},
formatParserOverride: {},
processorOverride: {},
styleSanitizers: {},
attributeSanitizers: {},
};

const event: BeforePasteEvent = {
eventType: 'beforePaste',
clipboardData,
Expand All @@ -35,7 +36,7 @@ export function generatePasteOptionFromPlugins(
htmlAfter: htmlFromClipboard.htmlAfter ?? '',
htmlAttributes: htmlFromClipboard.metadata,
pasteType: pasteType,
domToModelOption,
domToModelOption: Object.assign({}, emptyDomToModelOption, domToModelOption),
containsBlockElements: !!htmlFromClipboard.containsBlockElements,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
ClipboardData,
TrustedHTMLHandler,
IEditor,
DomToModelOptionForSanitizing,
} from 'roosterjs-content-model-types';

/**
Expand All @@ -19,7 +20,8 @@ import type {
export function paste(
editor: IEditor,
clipboardData: ClipboardData,
pasteTypeOrGetter: PasteTypeOrGetter = 'normal'
pasteTypeOrGetter: PasteTypeOrGetter = 'normal',
pasteDomToModelOption: Readonly<Partial<DomToModelOptionForSanitizing>> = {}
) {
editor.focus();

Expand Down Expand Up @@ -60,7 +62,8 @@ export function paste(
clipboardData,
sourceFragment,
htmlFromClipboard,
pasteType
pasteType,
pasteDomToModelOption
);

// 5. Convert global CSS to inline CSS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class CopyPastePlugin implements PluginWithState<CopyPastePluginState> {
allowedCustomPasteType: option.allowedCustomPasteType || [],
tempDiv: null,
defaultPasteType: option.defaultPasteType,
pasteOptionDomToModel: option.pasteOptionDomToModel,
};
}

Expand Down Expand Up @@ -205,7 +206,12 @@ class CopyPastePlugin implements PluginWithState<CopyPastePluginState> {
this.state.allowedCustomPasteType
).then((clipboardData: ClipboardData) => {
if (!editor.isDisposed()) {
paste(editor, clipboardData, this.state.defaultPasteType);
paste(
editor,
clipboardData,
this.state.defaultPasteType,
this.state.pasteOptionDomToModel
);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Colors, ColorTransformFunction } from '../context/DarkColorHandler
import type { EditorPlugin } from './EditorPlugin';
import type { ContentModelSegmentFormat } from '../contentModel/format/ContentModelSegmentFormat';
import type { CoreApiMap } from './EditorCore';
import type { DomToModelOption } from '../context/DomToModelOption';
import type { DomToModelOption, DomToModelOptionForSanitizing } from '../context/DomToModelOption';
import type { ModelToDomOption } from '../context/ModelToDomOption';
import type {
ContentModelDocument,
Expand Down Expand Up @@ -82,6 +82,11 @@ export interface ContentModelOptions {
* @deprecated
*/
disableCache?: boolean;

/**
* Paste options for sanitizing HTML before paste
*/
pasteOptionDomToModel?: Partial<DomToModelOptionForSanitizing>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DomToModelOptionForSanitizing } from '../context/DomToModelOption';
import type { PasteTypeOrGetter } from '../parameter/PasteTypeOrGetter';

/**
Expand All @@ -19,4 +20,9 @@ export interface CopyPastePluginState {
* Default paste type. By default will use the normal (as-is) paste type.
*/
defaultPasteType?: PasteTypeOrGetter;

/**
* Paste options for sanitizing HTML before paste
*/
pasteOptionDomToModel?: Partial<DomToModelOptionForSanitizing>;
}

0 comments on commit 5d0c2ad

Please sign in to comment.