Skip to content

Commit

Permalink
Content Model: keep default format when paste into empty editor (#2232)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong authored Dec 1, 2023
1 parent 5520f04 commit e014196
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
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

0 comments on commit e014196

Please sign in to comment.