UNPKG

devexpress-richedit

Version:

DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.

37 lines (36 loc) 2.25 kB
import { DocumentFormat } from '../../../common/document-format'; import { Errors } from '@devexpress/utils/lib/errors'; import { Base64Utils } from '@devexpress/utils/lib/utils/base64'; import { isString } from '@devexpress/utils/lib/utils/common'; import { Importer } from '../../formats/docx/import/importer'; import { ImporterOptions } from '../../formats/docx/import/importer-options'; import { RtfImporterOptions } from '../../../common/formats/rtf/import/importer-options'; import { RtfImporter } from '../../../common/formats/rtf/import/rtf-importer'; import { TxtImporter } from '../../../common/formats/txt/txt-importer'; import { getAfterInsertCallback, getAfterInsertReject } from '../../../common/formats/callback-helpers'; import { HtmlDocumentImporter } from '../../../common/formats/html/import/html-document-importer'; export const UNSUPPORTED_FORMAT_MESSAGE = 'Unsupported format'; export function createImporter(blob, format, throwInvalidFile = reason => { throw new Error(Errors.InternalException + " " + reason); }) { const isEmpty = blob.size === 0; switch (format) { case DocumentFormat.OpenXml: return !isEmpty ? new Importer(new ImporterOptions(throwInvalidFile)) : new TxtImporter(); case DocumentFormat.Rtf: return !isEmpty ? new RtfImporter(new RtfImporterOptions(throwInvalidFile)) : new TxtImporter(); case DocumentFormat.PlainText: return new TxtImporter(); case DocumentFormat.Html: return new HtmlDocumentImporter(); default: console.log(UNSUPPORTED_FORMAT_MESSAGE); return null; } } export function insertContentInSubDocumentPublic(processor, subDocument, position, content, documentFormat, callback) { if (content instanceof ArrayBuffer) content = Base64Utils.fromArrayBuffer(content); if (isString(content)) content = Base64Utils.getFileFromBase64(content); const importer = createImporter(content, documentFormat, () => { }); if (!importer) throw new Error(UNSUPPORTED_FORMAT_MESSAGE); importer.importFromFile(content, processor.modelManager.richOptions, getAfterInsertCallback(processor, subDocument, position, callback), getAfterInsertReject(callback)); }