diff --git a/apps/live/src/core/helpers/convert-document.ts b/apps/live/src/core/helpers/convert-document.ts deleted file mode 100644 index 12398919014..00000000000 --- a/apps/live/src/core/helpers/convert-document.ts +++ /dev/null @@ -1,44 +0,0 @@ -// plane editor -import { - getAllDocumentFormatsFromDocumentEditorBinaryData, - getAllDocumentFormatsFromRichTextEditorBinaryData, - getBinaryDataFromDocumentEditorHTMLString, - getBinaryDataFromRichTextEditorHTMLString, -} from "@plane/editor"; -// plane types -import { TDocumentPayload } from "@plane/types"; - -type TArgs = { - document_html: string; - variant: "rich" | "document"; -}; - -export const convertHTMLDocumentToAllFormats = (args: TArgs): TDocumentPayload => { - const { document_html, variant } = args; - - let allFormats: TDocumentPayload; - - if (variant === "rich") { - const contentBinary = getBinaryDataFromRichTextEditorHTMLString(document_html); - const { contentBinaryEncoded, contentHTML, contentJSON } = - getAllDocumentFormatsFromRichTextEditorBinaryData(contentBinary); - allFormats = { - description: contentJSON, - description_html: contentHTML, - description_binary: contentBinaryEncoded, - }; - } else if (variant === "document") { - const contentBinary = getBinaryDataFromDocumentEditorHTMLString(document_html); - const { contentBinaryEncoded, contentHTML, contentJSON } = - getAllDocumentFormatsFromDocumentEditorBinaryData(contentBinary); - allFormats = { - description: contentJSON, - description_html: contentHTML, - description_binary: contentBinaryEncoded, - }; - } else { - throw new Error(`Invalid variant provided: ${variant}`); - } - - return allFormats; -}; diff --git a/apps/live/src/core/helpers/page.ts b/apps/live/src/core/helpers/page.ts deleted file mode 100644 index d4322d1ad89..00000000000 --- a/apps/live/src/core/helpers/page.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { getSchema } from "@tiptap/core"; -import { generateHTML, generateJSON } from "@tiptap/html"; -import { prosemirrorJSONToYDoc, yXmlFragmentToProseMirrorRootNode } from "y-prosemirror"; -import * as Y from "yjs"; -// plane editor -import { CoreEditorExtensionsWithoutProps, DocumentEditorExtensionsWithoutProps } from "@plane/editor/lib"; - -const DOCUMENT_EDITOR_EXTENSIONS = [...CoreEditorExtensionsWithoutProps, ...DocumentEditorExtensionsWithoutProps]; -const documentEditorSchema = getSchema(DOCUMENT_EDITOR_EXTENSIONS); - -export const getAllDocumentFormatsFromBinaryData = ( - description: Uint8Array -): { - contentBinaryEncoded: string; - contentJSON: object; - contentHTML: string; -} => { - // encode binary description data - const base64Data = Buffer.from(description).toString("base64"); - const yDoc = new Y.Doc(); - Y.applyUpdate(yDoc, description); - // convert to JSON - const type = yDoc.getXmlFragment("default"); - const contentJSON = yXmlFragmentToProseMirrorRootNode(type, documentEditorSchema).toJSON(); - // convert to HTML - const contentHTML = generateHTML(contentJSON, DOCUMENT_EDITOR_EXTENSIONS); - - return { - contentBinaryEncoded: base64Data, - contentJSON, - contentHTML, - }; -}; - -export const getBinaryDataFromHTMLString = ( - descriptionHTML: string -): { - contentBinary: Uint8Array; -} => { - // convert HTML to JSON - const contentJSON = generateJSON(descriptionHTML ?? "

