-
Notifications
You must be signed in to change notification settings - Fork 13
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
[DEV-12566] Feature - Implement GalleryBookmark extension #529
Merged
kudlajz
merged 6 commits into
main
from
feature/dev-12566-implement-media-gallery-picker-in-the-editor
Feb 28, 2024
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
92bb351
Implement GalleryBookmark extension
kudlajz 9a5e471
Make prettier happy
kudlajz 5374541
Remove extra props
kudlajz 1a64b39
Update placeholder
kudlajz f2ac5e8
Make the notification more clear
kudlajz b0c5540
v0.104.0-alpha.0
kudlajz 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
7 changes: 7 additions & 0 deletions
7
packages/slate-editor/src/extensions/gallery-bookmark/GalleryBookmarkExtension.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,7 @@ | ||
import type { Extension } from '@prezly/slate-commons'; | ||
|
||
export const EXTENSION_ID = 'GalleryBookmarkExtension'; | ||
|
||
export const GalleryBookmarkExtension = (): Extension => ({ | ||
id: EXTENSION_ID, | ||
}); |
2 changes: 2 additions & 0 deletions
2
packages/slate-editor/src/extensions/gallery-bookmark/index.ts
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,2 @@ | ||
export { GalleryBookmarkExtension, EXTENSION_ID } from './GalleryBookmarkExtension'; | ||
export { createGalleryBookmark } from './lib'; |
26 changes: 26 additions & 0 deletions
26
packages/slate-editor/src/extensions/gallery-bookmark/lib/createGalleryBookmark.ts
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,26 @@ | ||
import { BookmarkNode } from '@prezly/slate-types'; | ||
import { v4 as uuidV4 } from 'uuid'; | ||
|
||
type RequiredProps = Pick<BookmarkNode, 'url' | 'oembed'>; | ||
type OptionalProps = Omit<BookmarkNode, 'type' | 'children'>; | ||
|
||
function withoutExtraAttributes<T extends BookmarkNode>(node: T): BookmarkNode { | ||
const { type, children, layout, new_tab, show_thumbnail, uuid, url, oembed, ...extra } = node; | ||
if (Object.keys(extra).length === 0) { | ||
return node; | ||
} | ||
|
||
return { type, children, layout, new_tab, show_thumbnail, uuid, url, oembed }; | ||
} | ||
|
||
export function createGalleryBookmark(props: RequiredProps & Partial<OptionalProps>): BookmarkNode { | ||
return withoutExtraAttributes({ | ||
layout: BookmarkNode.Layout.VERTICAL, | ||
new_tab: false, | ||
show_thumbnail: true, | ||
uuid: uuidV4(), | ||
...props, | ||
children: [{ text: '' }], | ||
type: BookmarkNode.TYPE, | ||
}); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/slate-editor/src/extensions/gallery-bookmark/lib/index.ts
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 @@ | ||
export * from './createGalleryBookmark'; |
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
131 changes: 131 additions & 0 deletions
131
...s/slate-editor/src/extensions/placeholders/elements/GalleryBookmarkPlaceholderElement.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,131 @@ | ||
import type { NewsroomGallery } from '@prezly/sdk'; | ||
import type { BookmarkNode } from '@prezly/slate-types'; | ||
import React from 'react'; | ||
import { useSelected, useSlateStatic } from 'slate-react'; | ||
|
||
import { SearchInput } from '#components'; | ||
import { PlaceholderGallery } from '#icons'; | ||
import { useFunction } from '#lib'; | ||
|
||
import { EventsEditor } from '#modules/events'; | ||
|
||
import { createGalleryBookmark } from '../../gallery-bookmark'; | ||
import type { Props as PlaceholderElementProps } from '../components/PlaceholderElement'; | ||
import { | ||
type Props as BaseProps, | ||
SearchInputPlaceholderElement, | ||
} from '../components/SearchInputPlaceholderElement'; | ||
import { replacePlaceholder } from '../lib'; | ||
import type { PlaceholderNode } from '../PlaceholderNode'; | ||
import { PlaceholdersManager, usePlaceholderManagement } from '../PlaceholdersManager'; | ||
import type { FetchOEmbedFn } from '../types'; | ||
|
||
export function GalleryBookmarkPlaceholderElement({ | ||
children, | ||
element, | ||
fetchOembed, | ||
format = 'card', | ||
getSuggestions, | ||
removable, | ||
renderAddon, | ||
renderEmpty, | ||
renderSuggestion, | ||
renderSuggestionsFooter, | ||
...props | ||
}: GalleryBookmarkPlaceholderElement.Props) { | ||
const editor = useSlateStatic(); | ||
const isSelected = useSelected(); | ||
|
||
const handleTrigger = useFunction(() => { | ||
PlaceholdersManager.activate(element); | ||
}); | ||
|
||
const handleSelect = useFunction(async (uuid: string, { url }: NewsroomGallery) => { | ||
EventsEditor.dispatchEvent(editor, 'gallery-bookmark-placeholder-submitted', { | ||
gallery: { uuid }, | ||
}); | ||
|
||
const loading = fetchOembed(url).then( | ||
(oembed) => ({ oembed, url }), | ||
() => ({ url }), // `oembed` is undefined if an error occurred | ||
); | ||
|
||
PlaceholdersManager.register(element.type, element.uuid, loading); | ||
PlaceholdersManager.deactivateAll(); | ||
}); | ||
|
||
const handleData = useFunction( | ||
(data: { url: BookmarkNode['url']; oembed?: BookmarkNode['oembed'] }) => { | ||
const { url, oembed } = data; | ||
if (!oembed) { | ||
EventsEditor.dispatchEvent(editor, 'notification', { | ||
children: 'Provided URL does not exist or is not supported.', | ||
type: 'error', | ||
}); | ||
return; | ||
} | ||
replacePlaceholder(editor, element, createGalleryBookmark({ url, oembed }), { | ||
select: isSelected, | ||
}); | ||
}, | ||
); | ||
|
||
usePlaceholderManagement(element.type, element.uuid, { | ||
onTrigger: handleTrigger, | ||
onResolve: handleData, | ||
}); | ||
|
||
return ( | ||
<SearchInputPlaceholderElement<NewsroomGallery> | ||
{...props} | ||
element={element} | ||
// Core | ||
format={format} | ||
icon={PlaceholderGallery} | ||
title="Click to insert a media gallery bookmark" | ||
description="Add a link to your media gallery" | ||
// Input | ||
getSuggestions={getSuggestions} | ||
renderAddon={renderAddon} | ||
renderEmpty={renderEmpty} | ||
renderSuggestion={renderSuggestion} | ||
renderSuggestions={(props) => ( | ||
<SearchInput.Suggestions | ||
activeElement={props.activeElement} | ||
query={props.query} | ||
suggestions={props.suggestions} | ||
footer={renderSuggestionsFooter?.(props)} | ||
> | ||
{props.children} | ||
</SearchInput.Suggestions> | ||
)} | ||
inputTitle="Media gallery bookmark" | ||
inputDescription="Add a media gallery card to your stories, campaigns and pitches" | ||
inputPlaceholder="Search media galleries" | ||
onSelect={handleSelect} | ||
removable={removable} | ||
> | ||
{children} | ||
</SearchInputPlaceholderElement> | ||
); | ||
} | ||
|
||
export namespace GalleryBookmarkPlaceholderElement { | ||
export interface Props | ||
extends Omit< | ||
BaseProps<NewsroomGallery>, | ||
| 'onSelect' | ||
| 'icon' | ||
| 'title' | ||
| 'description' | ||
| 'inputTitle' | ||
| 'inputDescription' | ||
| 'inputPlaceholder' | ||
| 'renderSuggestions' | ||
>, | ||
Pick<PlaceholderElementProps, 'removable'> { | ||
element: PlaceholderNode<PlaceholderNode.Type.GALLERY_BOOKMARK>; | ||
fetchOembed: FetchOEmbedFn; | ||
renderSuggestionsFooter?: BaseProps<NewsroomGallery>['renderSuggestions']; | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The extension feels kinda useless here since it doesn't provide anything and we're using web bookmarks under the hood.
The only thing is the
createGalleryBookmark
function, but I was thinking if it would be easier to just place it inline to theGalleryBookmarkPlaceholderElement
and get rid of this whole extension? 🤔