Skip to content

Commit 89c9881

Browse files
authored
fix: Gpt extension render (#519)
1 parent 9126756 commit 89c9881

File tree

6 files changed

+18
-35
lines changed

6 files changed

+18
-35
lines changed

demo/stories/gpt/GPT.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import React, {useState} from 'react';
33
import cloneDeep from 'lodash/cloneDeep';
44

55
import {
6-
type MarkupString,
76
gptExtension,
87
logger,
98
mGptExtension,
109
mGptToolbarItem,
1110
markupToolbarConfigs,
12-
wGptToolbarItem,
11+
wGptItemData,
1312
wysiwygToolbarConfigs,
1413
} from '../../../src';
1514
import {Playground} from '../../components/Playground';
@@ -18,7 +17,7 @@ import {initialMdContent} from './content';
1817
import {gptWidgetProps} from './gptWidgetOptions';
1918

2019
const wToolbarConfig = cloneDeep(wysiwygToolbarConfigs.wToolbarConfig);
21-
wToolbarConfig.unshift([wGptToolbarItem]);
20+
wToolbarConfig.unshift([wGptItemData]);
2221
wToolbarConfig.push([
2322
wysiwygToolbarConfigs.wMermaidItemData,
2423
wysiwygToolbarConfigs.wYfmHtmlBlockItemData,
@@ -37,7 +36,7 @@ const wCommandMenuConfig = wysiwygToolbarConfigs.wCommandMenuConfig.concat(
3736
wysiwygToolbarConfigs.wYfmHtmlBlockItemData,
3837
);
3938

40-
wCommandMenuConfig.unshift(wysiwygToolbarConfigs.wGptItemData);
39+
wCommandMenuConfig.unshift(wGptItemData);
4140

4241
const mToolbarConfig = cloneDeep(markupToolbarConfigs.mToolbarConfig);
4342

@@ -49,24 +48,22 @@ mToolbarConfig.push([
4948
mToolbarConfig.unshift([mGptToolbarItem]);
5049

5150
export const GPT = React.memo(() => {
52-
const [yfmRaw, setYfmRaw] = React.useState<MarkupString>(initialMdContent);
53-
5451
const [showedAlertGpt, setShowedAlertGpt] = useState(true);
5552

56-
const gptExtensionProps = gptWidgetProps(setYfmRaw, {
53+
const gptExtensionProps = gptWidgetProps({
5754
showedGptAlert: Boolean(showedAlertGpt),
5855
onCloseGptAlert: () => {
5956
setShowedAlertGpt(false);
6057
},
6158
});
6259

6360
const markupExtension = mGptExtension(gptExtensionProps);
64-
const wSelectionMenuConfig = [[wGptToolbarItem], ...wysiwygToolbarConfigs.wSelectionMenuConfig];
61+
const wSelectionMenuConfig = [[wGptItemData], ...wysiwygToolbarConfigs.wSelectionMenuConfig];
6562

6663
return (
6764
<Playground
6865
settingsVisible
69-
initial={yfmRaw}
66+
initial={initialMdContent}
7067
extraExtensions={(builder) => builder.use(gptExtension, gptExtensionProps)}
7168
wysiwygCommandMenuConfig={wCommandMenuConfig}
7269
extensionOptions={{selectionContext: {config: wSelectionMenuConfig}}}

demo/stories/gpt/gptWidgetOptions.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const gptRequestHandler = async ({
3030
};
3131

3232
export const gptWidgetProps = (
33-
setYfmRaw: (yfmRaw: string) => void,
3433
gptAlertProps?: GptWidgetOptions['gptAlertProps'],
3534
): GptWidgetOptions => {
3635
return {
@@ -64,12 +63,10 @@ export const gptWidgetProps = (
6463
onLike: async () => {},
6564
onDislike: async () => {},
6665
onApplyResult: (markup) => {
67-
setYfmRaw(markup);
66+
console.log('onApplyResult:', markup);
6867
},
6968
onUpdate: (event) => {
70-
if (event?.rawText) {
71-
setYfmRaw(event.rawText);
72-
}
69+
console.log('update:', event);
7370
},
7471
};
7572
};

docs/how-to-connect-gpt-extensions.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Add in tool bar
151151
```ts
152152
import {
153153
...
154-
wGptToolbarItem,
154+
wGptItemData,
155155
wysiwygToolbarConfigs,
156156
} from '@gravity-ui/markdown-editor';
157157

@@ -160,7 +160,7 @@ import {cloneDeep} from '@gravity-ui/markdown-editor/_/lodash';
160160
export const Editor: React.FC<EditorProps> = (props) => {
161161
...
162162
const wToolbarConfig = cloneDeep(wysiwygToolbarConfigs.wToolbarConfig);
163-
wToolbarConfig.unshift([wGptToolbarItem]);
163+
wToolbarConfig.unshift([wGptItemData]);
164164

165165
...
166166

@@ -176,7 +176,7 @@ Add in menu bar
176176
```ts
177177
export const Editor: React.FC<EditorProps> = (props) => {
178178
...
179-
const wSelectionMenuConfig = [[wGptToolbarItem], ...wysiwygToolbarConfigs.wSelectionMenuConfig];
179+
const wSelectionMenuConfig = [[wGptItemData], ...wysiwygToolbarConfigs.wSelectionMenuConfig];
180180

181181
const mdEditor = useMarkdownEditor({
182182
...
@@ -195,7 +195,7 @@ Add in command menu config (/)
195195
export const Editor: React.FC<EditorProps> = (props) => {
196196
...
197197
const wCommandMenuConfig = wysiwygToolbarConfigs.wCommandMenuConfig // main commands
198-
wCommandMenuConfig.unshift(wysiwygToolbarConfigs.wGptItemData); // add GPT command
198+
wCommandMenuConfig.unshift(wGptItemData); // add GPT command
199199

200200
const mdEditor = useMarkdownEditor({
201201
...
@@ -217,7 +217,6 @@ import React from 'react';
217217
import {
218218
gptExtension,
219219
MarkdownEditorView,
220-
wGptToolbarItem,
221220
wysiwygToolbarConfigs,
222221
useMarkdownEditor,
223222
} from '@gravity-ui/markdown-editor';
@@ -227,12 +226,12 @@ import {gptWidgetProps} from './gptWidgetProps';
227226

228227
export const Editor: React.FC<EditorProps> = (props) => {
229228
const wToolbarConfig = cloneDeep(wysiwygToolbarConfigs.wToolbarConfig);
230-
wToolbarConfig.unshift([wGptToolbarItem]);
229+
wToolbarConfig.unshift([wGptItemData]);
231230

232-
const wSelectionMenuConfig = [[wGptToolbarItem], ...wysiwygToolbarConfigs.wSelectionMenuConfig];
231+
const wSelectionMenuConfig = [[wGptItemData], ...wysiwygToolbarConfigs.wSelectionMenuConfig];
233232

234233
const wCommandMenuConfig = wysiwygToolbarConfigs.wCommandMenuConfig // main commands
235-
wCommandMenuConfig.unshift(wysiwygToolbarConfigs.wGptItemData); // add GPT command
234+
wCommandMenuConfig.unshift(wGptItemData); // add GPT command
236235

237236
const mdEditor = useMarkdownEditor({
238237
// ...

src/bundle/config/wysiwyg.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {ActionStorage} from 'src/core';
22

33
import {headingType, pType} from '../../extensions';
4-
import {gptHotKeys} from '../../extensions/additional/GPT/constants';
54
// for typings from Math
65
import type {} from '../../extensions/additional/Math';
76
import type {
@@ -244,16 +243,7 @@ export const wYfmHtmlBlockItemData: WToolbarSingleItemData = {
244243
isActive: (e) => e.actions.createYfmHtmlBlock.isActive(),
245244
isEnable: (e) => e.actions.createYfmHtmlBlock.isEnable(),
246245
};
247-
export const wGptItemData: WToolbarSingleItemData = {
248-
id: ActionName.gpt,
249-
type: ToolbarDataType.SingleButton,
250-
title: i18n.bind(null, 'gpt'),
251-
hotkey: gptHotKeys.openGptKeyTooltip,
252-
icon: icons.gpt,
253-
exec: (e) => e.actions.addGptWidget.run({}),
254-
isActive: (e) => e.actions.addGptWidget.isActive(),
255-
isEnable: (e) => e.actions.addGptWidget.isEnable(),
256-
};
246+
257247
export const wMermaidItemData: WToolbarSingleItemData = {
258248
id: ActionName.mermaid,
259249
type: ToolbarDataType.SingleButton,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './toolbar';
1+
export * from './wGptItemData';
22
export * from './gptExtension/gptExtension';
33
export * from './MarkupGpt';

src/extensions/additional/GPT/toolbar.ts renamed to src/extensions/additional/GPT/wGptItemData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {gptHotKeys} from './constants';
88

99
export const cnGptButton = cn('gpt-button');
1010

11-
export const wGptToolbarItem: WToolbarSingleItemData = {
11+
export const wGptItemData: WToolbarSingleItemData = {
1212
type: ToolbarDataType.SingleButton,
1313
id: 'gpt',
1414
title: () => `${i18n('help-with-text')}`,

0 commit comments

Comments
 (0)