@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.mjs CHANGED
@@ -1892,14 +1892,39 @@ var ImageDialog = ({
1892
1892
  const [dragActive, setDragActive] = useState6(false);
1893
1893
  const inputRef = useRef6(null);
1894
1894
  const resolvedInitialUrl = initialAttrs ? resolveImageSrc({ fileId: initialAttrs.fileId, src: initialAttrs.src }, { getImageById }) : "";
1895
+ const getConstrainedDimensions = () => {
1896
+ if (!initialAttrs?.width || !initialAttrs?.height) {
1897
+ return { width: 0, height: 0 };
1898
+ }
1899
+ let maxWidth;
1900
+ try {
1901
+ const editorElement = document.querySelector(".tiptap-content-wrapper");
1902
+ if (editorElement && editorElement.clientWidth > 0) {
1903
+ maxWidth = editorElement.clientWidth - 32;
1904
+ }
1905
+ } catch {
1906
+ }
1907
+ if (maxWidth && initialAttrs.width > maxWidth) {
1908
+ const aspectRatio = initialAttrs.height / initialAttrs.width;
1909
+ return {
1910
+ width: maxWidth,
1911
+ height: Math.round(maxWidth * aspectRatio)
1912
+ };
1913
+ }
1914
+ return {
1915
+ width: initialAttrs.width,
1916
+ height: initialAttrs.height
1917
+ };
1918
+ };
1919
+ const constrainedDimensions = getConstrainedDimensions();
1895
1920
  const [formState, setFormState] = useState6({
1896
1921
  file: null,
1897
1922
  title: initialAttrs?.title || "",
1898
1923
  alt: initialAttrs?.alt || "",
1899
1924
  caption: initialAttrs?.caption || "",
1900
1925
  fileUrl: resolvedInitialUrl,
1901
- width: initialAttrs?.width || 0,
1902
- height: initialAttrs?.height || 0
1926
+ width: constrainedDimensions.width,
1927
+ height: constrainedDimensions.height
1903
1928
  });
1904
1929
  const handleSelectFile = (file) => {
1905
1930
  if (!file) return;
@@ -1982,6 +2007,7 @@ var ImageDialog = ({
1982
2007
  setIsUploading(true);
1983
2008
  try {
1984
2009
  const result = await uploadHandler(formState.file);
2010
+ console.log("\u{1F440} ~ handleAddImage ~ result:", result);
1985
2011
  onImageAdd({
1986
2012
  fileId: result.fileId,
1987
2013
  src: result.fileUrl,
@@ -2002,7 +2028,9 @@ var ImageDialog = ({
2002
2028
  onImageEdit({
2003
2029
  alt: formState.alt,
2004
2030
  title: formState.title,
2005
- caption: formState.caption
2031
+ caption: formState.caption,
2032
+ width: formState.width || void 0,
2033
+ height: formState.height || void 0
2006
2034
  });
2007
2035
  handleClose();
2008
2036
  };
@@ -5215,6 +5243,19 @@ var Figure2 = BaseFigure.extend({
5215
5243
  if (maxWidth && naturalAspectRatio) {
5216
5244
  maxHeight = Math.round(maxWidth * naturalAspectRatio);
5217
5245
  }
5246
+ if (attrs.width && attrs.height && maxWidth && attrs.width > maxWidth) {
5247
+ const width = maxWidth;
5248
+ const height = Math.round(attrs.height / attrs.width * maxWidth);
5249
+ imageContainer.style.width = `${width}px`;
5250
+ imageContainer.style.height = `${height}px`;
5251
+ const pos = getPos();
5252
+ if (typeof pos === "number") {
5253
+ editor.chain().updateAttributes("image", {
5254
+ width: Math.round(width),
5255
+ height: Math.round(height)
5256
+ }).run();
5257
+ }
5258
+ }
5218
5259
  };
5219
5260
  imageContainer.appendChild(img);
5220
5261
  if (editor.isEditable) {
@@ -5234,6 +5275,7 @@ var Figure2 = BaseFigure.extend({
5234
5275
  }
5235
5276
  const resizableView = new ResizableNodeView({
5236
5277
  element: imageContainer,
5278
+ editor,
5237
5279
  node,
5238
5280
  getPos,
5239
5281
  onResize: (w, h) => {
@@ -6424,7 +6466,39 @@ var Tiptap = (props) => {
6424
6466
  const translations2 = getTranslations(lang);
6425
6467
  return /* @__PURE__ */ jsx92(TiptapTranslationsProvider, { translations: translations2, children: /* @__PURE__ */ jsx92(DialogCommandProvider, { children: /* @__PURE__ */ jsx92(TiptapContent, { ...props }) }) });
6426
6468
  };
6469
+
6470
+ // src/utils/html-to-tiptap-content.ts
6471
+ import { generateJSON } from "@tiptap/core";
6472
+ function isJSONContent(value) {
6473
+ return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
6474
+ }
6475
+ function htmlToTiptapContent(content) {
6476
+ if (isJSONContent(content)) {
6477
+ return content;
6478
+ }
6479
+ if (typeof content === "string") {
6480
+ try {
6481
+ const parsed = JSON.parse(content);
6482
+ if (isJSONContent(parsed)) {
6483
+ return parsed;
6484
+ }
6485
+ } catch {
6486
+ }
6487
+ }
6488
+ if (!content || content.trim() === "") {
6489
+ return { type: "doc", content: [{ type: "paragraph" }] };
6490
+ }
6491
+ const sharedExtensions = getSharedExtensions();
6492
+ const extensions = [...sharedExtensions, BaseFigure];
6493
+ try {
6494
+ return generateJSON(content, extensions);
6495
+ } catch (error) {
6496
+ console.error("Failed to convert HTML to Tiptap content:", error);
6497
+ return { type: "doc", content: [{ type: "paragraph" }] };
6498
+ }
6499
+ }
6427
6500
  export {
6428
- Tiptap
6501
+ Tiptap,
6502
+ htmlToTiptapContent
6429
6503
  };
6430
6504
  //# sourceMappingURL=editor.mjs.map