Skip to content

Commit

Permalink
Patch version bump: Support adding options for exportContent (#2443) (#…
Browse files Browse the repository at this point in the history
…2446)

* Support adding options for exportContent (#2443)

* fix build
  • Loading branch information
JiuqingSong authored Feb 27, 2024
1 parent 6e9943b commit f12cf2f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
contentModelToText,
createModelToDomContext,
} from 'roosterjs-content-model-dom';
import type { ExportContentMode, IStandaloneEditor } from 'roosterjs-content-model-types';
import type {
ExportContentMode,
IStandaloneEditor,
ModelToDomOption,
} from 'roosterjs-content-model-types';

/**
* Export string content of editor
Expand All @@ -12,8 +16,13 @@ import type { ExportContentMode, IStandaloneEditor } from 'roosterjs-content-mod
* - HTML: Export HTML content. If there are entities, this will cause EntityOperation event with option = 'replaceTemporaryContent' to get a dehydrated entity
* - PlainText: Export plain text content
* - PlainTextFast: Export plain text using editor's textContent property directly
* @param options @optional Options for Model to DOM conversion
*/
export function exportContent(editor: IStandaloneEditor, mode: ExportContentMode = 'HTML'): string {
export function exportContent(
editor: IStandaloneEditor,
mode: ExportContentMode = 'HTML',
options?: ModelToDomOption
): string {
if (mode == 'PlainTextFast') {
return editor.getDOMHelper().getTextContent();
} else {
Expand All @@ -25,7 +34,12 @@ export function exportContent(editor: IStandaloneEditor, mode: ExportContentMode
const doc = editor.getDocument();
const div = doc.createElement('div');

contentModelToDom(doc, div, model, createModelToDomContext());
contentModelToDom(
doc,
div,
model,
createModelToDomContext(undefined /*editorContext*/, options)
);

editor.triggerEvent('extractContentWithDom', { clonedRoot: div }, true /*broadcast*/);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,51 @@ describe('exportContent', () => {

expect(html).toBe(mockedHTML);
expect(getContentModelCopySpy).toHaveBeenCalledWith('disconnected');
expect(createModelToDomContextSpy).toHaveBeenCalledWith();
expect(createModelToDomContextSpy).toHaveBeenCalledWith(undefined, undefined);
expect(contentModelToDomSpy).toHaveBeenCalledWith(
mockedDoc,
mockedDiv,
mockedModel,
mockedContext
);
expect(triggerEventSpy).toHaveBeenCalledWith(
'extractContentWithDom',
{ clonedRoot: mockedDiv },
true
);
});

it('HTML with options', () => {
const mockedModel = 'MODEL' as any;
const getContentModelCopySpy = jasmine
.createSpy('getContentModelCopy')
.and.returnValue(mockedModel);
const mockedHTML = 'HTML';
const mockedDiv = {
innerHTML: mockedHTML,
} as any;
const mockedDoc = {
createElement: () => mockedDiv,
} as any;
const triggerEventSpy = jasmine.createSpy('triggerEvent');
const editor: IStandaloneEditor = {
getContentModelCopy: getContentModelCopySpy,
getDocument: () => mockedDoc,
triggerEvent: triggerEventSpy,
} as any;
const contentModelToDomSpy = spyOn(contentModelToDom, 'contentModelToDom');
const mockedContext = 'CONTEXT' as any;
const createModelToDomContextSpy = spyOn(
createModelToDomContext,
'createModelToDomContext'
).and.returnValue(mockedContext);
const mockedOptions = 'OPTIONS' as any;

const html = exportContent(editor, 'HTML', mockedOptions);

expect(html).toBe(mockedHTML);
expect(getContentModelCopySpy).toHaveBeenCalledWith('disconnected');
expect(createModelToDomContextSpy).toHaveBeenCalledWith(undefined, mockedOptions);
expect(contentModelToDomSpy).toHaveBeenCalledWith(
mockedDoc,
mockedDiv,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ export class EditorAdapter extends StandaloneEditor implements IEditor {
* @returns HTML string representing current editor content
*/
getContent(mode: GetContentMode | CompatibleGetContentMode = GetContentMode.CleanHTML): string {
return exportContent(this, GetContentModeMap[mode]);
return exportContent(
this,
GetContentModeMap[mode],
this.getCore().modelToDomSettings.customized
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"packages": "8.60.0",
"packages-ui": "8.55.0",
"packages-content-model": "0.26.2",
"packages-content-model": "0.26.3",
"overrides": {
"roosterjs-editor-plugins": "8.60.2",
"roosterjs-editor-adapter": "0.26.1"
"roosterjs-editor-adapter": "0.26.2"
}
}

0 comments on commit f12cf2f

Please sign in to comment.