-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…lder [DEV-8821] Feature - Coverage placeholder
- Loading branch information
Showing
13 changed files
with
160 additions
and
4 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
98 changes: 98 additions & 0 deletions
98
packages/slate-editor/src/extensions/placeholders/elements/CoveragePlaceholderElement.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,98 @@ | ||
import type { CoverageNode } from '@prezly/slate-types'; | ||
import React from 'react'; | ||
import { useSlateStatic } from 'slate-react'; | ||
|
||
import { SearchInput } from '#components'; | ||
import { PlaceholderCoverage } from '#icons'; | ||
import { useFunction } from '#lib'; | ||
|
||
import { createCoverage } from '#extensions/coverage'; | ||
import { EventsEditor } from '#modules/events'; | ||
|
||
import { | ||
type Props as BaseProps, | ||
SearchInputPlaceholderElement, | ||
} from '../components/SearchInputPlaceholderElement'; | ||
import { replacePlaceholder } from '../lib'; | ||
import type { PlaceholderNode } from '../PlaceholderNode'; | ||
import { PlaceholdersManager, usePlaceholderManagement } from '../PlaceholdersManager'; | ||
|
||
type CoverageRef = Pick<CoverageNode, 'coverage'>; | ||
|
||
export function CoveragePlaceholderElement({ | ||
children, | ||
element, | ||
getSuggestions, | ||
renderEmpty, | ||
renderSuggestion, | ||
renderSuggestionsFooter, | ||
...props | ||
}: CoveragePlaceholderElement.Props) { | ||
const editor = useSlateStatic(); | ||
|
||
const handleTrigger = useFunction(() => { | ||
PlaceholdersManager.activate(element); | ||
}); | ||
|
||
const handleSelect = useFunction((data: CoverageRef) => { | ||
EventsEditor.dispatchEvent(editor, 'coverage-dialog-submitted', { | ||
coverage_id: data.coverage.id, | ||
}); | ||
|
||
replacePlaceholder(editor, element, createCoverage(data.coverage.id)); | ||
}); | ||
|
||
usePlaceholderManagement(element.type, element.uuid, { | ||
onTrigger: handleTrigger, | ||
}); | ||
|
||
return ( | ||
<SearchInputPlaceholderElement<CoverageRef> | ||
{...props} | ||
element={element} | ||
// Core | ||
format="card" | ||
icon={PlaceholderCoverage} | ||
title="Click to insert coverage" | ||
description="Add your Prezly coverage" | ||
// Input | ||
getSuggestions={getSuggestions} | ||
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="Coverage" | ||
inputDescription="Select coverage to insert" | ||
inputPlaceholder="Search for coverage" | ||
onSelect={handleSelect} | ||
> | ||
{children} | ||
</SearchInputPlaceholderElement> | ||
); | ||
} | ||
|
||
export namespace CoveragePlaceholderElement { | ||
export interface Props | ||
extends Omit< | ||
BaseProps<CoverageRef>, | ||
| 'onSelect' | ||
| 'icon' | ||
| 'title' | ||
| 'description' | ||
| 'inputTitle' | ||
| 'inputDescription' | ||
| 'inputPlaceholder' | ||
| 'renderSuggestions' | ||
> { | ||
element: PlaceholderNode<PlaceholderNode.Type.COVERAGE>; | ||
renderSuggestionsFooter?: BaseProps<CoverageRef>['renderSuggestions']; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/slate-editor/src/extensions/placeholders/elements/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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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