-
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(yfm): add GPT extensions (#361)
- Loading branch information
Showing
59 changed files
with
2,278 additions
and
9 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,16 @@ | ||
import React from 'react'; | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import type {StoryFn} from '@storybook/react'; | ||
|
||
import {PlaygroundGPT} from './PlaygroundGPT'; | ||
|
||
export default { | ||
title: 'Markdown Editor / YFM examples', | ||
component: PlaygroundGPT, | ||
}; | ||
|
||
type PlaygroundStoryProps = {}; | ||
export const Playground: StoryFn<PlaygroundStoryProps> = (props) => <PlaygroundGPT {...props} />; | ||
|
||
Playground.storyName = 'GPT'; |
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,55 @@ | ||
import React, {useState} from 'react'; | ||
|
||
import cloneDeep from 'lodash/cloneDeep'; | ||
|
||
import { | ||
type MarkupString, | ||
gptExtension, | ||
logger, | ||
wGptToolbarItem, | ||
wysiwygToolbarConfigs, | ||
} from '../../src'; | ||
import {Playground} from '../Playground'; | ||
|
||
import {gptWidgetProps} from './gptWidgetOptions'; | ||
import {initialMdContent} from './md-content'; | ||
|
||
import '../Playground.scss'; | ||
|
||
const wToolbarConfig = cloneDeep(wysiwygToolbarConfigs.wToolbarConfig); | ||
wToolbarConfig.unshift([wGptToolbarItem]); | ||
|
||
logger.setLogger({ | ||
metrics: console.info, | ||
action: (data) => console.info(`Action: ${data.action}`, data), | ||
...console, | ||
}); | ||
|
||
export const PlaygroundGPT = React.memo(() => { | ||
const [yfmRaw, setYfmRaw] = React.useState<MarkupString>(initialMdContent); | ||
|
||
const [showedAlertGpt, setShowedAlertGpt] = useState(true); | ||
|
||
const wSelectionMenuConfig = [[wGptToolbarItem], ...wysiwygToolbarConfigs.wSelectionMenuConfig]; | ||
return ( | ||
<Playground | ||
settingsVisible | ||
initial={yfmRaw} | ||
extraExtensions={(builder) => | ||
builder.use( | ||
gptExtension, | ||
gptWidgetProps(setYfmRaw, { | ||
showedGptAlert: Boolean(showedAlertGpt), | ||
onCloseGptAlert: () => { | ||
setShowedAlertGpt(false); | ||
}, | ||
}), | ||
) | ||
} | ||
extensionOptions={{selectionContext: {config: wSelectionMenuConfig}}} | ||
wysiwygToolbarConfig={wToolbarConfig} | ||
/> | ||
); | ||
}); | ||
|
||
PlaygroundGPT.displayName = 'GPT'; |
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,75 @@ | ||
import React from 'react'; | ||
|
||
import type {GptWidgetOptions} from '../../src/extensions/yfm/GPT/gptExtension/gptExtension'; | ||
|
||
const gptRequestHandler = async ({ | ||
markup, | ||
customPrompt, | ||
promptData, | ||
}: { | ||
markup: string; | ||
customPrompt?: string; | ||
promptData: unknown; | ||
}) => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
let gptResponseMarkup = markup; | ||
if (customPrompt) { | ||
gptResponseMarkup = markup + ` \`enhanced with ${customPrompt}\``; | ||
} else if (promptData === 'do-uno-reverse') { | ||
gptResponseMarkup = gptResponseMarkup.replace(/[\wа-яА-ЯёЁ]+/g, (match) => | ||
match.split('').reverse().join(''), | ||
); | ||
} else if (promptData === 'do-shout-out') { | ||
gptResponseMarkup = gptResponseMarkup.toLocaleUpperCase(); | ||
} | ||
|
||
return { | ||
rawText: gptResponseMarkup, | ||
}; | ||
}; | ||
|
||
export const gptWidgetProps = ( | ||
setYfmRaw: (yfmRaw: string) => void, | ||
gptAlertProps?: GptWidgetOptions['gptAlertProps'], | ||
): GptWidgetOptions => { | ||
return { | ||
answerRender: (data) => <div>{data.rawText}</div>, | ||
customPromptPlaceholder: 'Ask GPT to edit the text highlighted text', | ||
disabledPromptPlaceholder: 'Ask GPT to generate the text', | ||
gptAlertProps: gptAlertProps, | ||
promptPresets: [ | ||
{ | ||
hotKey: 'control+3', | ||
data: 'do-uno-reverse', | ||
display: 'Use the uno card', | ||
key: 'do-uno-reverse', | ||
}, | ||
{ | ||
hotKey: 'control+4', | ||
data: 'do-shout-out', | ||
display: 'Make the text flashy', | ||
key: 'do-shout-out', | ||
}, | ||
], | ||
onCustomPromptApply: async ({markup, customPrompt, promptData}) => { | ||
return gptRequestHandler({markup, customPrompt, promptData}); | ||
}, | ||
onPromptPresetClick: async ({markup, customPrompt, promptData}) => { | ||
return gptRequestHandler({markup, customPrompt, promptData}); | ||
}, | ||
onTryAgain: async ({markup, customPrompt, promptData}) => { | ||
return gptRequestHandler({markup, customPrompt, promptData}); | ||
}, | ||
onLike: async () => {}, | ||
onDislike: async () => {}, | ||
onApplyResult: (markup) => { | ||
setYfmRaw(markup); | ||
}, | ||
onUpdate: (event) => { | ||
if (event?.rawText) { | ||
setYfmRaw(event.rawText); | ||
} | ||
}, | ||
}; | ||
}; |
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 const initialMdContent = `Markdown Editor GPT Playground`; |
Oops, something went wrong.