-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Image): convert pasted image urls to images (#464)
Co-authored-by: kseniyakuzina <[email protected]>
- Loading branch information
Showing
9 changed files
with
150 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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,12 @@ | ||
const knownImageHostsRegexString = '(jing|avatars)'; | ||
|
||
const supportedImageExtensionsRegexString = '\\.(jpe?g|png|svgz?|gif|webp)'; | ||
|
||
export const imageUrlRegex = new RegExp( | ||
`^https?:\\/\\/(\\S*?${supportedImageExtensionsRegexString}|${knownImageHostsRegexString}\\S+)$`, | ||
); | ||
|
||
export const imageNameRegex = new RegExp(`\\/([^/]*?)(${supportedImageExtensionsRegexString})?$`); | ||
|
||
export const parseInsertedUrlAsImage = (text: string) => | ||
imageUrlRegex.test(text) ? {imageUrl: text, title: text.match(imageNameRegex)?.[1]} : null; |
This file contains 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 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,54 @@ | ||
import {Plugin} from 'prosemirror-state'; | ||
|
||
import {ExtensionAuto} from '../../../../core'; | ||
import {DataTransferType} from '../../../behavior/Clipboard/utils'; | ||
import {imageType} from '../ImageSpecs'; | ||
|
||
export type ImageUrlPasteOptions = { | ||
/** | ||
* The function, used to determine if the pasted text is the image url and should be inserted as an image | ||
*/ | ||
parseInsertedUrlAsImage?: (text: string) => {imageUrl: string; title?: string} | null; | ||
}; | ||
|
||
export const imageUrlPaste: ExtensionAuto<ImageUrlPasteOptions> = (builder, opts) => { | ||
builder.addPlugin( | ||
() => | ||
new Plugin({ | ||
props: { | ||
handleDOMEvents: { | ||
paste(view, e) { | ||
if ( | ||
!opts.parseInsertedUrlAsImage || | ||
!e.clipboardData || | ||
view.state.selection.$from.parent.type.spec.code | ||
) | ||
return false; | ||
|
||
const {imageUrl, title} = | ||
opts.parseInsertedUrlAsImage( | ||
e.clipboardData.getData(DataTransferType.Text) ?? '', | ||
) || {}; | ||
|
||
if (!imageUrl) { | ||
return false; | ||
} | ||
|
||
e.preventDefault(); | ||
|
||
const imageNode = imageType(view.state.schema).create({ | ||
src: imageUrl, | ||
alt: title, | ||
}); | ||
|
||
const tr = view.state.tr.replaceSelectionWith(imageNode); | ||
view.dispatch(tr.scrollIntoView()); | ||
|
||
return true; | ||
}, | ||
}, | ||
}, | ||
}), | ||
builder.Priority.High, | ||
); | ||
}; |
This file contains 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 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 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 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