@cyberyozh/text-editor 0.1.12 → 0.1.14
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/dist/editor.d.mts +4 -1
- package/dist/editor.d.ts +4 -1
- package/dist/editor.js +80 -5
- package/dist/editor.js.map +1 -1
- package/dist/editor.mjs +78 -4
- package/dist/editor.mjs.map +1 -1
- package/dist/render.d.mts +4 -1
- package/dist/render.d.ts +4 -1
- package/dist/render.js +35 -2
- package/dist/render.js.map +1 -1
- package/dist/render.mjs +33 -1
- package/dist/render.mjs.map +1 -1
- package/package.json +35 -35
- package/src/editor/dialogs/image-dialog/image-dialog.tsx +34 -2
- package/src/editor/editor.tsx +2 -2
- package/src/editor/index.ts +1 -0
- package/src/extensions/editor/figure.ts +21 -1
- package/src/render/index.ts +1 -0
- package/src/utils/html-to-tiptap-content.ts +41 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cyberyozh/text-editor",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"description": "Rich text editor library based on Tiptap for CyberYozh",
|
|
5
5
|
"author": "CyberYozh",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,42 +48,42 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@floating-ui/dom": "^1.7.4",
|
|
51
|
-
"@tiptap/core": "^3.
|
|
52
|
-
"@tiptap/extension-blockquote": "^3.
|
|
53
|
-
"@tiptap/extension-bubble-menu": "^3.
|
|
54
|
-
"@tiptap/extension-code": "^3.
|
|
55
|
-
"@tiptap/extension-code-block-lowlight": "^3.
|
|
56
|
-
"@tiptap/extension-collaboration": "^3.
|
|
57
|
-
"@tiptap/extension-details": "^3.
|
|
58
|
-
"@tiptap/extension-drag-handle": "^3.
|
|
59
|
-
"@tiptap/extension-drag-handle-react": "^3.
|
|
60
|
-
"@tiptap/extension-floating-menu": "^3.
|
|
61
|
-
"@tiptap/extension-hard-break": "^3.
|
|
51
|
+
"@tiptap/core": "^3.17.1",
|
|
52
|
+
"@tiptap/extension-blockquote": "^3.17.1",
|
|
53
|
+
"@tiptap/extension-bubble-menu": "^3.17.1",
|
|
54
|
+
"@tiptap/extension-code": "^3.17.1",
|
|
55
|
+
"@tiptap/extension-code-block-lowlight": "^3.17.1",
|
|
56
|
+
"@tiptap/extension-collaboration": "^3.17.1",
|
|
57
|
+
"@tiptap/extension-details": "^3.17.1",
|
|
58
|
+
"@tiptap/extension-drag-handle": "^3.17.1",
|
|
59
|
+
"@tiptap/extension-drag-handle-react": "^3.17.1",
|
|
60
|
+
"@tiptap/extension-floating-menu": "^3.17.1",
|
|
61
|
+
"@tiptap/extension-hard-break": "^3.17.1",
|
|
62
62
|
"@tiptap/extension-highlight": "^3.10.6",
|
|
63
|
-
"@tiptap/extension-horizontal-rule": "^3.
|
|
64
|
-
"@tiptap/extension-image": "^3.
|
|
65
|
-
"@tiptap/extension-link": "^3.
|
|
66
|
-
"@tiptap/extension-list": "^3.
|
|
67
|
-
"@tiptap/extension-mathematics": "^3.
|
|
68
|
-
"@tiptap/extension-node-range": "^3.
|
|
69
|
-
"@tiptap/extension-subscript": "^3.
|
|
70
|
-
"@tiptap/extension-superscript": "^3.
|
|
71
|
-
"@tiptap/extension-table": "^3.
|
|
72
|
-
"@tiptap/extension-text-align": "^3.
|
|
73
|
-
"@tiptap/extensions": "^3.
|
|
74
|
-
"@tiptap/html": "^3.
|
|
75
|
-
"@tiptap/pm": "^3.
|
|
76
|
-
"@tiptap/react": "^3.
|
|
77
|
-
"@tiptap/starter-kit": "^3.
|
|
78
|
-
"@tiptap/static-renderer": "^3.
|
|
79
|
-
"@tiptap/suggestion": "^3.
|
|
80
|
-
"@tiptap/y-tiptap": "^3.0.
|
|
81
|
-
"emoji-picker-react": "^4.
|
|
82
|
-
"highlight.js": "^11.11.
|
|
83
|
-
"katex": "^0.16.
|
|
63
|
+
"@tiptap/extension-horizontal-rule": "^3.17.1",
|
|
64
|
+
"@tiptap/extension-image": "^3.17.1",
|
|
65
|
+
"@tiptap/extension-link": "^3.17.1",
|
|
66
|
+
"@tiptap/extension-list": "^3.17.1",
|
|
67
|
+
"@tiptap/extension-mathematics": "^3.17.1",
|
|
68
|
+
"@tiptap/extension-node-range": "^3.17.1",
|
|
69
|
+
"@tiptap/extension-subscript": "^3.17.1",
|
|
70
|
+
"@tiptap/extension-superscript": "^3.17.1",
|
|
71
|
+
"@tiptap/extension-table": "^3.17.1",
|
|
72
|
+
"@tiptap/extension-text-align": "^3.17.1",
|
|
73
|
+
"@tiptap/extensions": "^3.17.1",
|
|
74
|
+
"@tiptap/html": "^3.17.1",
|
|
75
|
+
"@tiptap/pm": "^3.17.1",
|
|
76
|
+
"@tiptap/react": "^3.17.1",
|
|
77
|
+
"@tiptap/starter-kit": "^3.17.1",
|
|
78
|
+
"@tiptap/static-renderer": "^3.17.1",
|
|
79
|
+
"@tiptap/suggestion": "^3.17.1",
|
|
80
|
+
"@tiptap/y-tiptap": "^3.0.2",
|
|
81
|
+
"emoji-picker-react": "^4.17.1",
|
|
82
|
+
"highlight.js": "^11.11.1",
|
|
83
|
+
"katex": "^0.16.27",
|
|
84
84
|
"lowlight": "^3.3.0",
|
|
85
|
-
"y-protocols": "^1.0.
|
|
86
|
-
"yjs": "^13.6.
|
|
85
|
+
"y-protocols": "^1.0.7",
|
|
86
|
+
"yjs": "^13.6.29"
|
|
87
87
|
},
|
|
88
88
|
"devDependencies": {
|
|
89
89
|
"@changesets/cli": "^2.29.7",
|
|
@@ -49,14 +49,43 @@ export const ImageDialog = ({
|
|
|
49
49
|
? resolveImageSrc({ fileId: initialAttrs.fileId, src: initialAttrs.src }, { getImageById })
|
|
50
50
|
: "";
|
|
51
51
|
|
|
52
|
+
const getConstrainedDimensions = () => {
|
|
53
|
+
if (!initialAttrs?.width || !initialAttrs?.height) {
|
|
54
|
+
return { width: 0, height: 0 };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let maxWidth: number | undefined;
|
|
58
|
+
try {
|
|
59
|
+
const editorElement = document.querySelector(".tiptap-content-wrapper") as HTMLElement;
|
|
60
|
+
if (editorElement && editorElement.clientWidth > 0) {
|
|
61
|
+
maxWidth = editorElement.clientWidth - 32;
|
|
62
|
+
}
|
|
63
|
+
} catch {}
|
|
64
|
+
|
|
65
|
+
if (maxWidth && initialAttrs.width > maxWidth) {
|
|
66
|
+
const aspectRatio = initialAttrs.height / initialAttrs.width;
|
|
67
|
+
return {
|
|
68
|
+
width: maxWidth,
|
|
69
|
+
height: Math.round(maxWidth * aspectRatio),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
width: initialAttrs.width,
|
|
75
|
+
height: initialAttrs.height,
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const constrainedDimensions = getConstrainedDimensions();
|
|
80
|
+
|
|
52
81
|
const [formState, setFormState] = useState<ImageFormState>({
|
|
53
82
|
file: null,
|
|
54
83
|
title: initialAttrs?.title || "",
|
|
55
84
|
alt: initialAttrs?.alt || "",
|
|
56
85
|
caption: initialAttrs?.caption || "",
|
|
57
86
|
fileUrl: resolvedInitialUrl,
|
|
58
|
-
width:
|
|
59
|
-
height:
|
|
87
|
+
width: constrainedDimensions.width,
|
|
88
|
+
height: constrainedDimensions.height,
|
|
60
89
|
});
|
|
61
90
|
|
|
62
91
|
const handleSelectFile = (file: File | undefined) => {
|
|
@@ -154,6 +183,7 @@ export const ImageDialog = ({
|
|
|
154
183
|
setIsUploading(true);
|
|
155
184
|
try {
|
|
156
185
|
const result = await uploadHandler(formState.file);
|
|
186
|
+
console.log("👀 ~ handleAddImage ~ result:", result);
|
|
157
187
|
|
|
158
188
|
onImageAdd({
|
|
159
189
|
fileId: result.fileId,
|
|
@@ -179,6 +209,8 @@ export const ImageDialog = ({
|
|
|
179
209
|
alt: formState.alt,
|
|
180
210
|
title: formState.title,
|
|
181
211
|
caption: formState.caption,
|
|
212
|
+
width: formState.width || undefined,
|
|
213
|
+
height: formState.height || undefined,
|
|
182
214
|
});
|
|
183
215
|
|
|
184
216
|
handleClose();
|
package/src/editor/editor.tsx
CHANGED
|
@@ -77,7 +77,7 @@ function TiptapContent({
|
|
|
77
77
|
}: TiptapProps) {
|
|
78
78
|
const [linkDialogContext, setLinkDialogContext] = useState<LinkDialogContextProps | null>(null);
|
|
79
79
|
const [katexDialogContext, setKatexDialogContext] = useState<KatexDialogContextProps | null>(
|
|
80
|
-
null
|
|
80
|
+
null,
|
|
81
81
|
);
|
|
82
82
|
const [editFigureContext, setEditFigureContext] = useState<{
|
|
83
83
|
attrs: FigureAttrs;
|
|
@@ -136,7 +136,7 @@ function TiptapContent({
|
|
|
136
136
|
onCreate: ({ editor }) => handleChange?.(editor.getJSON()),
|
|
137
137
|
onBlur: ({ editor }) => handleChange?.(editor.getJSON()),
|
|
138
138
|
},
|
|
139
|
-
[lang]
|
|
139
|
+
[lang],
|
|
140
140
|
);
|
|
141
141
|
|
|
142
142
|
useEffect(() => {
|
package/src/editor/index.ts
CHANGED
|
@@ -17,7 +17,7 @@ export const Figure = BaseFigure.extend({
|
|
|
17
17
|
const attrs = node.attrs;
|
|
18
18
|
|
|
19
19
|
const imageExtension = editor.extensionManager.extensions.find(
|
|
20
|
-
(ext) => ext.name === "image"
|
|
20
|
+
(ext) => ext.name === "image",
|
|
21
21
|
) as typeof BaseFigure | undefined;
|
|
22
22
|
const getImageById = imageExtension?.options.getImageById;
|
|
23
23
|
|
|
@@ -79,6 +79,25 @@ export const Figure = BaseFigure.extend({
|
|
|
79
79
|
if (maxWidth && naturalAspectRatio) {
|
|
80
80
|
maxHeight = Math.round(maxWidth * naturalAspectRatio);
|
|
81
81
|
}
|
|
82
|
+
|
|
83
|
+
if (attrs.width && attrs.height && maxWidth && attrs.width > maxWidth) {
|
|
84
|
+
const width = maxWidth;
|
|
85
|
+
const height = Math.round((attrs.height / attrs.width) * maxWidth);
|
|
86
|
+
|
|
87
|
+
imageContainer.style.width = `${width}px`;
|
|
88
|
+
imageContainer.style.height = `${height}px`;
|
|
89
|
+
|
|
90
|
+
const pos = getPos();
|
|
91
|
+
if (typeof pos === "number") {
|
|
92
|
+
editor
|
|
93
|
+
.chain()
|
|
94
|
+
.updateAttributes("image", {
|
|
95
|
+
width: Math.round(width),
|
|
96
|
+
height: Math.round(height),
|
|
97
|
+
})
|
|
98
|
+
.run();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
82
101
|
};
|
|
83
102
|
|
|
84
103
|
imageContainer.appendChild(img);
|
|
@@ -104,6 +123,7 @@ export const Figure = BaseFigure.extend({
|
|
|
104
123
|
|
|
105
124
|
const resizableView = new ResizableNodeView({
|
|
106
125
|
element: imageContainer,
|
|
126
|
+
editor: editor,
|
|
107
127
|
node,
|
|
108
128
|
getPos,
|
|
109
129
|
onResize: (w, h) => {
|
package/src/render/index.ts
CHANGED
|
@@ -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
|
+
}
|