", DOCUMENT_EDITOR_EXTENSIONS); - // convert JSON to Y.Doc format - const transformedData = prosemirrorJSONToYDoc(documentEditorSchema, contentJSON, "default"); - // convert Y.Doc to Uint8Array format - const encodedData = Y.encodeStateAsUpdate(transformedData); - - return { - contentBinary: encodedData, - }; -}; diff --git a/apps/live/src/core/lib/page.ts b/apps/live/src/core/lib/page.ts index 7d23d8b195f..932fcd84f7c 100644 --- a/apps/live/src/core/lib/page.ts +++ b/apps/live/src/core/lib/page.ts @@ -1,5 +1,5 @@ // helpers -import { getAllDocumentFormatsFromBinaryData, getBinaryDataFromHTMLString } from "@/core/helpers/page.js"; +import { getAllDocumentFormatsFromBinaryData, getBinaryDataFromHTMLString } from "@plane/editor/lib"; // services import { PageService } from "@/core/services/page.service.js"; import { manualLogger } from "../helpers/logger.js"; @@ -19,7 +19,9 @@ export const updatePageDescription = async ( const projectId = params.get("projectId")?.toString(); if (!workspaceSlug || !projectId || !cookie) return; - const { contentBinaryEncoded, contentHTML, contentJSON } = getAllDocumentFormatsFromBinaryData(updatedDescription); + const { contentBinaryEncoded, contentHTML, contentJSON } = getAllDocumentFormatsFromBinaryData({ + descriptionBinary: updatedDescription, + }); try { const payload = { description_binary: contentBinaryEncoded, @@ -44,7 +46,9 @@ const fetchDescriptionHTMLAndTransform = async ( try { const pageDetails = await pageService.fetchDetails(workspaceSlug, projectId, pageId, cookie); - const { contentBinary } = getBinaryDataFromHTMLString(pageDetails.description_html ?? "

"); + const contentBinary = getBinaryDataFromHTMLString({ + descriptionHTML: pageDetails.description_html ?? "

", + }); return contentBinary; } catch (error) { manualLogger.error("Error while transforming from HTML to Uint8Array", error); diff --git a/apps/live/src/server.ts b/apps/live/src/server.ts index 69d0e642ea2..af16202e5a5 100644 --- a/apps/live/src/server.ts +++ b/apps/live/src/server.ts @@ -5,11 +5,11 @@ import expressWs from "express-ws"; import helmet from "helmet"; // hocuspocus server // helpers -import { convertHTMLDocumentToAllFormats } from "@/core/helpers/convert-document.js"; import { logger, manualLogger } from "@/core/helpers/logger.js"; import { getHocusPocusServer } from "@/core/hocuspocus-server.js"; // types import { TConvertDocumentRequestBody } from "@/core/types/common.js"; +import { getAllDocumentFormatsFromHTMLString } from "@plane/editor/lib"; export class Server { private app: any; @@ -72,9 +72,8 @@ export class Server { }); return; } - const { description, description_binary } = convertHTMLDocumentToAllFormats({ - document_html: description_html, - variant, + const { description, description_binary } = getAllDocumentFormatsFromHTMLString({ + descriptionHTML: description_html, }); res.status(200).json({ description, diff --git a/apps/web/core/hooks/use-page-fallback.ts b/apps/web/core/hooks/use-page-fallback.ts index 98aa1f66c3a..b4d8071c235 100644 --- a/apps/web/core/hooks/use-page-fallback.ts +++ b/apps/web/core/hooks/use-page-fallback.ts @@ -1,6 +1,6 @@ import { useCallback, useEffect } from "react"; // plane editor -import { type EditorRefApi, getBinaryDataFromDocumentEditorHTMLString } from "@plane/editor"; +import { type EditorRefApi, getBinaryDataFromHTMLString } from "@plane/editor"; // plane types import { TDocumentPayload } from "@plane/types"; // hooks @@ -27,7 +27,9 @@ export const usePageFallback = (args: TArgs) => { if (latestEncodedDescription && latestEncodedDescription.byteLength > 0) { latestDecodedDescription = new Uint8Array(latestEncodedDescription); } else { - latestDecodedDescription = getBinaryDataFromDocumentEditorHTMLString("

"); + latestDecodedDescription = getBinaryDataFromHTMLString({ + descriptionHTML: "

