@gravity-ui/markdown-editor 13.17.1 → 13.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/build/cjs/bundle/config/action-names.d.ts +1 -1
- package/build/cjs/bundle/config/action-names.js +1 -0
- package/build/cjs/bundle/config/icons.d.ts +1 -1
- package/build/cjs/bundle/config/icons.js +3 -0
- package/build/cjs/bundle/config/wysiwyg.d.ts +1 -0
- package/build/cjs/bundle/config/wysiwyg.js +13 -1
- package/build/cjs/extensions/markdown/Table/actions/innerActions.d.ts +1 -1
- package/build/cjs/extensions/yfm/GPT/ErrorScreen/ErrorScreen.css +18 -0
- package/build/cjs/extensions/yfm/GPT/ErrorScreen/ErrorScreen.d.ts +8 -0
- package/build/cjs/extensions/yfm/GPT/ErrorScreen/ErrorScreen.js +21 -0
- package/build/cjs/extensions/yfm/GPT/ErrorScreen/types.d.ts +14 -0
- package/build/cjs/extensions/yfm/GPT/ErrorScreen/types.js +2 -0
- package/build/cjs/extensions/yfm/GPT/GptDialog/GptDialog.css +75 -0
- package/build/cjs/extensions/yfm/GPT/GptDialog/GptDialog.d.ts +28 -0
- package/build/cjs/extensions/yfm/GPT/GptDialog/GptDialog.js +102 -0
- package/build/cjs/extensions/yfm/GPT/GptDialog/LoadingScreen/LoadingScreen.css +22 -0
- package/build/cjs/extensions/yfm/GPT/GptDialog/LoadingScreen/LoadingScreen.d.ts +3 -0
- package/build/cjs/extensions/yfm/GPT/GptDialog/LoadingScreen/LoadingScreen.js +21 -0
- package/build/cjs/extensions/yfm/GPT/IconRefuge/IconRefuge.classname.d.ts +1 -0
- package/build/cjs/extensions/yfm/GPT/IconRefuge/IconRefuge.classname.js +5 -0
- package/build/cjs/extensions/yfm/GPT/IconRefuge/IconRefuge.css +8 -0
- package/build/cjs/extensions/yfm/GPT/IconRefuge/IconRefuge.d.ts +14 -0
- package/build/cjs/extensions/yfm/GPT/IconRefuge/IconRefuge.js +28 -0
- package/build/cjs/extensions/yfm/GPT/IconRefuge/index.d.ts +2 -0
- package/build/cjs/extensions/yfm/GPT/IconRefuge/index.js +5 -0
- package/build/cjs/extensions/yfm/GPT/PresetList/PresetList.d.ts +9 -0
- package/build/cjs/extensions/yfm/GPT/PresetList/PresetList.js +29 -0
- package/build/cjs/extensions/yfm/GPT/PresetList/Presetlist.css +6 -0
- package/build/cjs/extensions/yfm/GPT/actions.d.ts +2 -0
- package/build/cjs/extensions/yfm/GPT/actions.js +14 -0
- package/build/cjs/extensions/yfm/GPT/commands.d.ts +2 -0
- package/build/cjs/extensions/yfm/GPT/commands.js +13 -0
- package/build/cjs/extensions/yfm/GPT/constants.d.ts +13 -0
- package/build/cjs/extensions/yfm/GPT/constants.js +15 -0
- package/build/cjs/extensions/yfm/GPT/gptExtension/gptExtension.d.ts +13 -0
- package/build/cjs/extensions/yfm/GPT/gptExtension/gptExtension.js +18 -0
- package/build/cjs/extensions/yfm/GPT/gptExtension/view.css +17 -0
- package/build/cjs/extensions/yfm/GPT/gptExtension/view.d.ts +36 -0
- package/build/cjs/extensions/yfm/GPT/gptExtension/view.js +171 -0
- package/build/cjs/extensions/yfm/GPT/hooks/useGpt.d.ts +27 -0
- package/build/cjs/extensions/yfm/GPT/hooks/useGpt.js +148 -0
- package/build/cjs/extensions/yfm/GPT/hooks/useGptHotKeys.d.ts +2 -0
- package/build/cjs/extensions/yfm/GPT/hooks/useGptHotKeys.js +8 -0
- package/build/cjs/extensions/yfm/GPT/hooks/useOverflowingHorizontalItems.d.ts +13 -0
- package/build/cjs/extensions/yfm/GPT/hooks/useOverflowingHorizontalItems.js +70 -0
- package/build/cjs/extensions/yfm/GPT/hooks/usePresetList.d.ts +14 -0
- package/build/cjs/extensions/yfm/GPT/hooks/usePresetList.js +36 -0
- package/build/cjs/extensions/yfm/GPT/index.d.ts +2 -0
- package/build/cjs/extensions/yfm/GPT/index.js +5 -0
- package/build/cjs/extensions/yfm/GPT/plugin.d.ts +13 -0
- package/build/cjs/extensions/yfm/GPT/plugin.js +47 -0
- package/build/cjs/extensions/yfm/GPT/toolbar.d.ts +3 -0
- package/build/cjs/extensions/yfm/GPT/toolbar.js +22 -0
- package/build/cjs/extensions/yfm/GPT/utils.d.ts +13 -0
- package/build/cjs/extensions/yfm/GPT/utils.js +31 -0
- package/build/cjs/extensions/yfm/YfmTable/plugins/YfmTableControls/actions.d.ts +1 -1
- package/build/cjs/extensions/yfm/YfmTable/plugins/YfmTableControls/actions.js +0 -8
- package/build/cjs/extensions/yfm/index.d.ts +1 -0
- package/build/cjs/extensions/yfm/index.js +1 -0
- package/build/cjs/i18n/gpt/dialog/en.json +16 -0
- package/build/cjs/i18n/gpt/dialog/index.d.ts +19 -0
- package/build/cjs/i18n/gpt/dialog/index.js +8 -0
- package/build/cjs/i18n/gpt/dialog/ru.json +16 -0
- package/build/cjs/i18n/gpt/errors/en.json +5 -0
- package/build/cjs/i18n/gpt/errors/index.d.ts +8 -0
- package/build/cjs/i18n/gpt/errors/index.js +8 -0
- package/build/cjs/i18n/gpt/errors/ru.json +5 -0
- package/build/cjs/i18n/gpt/extension/en.json +6 -0
- package/build/cjs/i18n/gpt/extension/index.d.ts +9 -0
- package/build/cjs/i18n/gpt/extension/index.js +8 -0
- package/build/cjs/i18n/gpt/extension/ru.json +6 -0
- package/build/cjs/i18n/gpt/loading/en.json +3 -0
- package/build/cjs/i18n/gpt/loading/index.d.ts +6 -0
- package/build/cjs/i18n/gpt/loading/index.js +8 -0
- package/build/cjs/i18n/gpt/loading/ru.json +3 -0
- package/build/cjs/i18n/menubar/en.json +1 -0
- package/build/cjs/i18n/menubar/index.d.ts +2 -1
- package/build/cjs/i18n/menubar/ru.json +1 -0
- package/build/cjs/icons/GPT.d.ts +2 -0
- package/build/cjs/icons/GPT.js +22 -0
- package/build/cjs/icons/GPTLoading.d.ts +2 -0
- package/build/cjs/icons/GPTLoading.js +12 -0
- package/build/cjs/icons/index.d.ts +3 -1
- package/build/cjs/icons/index.js +5 -1
- package/build/cjs/react-utils/useAutoFocus.d.ts +1 -1
- package/build/cjs/react-utils/useAutoFocus.js +2 -2
- package/build/cjs/table-utils/commands/appendColumn.js +42 -33
- package/build/cjs/table-utils/commands/appendRow.js +40 -22
- package/build/cjs/table-utils/commands/removeColumn.js +18 -20
- package/build/cjs/table-utils/commands/removeRow.js +12 -9
- package/build/cjs/table-utils/table-desc.d.ts +53 -0
- package/build/cjs/table-utils/table-desc.js +149 -0
- package/build/cjs/table-utils/utils.d.ts +1 -1
- package/build/cjs/table-utils/utils.js +19 -12
- package/build/cjs/version.js +1 -1
- package/build/esm/bundle/config/action-names.d.ts +1 -1
- package/build/esm/bundle/config/action-names.js +1 -0
- package/build/esm/bundle/config/icons.d.ts +1 -1
- package/build/esm/bundle/config/icons.js +2 -0
- package/build/esm/bundle/config/wysiwyg.d.ts +1 -0
- package/build/esm/bundle/config/wysiwyg.js +12 -0
- package/build/esm/extensions/markdown/Table/actions/innerActions.d.ts +1 -1
- package/build/esm/extensions/yfm/GPT/ErrorScreen/ErrorScreen.css +18 -0
- package/build/esm/extensions/yfm/GPT/ErrorScreen/ErrorScreen.d.ts +9 -0
- package/build/esm/extensions/yfm/GPT/ErrorScreen/ErrorScreen.js +17 -0
- package/build/esm/extensions/yfm/GPT/ErrorScreen/types.d.ts +14 -0
- package/build/esm/extensions/yfm/GPT/ErrorScreen/types.js +1 -0
- package/build/esm/extensions/yfm/GPT/GptDialog/GptDialog.css +75 -0
- package/build/esm/extensions/yfm/GPT/GptDialog/GptDialog.d.ts +29 -0
- package/build/esm/extensions/yfm/GPT/GptDialog/GptDialog.js +98 -0
- package/build/esm/extensions/yfm/GPT/GptDialog/LoadingScreen/LoadingScreen.css +22 -0
- package/build/esm/extensions/yfm/GPT/GptDialog/LoadingScreen/LoadingScreen.d.ts +4 -0
- package/build/esm/extensions/yfm/GPT/GptDialog/LoadingScreen/LoadingScreen.js +17 -0
- package/build/esm/extensions/yfm/GPT/IconRefuge/IconRefuge.classname.d.ts +1 -0
- package/build/esm/extensions/yfm/GPT/IconRefuge/IconRefuge.classname.js +2 -0
- package/build/esm/extensions/yfm/GPT/IconRefuge/IconRefuge.css +8 -0
- package/build/esm/extensions/yfm/GPT/IconRefuge/IconRefuge.d.ts +15 -0
- package/build/esm/extensions/yfm/GPT/IconRefuge/IconRefuge.js +25 -0
- package/build/esm/extensions/yfm/GPT/IconRefuge/index.d.ts +2 -0
- package/build/esm/extensions/yfm/GPT/IconRefuge/index.js +1 -0
- package/build/esm/extensions/yfm/GPT/PresetList/PresetList.d.ts +10 -0
- package/build/esm/extensions/yfm/GPT/PresetList/PresetList.js +25 -0
- package/build/esm/extensions/yfm/GPT/PresetList/Presetlist.css +6 -0
- package/build/esm/extensions/yfm/GPT/actions.d.ts +2 -0
- package/build/esm/extensions/yfm/GPT/actions.js +10 -0
- package/build/esm/extensions/yfm/GPT/commands.d.ts +2 -0
- package/build/esm/extensions/yfm/GPT/commands.js +9 -0
- package/build/esm/extensions/yfm/GPT/constants.d.ts +13 -0
- package/build/esm/extensions/yfm/GPT/constants.js +12 -0
- package/build/esm/extensions/yfm/GPT/gptExtension/gptExtension.d.ts +13 -0
- package/build/esm/extensions/yfm/GPT/gptExtension/gptExtension.js +14 -0
- package/build/esm/extensions/yfm/GPT/gptExtension/view.css +17 -0
- package/build/esm/extensions/yfm/GPT/gptExtension/view.d.ts +37 -0
- package/build/esm/extensions/yfm/GPT/gptExtension/view.js +167 -0
- package/build/esm/extensions/yfm/GPT/hooks/useGpt.d.ts +27 -0
- package/build/esm/extensions/yfm/GPT/hooks/useGpt.js +144 -0
- package/build/esm/extensions/yfm/GPT/hooks/useGptHotKeys.d.ts +2 -0
- package/build/esm/extensions/yfm/GPT/hooks/useGptHotKeys.js +4 -0
- package/build/esm/extensions/yfm/GPT/hooks/useOverflowingHorizontalItems.d.ts +13 -0
- package/build/esm/extensions/yfm/GPT/hooks/useOverflowingHorizontalItems.js +65 -0
- package/build/esm/extensions/yfm/GPT/hooks/usePresetList.d.ts +14 -0
- package/build/esm/extensions/yfm/GPT/hooks/usePresetList.js +32 -0
- package/build/esm/extensions/yfm/GPT/index.d.ts +2 -0
- package/build/esm/extensions/yfm/GPT/index.js +2 -0
- package/build/esm/extensions/yfm/GPT/plugin.d.ts +13 -0
- package/build/esm/extensions/yfm/GPT/plugin.js +43 -0
- package/build/esm/extensions/yfm/GPT/toolbar.d.ts +3 -0
- package/build/esm/extensions/yfm/GPT/toolbar.js +18 -0
- package/build/esm/extensions/yfm/GPT/utils.d.ts +13 -0
- package/build/esm/extensions/yfm/GPT/utils.js +23 -0
- package/build/esm/extensions/yfm/YfmTable/plugins/YfmTableControls/actions.d.ts +1 -1
- package/build/esm/extensions/yfm/YfmTable/plugins/YfmTableControls/actions.js +1 -9
- package/build/esm/extensions/yfm/index.d.ts +1 -0
- package/build/esm/extensions/yfm/index.js +1 -0
- package/build/esm/i18n/gpt/dialog/en.json +16 -0
- package/build/esm/i18n/gpt/dialog/index.d.ts +19 -0
- package/build/esm/i18n/gpt/dialog/index.js +4 -0
- package/build/esm/i18n/gpt/dialog/ru.json +16 -0
- package/build/esm/i18n/gpt/errors/en.json +5 -0
- package/build/esm/i18n/gpt/errors/index.d.ts +8 -0
- package/build/esm/i18n/gpt/errors/index.js +4 -0
- package/build/esm/i18n/gpt/errors/ru.json +5 -0
- package/build/esm/i18n/gpt/extension/en.json +6 -0
- package/build/esm/i18n/gpt/extension/index.d.ts +9 -0
- package/build/esm/i18n/gpt/extension/index.js +4 -0
- package/build/esm/i18n/gpt/extension/ru.json +6 -0
- package/build/esm/i18n/gpt/loading/en.json +3 -0
- package/build/esm/i18n/gpt/loading/index.d.ts +6 -0
- package/build/esm/i18n/gpt/loading/index.js +4 -0
- package/build/esm/i18n/gpt/loading/ru.json +3 -0
- package/build/esm/i18n/menubar/en.json +1 -0
- package/build/esm/i18n/menubar/index.d.ts +2 -1
- package/build/esm/i18n/menubar/ru.json +1 -0
- package/build/esm/icons/GPT.d.ts +2 -0
- package/build/esm/icons/GPT.js +19 -0
- package/build/esm/icons/GPTLoading.d.ts +2 -0
- package/build/esm/icons/GPTLoading.js +9 -0
- package/build/esm/icons/index.d.ts +3 -1
- package/build/esm/icons/index.js +3 -1
- package/build/esm/react-utils/useAutoFocus.d.ts +1 -1
- package/build/esm/react-utils/useAutoFocus.js +2 -2
- package/build/esm/table-utils/commands/appendColumn.js +44 -35
- package/build/esm/table-utils/commands/appendRow.js +42 -24
- package/build/esm/table-utils/commands/removeColumn.js +18 -20
- package/build/esm/table-utils/commands/removeRow.js +14 -11
- package/build/esm/table-utils/table-desc.d.ts +53 -0
- package/build/esm/table-utils/table-desc.js +144 -0
- package/build/esm/table-utils/utils.d.ts +1 -1
- package/build/esm/table-utils/utils.js +18 -12
- package/build/esm/version.js +1 -1
- package/build/styles.css +146 -0
- package/package.json +2 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Action, ExtensionWithOptions } from 'src/core';
|
|
2
|
+
import type { CommonAnswer } from '../ErrorScreen/types';
|
|
3
|
+
import type { GptWidgetDecoViewParams } from './view';
|
|
4
|
+
export declare const gptActionName = "addGptWidget";
|
|
5
|
+
export declare type GptWidgetOptions<AnswerData extends CommonAnswer = CommonAnswer, PromptData extends unknown = unknown> = Pick<GptWidgetDecoViewParams<AnswerData, PromptData>, 'gptPopupContainer' | 'answerRender' | 'onApplyResult' | 'promptPresets' | 'customPromptPlaceholder' | 'disabledPromptPlaceholder' | 'onCustomPromptApply' | 'onPromptPresetClick' | 'onTryAgain' | 'onLike' | 'onDislike' | 'onClose' | 'onUpdate' | 'gptAlertProps'>;
|
|
6
|
+
export declare const gptExtension: ExtensionWithOptions<GptWidgetOptions>;
|
|
7
|
+
declare global {
|
|
8
|
+
namespace WysiwygEditor {
|
|
9
|
+
interface Actions {
|
|
10
|
+
[gptActionName]: Action<{}>;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.gptExtension = exports.gptActionName = void 0;
|
|
4
|
+
const actions_1 = require("../actions");
|
|
5
|
+
const commands_1 = require("../commands");
|
|
6
|
+
const constants_1 = require("../constants");
|
|
7
|
+
const plugin_1 = require("../plugin");
|
|
8
|
+
exports.gptActionName = 'addGptWidget';
|
|
9
|
+
const gptExtension = (builder, options) => {
|
|
10
|
+
builder.addAction(exports.gptActionName, actions_1.showGptWidget);
|
|
11
|
+
builder.addPlugin(({ serializer, markupParser }) => {
|
|
12
|
+
return (0, plugin_1.gptWidgetPlugin)(Object.assign(Object.assign({}, options), { serializer, parser: markupParser }));
|
|
13
|
+
});
|
|
14
|
+
builder.addKeymap(() => ({
|
|
15
|
+
[constants_1.gptHotKeys.openGptKey]: commands_1.runGpt,
|
|
16
|
+
}), builder.Priority.VeryLow);
|
|
17
|
+
};
|
|
18
|
+
exports.gptExtension = gptExtension;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
.g-md-gpt-widget-deco {
|
|
2
|
+
background-color: var(--g-color-base-info-light-hover);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.g-md-gpt-popup {
|
|
6
|
+
--layer-navigation-bar: 900;
|
|
7
|
+
pointer-events: initial;
|
|
8
|
+
}
|
|
9
|
+
.g-md-gpt-popup[class] {
|
|
10
|
+
border-radius: var(--g-spacing-2);
|
|
11
|
+
}
|
|
12
|
+
.g-md-gpt-popup__content {
|
|
13
|
+
width: 720px;
|
|
14
|
+
padding: var(--g-spacing-3);
|
|
15
|
+
pointer-events: all;
|
|
16
|
+
box-shadow: -8px 2px 16px -4px var(--g-color-private-purple-150), 8px 2px 16px -4px var(--g-color-private-blue-150);
|
|
17
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PopupProps } from '@gravity-ui/uikit';
|
|
2
|
+
import type { EditorState, PluginView } from 'prosemirror-state';
|
|
3
|
+
import { EditorView } from 'prosemirror-view';
|
|
4
|
+
import { Parser, Serializer } from 'src/core';
|
|
5
|
+
import type { CommonAnswer } from '../ErrorScreen/types';
|
|
6
|
+
import type { GptDialogProps } from '../GptDialog/GptDialog';
|
|
7
|
+
export declare const cnGptPopup: import("@bem-react/classname").ClassNameFormatter;
|
|
8
|
+
export declare type GptWidgetDecoViewParams<AnswerData extends CommonAnswer = CommonAnswer, PromptData extends unknown = unknown> = Omit<GptDialogProps<AnswerData, PromptData>, 'markup' | 'onApplyResult'> & {
|
|
9
|
+
serializer: Serializer;
|
|
10
|
+
parser: Parser;
|
|
11
|
+
} & {
|
|
12
|
+
onApplyResult?: GptDialogProps['onApplyResult'];
|
|
13
|
+
gptPopupContainer?: PopupProps['container'];
|
|
14
|
+
};
|
|
15
|
+
export declare class GptWidgetDecoView implements Required<PluginView> {
|
|
16
|
+
private readonly _view;
|
|
17
|
+
private readonly _renderer;
|
|
18
|
+
private _decoElem;
|
|
19
|
+
private _params;
|
|
20
|
+
private _serializer;
|
|
21
|
+
private _parser;
|
|
22
|
+
private _confirmOpen;
|
|
23
|
+
constructor(view: EditorView, params: GptWidgetDecoViewParams);
|
|
24
|
+
update(view: EditorView, prevState: EditorState): void;
|
|
25
|
+
destroy(): void;
|
|
26
|
+
private _resetState;
|
|
27
|
+
private _onConfirmCancel;
|
|
28
|
+
private _onConfirmOk;
|
|
29
|
+
private _render;
|
|
30
|
+
private _onGptAnswerUpdate;
|
|
31
|
+
private _onSubmit;
|
|
32
|
+
private _onClose;
|
|
33
|
+
private _getContentOfDecoration;
|
|
34
|
+
private _getCurrentDecoration;
|
|
35
|
+
private _getPluginState;
|
|
36
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GptWidgetDecoView = exports.cnGptPopup = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const react_1 = tslib_1.__importStar(require("react"));
|
|
6
|
+
const uikit_1 = require("@gravity-ui/uikit");
|
|
7
|
+
const prosemirror_model_1 = require("prosemirror-model");
|
|
8
|
+
const prosemirror_state_1 = require("prosemirror-state");
|
|
9
|
+
const react_use_1 = require("react-use");
|
|
10
|
+
const classname_1 = require("../../../../classname");
|
|
11
|
+
const behavior_1 = require("../../../behavior");
|
|
12
|
+
const GptDialog_1 = require("../GptDialog/GptDialog");
|
|
13
|
+
const constants_1 = require("../constants");
|
|
14
|
+
const plugin_1 = require("../plugin");
|
|
15
|
+
exports.cnGptPopup = (0, classname_1.cn)('gpt-popup');
|
|
16
|
+
class GptWidgetDecoView {
|
|
17
|
+
constructor(view, params) {
|
|
18
|
+
this._decoElem = null;
|
|
19
|
+
this._onConfirmCancel = () => {
|
|
20
|
+
this._confirmOpen = false;
|
|
21
|
+
this._renderer.rerender();
|
|
22
|
+
};
|
|
23
|
+
this._onConfirmOk = () => {
|
|
24
|
+
this._onClose();
|
|
25
|
+
};
|
|
26
|
+
this._onGptAnswerUpdate = (answer) => {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
(_b = (_a = this._params).onUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, answer);
|
|
29
|
+
};
|
|
30
|
+
this._onSubmit = (answer) => {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
const deco = this._getCurrentDecoration();
|
|
33
|
+
if (!deco)
|
|
34
|
+
return;
|
|
35
|
+
const { from, to } = deco;
|
|
36
|
+
const answerNode = this._parser.parse(answer);
|
|
37
|
+
const tr = this._view.state.tr;
|
|
38
|
+
const meta = { action: 'hide' };
|
|
39
|
+
tr.setMeta(plugin_1.pluginKey, meta);
|
|
40
|
+
tr.replace(from, to, new prosemirror_model_1.Slice(answerNode.content, 1, 1));
|
|
41
|
+
tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, tr.mapping.map(to)));
|
|
42
|
+
this._view.dispatch(tr);
|
|
43
|
+
setTimeout(() => {
|
|
44
|
+
this._view.focus();
|
|
45
|
+
}, 0);
|
|
46
|
+
(_b = (_a = this._params).onApplyResult) === null || _b === void 0 ? void 0 : _b.call(_a, answer);
|
|
47
|
+
this._resetState();
|
|
48
|
+
};
|
|
49
|
+
this._onClose = () => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
const deco = this._getCurrentDecoration();
|
|
52
|
+
if (!deco) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const tr = this._view.state.tr;
|
|
56
|
+
const meta = { action: 'hide' };
|
|
57
|
+
tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, deco.from, deco.to));
|
|
58
|
+
tr.setMeta(plugin_1.pluginKey, meta);
|
|
59
|
+
this._view.dispatch(tr);
|
|
60
|
+
setTimeout(() => {
|
|
61
|
+
this._view.focus();
|
|
62
|
+
}, 0);
|
|
63
|
+
(_b = (_a = this._params).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
64
|
+
this._resetState();
|
|
65
|
+
};
|
|
66
|
+
this._view = view;
|
|
67
|
+
this._params = params;
|
|
68
|
+
this._serializer = params.serializer;
|
|
69
|
+
this._parser = params.parser;
|
|
70
|
+
this._confirmOpen = false;
|
|
71
|
+
this._renderer = (0, behavior_1.getReactRendererFromState)(view.state).createItem('gpt-widget-view', this._render.bind(this));
|
|
72
|
+
}
|
|
73
|
+
update(view, prevState) {
|
|
74
|
+
var _a;
|
|
75
|
+
const { state, dispatch } = view;
|
|
76
|
+
if (this._decoElem && !state.selection.eq(prevState.selection)) {
|
|
77
|
+
const transaction = state.tr;
|
|
78
|
+
const meta = { action: 'hide' };
|
|
79
|
+
dispatch(transaction.setMeta(plugin_1.pluginKey, meta));
|
|
80
|
+
this._view.focus();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const decoElements = Array.from(view.dom.querySelectorAll(`.${constants_1.WIDGET_DECO_CLASS_NAME}`));
|
|
84
|
+
this._decoElem = (_a = decoElements.at(-1)) !== null && _a !== void 0 ? _a : null;
|
|
85
|
+
this._renderer.rerender();
|
|
86
|
+
}
|
|
87
|
+
destroy() {
|
|
88
|
+
this._resetState();
|
|
89
|
+
this._renderer.remove();
|
|
90
|
+
}
|
|
91
|
+
_resetState() {
|
|
92
|
+
this._decoElem = null;
|
|
93
|
+
this._confirmOpen = false;
|
|
94
|
+
}
|
|
95
|
+
_render() {
|
|
96
|
+
if (!this._decoElem)
|
|
97
|
+
return null;
|
|
98
|
+
const markup = this._getContentOfDecoration();
|
|
99
|
+
if (markup === undefined) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
return (react_1.default.createElement(Widget, { markup: markup, anchorRef: { current: this._decoElem }, answerRender: this._params.answerRender, promptPresets: this._params.promptPresets, disablePromptPresets: this._params.disablePromptPresets, customPromptPlaceholder: this._params.customPromptPlaceholder, disabledPromptPlaceholder: this._params.disabledPromptPlaceholder, gptAlertProps: this._params.gptAlertProps,
|
|
103
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
104
|
+
onApplyResult: this._onSubmit,
|
|
105
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
106
|
+
onCustomPromptApply: this._params.onCustomPromptApply,
|
|
107
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
108
|
+
onPromptPresetClick: this._params.onPromptPresetClick,
|
|
109
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
110
|
+
onTryAgain: this._params.onTryAgain,
|
|
111
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
112
|
+
onLike: this._params.onLike,
|
|
113
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
114
|
+
onDislike: this._params.onDislike,
|
|
115
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
116
|
+
onClose: this._onClose,
|
|
117
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
118
|
+
onUpdate: this._onGptAnswerUpdate,
|
|
119
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
120
|
+
confirmOpen: this._confirmOpen,
|
|
121
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
122
|
+
onConfirmOk: this._onConfirmOk,
|
|
123
|
+
/* eslint-disable-next-line react/jsx-handler-names */
|
|
124
|
+
onConfirmCancel: this._onConfirmCancel, container: this._params.gptPopupContainer }));
|
|
125
|
+
}
|
|
126
|
+
_getContentOfDecoration() {
|
|
127
|
+
const deco = this._getCurrentDecoration();
|
|
128
|
+
if (!deco)
|
|
129
|
+
return undefined;
|
|
130
|
+
const { from, to } = deco;
|
|
131
|
+
try {
|
|
132
|
+
const fragment = this._view.state.doc.slice(from, to, true).content;
|
|
133
|
+
const yfmMarkup = this._serializer.serialize(fragment);
|
|
134
|
+
return yfmMarkup;
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
console.error(error);
|
|
138
|
+
return this._view.state.doc.textBetween(from, to, '\n', '');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
_getCurrentDecoration() {
|
|
142
|
+
var _a;
|
|
143
|
+
return (_a = this._getPluginState()) === null || _a === void 0 ? void 0 : _a.find(undefined, undefined,
|
|
144
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return
|
|
145
|
+
(spec) => spec[constants_1.WIDGET_DECO_SPEC_FLAG])[0];
|
|
146
|
+
}
|
|
147
|
+
_getPluginState() {
|
|
148
|
+
return plugin_1.pluginKey.getState(this._view.state);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.GptWidgetDecoView = GptWidgetDecoView;
|
|
152
|
+
function Widget({ markup, anchorRef, answerRender, promptPresets, disablePromptPresets, customPromptPlaceholder, disabledPromptPlaceholder, onCustomPromptApply, onApplyResult, onPromptPresetClick, onTryAgain, onLike, onDislike, onClose, onUpdate, container, gptAlertProps, }) {
|
|
153
|
+
(0, react_1.useEffect)(() => {
|
|
154
|
+
// rerender the popup
|
|
155
|
+
window.dispatchEvent(new CustomEvent('scroll'));
|
|
156
|
+
}, [anchorRef]);
|
|
157
|
+
(0, react_use_1.useMount)(() => {
|
|
158
|
+
if ((anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.current) && 'scrollIntoView' in anchorRef.current) {
|
|
159
|
+
anchorRef.current.scrollIntoView({
|
|
160
|
+
behavior: 'smooth',
|
|
161
|
+
block: 'center',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
const handleUpdate = (0, react_1.useCallback)((result) => {
|
|
166
|
+
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(result);
|
|
167
|
+
}, [onUpdate]);
|
|
168
|
+
return (react_1.default.createElement(react_1.default.Fragment, null,
|
|
169
|
+
react_1.default.createElement(uikit_1.Popup, { className: (0, exports.cnGptPopup)(), contentClassName: (0, exports.cnGptPopup)('content'), open: true, anchorRef: anchorRef, placement: constants_1.gptPopupPlacement, onOutsideClick: onClose, focusTrap: true, strategy: "absolute", container: container, onEscapeKeyDown: onClose },
|
|
170
|
+
react_1.default.createElement(GptDialog_1.GptDialog, { markup: markup, answerRender: answerRender, promptPresets: promptPresets, disablePromptPresets: disablePromptPresets, customPromptPlaceholder: customPromptPlaceholder, disabledPromptPlaceholder: disabledPromptPlaceholder, onApplyResult: onApplyResult, onCustomPromptApply: onCustomPromptApply, onPromptPresetClick: onPromptPresetClick, onTryAgain: onTryAgain, onLike: onLike, onDislike: onDislike, onClose: onClose, onUpdate: handleUpdate, gptAlertProps: gptAlertProps }))));
|
|
171
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { CommonAnswer, GptRequestData } from '../ErrorScreen/types';
|
|
2
|
+
import type { GptDialogProps } from '../GptDialog/GptDialog';
|
|
3
|
+
declare type UseGptProps<AnswerData extends CommonAnswer = CommonAnswer, PromptData extends unknown = unknown> = Pick<GptDialogProps<AnswerData, PromptData>, 'markup' | 'promptPresets' | 'onCustomPromptApply' | 'onPromptPresetClick' | 'onTryAgain' | 'onLike' | 'onDislike' | 'onApplyResult' | 'onUpdate'>;
|
|
4
|
+
export declare const useGpt: <AnswerData extends CommonAnswer = CommonAnswer, PromptData extends unknown = unknown>({ markup, promptPresets, onCustomPromptApply, onPromptPresetClick, onTryAgain, onLike, onDislike, onApplyResult, onUpdate, }: UseGptProps<AnswerData, PromptData>) => {
|
|
5
|
+
answer: AnswerData | undefined;
|
|
6
|
+
customPrompt: string;
|
|
7
|
+
lastRequestData: GptRequestData<PromptData> | undefined;
|
|
8
|
+
loading: boolean;
|
|
9
|
+
error: boolean;
|
|
10
|
+
mode: "custom-and-presets" | "only-custom" | "only-presets";
|
|
11
|
+
feedbackType: "dislike" | "like" | undefined;
|
|
12
|
+
feedbackTypeLoading: "dislike" | "like" | undefined;
|
|
13
|
+
handleLike: () => Promise<void>;
|
|
14
|
+
handleDislike: () => Promise<void>;
|
|
15
|
+
handleCustomPromptUpdate: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
16
|
+
handleCustomPromptKeyPress: import("react").KeyboardEventHandler<HTMLInputElement>;
|
|
17
|
+
handleCustomPromptApply: () => Promise<void>;
|
|
18
|
+
handlePresetClick: (data: PromptData) => void;
|
|
19
|
+
handleTryAgain: () => Promise<void>;
|
|
20
|
+
handleFreshStart: () => void;
|
|
21
|
+
handleApplyResult: () => void;
|
|
22
|
+
showTryAgainButton: boolean;
|
|
23
|
+
showAnswer: boolean;
|
|
24
|
+
showError: boolean;
|
|
25
|
+
showAnswerActions: boolean | undefined;
|
|
26
|
+
};
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useGpt = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
const useGpt = ({ markup, promptPresets, onCustomPromptApply, onPromptPresetClick, onTryAgain, onLike, onDislike, onApplyResult, onUpdate, }) => {
|
|
7
|
+
const [answer, setAnswer] = (0, react_1.useState)();
|
|
8
|
+
const [lastRequestData, setLastRequestData] = (0, react_1.useState)();
|
|
9
|
+
const [customPrompt, setCustomPrompt] = (0, react_1.useState)('');
|
|
10
|
+
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
11
|
+
const [error, setError] = (0, react_1.useState)(false);
|
|
12
|
+
const [feedbackType, setFeedbackType] = (0, react_1.useState)(undefined);
|
|
13
|
+
const [feedbackTypeLoading, setFeedbackTypeLoading] = (0, react_1.useState)(undefined);
|
|
14
|
+
const makeRequest = (0, react_1.useCallback)(async (requestFunction, data) => {
|
|
15
|
+
if (!requestFunction) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
let result;
|
|
19
|
+
try {
|
|
20
|
+
setLoading(true);
|
|
21
|
+
setError(false);
|
|
22
|
+
setLastRequestData(data);
|
|
23
|
+
result = await requestFunction(data);
|
|
24
|
+
if (result) {
|
|
25
|
+
setAnswer(result);
|
|
26
|
+
setFeedbackType(undefined);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.error(error);
|
|
31
|
+
setError(true);
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
setLoading(false);
|
|
35
|
+
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(result);
|
|
36
|
+
}
|
|
37
|
+
}, [onUpdate]);
|
|
38
|
+
const handleLike = (0, react_1.useCallback)(async () => {
|
|
39
|
+
if (!onLike || !lastRequestData) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
setFeedbackType(undefined);
|
|
44
|
+
setFeedbackTypeLoading('like');
|
|
45
|
+
await onLike(lastRequestData);
|
|
46
|
+
setFeedbackType('like');
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
console.error(error);
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
setFeedbackTypeLoading(undefined);
|
|
53
|
+
}
|
|
54
|
+
}, [lastRequestData, onLike]);
|
|
55
|
+
const handleDislike = (0, react_1.useCallback)(async () => {
|
|
56
|
+
if (!onDislike || !lastRequestData) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
setFeedbackType(undefined);
|
|
61
|
+
setFeedbackTypeLoading('dislike');
|
|
62
|
+
await onDislike(lastRequestData);
|
|
63
|
+
setFeedbackType('dislike');
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
console.error(error);
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
setFeedbackTypeLoading(undefined);
|
|
70
|
+
}
|
|
71
|
+
}, [lastRequestData, onDislike]);
|
|
72
|
+
const handleCustomPromptApply = (0, react_1.useCallback)(async () => {
|
|
73
|
+
if (!customPrompt) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const gptRequestData = {
|
|
77
|
+
markup,
|
|
78
|
+
customPrompt,
|
|
79
|
+
};
|
|
80
|
+
await makeRequest(onCustomPromptApply, gptRequestData);
|
|
81
|
+
}, [customPrompt, makeRequest, markup, onCustomPromptApply]);
|
|
82
|
+
const handleCustomPromptKeyPress = (0, react_1.useCallback)(async (event) => {
|
|
83
|
+
if (!(0, utils_1.isEnter)(event)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
await handleCustomPromptApply();
|
|
87
|
+
}, [handleCustomPromptApply]);
|
|
88
|
+
const handlePresetClick = (0, react_1.useCallback)(async (data) => {
|
|
89
|
+
const gptRequestData = {
|
|
90
|
+
markup,
|
|
91
|
+
promptData: data,
|
|
92
|
+
};
|
|
93
|
+
await makeRequest(onPromptPresetClick, gptRequestData);
|
|
94
|
+
}, [makeRequest, markup, onPromptPresetClick]);
|
|
95
|
+
const handleTryAgain = (0, react_1.useCallback)(async () => {
|
|
96
|
+
if (!lastRequestData) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
await makeRequest(onTryAgain, lastRequestData);
|
|
100
|
+
}, [lastRequestData, makeRequest, onTryAgain]);
|
|
101
|
+
const handleApplyResult = (0, react_1.useCallback)(() => {
|
|
102
|
+
var _a;
|
|
103
|
+
onApplyResult((_a = answer === null || answer === void 0 ? void 0 : answer.rawText) !== null && _a !== void 0 ? _a : '');
|
|
104
|
+
}, [answer === null || answer === void 0 ? void 0 : answer.rawText, onApplyResult]);
|
|
105
|
+
const handleFreshStart = (0, react_1.useCallback)(() => {
|
|
106
|
+
setError(false);
|
|
107
|
+
setLastRequestData(undefined);
|
|
108
|
+
setAnswer(undefined);
|
|
109
|
+
setCustomPrompt('');
|
|
110
|
+
setFeedbackType(undefined);
|
|
111
|
+
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(undefined);
|
|
112
|
+
}, [onUpdate]);
|
|
113
|
+
const showTryAgainButton = Boolean(lastRequestData && onTryAgain && !loading);
|
|
114
|
+
const showAnswer = Boolean(answer && !loading && !error);
|
|
115
|
+
const showError = error && !loading;
|
|
116
|
+
const showAnswerActions = (onLike || onDislike) && showAnswer;
|
|
117
|
+
let mode = 'custom-and-presets';
|
|
118
|
+
if (onCustomPromptApply && !(promptPresets === null || promptPresets === void 0 ? void 0 : promptPresets.length)) {
|
|
119
|
+
mode = 'only-custom';
|
|
120
|
+
}
|
|
121
|
+
else if (!onCustomPromptApply && (promptPresets === null || promptPresets === void 0 ? void 0 : promptPresets.length)) {
|
|
122
|
+
mode = 'only-presets';
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
answer,
|
|
126
|
+
customPrompt,
|
|
127
|
+
lastRequestData,
|
|
128
|
+
loading,
|
|
129
|
+
error,
|
|
130
|
+
mode,
|
|
131
|
+
feedbackType,
|
|
132
|
+
feedbackTypeLoading,
|
|
133
|
+
handleLike,
|
|
134
|
+
handleDislike,
|
|
135
|
+
handleCustomPromptUpdate: setCustomPrompt,
|
|
136
|
+
handleCustomPromptKeyPress,
|
|
137
|
+
handleCustomPromptApply,
|
|
138
|
+
handlePresetClick,
|
|
139
|
+
handleTryAgain,
|
|
140
|
+
handleFreshStart,
|
|
141
|
+
handleApplyResult,
|
|
142
|
+
showTryAgainButton,
|
|
143
|
+
showAnswer,
|
|
144
|
+
showError,
|
|
145
|
+
showAnswerActions,
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
exports.useGpt = useGpt;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useGptHotKeys = void 0;
|
|
4
|
+
const react_hotkeys_hook_1 = require("react-hotkeys-hook");
|
|
5
|
+
function useGptHotKeys(key, callback, options = {}, dependencies) {
|
|
6
|
+
(0, react_hotkeys_hook_1.useHotkeys)(key, callback, Object.assign({ preventDefault: true }, options), dependencies);
|
|
7
|
+
}
|
|
8
|
+
exports.useGptHotKeys = useGptHotKeys;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RefObject } from 'react';
|
|
2
|
+
export declare type UseOverflowingContainerListItemsProps<ItemType extends unknown> = {
|
|
3
|
+
containerRef: RefObject<HTMLElement>;
|
|
4
|
+
items?: ItemType[];
|
|
5
|
+
itemSelector: string;
|
|
6
|
+
moreButtonSelector: string;
|
|
7
|
+
marginBetweenItems?: number;
|
|
8
|
+
};
|
|
9
|
+
export declare function useOverflowingHorizontalItems<ItemType>({ containerRef, items, itemSelector, moreButtonSelector, marginBetweenItems, }: UseOverflowingContainerListItemsProps<ItemType>): {
|
|
10
|
+
visibleItems: ItemType[];
|
|
11
|
+
hiddenItems: ItemType[];
|
|
12
|
+
measured: boolean;
|
|
13
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOverflowingHorizontalItems = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const debounce_1 = tslib_1.__importDefault(require("lodash/debounce"));
|
|
7
|
+
function useOverflowingHorizontalItems({ containerRef, items, itemSelector, moreButtonSelector, marginBetweenItems = 0, }) {
|
|
8
|
+
const [containerWidth, setContainerWidth] = (0, react_1.useState)(0);
|
|
9
|
+
const [itemWidths, setItemWidths] = (0, react_1.useState)([]);
|
|
10
|
+
const [moreButtonWidth, setMoreButtonWidth] = (0, react_1.useState)(0);
|
|
11
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
12
|
+
const measureItemSizes = () => {
|
|
13
|
+
var _a, _b, _c;
|
|
14
|
+
const itemElements = Array.from((_b = (_a = containerRef.current) === null || _a === void 0 ? void 0 : _a.querySelectorAll(itemSelector)) !== null && _b !== void 0 ? _b : []);
|
|
15
|
+
const moreButton = (_c = containerRef.current) === null || _c === void 0 ? void 0 : _c.querySelector(moreButtonSelector);
|
|
16
|
+
setItemWidths(itemElements.map((item) => item.clientWidth));
|
|
17
|
+
if (moreButton) {
|
|
18
|
+
setMoreButtonWidth(moreButton.clientWidth);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
requestAnimationFrame(measureItemSizes);
|
|
22
|
+
}, [containerRef, itemSelector, moreButtonSelector]);
|
|
23
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
24
|
+
const container = containerRef.current;
|
|
25
|
+
if (!container) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const updateContainerSize = (entries) => {
|
|
29
|
+
if (entries.length > 0) {
|
|
30
|
+
setContainerWidth(entries[0].contentRect.width);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const updateContainerSizeDebounced = (0, debounce_1.default)(updateContainerSize, 100);
|
|
34
|
+
const containerResizeObserver = new ResizeObserver(updateContainerSizeDebounced);
|
|
35
|
+
containerResizeObserver.observe(container);
|
|
36
|
+
return () => containerResizeObserver.unobserve(container);
|
|
37
|
+
}, [containerRef]);
|
|
38
|
+
const isMeasured = itemWidths.length > 0;
|
|
39
|
+
const { visibleItems, hiddenItems } = (0, react_1.useMemo)(() => {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
if (!isMeasured) {
|
|
42
|
+
return {
|
|
43
|
+
visibleItems: items !== null && items !== void 0 ? items : [],
|
|
44
|
+
hiddenItems: [],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const itemsCount = itemWidths.length;
|
|
48
|
+
let visibleItemsCount = 0;
|
|
49
|
+
const spaceForMoreButton = moreButtonWidth + marginBetweenItems;
|
|
50
|
+
let remainingContainerWidth = containerWidth;
|
|
51
|
+
for (const width of itemWidths) {
|
|
52
|
+
const itemWidthWithMargin = width + marginBetweenItems;
|
|
53
|
+
remainingContainerWidth -= itemWidthWithMargin;
|
|
54
|
+
if (remainingContainerWidth < spaceForMoreButton) {
|
|
55
|
+
const isMoreThanOneItemLeft = itemsCount !== visibleItemsCount + 1;
|
|
56
|
+
const hasNoSpaceForTheLastItem = remainingContainerWidth < 0;
|
|
57
|
+
if (isMoreThanOneItemLeft || hasNoSpaceForTheLastItem) {
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
visibleItemsCount++;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
visibleItems: (_a = items === null || items === void 0 ? void 0 : items.slice(0, visibleItemsCount)) !== null && _a !== void 0 ? _a : [],
|
|
65
|
+
hiddenItems: (_b = items === null || items === void 0 ? void 0 : items.slice(visibleItemsCount)) !== null && _b !== void 0 ? _b : [],
|
|
66
|
+
};
|
|
67
|
+
}, [containerWidth, isMeasured, itemWidths, items, marginBetweenItems, moreButtonWidth]);
|
|
68
|
+
return { visibleItems, hiddenItems, measured: isMeasured };
|
|
69
|
+
}
|
|
70
|
+
exports.useOverflowingHorizontalItems = useOverflowingHorizontalItems;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DropdownMenuItem } from '@gravity-ui/uikit';
|
|
2
|
+
import type { GptDialogProps } from '../GptDialog/GptDialog';
|
|
3
|
+
import type { PresetListProps } from '../PresetList/PresetList';
|
|
4
|
+
declare type UsePresetListProps = Pick<GptDialogProps, 'promptPresets'> & {
|
|
5
|
+
onPresetClick: PresetListProps['onPresetClick'];
|
|
6
|
+
};
|
|
7
|
+
export declare const usePresetList: ({ promptPresets, onPresetClick }: UsePresetListProps) => {
|
|
8
|
+
measured: boolean;
|
|
9
|
+
showMoreButton: boolean;
|
|
10
|
+
presetsContainerRef: import("react").RefObject<HTMLDivElement>;
|
|
11
|
+
visiblePresets: import("../ErrorScreen/types").PromptPreset<unknown>[];
|
|
12
|
+
hiddenPresets: DropdownMenuItem<unknown>[];
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePresetList = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const PresetList_1 = require("../PresetList/PresetList");
|
|
6
|
+
const useOverflowingHorizontalItems_1 = require("./useOverflowingHorizontalItems");
|
|
7
|
+
const usePresetList = ({ promptPresets, onPresetClick }) => {
|
|
8
|
+
const presetsContainerRef = (0, react_1.useRef)(null);
|
|
9
|
+
const { visibleItems, hiddenItems, measured } = (0, useOverflowingHorizontalItems_1.useOverflowingHorizontalItems)({
|
|
10
|
+
containerRef: presetsContainerRef,
|
|
11
|
+
items: promptPresets,
|
|
12
|
+
itemSelector: `.${(0, PresetList_1.cnGptDialogPresetList)('preset')}`,
|
|
13
|
+
moreButtonSelector: `.${(0, PresetList_1.cnGptDialogPresetList)('more-button-wrapper')}`,
|
|
14
|
+
marginBetweenItems: 8,
|
|
15
|
+
});
|
|
16
|
+
const hiddenPresets = (0, react_1.useMemo)(() => {
|
|
17
|
+
const items = [];
|
|
18
|
+
for (const item of hiddenItems) {
|
|
19
|
+
items.push({
|
|
20
|
+
text: item.display,
|
|
21
|
+
action: () => onPresetClick(item.data),
|
|
22
|
+
items: [],
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return items;
|
|
26
|
+
}, [onPresetClick, hiddenItems]);
|
|
27
|
+
const showMoreButton = !measured || hiddenPresets.length > 0;
|
|
28
|
+
return {
|
|
29
|
+
measured,
|
|
30
|
+
showMoreButton,
|
|
31
|
+
presetsContainerRef,
|
|
32
|
+
visiblePresets: visibleItems,
|
|
33
|
+
hiddenPresets,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
exports.usePresetList = usePresetList;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Plugin, PluginKey } from 'prosemirror-state';
|
|
2
|
+
import { DecorationSet } from 'prosemirror-view';
|
|
3
|
+
import type { GptWidgetDecoViewParams } from './gptExtension/view';
|
|
4
|
+
export declare type GptWidgetMeta = {
|
|
5
|
+
action: 'show';
|
|
6
|
+
from: number;
|
|
7
|
+
to: number;
|
|
8
|
+
} | {
|
|
9
|
+
action: 'hide';
|
|
10
|
+
};
|
|
11
|
+
declare const key: PluginKey<DecorationSet>;
|
|
12
|
+
export { key as pluginKey };
|
|
13
|
+
export declare const gptWidgetPlugin: (params: GptWidgetDecoViewParams) => Plugin;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.gptWidgetPlugin = exports.pluginKey = void 0;
|
|
4
|
+
const prosemirror_state_1 = require("prosemirror-state");
|
|
5
|
+
const prosemirror_view_1 = require("prosemirror-view");
|
|
6
|
+
const constants_1 = require("./constants");
|
|
7
|
+
const view_1 = require("./gptExtension/view");
|
|
8
|
+
const key = new prosemirror_state_1.PluginKey('gpt-widget');
|
|
9
|
+
exports.pluginKey = key;
|
|
10
|
+
const gptWidgetPlugin = (params) => {
|
|
11
|
+
return new prosemirror_state_1.Plugin({
|
|
12
|
+
key,
|
|
13
|
+
state: {
|
|
14
|
+
init: () => prosemirror_view_1.DecorationSet.empty,
|
|
15
|
+
apply: (tr, decos) => {
|
|
16
|
+
const meta = tr.getMeta(key);
|
|
17
|
+
const paramsGpt = params;
|
|
18
|
+
if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'show') {
|
|
19
|
+
if (meta.to === meta.from) {
|
|
20
|
+
const spanElem = document.createElement('span');
|
|
21
|
+
spanElem.className = constants_1.WIDGET_DECO_CLASS_NAME;
|
|
22
|
+
spanElem.textContent = ' ';
|
|
23
|
+
paramsGpt.disablePromptPresets = true;
|
|
24
|
+
return prosemirror_view_1.DecorationSet.create(tr.doc, [
|
|
25
|
+
prosemirror_view_1.Decoration.widget(meta.from, spanElem, {
|
|
26
|
+
[constants_1.WIDGET_DECO_SPEC_FLAG]: true,
|
|
27
|
+
}),
|
|
28
|
+
]);
|
|
29
|
+
}
|
|
30
|
+
return prosemirror_view_1.DecorationSet.create(tr.doc, [
|
|
31
|
+
prosemirror_view_1.Decoration.inline(meta.from, meta.to, { nodeName: 'span', class: constants_1.WIDGET_DECO_CLASS_NAME }, { [constants_1.WIDGET_DECO_SPEC_FLAG]: true }),
|
|
32
|
+
]);
|
|
33
|
+
}
|
|
34
|
+
if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'hide') {
|
|
35
|
+
paramsGpt.disablePromptPresets = false;
|
|
36
|
+
return prosemirror_view_1.DecorationSet.empty;
|
|
37
|
+
}
|
|
38
|
+
return decos.map(tr.mapping, tr.doc);
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
props: {
|
|
42
|
+
decorations: (state) => key.getState(state),
|
|
43
|
+
},
|
|
44
|
+
view: (view) => new view_1.GptWidgetDecoView(view, params),
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
exports.gptWidgetPlugin = gptWidgetPlugin;
|