@cyberyozh/text-editor 0.1.11 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ import { generateJSON, type JSONContent } from "@tiptap/core";
2
+ import { getSharedExtensions } from "./get-shared-extensions";
3
+ import { BaseFigure } from "@/extensions/base/figure";
4
+
5
+ function isJSONContent(value: unknown): value is JSONContent {
6
+ return (
7
+ typeof value === "object" && value !== null && "type" in value && typeof value.type === "string"
8
+ );
9
+ }
10
+
11
+ export function htmlToTiptapContent(content: string | JSONContent): JSONContent {
12
+ if (isJSONContent(content)) {
13
+ return content;
14
+ }
15
+
16
+ if (typeof content === "string") {
17
+ try {
18
+ const parsed = JSON.parse(content);
19
+ if (isJSONContent(parsed)) {
20
+ return parsed;
21
+ }
22
+ } catch {
23
+ // Not valid JSON, treat as HTML
24
+ }
25
+ }
26
+
27
+ if (!content || content.trim() === "") {
28
+ return { type: "doc", content: [{ type: "paragraph" }] };
29
+ }
30
+
31
+ const sharedExtensions = getSharedExtensions();
32
+
33
+ const extensions = [...sharedExtensions, BaseFigure];
34
+
35
+ try {
36
+ return generateJSON(content, extensions);
37
+ } catch (error) {
38
+ console.error("Failed to convert HTML to Tiptap content:", error);
39
+ return { type: "doc", content: [{ type: "paragraph" }] };
40
+ }
41
+ }
@@ -1,75 +0,0 @@
1
- import { useEffect, useRef, useState, type ReactNode } from "react";
2
-
3
- interface TableProps {
4
- children: ReactNode;
5
- }
6
-
7
- /**
8
- * Table component for static rendering
9
- * Applies column widths from Tiptap table cells
10
- */
11
- export function Table({ children }: TableProps) {
12
- const wrapperRef = useRef<HTMLDivElement>(null);
13
- const [undefinedColWidth, setUndefinedColWidth] = useState<number | undefined>(undefined);
14
-
15
- let colWidths: (number | undefined)[] = [];
16
-
17
- if (Array.isArray(children) && children.length > 0) {
18
- const firstRow = children[0];
19
-
20
- if (firstRow?.props?.children) {
21
- const cells = Array.isArray(firstRow.props.children)
22
- ? firstRow.props.children
23
- : [firstRow.props.children];
24
-
25
- colWidths = cells.map((cell: any) => cell?.props?.node?.attrs?.colwidth?.[0]);
26
- }
27
- }
28
-
29
- const definedWidths = colWidths.filter((w): w is number => w !== undefined);
30
- const totalDefinedWidth = definedWidths.reduce((total, width) => total + width, 0);
31
- const undefinedCount = colWidths.filter((w) => w === undefined).length;
32
-
33
- useEffect(() => {
34
- if (wrapperRef.current && undefinedCount > 0) {
35
- const containerWidth = wrapperRef.current.clientWidth;
36
- const remainingSpace = containerWidth - totalDefinedWidth - 2;
37
- const calculatedWidth = Math.floor(remainingSpace / undefinedCount);
38
- setUndefinedColWidth(calculatedWidth > 0 ? calculatedWidth : undefined);
39
- }
40
- }, [totalDefinedWidth, undefinedCount]);
41
-
42
- const tableWidth =
43
- undefinedCount > 0 && undefinedColWidth
44
- ? totalDefinedWidth + undefinedColWidth * undefinedCount + 2
45
- : totalDefinedWidth > 0
46
- ? totalDefinedWidth + 2
47
- : undefined;
48
-
49
- return (
50
- <div
51
- ref={wrapperRef}
52
- className="tableWrapper"
53
- >
54
- <table style={tableWidth ? { width: `${tableWidth}px` } : undefined}>
55
- {colWidths.length > 0 && (
56
- <colgroup>
57
- {colWidths.map((width, index) => (
58
- <col
59
- key={index}
60
- style={
61
- width
62
- ? { width: `${width}px` }
63
- : undefinedColWidth
64
- ? { width: `${undefinedColWidth}px` }
65
- : undefined
66
- }
67
- />
68
- ))}
69
- </colgroup>
70
- )}
71
- <tbody>{children}</tbody>
72
- </table>
73
- </div>
74
- );
75
- }