", + }); } editor.setProviderDocument(latestDecodedDescription); diff --git a/packages/editor/src/ce/extensions/core/without-props.ts b/packages/editor/src/ce/extensions/core/without-props.ts deleted file mode 100644 index 0debff0ea50..00000000000 --- a/packages/editor/src/ce/extensions/core/without-props.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { Extensions } from "@tiptap/core"; - -export const CoreEditorAdditionalExtensionsWithoutProps: Extensions = []; diff --git a/packages/editor/src/ce/extensions/parser-kit.ts b/packages/editor/src/ce/extensions/parser-kit.ts new file mode 100644 index 00000000000..7be95405026 --- /dev/null +++ b/packages/editor/src/ce/extensions/parser-kit.ts @@ -0,0 +1,3 @@ +import { Extensions } from "@tiptap/core"; + +export const PARSER_KIT_ADDITIONAL_EXTENSIONS: Extensions = []; diff --git a/packages/editor/src/core/components/editors/document/collaborative-editor.tsx b/packages/editor/src/core/components/editors/document/collaborative-editor.tsx index 535f1b6ba13..d4be4623f0a 100644 --- a/packages/editor/src/core/components/editors/document/collaborative-editor.tsx +++ b/packages/editor/src/core/components/editors/document/collaborative-editor.tsx @@ -1,13 +1,10 @@ -import type { Extensions } from "@tiptap/core"; -import React, { useMemo } from "react"; +import React from "react"; // plane imports import { cn } from "@plane/utils"; // components import { PageRenderer } from "@/components/editors"; // constants import { DEFAULT_DISPLAY_CONFIG } from "@/constants/config"; -// extensions -import { WorkItemEmbedExtension } from "@/extensions"; // helpers import { getEditorClassNames } from "@/helpers/common"; // hooks @@ -21,7 +18,7 @@ const CollaborativeDocumentEditor: React.FC = bubbleMenuEnabled = true, containerClassName, documentLoaderClassName, - extensions: externalExtensions = [], + extensions, disabledExtensions, displayConfig = DEFAULT_DISPLAY_CONFIG, editable, @@ -47,20 +44,6 @@ const CollaborativeDocumentEditor: React.FC = user, } = props; - const extensions: Extensions = useMemo(() => { - const allExtensions = [...externalExtensions]; - - if (embedHandler?.issue) { - allExtensions.push( - WorkItemEmbedExtension({ - widgetCallback: embedHandler.issue.widgetCallback, - }) - ); - } - - return allExtensions; - }, [externalExtensions, embedHandler.issue]); - // use document editor const { editor, hasServerConnectionFailed, hasServerSynced } = useCollaborativeEditor({ disabledExtensions, diff --git a/packages/editor/src/core/components/editors/document/editor.tsx b/packages/editor/src/core/components/editors/document/editor.tsx index e2b3754cc1b..91cd15339ba 100644 --- a/packages/editor/src/core/components/editors/document/editor.tsx +++ b/packages/editor/src/core/components/editors/document/editor.tsx @@ -7,7 +7,7 @@ import { PageRenderer } from "@/components/editors"; // constants import { DEFAULT_DISPLAY_CONFIG } from "@/constants/config"; // extensions -import { HeadingListExtension, WorkItemEmbedExtension, SideMenuExtension } from "@/extensions"; +import { HeadingListExtension, SideMenuExtension } from "@/extensions"; // helpers import { getEditorClassNames } from "@/helpers/common"; // hooks @@ -39,13 +39,6 @@ const DocumentEditor = (props: IDocumentEditorProps) => { } = props; const extensions: Extensions = useMemo(() => { const additionalExtensions: Extensions = []; - if (embedHandler?.issue) { - additionalExtensions.push( - WorkItemEmbedExtension({ - widgetCallback: embedHandler.issue.widgetCallback, - }) - ); - } additionalExtensions.push( SideMenuExtension({ aiEnabled: !disabledExtensions?.includes("ai"), diff --git a/packages/editor/src/core/extensions/index.ts b/packages/editor/src/core/extensions/index.ts index 48692c09181..3a7d8f6bbba 100644 --- a/packages/editor/src/core/extensions/index.ts +++ b/packages/editor/src/core/extensions/index.ts @@ -8,8 +8,6 @@ export * from "./mentions"; export * from "./slash-commands"; export * from "./table"; export * from "./typography"; -export * from "./work-item-embed"; -export * from "./core-without-props"; export * from "./custom-color"; export * from "./enter-key"; export * from "./extensions"; diff --git a/packages/editor/src/core/extensions/core-without-props.ts b/packages/editor/src/core/extensions/parser-kit.ts similarity index 88% rename from packages/editor/src/core/extensions/core-without-props.ts rename to packages/editor/src/core/extensions/parser-kit.ts index 421f48e0a6b..c2b8679de87 100644 --- a/packages/editor/src/core/extensions/core-without-props.ts +++ b/packages/editor/src/core/extensions/parser-kit.ts @@ -6,7 +6,7 @@ import StarterKit from "@tiptap/starter-kit"; // helpers import { isValidHttpUrl } from "@/helpers/common"; // plane editor imports -import { CoreEditorAdditionalExtensionsWithoutProps } from "@/plane-editor/extensions/core/without-props"; +import { PARSER_KIT_ADDITIONAL_EXTENSIONS } from "@/plane-editor/extensions/parser-kit"; // extensions import { CustomCalloutExtensionConfig } from "./callout/extension-config"; import { CustomCodeBlockExtensionWithoutProps } from "./code/without-props"; @@ -21,9 +21,8 @@ import { CustomMentionExtensionConfig } from "./mentions/extension-config"; import { CustomQuoteExtension } from "./quote"; import { TableHeader, TableCell, TableRow, Table } from "./table"; import { CustomTextAlignExtension } from "./text-align"; -import { WorkItemEmbedExtensionConfig } from "./work-item-embed/extension-config"; -export const CoreEditorExtensionsWithoutProps = [ +export const PARSER_KIT = [ StarterKit.configure({ bulletList: { HTMLAttributes: { @@ -99,7 +98,5 @@ export const CoreEditorExtensionsWithoutProps = [ CustomTextAlignExtension, CustomCalloutExtensionConfig, CustomColorExtension, - ...CoreEditorAdditionalExtensionsWithoutProps, + ...PARSER_KIT_ADDITIONAL_EXTENSIONS, ]; - -export const DocumentEditorExtensionsWithoutProps = [WorkItemEmbedExtensionConfig]; diff --git a/packages/editor/src/core/extensions/work-item-embed/extension-config.ts b/packages/editor/src/core/extensions/work-item-embed/extension-config.ts deleted file mode 100644 index 0ea25c770d5..00000000000 --- a/packages/editor/src/core/extensions/work-item-embed/extension-config.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { mergeAttributes, Node } from "@tiptap/core"; -// constants -import { CORE_EXTENSIONS } from "@/constants/extension"; - -export const WorkItemEmbedExtensionConfig = Node.create({ - name: CORE_EXTENSIONS.WORK_ITEM_EMBED, - group: "block", - atom: true, - selectable: true, - draggable: true, - - addAttributes() { - return { - entity_identifier: { - default: undefined, - }, - project_identifier: { - default: undefined, - }, - workspace_identifier: { - default: undefined, - }, - id: { - default: undefined, - }, - entity_name: { - default: undefined, - }, - }; - }, - - parseHTML() { - return [ - { - tag: "issue-embed-component", - }, - ]; - }, - - renderHTML({ HTMLAttributes }) { - return ["issue-embed-component", mergeAttributes(HTMLAttributes)]; - }, -}); diff --git a/packages/editor/src/core/extensions/work-item-embed/extension.tsx b/packages/editor/src/core/extensions/work-item-embed/extension.tsx deleted file mode 100644 index 25418bdae11..00000000000 --- a/packages/editor/src/core/extensions/work-item-embed/extension.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import { ReactNodeViewRenderer, NodeViewWrapper, type NodeViewProps } from "@tiptap/react"; -// local imports -import { WorkItemEmbedExtensionConfig } from "./extension-config"; - -type Props = { - widgetCallback: ({ - issueId, - projectId, - workspaceSlug, - }: { - issueId: string; - projectId: string | undefined; - workspaceSlug: string | undefined; - }) => React.ReactNode; -}; - -export const WorkItemEmbedExtension = (props: Props) => - WorkItemEmbedExtensionConfig.extend({ - addNodeView() { - return ReactNodeViewRenderer((issueProps: NodeViewProps) => ( - - {props.widgetCallback({ - issueId: issueProps.node.attrs.entity_identifier, - projectId: issueProps.node.attrs.project_identifier, - workspaceSlug: issueProps.node.attrs.workspace_identifier, - })} - - )); - }, - }); diff --git a/packages/editor/src/core/extensions/work-item-embed/index.ts b/packages/editor/src/core/extensions/work-item-embed/index.ts deleted file mode 100644 index 2ce32da8ba5..00000000000 --- a/packages/editor/src/core/extensions/work-item-embed/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./extension"; diff --git a/packages/editor/src/core/helpers/editor-commands.ts b/packages/editor/src/core/helpers/editor-commands.ts index 3217e710bd9..a8a58f61a31 100644 --- a/packages/editor/src/core/helpers/editor-commands.ts +++ b/packages/editor/src/core/helpers/editor-commands.ts @@ -1,4 +1,5 @@ import type { Editor, Range } from "@tiptap/core"; +import type { Level } from "@tiptap/extension-heading"; // constants import { CORE_EXTENSIONS } from "@/constants/extension"; // extensions @@ -12,7 +13,7 @@ export const setText = (editor: Editor, range?: Range) => { else editor.chain().focus().setNode(CORE_EXTENSIONS.PARAGRAPH).run(); }; -export const toggleHeading = (editor: Editor, level: 1 | 2 | 3 | 4 | 5 | 6, range?: Range) => { +export const toggleHeading = (editor: Editor, level: Level, range?: Range) => { if (range) editor.chain().focus().deleteRange(range).setNode(CORE_EXTENSIONS.HEADING, { level }).run(); else editor.chain().focus().toggleHeading({ level }).run(); }; diff --git a/packages/editor/src/core/helpers/parser.ts b/packages/editor/src/core/helpers/parser.ts index bf84b2fdb2d..76b6baca4ac 100644 --- a/packages/editor/src/core/helpers/parser.ts +++ b/packages/editor/src/core/helpers/parser.ts @@ -6,7 +6,7 @@ import { replaceAdditionalAssetsInHTMLContent, } from "@/plane-editor/helpers/parser"; // local imports -import { convertHTMLDocumentToAllFormats } from "./yjs-utils"; +import { getAllDocumentFormatsFromHTMLString } from "./yjs-utils"; /** * @description function to extract all assets from HTML content @@ -62,10 +62,9 @@ export const getEditorContentWithReplacedAssets = async (props: { entityId: string; entityType: TEditorAssetType; projectId: string | undefined; - variant: "rich" | "document"; duplicateAssetService: (params: TDuplicateAssetData) => Promise; }): Promise => { - const { descriptionHTML, entityId, entityType, projectId, variant, duplicateAssetService } = props; + const { descriptionHTML, entityId, entityType, projectId, duplicateAssetService } = props; let replacedDescription = descriptionHTML; // step 1: extract image assets from the description const assetIds = extractAssetsFromHTMLContent(descriptionHTML); @@ -86,9 +85,8 @@ export const getEditorContentWithReplacedAssets = async (props: { } } // step 4: convert the description to the document payload - const documentPayload = convertHTMLDocumentToAllFormats({ - document_html: replacedDescription, - variant, + const documentPayload = getAllDocumentFormatsFromHTMLString({ + descriptionHTML: replacedDescription, }); return documentPayload; }; diff --git a/packages/editor/src/core/helpers/yjs-utils.ts b/packages/editor/src/core/helpers/yjs-utils.ts index 92240bbb45a..3f93d7ad60b 100644 --- a/packages/editor/src/core/helpers/yjs-utils.ts +++ b/packages/editor/src/core/helpers/yjs-utils.ts @@ -4,17 +4,10 @@ import { prosemirrorJSONToYDoc, yXmlFragmentToProseMirrorRootNode } from "y-pros import * as Y from "yjs"; // extensions import { TDocumentPayload } from "@plane/types"; -import { - CoreEditorExtensionsWithoutProps, - DocumentEditorExtensionsWithoutProps, -} from "@/extensions/core-without-props"; +import { PARSER_KIT } from "@/extensions/parser-kit"; -// editor extension configs -const RICH_TEXT_EDITOR_EXTENSIONS = CoreEditorExtensionsWithoutProps; -const DOCUMENT_EDITOR_EXTENSIONS = [...CoreEditorExtensionsWithoutProps, ...DocumentEditorExtensionsWithoutProps]; -// editor schemas -const richTextEditorSchema = getSchema(RICH_TEXT_EDITOR_EXTENSIONS); -const documentEditorSchema = getSchema(DOCUMENT_EDITOR_EXTENSIONS); +// parser schema +const parserSchema = getSchema(PARSER_KIT); /** * @description apply updates to a doc and return the updated doc in binary format @@ -49,56 +42,41 @@ export const convertBinaryDataToBase64String = (document: Uint8Array): string => export const convertBase64StringToBinaryData = (document: string): ArrayBuffer => Buffer.from(document, "base64"); /** - * @description this function generates the binary equivalent of html content for the rich text editor - * @param {string} descriptionHTML + * @description Converts HTML string to binary data * @returns {Uint8Array} */ -export const getBinaryDataFromRichTextEditorHTMLString = (descriptionHTML: string): Uint8Array => { +export const getBinaryDataFromHTMLString = ({ descriptionHTML }: { descriptionHTML: string }): Uint8Array => { // convert HTML to JSON - const contentJSON = generateJSON(descriptionHTML ?? "

