-
-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added "Image from URL" option and markdown image syntax support…
… to Super. Also allows pasting images from copied web content (#2218)
- Loading branch information
1 parent
adff597
commit a15fc1e
Showing
14 changed files
with
333 additions
and
31 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
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
12 changes: 12 additions & 0 deletions
12
.../src/javascripts/Components/SuperEditor/Plugins/BlockPickerPlugin/Options/RemoteImage.tsx
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 @@ | ||
import { LexicalIconName } from '@/Components/Icon/LexicalIcons' | ||
import { GetRemoteImageBlock } from '../../Blocks/RemoteImage' | ||
import { BlockPickerOption } from '../BlockPickerOption' | ||
|
||
export function GetRemoteImageBlockOption(onSelect: () => void) { | ||
const block = GetRemoteImageBlock(onSelect) | ||
return new BlockPickerOption(block.name, { | ||
iconName: block.iconName as LexicalIconName, | ||
keywords: block.keywords, | ||
onSelect: block.onSelect, | ||
}) | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/web/src/javascripts/Components/SuperEditor/Plugins/Blocks/RemoteImage.tsx
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,3 @@ | ||
export function GetRemoteImageBlock(onSelect: () => void) { | ||
return { name: 'Image from URL', iconName: 'file-image', keywords: ['image', 'url'], onSelect } | ||
} |
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
88 changes: 88 additions & 0 deletions
88
...src/javascripts/Components/SuperEditor/Plugins/RemoteImagePlugin/RemoteImageComponent.tsx
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,88 @@ | ||
import { useApplication } from '@/Components/ApplicationProvider' | ||
import Icon from '@/Components/Icon/Icon' | ||
import Spinner from '@/Components/Spinner/Spinner' | ||
import { isDesktopApplication } from '@/Utils' | ||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' | ||
import { classNames } from '@standardnotes/snjs' | ||
import { useCallback, useState } from 'react' | ||
import { $createFileNode } from '../EncryptedFilePlugin/Nodes/FileUtils' | ||
import { RemoteImageNode } from './RemoteImageNode' | ||
|
||
const RemoteImageComponent = ({ src, alt, node }: { src: string; alt?: string; node: RemoteImageNode }) => { | ||
const application = useApplication() | ||
const [editor] = useLexicalComposerContext() | ||
|
||
const [didImageLoad, setDidImageLoad] = useState(false) | ||
const [isSaving, setIsSaving] = useState(false) | ||
|
||
const fetchAndUploadImage = useCallback(async () => { | ||
setIsSaving(true) | ||
try { | ||
const response = await fetch(src) | ||
|
||
if (!response.ok) { | ||
return | ||
} | ||
|
||
const blob = await response.blob() | ||
const file = new File([blob], src, { type: blob.type }) | ||
|
||
const { filesController, linkingController } = application.getViewControllerManager() | ||
|
||
const uploadedFile = await filesController.uploadNewFile(file, false) | ||
|
||
if (!uploadedFile) { | ||
return | ||
} | ||
|
||
editor.update(() => { | ||
const fileNode = $createFileNode(uploadedFile.uuid) | ||
node.replace(fileNode) | ||
}) | ||
|
||
void linkingController.linkItemToSelectedItem(uploadedFile) | ||
} catch (error) { | ||
console.error(error) | ||
} finally { | ||
setIsSaving(false) | ||
} | ||
}, [application, editor, node, src]) | ||
|
||
const canShowSaveButton = application.isNativeMobileWeb() || isDesktopApplication() | ||
|
||
return ( | ||
<div className="relative flex min-h-[2rem] flex-col items-center gap-2.5"> | ||
<img | ||
alt={alt} | ||
src={src} | ||
onLoad={() => { | ||
setDidImageLoad(true) | ||
}} | ||
/> | ||
{didImageLoad && canShowSaveButton && ( | ||
<button | ||
className={classNames( | ||
'flex items-center gap-2.5 rounded border border-border bg-default px-2.5 py-1.5', | ||
!isSaving && 'hover:bg-info hover:text-info-contrast', | ||
)} | ||
onClick={fetchAndUploadImage} | ||
disabled={isSaving} | ||
> | ||
{isSaving ? ( | ||
<> | ||
<Spinner className="h-4 w-4" /> | ||
Saving... | ||
</> | ||
) : ( | ||
<> | ||
<Icon type="download" /> | ||
Save image to Files | ||
</> | ||
)} | ||
</button> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default RemoteImageComponent |
89 changes: 89 additions & 0 deletions
89
.../web/src/javascripts/Components/SuperEditor/Plugins/RemoteImagePlugin/RemoteImageNode.tsx
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,89 @@ | ||
import { DecoratorBlockNode, SerializedDecoratorBlockNode } from '@lexical/react/LexicalDecoratorBlockNode' | ||
import { DOMConversionMap, DOMExportOutput, LexicalNode, Spread } from 'lexical' | ||
import RemoteImageComponent from './RemoteImageComponent' | ||
|
||
type SerializedRemoteImageNode = Spread< | ||
{ | ||
version: 1 | ||
type: 'unencrypted-image' | ||
alt: string | undefined | ||
src: string | ||
}, | ||
SerializedDecoratorBlockNode | ||
> | ||
|
||
export class RemoteImageNode extends DecoratorBlockNode { | ||
__alt: string | undefined | ||
__src: string | ||
|
||
static getType(): string { | ||
return 'unencrypted-image' | ||
} | ||
|
||
constructor(src: string, alt?: string) { | ||
super() | ||
this.__src = src | ||
this.__alt = alt | ||
} | ||
|
||
static clone(node: RemoteImageNode): RemoteImageNode { | ||
return new RemoteImageNode(node.__src, node.__alt) | ||
} | ||
|
||
static importJSON(serializedNode: SerializedRemoteImageNode): RemoteImageNode { | ||
const node = $createRemoteImageNode(serializedNode.src, serializedNode.alt) | ||
return node | ||
} | ||
|
||
exportJSON(): SerializedRemoteImageNode { | ||
return { | ||
...super.exportJSON(), | ||
src: this.__src, | ||
alt: this.__alt, | ||
version: 1, | ||
type: 'unencrypted-image', | ||
} | ||
} | ||
|
||
static importDOM(): DOMConversionMap<HTMLDivElement> | null { | ||
return { | ||
img: (domNode: HTMLDivElement) => { | ||
if (domNode.tagName !== 'IMG') { | ||
return null | ||
} | ||
return { | ||
conversion: () => { | ||
if (!(domNode instanceof HTMLImageElement)) { | ||
return null | ||
} | ||
return { | ||
node: $createRemoteImageNode(domNode.currentSrc || domNode.src, domNode.alt), | ||
} | ||
}, | ||
priority: 2, | ||
} | ||
}, | ||
} | ||
} | ||
|
||
exportDOM(): DOMExportOutput { | ||
const element = document.createElement('img') | ||
if (this.__alt) { | ||
element.setAttribute('alt', this.__alt) | ||
} | ||
element.setAttribute('src', this.__src) | ||
return { element } | ||
} | ||
|
||
decorate(): JSX.Element { | ||
return <RemoteImageComponent node={this} src={this.__src} alt={this.__alt} /> | ||
} | ||
} | ||
|
||
export function $isRemoteImageNode(node: RemoteImageNode | LexicalNode | null | undefined): node is RemoteImageNode { | ||
return node instanceof RemoteImageNode | ||
} | ||
|
||
export function $createRemoteImageNode(src: string, alt?: string): RemoteImageNode { | ||
return new RemoteImageNode(src, alt) | ||
} |
56 changes: 56 additions & 0 deletions
56
...eb/src/javascripts/Components/SuperEditor/Plugins/RemoteImagePlugin/RemoteImagePlugin.tsx
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,56 @@ | ||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' | ||
import { $createParagraphNode, $insertNodes, $isRootOrShadowRoot, COMMAND_PRIORITY_NORMAL } from 'lexical' | ||
import { useEffect, useState } from 'react' | ||
import Button from '../../Lexical/UI/Button' | ||
import { DialogActions } from '../../Lexical/UI/Dialog' | ||
import TextInput from '../../Lexical/UI/TextInput' | ||
import { INSERT_REMOTE_IMAGE_COMMAND } from '../Commands' | ||
import { $createRemoteImageNode } from './RemoteImageNode' | ||
import { $wrapNodeInElement } from '@lexical/utils' | ||
|
||
export function InsertRemoteImageDialog({ onClose }: { onClose: () => void }) { | ||
const [url, setURL] = useState('') | ||
const [editor] = useLexicalComposerContext() | ||
|
||
const onClick = () => { | ||
if (url.length < 1) { | ||
return | ||
} | ||
|
||
editor.dispatchCommand(INSERT_REMOTE_IMAGE_COMMAND, url) | ||
onClose() | ||
} | ||
|
||
return ( | ||
<> | ||
<TextInput label="URL:" onChange={setURL} value={url} /> | ||
<DialogActions> | ||
<Button onClick={onClick}>Confirm</Button> | ||
</DialogActions> | ||
</> | ||
) | ||
} | ||
|
||
export default function RemoteImagePlugin() { | ||
const [editor] = useLexicalComposerContext() | ||
|
||
useEffect(() => { | ||
return editor.registerCommand<string>( | ||
INSERT_REMOTE_IMAGE_COMMAND, | ||
(payload) => { | ||
const imageNode = $createRemoteImageNode(payload) | ||
$insertNodes([imageNode]) | ||
if ($isRootOrShadowRoot(imageNode.getParentOrThrow())) { | ||
$wrapNodeInElement(imageNode, $createParagraphNode).selectEnd() | ||
} | ||
const newLineNode = $createParagraphNode() | ||
$insertNodes([newLineNode]) | ||
|
||
return true | ||
}, | ||
COMMAND_PRIORITY_NORMAL, | ||
) | ||
}, [editor]) | ||
|
||
return null | ||
} |
Oops, something went wrong.