-
-
Notifications
You must be signed in to change notification settings - Fork 68
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
Integrated CTA Node Component into Koenig-Lexical #1413
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1c072fe
Integrated CTA Node into Koenig-Lexical
ronaldlangeveld 8f64aa5
Merge branch 'main' into added-call-to-action-nodes-components
ronaldlangeveld 2c2608c
Added CTA Card UI component to CTA Node Comp
ronaldlangeveld ad68da6
Added buttonColor to node
ronaldlangeveld 87cafbd
Added HTML Editor
ronaldlangeveld 8fa8894
Wired up html editor
ronaldlangeveld 51ee88a
Added contentVisibilityAlpha flag
ronaldlangeveld 6ea054a
Added cta-card.test
ronaldlangeveld f86fa2b
Fixed linting
ronaldlangeveld 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 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
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,81 @@ | ||
import CalloutCardIcon from '../assets/icons/kg-card-type-callout.svg?react'; | ||
import KoenigCardWrapper from '../components/KoenigCardWrapper'; | ||
import {BASIC_NODES} from '../index.js'; | ||
import {CallToActionNode as BaseCallToActionNode} from '@tryghost/kg-default-nodes'; | ||
import {CallToActionNodeComponent} from './CallToActionNodeComponent'; | ||
import {createCommand} from 'lexical'; | ||
import {populateNestedEditor, setupNestedEditor} from '../utils/nested-editors'; | ||
|
||
export const INSERT_CTA_COMMAND = createCommand(); | ||
|
||
export class CallToActionNode extends BaseCallToActionNode { | ||
__htmlEditor; | ||
__htmlEditorInitialState; | ||
// TODO: Improve the copy of the menu item | ||
static kgMenu = { | ||
label: 'Call to Action', | ||
desc: 'Add a call to action to your post', | ||
Icon: CalloutCardIcon, // TODO: Replace with correct icon | ||
insertCommand: INSERT_CTA_COMMAND, | ||
matches: ['cta', 'call-to-action'], | ||
priority: 10, | ||
shortcut: '/cta', | ||
isHidden: ({config}) => { | ||
return !(config?.feature?.contentVisibilityAlpha === true); | ||
} | ||
}; | ||
|
||
static getType() { | ||
return 'call-to-action'; | ||
} | ||
|
||
getIcon() { | ||
// TODO: replace with correct icon | ||
return CalloutCardIcon; | ||
} | ||
|
||
constructor(dataset = {}, key) { | ||
super(dataset, key); | ||
|
||
// set up nested editor instances | ||
setupNestedEditor(this, '__htmlEditor', {editor: dataset.htmlEditor, nodes: BASIC_NODES}); | ||
|
||
// populate nested editors on initial construction | ||
if (!dataset.htmlEditor) { | ||
populateNestedEditor(this, '__htmlEditor', dataset.html || '<p>Hey <code>{first_name, "there"}</code>,</p>'); | ||
} | ||
} | ||
|
||
decorate() { | ||
return ( | ||
<KoenigCardWrapper | ||
nodeKey={this.getKey()} | ||
wrapperStyle="wide" | ||
> | ||
<CallToActionNodeComponent | ||
backgroundColor={this.backgroundColor} | ||
buttonColor={this.buttonColor} | ||
buttonText={this.buttonText} | ||
buttonUrl={this.buttonUrl} | ||
hasBackground={this.hasBackground} | ||
hasImage={this.hasImage} | ||
hasSponsorLabel={this.hasSponsorLabel} | ||
htmlEditor={this.__htmlEditor} | ||
imageUrl={this.imageUrl} | ||
layout={this.layout} | ||
nodeKey={this.getKey()} | ||
showButton={this.showButton} | ||
textValue={this.textValue} | ||
/> | ||
</KoenigCardWrapper> | ||
); | ||
} | ||
} | ||
|
||
export function $createCallToActionNode(dataset) { | ||
return new CallToActionNode(dataset); | ||
} | ||
|
||
export function $isCallToActionNode(node) { | ||
return node instanceof CallToActionNode; | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/koenig-lexical/src/nodes/CallToActionNodeComponent.jsx
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,85 @@ | ||
import CardContext from '../context/CardContext'; | ||
import KoenigComposerContext from '../context/KoenigComposerContext.jsx'; | ||
import React from 'react'; | ||
import {ActionToolbar} from '../components/ui/ActionToolbar.jsx'; | ||
import {CtaCard} from '../components/ui/cards/CtaCard'; | ||
import {SnippetActionToolbar} from '../components/ui/SnippetActionToolbar.jsx'; | ||
import {ToolbarMenu, ToolbarMenuItem, ToolbarMenuSeparator} from '../components/ui/ToolbarMenu.jsx'; | ||
|
||
export const CallToActionNodeComponent = ({ | ||
nodeKey, | ||
backgroundColor, | ||
buttonText, | ||
buttonUrl, | ||
hasBackground, | ||
hasImage, | ||
hasSponsorLabel, | ||
imageUrl, | ||
layout, | ||
showButton, | ||
textValue, | ||
buttonColor, | ||
htmlEditor | ||
}) => { | ||
// const [editor] = useLexicalComposerContext(); | ||
const {isEditing, isSelected, setEditing} = React.useContext(CardContext); | ||
const {cardConfig} = React.useContext(KoenigComposerContext); | ||
const [showSnippetToolbar, setShowSnippetToolbar] = React.useState(false); | ||
const handleToolbarEdit = (event) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
setEditing(true); | ||
}; | ||
return ( | ||
<> | ||
<CtaCard | ||
buttonColor={buttonColor} | ||
buttonText={buttonText} | ||
buttonUrl={buttonUrl} | ||
color={backgroundColor} | ||
handleButtonColor={() => {}} | ||
handleColorChange={() => {}} | ||
hasBackground={hasBackground} | ||
hasImage={hasImage} | ||
hasSponsorLabel={hasSponsorLabel} | ||
htmlEditor={htmlEditor} | ||
imageSrc={imageUrl} | ||
isEditing={isEditing} | ||
isSelected={isSelected} | ||
layout={layout} | ||
setEditing={setEditing} | ||
showButton={showButton} | ||
text={textValue} | ||
updateButtonText={() => {}} | ||
updateButtonUrl={() => {}} | ||
updateHasSponsorLabel={() => {}} | ||
updateLayout={() => {}} | ||
updateShowButton={() => {}} | ||
/> | ||
<ActionToolbar | ||
data-kg-card-toolbar="button" | ||
isVisible={showSnippetToolbar} | ||
> | ||
<SnippetActionToolbar onClose={() => setShowSnippetToolbar(false)} /> | ||
</ActionToolbar> | ||
|
||
<ActionToolbar | ||
data-kg-card-toolbar="button" | ||
isVisible={isSelected && !isEditing} | ||
> | ||
<ToolbarMenu> | ||
<ToolbarMenuItem dataTestId="edit-button-card" icon="edit" isActive={false} label="Edit" onClick={handleToolbarEdit} /> | ||
<ToolbarMenuSeparator hide={!cardConfig.createSnippet} /> | ||
<ToolbarMenuItem | ||
dataTestId="create-snippet" | ||
hide={!cardConfig.createSnippet} | ||
icon="snippet" | ||
isActive={false} | ||
label="Save as snippet" | ||
onClick={() => setShowSnippetToolbar(true)} | ||
/> | ||
</ToolbarMenu> | ||
</ActionToolbar> | ||
</> | ||
); | ||
}; |
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
33 changes: 33 additions & 0 deletions
33
packages/koenig-lexical/src/plugins/CallToActionPlugin.jsx
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,33 @@ | ||
import React from 'react'; | ||
import {$createCallToActionNode, CallToActionNode, INSERT_CTA_COMMAND} from '../nodes/CallToActionNode'; | ||
import {COMMAND_PRIORITY_LOW} from 'lexical'; | ||
import {INSERT_CARD_COMMAND} from './KoenigBehaviourPlugin'; | ||
import {mergeRegister} from '@lexical/utils'; | ||
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; | ||
|
||
export const CallToActionPlugin = () => { | ||
const [editor] = useLexicalComposerContext(); | ||
|
||
React.useEffect(() => { | ||
if (!editor.hasNodes([CallToActionNode])){ | ||
console.error('CallToActionPlugin: CallToActionNode not registered'); // eslint-disable-line no-console | ||
return; | ||
} | ||
return mergeRegister( | ||
editor.registerCommand( | ||
INSERT_CTA_COMMAND, | ||
async (dataset) => { | ||
const cardNode = $createCallToActionNode(dataset); | ||
editor.dispatchCommand(INSERT_CARD_COMMAND, {cardNode, openInEditMode: true}); | ||
|
||
return true; | ||
}, | ||
COMMAND_PRIORITY_LOW | ||
) | ||
); | ||
}, [editor]); | ||
|
||
return null; | ||
}; | ||
|
||
export default CallToActionPlugin; |
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.
Implement missing handler functions.
Several handler functions are passed as empty arrow functions. These need to be implemented to provide the expected functionality:
Also applies to: 53-57