@cyberyozh/text-editor 0.1.12 → 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.
- package/dist/editor.d.mts +4 -1
- package/dist/editor.d.ts +4 -1
- package/dist/editor.js +78 -5
- package/dist/editor.js.map +1 -1
- package/dist/editor.mjs +76 -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 +1 -1
- package/src/editor/dialogs/image-dialog/image-dialog.tsx +33 -2
- package/src/editor/editor.tsx +2 -2
- package/src/editor/index.ts +1 -0
- package/src/extensions/editor/figure.ts +20 -1
- package/src/render/index.ts +1 -0
- package/src/utils/html-to-tiptap-content.ts +41 -0
package/package.json
CHANGED
|
@@ -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) => {
|
|
@@ -179,6 +208,8 @@ export const ImageDialog = ({
|
|
|
179
208
|
alt: formState.alt,
|
|
180
209
|
title: formState.title,
|
|
181
210
|
caption: formState.caption,
|
|
211
|
+
width: formState.width || undefined,
|
|
212
|
+
height: formState.height || undefined,
|
|
182
213
|
});
|
|
183
214
|
|
|
184
215
|
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);
|
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
|
+
}
|