@cyberyozh/text-editor 0.1.9 → 0.1.11

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.11",
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,48 @@ 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
+ 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
+
28
42
  const tableWidth =
29
- colWidths.length > 0 ? colWidths.reduce((total, width) => total + width, 0) : null;
43
+ undefinedCount > 0 && undefinedColWidth
44
+ ? totalDefinedWidth + undefinedColWidth * undefinedCount + 2
45
+ : totalDefinedWidth > 0
46
+ ? totalDefinedWidth + 2
47
+ : undefined;
30
48
 
31
49
  return (
32
- <div className="tableWrapper">
50
+ <div
51
+ ref={wrapperRef}
52
+ className="tableWrapper"
53
+ >
33
54
  <table style={tableWidth ? { width: `${tableWidth}px` } : undefined}>
34
55
  {colWidths.length > 0 && (
35
56
  <colgroup>
36
57
  {colWidths.map((width, index) => (
37
58
  <col
38
59
  key={index}
39
- style={{ width: `${width}px` }}
60
+ style={
61
+ width
62
+ ? { width: `${width}px` }
63
+ : undefinedColWidth
64
+ ? { width: `${undefinedColWidth}px` }
65
+ : undefined
66
+ }
40
67
  />
41
68
  ))}
42
69
  </colgroup>