@overlap/rte 0.1.7 → 0.1.8

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.
@@ -1 +1 @@
1
- {"version":3,"file":"Editor.d.ts","sourceRoot":"","sources":["../../src/components/Editor.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAkD,MAAM,OAAO,CAAC;AASvE,OAAO,EAA4B,WAAW,EAAE,MAAM,UAAU,CAAC;AAwBjE,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,WAAW,CA2exC,CAAC"}
1
+ {"version":3,"file":"Editor.d.ts","sourceRoot":"","sources":["../../src/components/Editor.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAkD,MAAM,OAAO,CAAC;AASvE,OAAO,EAA4B,WAAW,EAAE,MAAM,UAAU,CAAC;AAwBjE,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,WAAW,CAukBxC,CAAC"}
package/dist/index.esm.js CHANGED
@@ -3049,8 +3049,75 @@ const Editor = ({ initialContent, onChange, plugins: providedPlugins, placeholde
3049
3049
  if (onEditorAPIReady)
3050
3050
  onEditorAPIReady(editorAPI);
3051
3051
  }, [editorAPI, onEditorAPIReady]);
3052
+ // --- Helper: insert an image file via the onImageUpload callback ---
3053
+ const insertImageFile = useCallback(async (file) => {
3054
+ if (!onImageUpload || !file.type.startsWith("image/"))
3055
+ return;
3056
+ const editor = editorRef.current;
3057
+ if (!editor)
3058
+ return;
3059
+ try {
3060
+ // Show a placeholder while uploading
3061
+ const placeholder = document.createElement("img");
3062
+ placeholder.setAttribute("data-uploading", "true");
3063
+ placeholder.style.maxWidth = "100%";
3064
+ placeholder.style.height = "auto";
3065
+ placeholder.style.display = "block";
3066
+ placeholder.style.margin = "16px 0";
3067
+ placeholder.style.opacity = "0.5";
3068
+ // Use a tiny transparent gif as placeholder src
3069
+ placeholder.src =
3070
+ "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
3071
+ placeholder.alt = file.name;
3072
+ const sel = window.getSelection();
3073
+ if (sel && sel.rangeCount > 0) {
3074
+ const range = sel.getRangeAt(0);
3075
+ range.deleteContents();
3076
+ range.insertNode(placeholder);
3077
+ range.setStartAfter(placeholder);
3078
+ range.collapse(true);
3079
+ sel.removeAllRanges();
3080
+ sel.addRange(range);
3081
+ }
3082
+ else {
3083
+ editor.appendChild(placeholder);
3084
+ }
3085
+ // Upload
3086
+ const url = await onImageUpload(file);
3087
+ // Replace placeholder with final image
3088
+ placeholder.src = url;
3089
+ placeholder.removeAttribute("data-uploading");
3090
+ placeholder.style.opacity = "1";
3091
+ // Preserve data-attachment-id if returned in a special format
3092
+ // The onImageUpload callback can return "url|attachmentId"
3093
+ if (url.includes("|__aid__:")) {
3094
+ const [realUrl, aid] = url.split("|__aid__:");
3095
+ placeholder.src = realUrl;
3096
+ placeholder.setAttribute("data-attachment-id", aid);
3097
+ }
3098
+ notifyChange(domToContent(editor));
3099
+ }
3100
+ catch (err) {
3101
+ console.error("Image upload failed:", err);
3102
+ // Remove failed placeholder
3103
+ const failedImg = editor.querySelector('img[data-uploading="true"]');
3104
+ failedImg?.remove();
3105
+ }
3106
+ }, [onImageUpload, notifyChange]);
3052
3107
  // --- Paste handler ---
3053
3108
  const handlePaste = (e) => {
3109
+ // Check for pasted image files first
3110
+ const items = e.clipboardData.items;
3111
+ for (let i = 0; i < items.length; i++) {
3112
+ const item = items[i];
3113
+ if (item.type.startsWith("image/")) {
3114
+ e.preventDefault();
3115
+ const file = item.getAsFile();
3116
+ if (file)
3117
+ insertImageFile(file);
3118
+ return;
3119
+ }
3120
+ }
3054
3121
  e.preventDefault();
3055
3122
  const html = e.clipboardData.getData("text/html");
3056
3123
  const text = e.clipboardData.getData("text/plain");
@@ -3117,7 +3184,22 @@ const Editor = ({ initialContent, onChange, plugins: providedPlugins, placeholde
3117
3184
  }),
3118
3185
  }
3119
3186
  : {};
3120
- return (jsxs("div", { className: `rte-container ${className || ""}`, style: containerStyle, children: [jsx(Toolbar, { plugins: plugins, editorAPI: editorAPI, className: toolbarClassName }), jsx("div", { ref: editorRef, contentEditable: true, className: `rte-editor ${editorClassName || ""}`, "data-placeholder": placeholder, onPaste: handlePaste, suppressContentEditableWarning: true })] }));
3187
+ return (jsxs("div", { className: `rte-container ${className || ""}`, style: containerStyle, children: [jsx(Toolbar, { plugins: plugins, editorAPI: editorAPI, className: toolbarClassName }), jsx("div", { ref: editorRef, contentEditable: true, className: `rte-editor ${editorClassName || ""}`, "data-placeholder": placeholder, onPaste: handlePaste, onDrop: (e) => {
3188
+ const files = e.dataTransfer.files;
3189
+ for (let i = 0; i < files.length; i++) {
3190
+ if (files[i].type.startsWith("image/")) {
3191
+ e.preventDefault();
3192
+ insertImageFile(files[i]);
3193
+ return;
3194
+ }
3195
+ }
3196
+ }, onDragOver: (e) => {
3197
+ // Allow drop
3198
+ const types = e.dataTransfer.types;
3199
+ if (types && Array.from(types).includes("Files")) {
3200
+ e.preventDefault();
3201
+ }
3202
+ }, suppressContentEditableWarning: true })] }));
3121
3203
  };
3122
3204
  // --- Helper: Insert Image ---
3123
3205
  function handleInsertImage(editor, value, isUpdatingRef, historyRef, notifyChange) {