@explita/editor
Version:
`@explita/editor` is a versatile, modern rich-text editor built on TipTap for seamless integration into React applications. It provides extensive customization options and advanced features to cater to diverse content creation needs.
70 lines (69 loc) • 2.38 kB
JavaScript
import clsx from "clsx";
import { twMerge } from "tailwind-merge";
import mammoth from "mammoth";
import { DEFAULT_MARGIN, PAGE_HEIGHT, PAGE_WIDTH } from "./constants";
export const defaultEditorOpts = {
padding: {
top: DEFAULT_MARGIN,
right: DEFAULT_MARGIN,
bottom: DEFAULT_MARGIN,
left: DEFAULT_MARGIN,
},
zoomLevel: 1.0,
editorHeight: `${PAGE_HEIGHT}px`,
editorWidth: `${PAGE_WIDTH}px`,
containerHeight: "100vh",
containerWidth: "100%",
};
export function initializeEditor(userOpts) {
const mergedOpts = {
...defaultEditorOpts,
...userOpts,
padding: {
...defaultEditorOpts.padding,
...(userOpts?.padding || {}),
},
zoomLevel: parseFloat(Math.max(userOpts?.zoomLevel ?? defaultEditorOpts.zoomLevel, 0.2)) || 1,
editorWidth: userOpts?.editorWidth || defaultEditorOpts.editorWidth,
editorHeight: userOpts?.editorHeight || defaultEditorOpts.editorHeight,
containerWidth: userOpts?.containerWidth || defaultEditorOpts.containerWidth,
containerHeight: userOpts?.containerHeight || defaultEditorOpts.containerHeight,
};
return mergedOpts;
}
export function cn(...inputs) {
return twMerge(clsx(inputs));
}
export function measureHeight(element) {
if (!(element instanceof HTMLElement)) {
return 0;
}
const computedStyle = window.getComputedStyle(element);
return (element.offsetHeight +
parseFloat(computedStyle.marginTop) +
parseFloat(computedStyle.marginBottom));
}
export function onSave(editor, format = "html") {
if (!editor)
return;
if (format === "html")
return editor.getHTML();
if (format === "json")
return editor.getJSON();
if (format === "text")
return editor.getText();
}
export async function parseWordDocument(file) {
try {
const arrayBuffer = await file.arrayBuffer();
const { value: html, messages } = await mammoth.convertToHtml({
arrayBuffer,
});
console.log("Parsing messages:", messages); // Logs warnings about unsupported content
return html;
}
catch (error) {
console.error("Error parsing Word document:", error);
throw new Error("Failed to parse the Word document, make sure you only import .docx files.");
}
}