@cyberyozh/text-editor 0.1.9 → 0.1.10

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyberyozh/text-editor",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "Rich text editor library based on Tiptap for CyberYozh",
5
5
  "author": "CyberYozh",
6
6
  "license": "MIT",
@@ -1,4 +1,4 @@
1
- import type { ReactNode } from "react";
1
+ import { useEffect, useRef, useState, type ReactNode } from "react";
2
2
 
3
3
  interface TableProps {
4
4
  children: ReactNode;
@@ -9,7 +9,10 @@ interface TableProps {
9
9
  * Applies column widths from Tiptap table cells
10
10
  */
11
11
  export function Table({ children }: TableProps) {
12
- let colWidths: number[] = [];
12
+ const wrapperRef = useRef<HTMLDivElement>(null);
13
+ const [undefinedColWidth, setUndefinedColWidth] = useState<number | undefined>(undefined);
14
+
15
+ let colWidths: (number | undefined)[] = [];
13
16
 
14
17
  if (Array.isArray(children) && children.length > 0) {
15
18
  const firstRow = children[0];
@@ -19,24 +22,47 @@ export function Table({ children }: TableProps) {
19
22
  ? firstRow.props.children
20
23
  : [firstRow.props.children];
21
24
 
22
- colWidths = cells
23
- .map((cell: any) => cell?.props?.node?.attrs?.colwidth?.[0])
24
- .filter((width: number | undefined) => width !== undefined);
25
+ colWidths = cells.map((cell: any) => cell?.props?.node?.attrs?.colwidth?.[0]);
25
26
  }
26
27
  }
27
28
 
29
+ // Calculate widths based on container
30
+ const definedWidths = colWidths.filter((w): w is number => w !== undefined);
31
+ const totalDefinedWidth = definedWidths.reduce((total, width) => total + width, 0);
32
+ const undefinedCount = colWidths.filter((w) => w === undefined).length;
33
+
34
+ useEffect(() => {
35
+ if (wrapperRef.current && undefinedCount > 0) {
36
+ const containerWidth = wrapperRef.current.clientWidth;
37
+ const remainingSpace = containerWidth - totalDefinedWidth;
38
+ const calculatedWidth = Math.floor(remainingSpace / undefinedCount);
39
+ setUndefinedColWidth(calculatedWidth > 0 ? calculatedWidth : undefined);
40
+ }
41
+ }, [totalDefinedWidth, undefinedCount]);
42
+
43
+ // Calculate total table width
28
44
  const tableWidth =
29
- colWidths.length > 0 ? colWidths.reduce((total, width) => total + width, 0) : null;
45
+ undefinedCount > 0 && undefinedColWidth
46
+ ? totalDefinedWidth + (undefinedColWidth * undefinedCount)
47
+ : totalDefinedWidth > 0
48
+ ? totalDefinedWidth
49
+ : undefined;
30
50
 
31
51
  return (
32
- <div className="tableWrapper">
52
+ <div ref={wrapperRef} className="tableWrapper">
33
53
  <table style={tableWidth ? { width: `${tableWidth}px` } : undefined}>
34
54
  {colWidths.length > 0 && (
35
55
  <colgroup>
36
56
  {colWidths.map((width, index) => (
37
57
  <col
38
58
  key={index}
39
- style={{ width: `${width}px` }}
59
+ style={
60
+ width
61
+ ? { width: `${width}px` }
62
+ : undefinedColWidth
63
+ ? { width: `${undefinedColWidth}px` }
64
+ : undefined
65
+ }
40
66
  />
41
67
  ))}
42
68
  </colgroup>