Skip to content

Commit

Permalink
Support Document Fragment in domToContentModel
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanValverdeU committed Apr 17, 2023
1 parent c5a1def commit db38f17
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { EditorContext } from '../publicTypes/context/EditorContext';
import { normalizeContentModel } from '../modelApi/common/normalizeContentModel';
import { parseFormat } from './utils/parseFormat';
import { rootDirectionFormatHandler } from '../formatHandlers/root/rootDirectionFormatHandler';
import { safeInstanceOf } from 'roosterjs-editor-dom';
import { zoomScaleFormatHandler } from '../formatHandlers/root/zoomScaleFormatHandler';

/**
Expand All @@ -17,27 +18,31 @@ import { zoomScaleFormatHandler } from '../formatHandlers/root/zoomScaleFormatHa
* @returns A ContentModelDocument object that contains all the models created from the give root element
*/
export default function domToContentModel(
root: HTMLElement,
root: HTMLElement | DocumentFragment,
editorContext: EditorContext,
option: DomToModelOption
): ContentModelDocument {
const model = createContentModelDocument(editorContext.defaultFormat);
const context = createDomToModelContext(editorContext, option);

// For root element, use computed style as initial value of segment formats
parseFormat(root, [computedSegmentFormatHandler.parse], context.segmentFormat, context);
if (safeInstanceOf(root, 'DocumentFragment')) {
context.elementProcessors.child(model, root, context);
} else {
// For root element, use computed style as initial value of segment formats
parseFormat(root, [computedSegmentFormatHandler.parse], context.segmentFormat, context);

// Need to calculate direction (ltr or rtl), use it as initial value
parseFormat(root, [rootDirectionFormatHandler.parse], context.blockFormat, context);
// Need to calculate direction (ltr or rtl), use it as initial value
parseFormat(root, [rootDirectionFormatHandler.parse], context.blockFormat, context);

// Need to calculate zoom scale value from root element, use this value to calculate sizes for elements
parseFormat(root, [zoomScaleFormatHandler.parse], context.zoomScaleFormat, context);
// Need to calculate zoom scale value from root element, use this value to calculate sizes for elements
parseFormat(root, [zoomScaleFormatHandler.parse], context.zoomScaleFormat, context);

const processor = option.includeRoot
? context.elementProcessors.element
: context.elementProcessors.child;
const processor = option.includeRoot
? context.elementProcessors.element
: context.elementProcessors.child;

processor(model, root, context);
processor(model, root, context);
}

normalizeContentModel(model);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ContentModelEditorCore, CreatePasteModel } from '../../publicTypes/Cont
import {
createDefaultHtmlSanitizerOptions,
createFragmentFromClipboardData,
wrap,
} from 'roosterjs-editor-dom';

export const createPasteModel: CreatePasteModel = (
Expand All @@ -29,7 +28,7 @@ export const createPasteModel: CreatePasteModel = (
event
);

return domToContentModel(wrap(fragment, 'span'), core.api.createEditorContext(core), {
return domToContentModel(fragment, core.api.createEditorContext(core), {
processorOverride: {
element: (group, element, context) => {
const wasHandled =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import domToContentModel from '../../domToModel/domToContentModel';
import { ContentModelDocument } from '../../publicTypes/group/ContentModelDocument';
import { mergeModel } from '../../modelApi/common/mergeModel';
import { safeInstanceOf, wrap } from 'roosterjs-editor-dom';
import { safeInstanceOf } from 'roosterjs-editor-dom';
import { setSelection } from '../../modelApi/selection/setSelection';

/**
Expand All @@ -12,11 +12,7 @@ export function insertContent(
htmlContent: DocumentFragment | HTMLElement | ContentModelDocument,
isFromDarkMode?: boolean
) {
if (safeInstanceOf(htmlContent, 'DocumentFragment')) {
htmlContent = wrap(htmlContent, 'span');
}

if (safeInstanceOf(htmlContent, 'HTMLElement')) {
if (safeInstanceOf(htmlContent, 'Node')) {
htmlContent = domToContentModel(
htmlContent,
{
Expand Down

0 comments on commit db38f17

Please sign in to comment.