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

Content Model: keep default format when paste into empty editor #2232

Merged
merged 2 commits into from
Dec 1, 2023
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
@@ -1,5 +1,9 @@
import { createDomToModelContext, domToContentModel } from 'roosterjs-content-model-dom';
import type { ContentModelDocument, DomToModelOption } from 'roosterjs-content-model-types';
import type {
ContentModelDocument,
ContentModelSegmentFormat,
DomToModelOption,
} from 'roosterjs-content-model-types';
import type { TrustedHTMLHandler } from 'roosterjs-editor-types';

/**
Expand All @@ -12,11 +16,20 @@ import type { TrustedHTMLHandler } from 'roosterjs-editor-types';
export function createModelFromHtml(
html: string,
options?: DomToModelOption,
trustedHTMLHandler?: TrustedHTMLHandler
trustedHTMLHandler?: TrustedHTMLHandler,
defaultSegmentFormat?: ContentModelSegmentFormat
): ContentModelDocument | undefined {
const doc = new DOMParser().parseFromString(trustedHTMLHandler?.(html) ?? html, 'text/html');

return doc?.body
? domToContentModel(doc.body, createDomToModelContext(undefined /*editorContext*/, options))
? domToContentModel(
doc.body,
createDomToModelContext(
{
defaultFormat: defaultSegmentFormat,
},
options
)
)
: undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ export function paste(
}

if (originalFormat) {
context.newPendingFormat = { ...EmptySegmentFormat, ...originalFormat }; // Use empty format as initial value to clear any other format inherits from pasted content
context.newPendingFormat = {
...EmptySegmentFormat,
...model.format,
...originalFormat,
}; // Use empty format as initial value to clear any other format inherits from pasted content
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export function createEditorCore(
options.initialModel = createModelFromHtml(
initContent,
options.defaultDomToModelOptions,
options.trustedHTMLHandler
options.trustedHTMLHandler,
options.defaultSegmentFormat
);
}

Expand Down
Loading