-
Notifications
You must be signed in to change notification settings - Fork 52
feat: register images from paste + HTML insertion, fixes #790 #834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ba46c18
feat: register images from paste + HTML insertion, fixes #790
johanneswilm 930e3ba
Merge branch 'main' into register-images
johanneswilm 7181284
chore: lint
johanneswilm 2781b1d
Merge branch 'register-images' of github.com:johanneswilm/SuperDoc in…
johanneswilm 2e275c4
chore: lint test
johanneswilm 83215d7
Merge branch 'main' of github.com:Harbour-Enterprises/SuperDoc into r…
johanneswilm 9ece456
chore: reset exporter.js
johanneswilm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --ignore-dir=packages/superdoc/dist | ||
| --ignore-dir=packages/super-editor/dist/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/super-editor/src/extensions/image/imageHelpers/handleBase64.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| const simpleHash = (str) => { | ||
| let hash = 0; | ||
| for (let i = 0; i < str.length; i++) { | ||
| const char = str.charCodeAt(i); | ||
| hash = (hash << 5) - hash + char; | ||
| hash = hash & hash; // Convert to 32-bit integer | ||
| } | ||
| return Math.abs(hash).toString(); | ||
| }; | ||
|
|
||
| export const base64ToFile = (base64String) => { | ||
| const arr = base64String.split(','); | ||
| const mimeMatch = arr[0].match(/:(.*?);/); | ||
| const mimeType = mimeMatch ? mimeMatch[1] : ''; | ||
| const data = arr[1]; | ||
|
|
||
| // Decode the base64 string | ||
| const binaryString = atob(data); | ||
|
|
||
| // Generate filename using a hash of the binary data | ||
| const hash = simpleHash(binaryString); | ||
| const extension = mimeType.split('/')[1] || 'bin'; // Simple way to get extension | ||
| const filename = `image-${hash}.${extension}`; | ||
|
|
||
| // Create a typed array from the binary string | ||
| const bytes = new Uint8Array(binaryString.length); | ||
| for (let i = 0; i < binaryString.length; i++) { | ||
| bytes[i] = binaryString.charCodeAt(i); | ||
| } | ||
|
|
||
| // Create a Blob and then a File | ||
| const blob = new Blob([bytes], { type: mimeType }); | ||
| return new File([blob], filename, { type: mimeType }); | ||
| }; |
106 changes: 106 additions & 0 deletions
106
packages/super-editor/src/extensions/image/imageHelpers/handleUrl.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * Handles URL to File conversion with comprehensive CORS error handling | ||
| */ | ||
|
|
||
| /** | ||
| * Converts a URL to a File object with proper CORS error handling | ||
| * @param {string} url - The image URL to fetch | ||
| * @param {string} [filename] - Optional filename for the resulting file | ||
| * @param {string} [mimeType] - Optional MIME type for the resulting file | ||
| * @returns {Promise<File|null>} File object or null if CORS prevents access | ||
| */ | ||
| export const urlToFile = async (url, filename, mimeType) => { | ||
| try { | ||
| // Try to fetch the image with credentials mode set to 'omit' to avoid CORS preflight | ||
| const response = await fetch(url, { | ||
| mode: 'cors', | ||
| credentials: 'omit', | ||
| headers: { | ||
| // Add common headers that might help with CORS | ||
| Accept: 'image/*,*/*;q=0.8', | ||
| }, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| console.warn(`Failed to fetch image from ${url}: ${response.status} ${response.statusText}`); | ||
| return null; | ||
| } | ||
|
|
||
| const blob = await response.blob(); | ||
|
|
||
| // Extract filename from URL if not provided | ||
| const finalFilename = filename || extractFilenameFromUrl(url); | ||
|
|
||
| // Determine MIME type from response if not provided | ||
| const finalMimeType = mimeType || response.headers.get('content-type') || blob.type || 'image/jpeg'; | ||
|
|
||
| return new File([blob], finalFilename, { type: finalMimeType }); | ||
| } catch (error) { | ||
| if (isCorsError(error)) { | ||
| console.warn(`CORS policy prevents accessing image from ${url}:`, error.message); | ||
| return null; | ||
| } | ||
|
|
||
| console.error(`Error fetching image from ${url}:`, error); | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Checks if an error is likely a CORS-related error | ||
| * @param {Error} error - The error to check | ||
| * @returns {boolean} True if the error appears to be CORS-related | ||
| */ | ||
| const isCorsError = (error) => { | ||
| const errorMessage = error.message.toLowerCase(); | ||
| const errorName = error.name.toLowerCase(); | ||
|
|
||
| return ( | ||
| errorName.includes('cors') || | ||
| errorMessage.includes('cors') || | ||
| errorMessage.includes('cross-origin') || | ||
| errorMessage.includes('access-control') || | ||
| errorMessage.includes('network error') || // Often indicates CORS in browsers | ||
| errorMessage.includes('failed to fetch') // Common CORS error message | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Extracts a filename from a URL | ||
| * @param {string} url - The URL to extract filename from | ||
| * @returns {string} The extracted filename | ||
| */ | ||
| const extractFilenameFromUrl = (url) => { | ||
| try { | ||
| const urlObj = new URL(url); | ||
| const pathname = urlObj.pathname; | ||
| const filename = pathname.split('/').pop(); | ||
|
|
||
| // If no extension, add a default one | ||
| if (filename && !filename.includes('.')) { | ||
| return `${filename}.jpg`; | ||
| } | ||
|
|
||
| return filename || 'image.jpg'; | ||
| } catch { | ||
| return 'image.jpg'; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Validates if a URL can be accessed without CORS issues | ||
| * @param {string} url - The URL to validate | ||
| * @returns {Promise<boolean>} True if the URL is accessible without CORS issues | ||
| */ | ||
| export const validateUrlAccessibility = async (url) => { | ||
| try { | ||
| const response = await fetch(url, { | ||
| method: 'HEAD', | ||
| mode: 'cors', | ||
| credentials: 'omit', | ||
| }); | ||
| return response.ok; | ||
| } catch (_error) { | ||
| return false; | ||
| } | ||
| }; | ||
57 changes: 0 additions & 57 deletions
57
packages/super-editor/src/extensions/image/imageHelpers/imagePlaceholderPlugin.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.