", RICH_TEXT_EDITOR_EXTENSIONS); + const contentJSON = generateJSON(descriptionHTML ?? "

", PARSER_KIT); // convert JSON to Y.Doc format - const transformedData = prosemirrorJSONToYDoc(richTextEditorSchema, contentJSON, "default"); + const transformedData = prosemirrorJSONToYDoc(parserSchema, contentJSON, "default"); // convert Y.Doc to Uint8Array format const encodedData = Y.encodeStateAsUpdate(transformedData); return encodedData; }; /** - * @description this function generates the binary equivalent of html content for the document editor - * @param {string} descriptionHTML - * @returns {Uint8Array} - */ -export const getBinaryDataFromDocumentEditorHTMLString = (descriptionHTML: string): Uint8Array => { - // convert HTML to JSON - const contentJSON = generateJSON(descriptionHTML ?? "

", DOCUMENT_EDITOR_EXTENSIONS); - // convert JSON to Y.Doc format - const transformedData = prosemirrorJSONToYDoc(documentEditorSchema, contentJSON, "default"); - // convert Y.Doc to Uint8Array format - const encodedData = Y.encodeStateAsUpdate(transformedData); - return encodedData; -}; - -/** - * @description this function generates all document formats for the provided binary data for the rich text editor - * @param {Uint8Array} description + * @description Converts binary data to all supported document formats (JSON, HTML, and binary) * @returns */ -export const getAllDocumentFormatsFromRichTextEditorBinaryData = ( - description: Uint8Array -): { +export const getAllDocumentFormatsFromBinaryData = ({ + descriptionBinary, +}: { + descriptionBinary: Uint8Array; +}): { contentBinaryEncoded: string; contentJSON: object; contentHTML: string; } => { // encode binary description data - const base64Data = convertBinaryDataToBase64String(description); + const base64Data = convertBinaryDataToBase64String(descriptionBinary); const yDoc = new Y.Doc(); - Y.applyUpdate(yDoc, description); + Y.applyUpdate(yDoc, descriptionBinary); // convert to JSON const type = yDoc.getXmlFragment("default"); - const contentJSON = yXmlFragmentToProseMirrorRootNode(type, richTextEditorSchema).toJSON(); + const contentJSON = yXmlFragmentToProseMirrorRootNode(type, parserSchema).toJSON(); // convert to HTML - const contentHTML = generateHTML(contentJSON, RICH_TEXT_EDITOR_EXTENSIONS); + const contentHTML = generateHTML(contentJSON, PARSER_KIT); return { contentBinaryEncoded: base64Data, @@ -108,77 +86,24 @@ export const getAllDocumentFormatsFromRichTextEditorBinaryData = ( }; /** - * @description this function generates all document formats for the provided binary data for the document editor - * @param {Uint8Array} description - * @returns + * @description Converts HTML string to all supported document formats (JSON, HTML, and binary) + * @returns {TDocumentPayload} Object containing the document in all supported formats */ -export const getAllDocumentFormatsFromDocumentEditorBinaryData = ( - description: Uint8Array -): { - contentBinaryEncoded: string; - contentJSON: object; - contentHTML: string; -} => { - // encode binary description data - const base64Data = convertBinaryDataToBase64String(description); - const yDoc = new Y.Doc(); - Y.applyUpdate(yDoc, description); - // convert to JSON - const type = yDoc.getXmlFragment("default"); - const contentJSON = yXmlFragmentToProseMirrorRootNode(type, documentEditorSchema).toJSON(); - // convert to HTML - const contentHTML = generateHTML(contentJSON, DOCUMENT_EDITOR_EXTENSIONS); +export const getAllDocumentFormatsFromHTMLString = ({ + descriptionHTML, +}: { + descriptionHTML: string; +}): TDocumentPayload => { + // Convert HTML to binary format for rich text editor + const contentBinary = getBinaryDataFromHTMLString({ descriptionHTML }); + // Generate all document formats from the binary data + const { contentBinaryEncoded, contentHTML, contentJSON } = getAllDocumentFormatsFromBinaryData({ + descriptionBinary: contentBinary, + }); return { - contentBinaryEncoded: base64Data, - contentJSON, - contentHTML, + description: contentJSON, + description_html: contentHTML, + description_binary: contentBinaryEncoded, }; }; - -type TConvertHTMLDocumentToAllFormatsArgs = { - document_html: string; - variant: "rich" | "document"; -}; - -/** - * @description Converts HTML content to all supported document formats (JSON, HTML, and binary) - * @param {TConvertHTMLDocumentToAllFormatsArgs} args - Arguments containing HTML content and variant type - * @param {string} args.document_html - The HTML content to convert - * @param {"rich" | "document"} args.variant - The type of editor variant to use for conversion - * @returns {TDocumentPayload} Object containing the document in all supported formats - * @throws {Error} If an invalid variant is provided - */ -export const convertHTMLDocumentToAllFormats = (args: TConvertHTMLDocumentToAllFormatsArgs): TDocumentPayload => { - const { document_html, variant } = args; - - let allFormats: TDocumentPayload; - - if (variant === "rich") { - // Convert HTML to binary format for rich text editor - const contentBinary = getBinaryDataFromRichTextEditorHTMLString(document_html); - // Generate all document formats from the binary data - const { contentBinaryEncoded, contentHTML, contentJSON } = - getAllDocumentFormatsFromRichTextEditorBinaryData(contentBinary); - allFormats = { - description: contentJSON, - description_html: contentHTML, - description_binary: contentBinaryEncoded, - }; - } else if (variant === "document") { - // Convert HTML to binary format for document editor - const contentBinary = getBinaryDataFromDocumentEditorHTMLString(document_html); - // Generate all document formats from the binary data - const { contentBinaryEncoded, contentHTML, contentJSON } = - getAllDocumentFormatsFromDocumentEditorBinaryData(contentBinary); - allFormats = { - description: contentJSON, - description_html: contentHTML, - description_binary: contentBinaryEncoded, - }; - } else { - throw new Error(`Invalid variant provided: ${variant}`); - } - - return allFormats; -}; diff --git a/packages/editor/src/lib.ts b/packages/editor/src/lib.ts index 44388a00eae..617bbee5133 100644 --- a/packages/editor/src/lib.ts +++ b/packages/editor/src/lib.ts @@ -1,4 +1,3 @@ -export * from "@/extensions/core-without-props"; export * from "@/constants/document-collaborative-events"; export * from "@/helpers/get-document-server-event"; export * from "@/helpers/yjs-utils";