@kedao/convert
Version:
A convert helper for Kedao Editor.
64 lines • 2.38 kB
JavaScript
import { convertToHTML, convertFromHTML } from 'draft-convert';
import { getToHTMLConfig, getFromHTMLConfig, defaultFontFamilies } from './configs';
import { convertFromRaw, convertToRaw, EditorState } from 'draft-js';
const defaultConvertOptions = {
fontFamilies: defaultFontFamilies
};
export const convertRawToHTML = (rawContent, options) => {
options = Object.assign(Object.assign({}, defaultConvertOptions), options);
try {
const contentState = convertFromRaw(rawContent);
options.contentState = contentState;
return convertToHTML(getToHTMLConfig(options))(contentState);
}
catch (error) {
console.warn(error);
return '';
}
};
export const convertHTMLToRaw = (HTMLString, options, source) => {
options = Object.assign(Object.assign({}, defaultConvertOptions), options);
try {
const contentState = convertFromHTML(getFromHTMLConfig(options, source))(HTMLString);
return convertToRaw(contentState);
}
catch (error) {
console.warn(error);
return {};
}
};
export const convertEditorStateToHTML = (editorState, options) => {
options = Object.assign(Object.assign({}, defaultConvertOptions), options);
try {
const contentState = editorState.getCurrentContent();
options.contentState = contentState;
return convertToHTML(getToHTMLConfig(options))(contentState);
}
catch (error) {
console.warn(error);
return '';
}
};
export const convertHTMLToEditorState = (HTMLString, editorDecorators, options, source) => {
options = Object.assign(Object.assign({}, defaultConvertOptions), options);
try {
return EditorState.createWithContent(convertFromHTML(getFromHTMLConfig(options, source))(HTMLString), editorDecorators);
}
catch (error) {
console.warn(error);
return EditorState.createEmpty(editorDecorators);
}
};
export const convertEditorStateToRaw = (editorState) => {
return convertToRaw(editorState.getCurrentContent());
};
export const convertRawToEditorState = (rawContent, editorDecorators) => {
try {
return EditorState.createWithContent(convertFromRaw(rawContent), editorDecorators);
}
catch (error) {
console.warn(error);
return EditorState.createEmpty(editorDecorators);
}
};
//# sourceMappingURL=index.js.map