one-design-next 0.0.123 → 0.0.124
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/dist/_genui-inline-ref.d.ts +1 -1
- package/dist/_genui-inline-ref.js +3 -2
- package/dist/_genui-types.d.ts +21 -4
- package/dist/_genui-types.js +4 -0
- package/dist/composer/editor.js +15 -1
- package/dist/composer/hooks/useChipManager.js +1 -0
- package/dist/composer/index.d.ts +42 -7
- package/dist/composer/index.js +312 -160
- package/dist/composer/inline-ref.d.ts +6 -1
- package/dist/composer/inline-ref.js +22 -2
- package/dist/composer/insert.d.ts +12 -0
- package/dist/composer/insert.js +23 -0
- package/dist/composer/mention-menu.d.ts +11 -0
- package/dist/composer/mention-menu.js +50 -0
- package/dist/composer/mention.d.ts +10 -0
- package/dist/composer/mention.js +18 -0
- package/dist/composer/segments.d.ts +8 -2
- package/dist/composer/segments.js +33 -2
- package/dist/composer/slot.d.ts +11 -0
- package/dist/composer/slot.js +121 -0
- package/dist/composer/style/index.css +257 -7
- package/dist/composer/utils.d.ts +7 -0
- package/dist/composer/utils.js +15 -0
- package/dist/dropdown/index.d.ts +4 -0
- package/dist/dropdown/index.js +14 -6
- package/dist/icon/svg-data.d.ts +4 -0
- package/dist/icon/svg-data.js +1 -1
- package/dist/icon/types.d.ts +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/mention/index.js +8 -2
- package/dist/user-bubble/render-segments.js +8 -1
- package/package.json +1 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { ChipData } from '../_genui-types';
|
|
3
3
|
export declare function isMentionChip(chip: ChipData): boolean;
|
|
4
|
+
export declare function isSlotChip(chip: ChipData): boolean;
|
|
5
|
+
export declare function slotValueOf(chip: ChipData): string;
|
|
4
6
|
/** 仅待补充 / 已填参数的 invocation 可点开面板;无参数 skill 无 hover、不可点。 */
|
|
5
7
|
export declare function chipHasEditableParams(chip: ChipData): boolean;
|
|
6
8
|
export interface ComposerInlineRefProps {
|
|
@@ -9,6 +11,9 @@ export interface ComposerInlineRefProps {
|
|
|
9
11
|
active?: boolean;
|
|
10
12
|
/** 有参数面板时由 Composer 注入;点击打开面板 */
|
|
11
13
|
onClick?: (anchor: HTMLElement) => void;
|
|
14
|
+
/** 槽位里的字变了。只对 kind=slot 调用 */
|
|
15
|
+
onSlotChange?: (value: string) => void;
|
|
16
|
+
disabled?: boolean;
|
|
12
17
|
}
|
|
13
18
|
/**
|
|
14
19
|
* Composer 内联引用视觉:按 kind 派发 Invocation(/)或 Mention(@)。
|
|
@@ -16,5 +21,5 @@ export interface ComposerInlineRefProps {
|
|
|
16
21
|
* 注:派生 hint 不在 host 内渲染——它是 host 的兄弟节点(见 `editor.tsx` 的
|
|
17
22
|
* `syncHintNode`),否则「chip 之后、hint 之前」这个 caret 位置会不存在。
|
|
18
23
|
*/
|
|
19
|
-
export declare function ComposerInlineRef({ data, active, onClick, }: ComposerInlineRefProps): React.JSX.Element;
|
|
24
|
+
export declare function ComposerInlineRef({ data, active, onClick, onSlotChange, disabled, }: ComposerInlineRefProps): React.JSX.Element;
|
|
20
25
|
export default ComposerInlineRef;
|
|
@@ -2,13 +2,22 @@ import React from 'react';
|
|
|
2
2
|
import { Invocation } from "../invocation";
|
|
3
3
|
import { Mention } from "../mention";
|
|
4
4
|
import { chipToInvocationData, chipToMentionData } from "./send-meta";
|
|
5
|
+
import { ComposerSlot } from "./slot";
|
|
5
6
|
export function isMentionChip(chip) {
|
|
6
7
|
return chip.kind === 'mention';
|
|
7
8
|
}
|
|
9
|
+
export function isSlotChip(chip) {
|
|
10
|
+
return chip.kind === 'slot';
|
|
11
|
+
}
|
|
12
|
+
export function slotValueOf(chip) {
|
|
13
|
+
var _chip$payload;
|
|
14
|
+
var value = (_chip$payload = chip.payload) === null || _chip$payload === void 0 ? void 0 : _chip$payload.value;
|
|
15
|
+
return typeof value === 'string' ? value : '';
|
|
16
|
+
}
|
|
8
17
|
|
|
9
18
|
/** 仅待补充 / 已填参数的 invocation 可点开面板;无参数 skill 无 hover、不可点。 */
|
|
10
19
|
export function chipHasEditableParams(chip) {
|
|
11
|
-
if (isMentionChip(chip)) return false;
|
|
20
|
+
if (isMentionChip(chip) || isSlotChip(chip)) return false;
|
|
12
21
|
if (chip.state === 'pending') return true;
|
|
13
22
|
var payload = chip.payload;
|
|
14
23
|
if (!payload) return false;
|
|
@@ -26,7 +35,18 @@ export function ComposerInlineRef(_ref) {
|
|
|
26
35
|
var data = _ref.data,
|
|
27
36
|
_ref$active = _ref.active,
|
|
28
37
|
active = _ref$active === void 0 ? false : _ref$active,
|
|
29
|
-
onClick = _ref.onClick
|
|
38
|
+
onClick = _ref.onClick,
|
|
39
|
+
onSlotChange = _ref.onSlotChange,
|
|
40
|
+
_ref$disabled = _ref.disabled,
|
|
41
|
+
disabled = _ref$disabled === void 0 ? false : _ref$disabled;
|
|
42
|
+
if (isSlotChip(data)) {
|
|
43
|
+
return /*#__PURE__*/React.createElement(ComposerSlot, {
|
|
44
|
+
name: data.label,
|
|
45
|
+
value: slotValueOf(data),
|
|
46
|
+
disabled: disabled,
|
|
47
|
+
onChange: onSlotChange !== null && onSlotChange !== void 0 ? onSlotChange : function () {}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
30
50
|
if (isMentionChip(data)) {
|
|
31
51
|
return /*#__PURE__*/React.createElement(Mention, {
|
|
32
52
|
data: chipToMentionData(data),
|
|
@@ -17,6 +17,7 @@ import type { ChipData, SkillItem } from '../_genui-types';
|
|
|
17
17
|
* insertContent 支持的内容片段:
|
|
18
18
|
* - `text`:普通文本(支持 `\n`;`\r\n` 会归一成 `\n`)
|
|
19
19
|
* - `skill`:按技能目录的条目生成一个 invocation chip(含 hint / initialState)
|
|
20
|
+
* - `slot`:提示词槽位。`name` 是空槽上的名称,`value` 是已经填好的字
|
|
20
21
|
* - `chip`:逃生舱,业务自带完整 chip 数据(mention / 自定义 kind / 已存草稿)
|
|
21
22
|
*/
|
|
22
23
|
export type ComposerInsertPart = {
|
|
@@ -25,6 +26,11 @@ export type ComposerInsertPart = {
|
|
|
25
26
|
} | {
|
|
26
27
|
type: 'skill';
|
|
27
28
|
skill: SkillItem;
|
|
29
|
+
} | {
|
|
30
|
+
type: 'slot';
|
|
31
|
+
name: string;
|
|
32
|
+
value?: string;
|
|
33
|
+
templateId?: string;
|
|
28
34
|
} | {
|
|
29
35
|
type: 'chip';
|
|
30
36
|
chip: ChipData;
|
|
@@ -65,6 +71,12 @@ export interface ComposerInsertPayload {
|
|
|
65
71
|
* hint 随 chip 快照下来(而不是渲染时回查技能目录):草稿用
|
|
66
72
|
* `defaultValue + defaultChips` 还原时,目录可能已经变了。
|
|
67
73
|
*/
|
|
74
|
+
/** 提示词槽位 chip。名称留在 label,填入的字放 payload.value,发送时再读出来。 */
|
|
75
|
+
export declare function createSlotChipData(slot: {
|
|
76
|
+
name: string;
|
|
77
|
+
value?: string;
|
|
78
|
+
templateId?: string;
|
|
79
|
+
}): ChipData;
|
|
68
80
|
export declare function createSkillChipData(skill: SkillItem): ChipData;
|
|
69
81
|
/**
|
|
70
82
|
* 把 parts 编成一次插入所需的 raw value + chips。
|
package/dist/composer/insert.js
CHANGED
|
@@ -22,6 +22,7 @@ import { createMarker, stripInvisibleChars } from "./utils";
|
|
|
22
22
|
* insertContent 支持的内容片段:
|
|
23
23
|
* - `text`:普通文本(支持 `\n`;`\r\n` 会归一成 `\n`)
|
|
24
24
|
* - `skill`:按技能目录的条目生成一个 invocation chip(含 hint / initialState)
|
|
25
|
+
* - `slot`:提示词槽位。`name` 是空槽上的名称,`value` 是已经填好的字
|
|
25
26
|
* - `chip`:逃生舱,业务自带完整 chip 数据(mention / 自定义 kind / 已存草稿)
|
|
26
27
|
*/
|
|
27
28
|
|
|
@@ -37,6 +38,19 @@ var randomSuffix = function randomSuffix() {
|
|
|
37
38
|
* hint 随 chip 快照下来(而不是渲染时回查技能目录):草稿用
|
|
38
39
|
* `defaultValue + defaultChips` 还原时,目录可能已经变了。
|
|
39
40
|
*/
|
|
41
|
+
/** 提示词槽位 chip。名称留在 label,填入的字放 payload.value,发送时再读出来。 */
|
|
42
|
+
export function createSlotChipData(slot) {
|
|
43
|
+
var _slot$value;
|
|
44
|
+
return {
|
|
45
|
+
id: "slot-".concat(randomSuffix()),
|
|
46
|
+
label: slot.name,
|
|
47
|
+
kind: 'slot',
|
|
48
|
+
refId: slot.templateId,
|
|
49
|
+
payload: {
|
|
50
|
+
value: (_slot$value = slot.value) !== null && _slot$value !== void 0 ? _slot$value : ''
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
40
54
|
export function createSkillChipData(skill) {
|
|
41
55
|
return {
|
|
42
56
|
id: "skill-".concat(skill.id, "-").concat(randomSuffix()),
|
|
@@ -106,6 +120,15 @@ export function buildInsertPayload(parts) {
|
|
|
106
120
|
var chip = null;
|
|
107
121
|
if (part.type === 'skill') {
|
|
108
122
|
chip = part.skill ? createSkillChipData(part.skill) : null;
|
|
123
|
+
} else if (part.type === 'slot') {
|
|
124
|
+
var name = part.name.trim();
|
|
125
|
+
if (name) {
|
|
126
|
+
chip = createSlotChipData({
|
|
127
|
+
name: name,
|
|
128
|
+
value: part.value,
|
|
129
|
+
templateId: part.templateId
|
|
130
|
+
});
|
|
131
|
+
}
|
|
109
132
|
} else if (part.chip) {
|
|
110
133
|
chip = _objectSpread({}, part.chip);
|
|
111
134
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { ComposerMentionItem } from './mention';
|
|
3
|
+
export interface MentionMenuProps {
|
|
4
|
+
items: ComposerMentionItem[];
|
|
5
|
+
activeIndex: number;
|
|
6
|
+
width?: number;
|
|
7
|
+
maxHeight?: number;
|
|
8
|
+
onActiveIndexChange: (index: number) => void;
|
|
9
|
+
onSelect: (item: ComposerMentionItem) => void;
|
|
10
|
+
}
|
|
11
|
+
export declare function MentionMenu({ items, activeIndex, width, maxHeight, onActiveIndexChange, onSelect, }: MentionMenuProps): React.JSX.Element;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
|
+
export function MentionMenu(_ref) {
|
|
3
|
+
var items = _ref.items,
|
|
4
|
+
activeIndex = _ref.activeIndex,
|
|
5
|
+
width = _ref.width,
|
|
6
|
+
maxHeight = _ref.maxHeight,
|
|
7
|
+
onActiveIndexChange = _ref.onActiveIndexChange,
|
|
8
|
+
onSelect = _ref.onSelect;
|
|
9
|
+
var itemRefs = useRef([]);
|
|
10
|
+
useEffect(function () {
|
|
11
|
+
var _itemRefs$current$act;
|
|
12
|
+
(_itemRefs$current$act = itemRefs.current[activeIndex]) === null || _itemRefs$current$act === void 0 || _itemRefs$current$act.scrollIntoView({
|
|
13
|
+
block: 'nearest'
|
|
14
|
+
});
|
|
15
|
+
}, [activeIndex]);
|
|
16
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
17
|
+
"data-odn-composer-mention-menu": true,
|
|
18
|
+
style: {
|
|
19
|
+
width: width,
|
|
20
|
+
maxHeight: maxHeight
|
|
21
|
+
}
|
|
22
|
+
}, items.length === 0 ? /*#__PURE__*/React.createElement("div", {
|
|
23
|
+
"data-odn-composer-mention-empty": true
|
|
24
|
+
}, "\u6682\u65E0\u6587\u4EF6") : items.map(function (item, index) {
|
|
25
|
+
return /*#__PURE__*/React.createElement("button", {
|
|
26
|
+
key: item.id,
|
|
27
|
+
type: "button",
|
|
28
|
+
ref: function ref(node) {
|
|
29
|
+
itemRefs.current[index] = node;
|
|
30
|
+
},
|
|
31
|
+
"data-odn-composer-mention-item": true,
|
|
32
|
+
"data-active": index === activeIndex ? '' : undefined,
|
|
33
|
+
onMouseEnter: function onMouseEnter() {
|
|
34
|
+
return onActiveIndexChange(index);
|
|
35
|
+
},
|
|
36
|
+
onMouseDown: function onMouseDown(event) {
|
|
37
|
+
return event.preventDefault();
|
|
38
|
+
},
|
|
39
|
+
onClick: function onClick() {
|
|
40
|
+
return onSelect(item);
|
|
41
|
+
}
|
|
42
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
43
|
+
"data-odn-composer-mention-copy": true
|
|
44
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
45
|
+
"data-odn-composer-mention-label": true
|
|
46
|
+
}, item.label), item.description ? /*#__PURE__*/React.createElement("span", {
|
|
47
|
+
"data-odn-composer-mention-desc": true
|
|
48
|
+
}, item.description) : null));
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ChipData } from '../_genui-types';
|
|
2
|
+
/** `@` 面板里的一条引用。`id` 会写进 mention 段的 `refId`。 */
|
|
3
|
+
export interface ComposerMentionItem {
|
|
4
|
+
id: string;
|
|
5
|
+
label: string;
|
|
6
|
+
/** 副文案,例如「我上传 · 会话名 · 时间」 */
|
|
7
|
+
description?: string;
|
|
8
|
+
}
|
|
9
|
+
/** 把一条引用编成编辑器里的 mention 块。 */
|
|
10
|
+
export declare function createMentionChipData(item: ComposerMentionItem): ChipData;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** `@` 面板里的一条引用。`id` 会写进 mention 段的 `refId`。 */
|
|
2
|
+
|
|
3
|
+
var randomSuffix = function randomSuffix() {
|
|
4
|
+
return "".concat(Date.now().toString(36), "-").concat(Math.random().toString(36).slice(2, 6));
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/** 把一条引用编成编辑器里的 mention 块。 */
|
|
8
|
+
export function createMentionChipData(item) {
|
|
9
|
+
return {
|
|
10
|
+
id: "mention-".concat(item.id, "-").concat(randomSuffix()),
|
|
11
|
+
label: item.label,
|
|
12
|
+
kind: 'mention',
|
|
13
|
+
refId: item.id,
|
|
14
|
+
payload: item.description ? {
|
|
15
|
+
description: item.description
|
|
16
|
+
} : undefined
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -14,13 +14,14 @@
|
|
|
14
14
|
* 一句话(默认 `[Skill: label]` / `[@label]`,与 ComposerMention 兼容)。
|
|
15
15
|
* - `segmentsHasContent`:是否含可见内容;库内 send 校验用,业务可复用。
|
|
16
16
|
*/
|
|
17
|
-
import type { ChipData, ComposerHintSegment, ComposerInvocationSegment, ComposerMentionSegment, ComposerSegment, ComposerTextSegment, HintData, TextData } from '../_genui-types';
|
|
17
|
+
import type { ChipData, ComposerHintSegment, ComposerInvocationSegment, ComposerMentionSegment, ComposerSegment, ComposerSlotSegment, ComposerTextSegment, HintData, SlotData, TextData } from '../_genui-types';
|
|
18
18
|
import { ComposerSegmentType } from '../_genui-types';
|
|
19
19
|
export { ComposerSegmentType };
|
|
20
|
-
export type { ComposerHintSegment, ComposerInvocationSegment, ComposerMentionSegment, ComposerSegment, ComposerTextSegment, HintData, TextData, };
|
|
20
|
+
export type { ComposerHintSegment, ComposerInvocationSegment, ComposerMentionSegment, ComposerSegment, ComposerSlotSegment, ComposerTextSegment, HintData, SlotData, TextData, };
|
|
21
21
|
export declare const isTextSegment: (s: ComposerSegment) => s is ComposerTextSegment;
|
|
22
22
|
export declare const isInvocationSegment: (s: ComposerSegment) => s is ComposerInvocationSegment;
|
|
23
23
|
export declare const isMentionSegment: (s: ComposerSegment) => s is ComposerMentionSegment;
|
|
24
|
+
export declare const isSlotSegment: (s: ComposerSegment) => s is ComposerSlotSegment;
|
|
24
25
|
export declare const isHintSegment: (s: ComposerSegment) => s is ComposerHintSegment;
|
|
25
26
|
/** hint 命中结果。 */
|
|
26
27
|
export interface ComposerHintTarget {
|
|
@@ -79,6 +80,11 @@ export interface SegmentsToReadableTextOptions {
|
|
|
79
80
|
formatInvocation?: (label: string) => string;
|
|
80
81
|
/** mention 占位格式,默认 `[@{label}]` */
|
|
81
82
|
formatMention?: (label: string) => string;
|
|
83
|
+
/**
|
|
84
|
+
* 槽位格式。传入后完全由调用方决定。
|
|
85
|
+
* 默认:填过的只输出填入的字;没填的写成 `[名称]`,并按相邻正文补上空格。
|
|
86
|
+
*/
|
|
87
|
+
formatSlot?: (slot: SlotData) => string;
|
|
82
88
|
}
|
|
83
89
|
/**
|
|
84
90
|
* 把 segments 拼成一条字符串,供需要单串 string 的 LLM / 旧后端使用。
|
|
@@ -34,6 +34,9 @@ export var isInvocationSegment = function isInvocationSegment(s) {
|
|
|
34
34
|
export var isMentionSegment = function isMentionSegment(s) {
|
|
35
35
|
return s.type === ComposerSegmentType.Mention;
|
|
36
36
|
};
|
|
37
|
+
export var isSlotSegment = function isSlotSegment(s) {
|
|
38
|
+
return s.type === ComposerSegmentType.Slot;
|
|
39
|
+
};
|
|
37
40
|
export var isHintSegment = function isHintSegment(s) {
|
|
38
41
|
return s.type === ComposerSegmentType.Hint;
|
|
39
42
|
};
|
|
@@ -186,6 +189,18 @@ export function rawValueToSegments(raw, chips) {
|
|
|
186
189
|
type: 'mention',
|
|
187
190
|
data: chipToMentionData(chip)
|
|
188
191
|
});
|
|
192
|
+
} else if (chip.kind === 'slot') {
|
|
193
|
+
var _chip$payload;
|
|
194
|
+
var rawValue = (_chip$payload = chip.payload) === null || _chip$payload === void 0 ? void 0 : _chip$payload.value;
|
|
195
|
+
segments.push({
|
|
196
|
+
type: 'slot',
|
|
197
|
+
data: {
|
|
198
|
+
id: chip.id,
|
|
199
|
+
name: chip.label,
|
|
200
|
+
value: typeof rawValue === 'string' ? rawValue : '',
|
|
201
|
+
templateId: chip.refId
|
|
202
|
+
}
|
|
203
|
+
});
|
|
189
204
|
} else {
|
|
190
205
|
segments.push({
|
|
191
206
|
type: 'invocation',
|
|
@@ -210,6 +225,18 @@ var defaultFormatInvocation = function defaultFormatInvocation(label) {
|
|
|
210
225
|
var defaultFormatMention = function defaultFormatMention(label) {
|
|
211
226
|
return "[@".concat(label, "]");
|
|
212
227
|
};
|
|
228
|
+
var SLOT_TRAIL_PUNCT = /^[\s。,、;:!?,.!?;:]/;
|
|
229
|
+
|
|
230
|
+
/** 没填的槽:句中写成 ` [名称] `,句首不加前空格,后面是标点不加后空格。 */
|
|
231
|
+
function formatEmptySlot(segments, index, name) {
|
|
232
|
+
var prev = segments[index - 1];
|
|
233
|
+
var next = segments[index + 1];
|
|
234
|
+
var prevText = (prev === null || prev === void 0 ? void 0 : prev.type) === 'text' ? prev.data.text : '';
|
|
235
|
+
var nextText = (next === null || next === void 0 ? void 0 : next.type) === 'text' ? next.data.text : '';
|
|
236
|
+
var lead = prev && (prev.type !== 'text' || prevText.length > 0 && !/\s$/.test(prevText)) ? ' ' : '';
|
|
237
|
+
var trail = nextText.length > 0 && !SLOT_TRAIL_PUNCT.test(nextText) ? ' ' : '';
|
|
238
|
+
return "".concat(lead, "[").concat(name, "]").concat(trail);
|
|
239
|
+
}
|
|
213
240
|
|
|
214
241
|
/**
|
|
215
242
|
* 把 segments 拼成一条字符串,供需要单串 string 的 LLM / 旧后端使用。
|
|
@@ -221,7 +248,7 @@ export function segmentsToReadableText(segments, options) {
|
|
|
221
248
|
var _options$formatInvoca, _options$formatMentio;
|
|
222
249
|
var fmtInv = (_options$formatInvoca = options === null || options === void 0 ? void 0 : options.formatInvocation) !== null && _options$formatInvoca !== void 0 ? _options$formatInvoca : defaultFormatInvocation;
|
|
223
250
|
var fmtMen = (_options$formatMentio = options === null || options === void 0 ? void 0 : options.formatMention) !== null && _options$formatMentio !== void 0 ? _options$formatMentio : defaultFormatMention;
|
|
224
|
-
return segments.map(function (seg) {
|
|
251
|
+
return segments.map(function (seg, index) {
|
|
225
252
|
switch (seg.type) {
|
|
226
253
|
case 'text':
|
|
227
254
|
return seg.data.text;
|
|
@@ -229,6 +256,10 @@ export function segmentsToReadableText(segments, options) {
|
|
|
229
256
|
return fmtInv(seg.data.label);
|
|
230
257
|
case 'mention':
|
|
231
258
|
return fmtMen(seg.data.label);
|
|
259
|
+
case 'slot':
|
|
260
|
+
if (options !== null && options !== void 0 && options.formatSlot) return options.formatSlot(seg.data);
|
|
261
|
+
if (seg.data.value) return seg.data.value;
|
|
262
|
+
return formatEmptySlot(segments, index, seg.data.name);
|
|
232
263
|
case 'hint':
|
|
233
264
|
return seg.data.text;
|
|
234
265
|
default:
|
|
@@ -240,6 +271,6 @@ export function segmentsToReadableText(segments, options) {
|
|
|
240
271
|
/** segments 是否含可见内容(文本非空、含内联引用,或含派生 hint)。 */
|
|
241
272
|
export function segmentsHasContent(segments) {
|
|
242
273
|
return segments.some(function (s) {
|
|
243
|
-
return s.type === 'invocation' || s.type === 'mention' || s.type === 'hint' || s.type === 'text' && s.data.text.trim().length > 0;
|
|
274
|
+
return s.type === 'invocation' || s.type === 'mention' || s.type === 'slot' || s.type === 'hint' || s.type === 'text' && s.data.text.trim().length > 0;
|
|
244
275
|
});
|
|
245
276
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface ComposerSlotProps {
|
|
3
|
+
/** 空槽上显示的名称 */
|
|
4
|
+
name: string;
|
|
5
|
+
/** 已经填入的字。空字符串表示还没填 */
|
|
6
|
+
value: string;
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
onChange: (value: string) => void;
|
|
9
|
+
}
|
|
10
|
+
export declare function ComposerSlot({ name, value, disabled, onChange, }: ComposerSlotProps): React.JSX.Element;
|
|
11
|
+
export default ComposerSlot;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
+
import React, { useLayoutEffect, useRef, useState } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* 提示词槽位。
|
|
5
|
+
*
|
|
6
|
+
* 空槽是 50% 品牌蓝的名称,填过是品牌蓝正文,底都是 8% 品牌蓝。
|
|
7
|
+
* 宽度跟当前文字走,由看不见的镜像撑开。一行放不下时整颗换到下一行;
|
|
8
|
+
* 比整行还长时,镜像和输入一起在槽里折行。
|
|
9
|
+
* 行中左右各留 4px;换到行首时去掉左侧间距,和正文左缘对齐。
|
|
10
|
+
*
|
|
11
|
+
* 输入发生在 contentEditable=false 的 chip 里,键盘事件不能冒泡到编辑器,
|
|
12
|
+
* 否则 Backspace 会删掉整块。Enter 仍交给编辑器,沿用原来的发送。
|
|
13
|
+
* 槽内不保留换行,折行只是宽度不够时的显示。
|
|
14
|
+
*/
|
|
15
|
+
/** 槽位左边距盒是否贴着当前行的左缘。贴着就不再加左侧间距。 */
|
|
16
|
+
function useAtLineStart(ref) {
|
|
17
|
+
var _useState = useState(false),
|
|
18
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
19
|
+
atLineStart = _useState2[0],
|
|
20
|
+
setAtLineStart = _useState2[1];
|
|
21
|
+
useLayoutEffect(function () {
|
|
22
|
+
var slot = ref.current;
|
|
23
|
+
if (!slot) return;
|
|
24
|
+
var editor = slot.closest('[data-odn-composer-editor]');
|
|
25
|
+
var frame = 0;
|
|
26
|
+
var measure = function measure() {
|
|
27
|
+
var el = ref.current;
|
|
28
|
+
var line = el === null || el === void 0 ? void 0 : el.closest('[data-composer-line]');
|
|
29
|
+
if (!el || !line) return;
|
|
30
|
+
var marginLeft = parseFloat(getComputedStyle(el).marginLeft) || 0;
|
|
31
|
+
var at = el.getBoundingClientRect().left - marginLeft - line.getBoundingClientRect().left <= 1;
|
|
32
|
+
setAtLineStart(function (prev) {
|
|
33
|
+
return prev === at ? prev : at;
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
var schedule = function schedule() {
|
|
37
|
+
cancelAnimationFrame(frame);
|
|
38
|
+
frame = requestAnimationFrame(measure);
|
|
39
|
+
};
|
|
40
|
+
measure();
|
|
41
|
+
var resize = new ResizeObserver(schedule);
|
|
42
|
+
resize.observe(slot);
|
|
43
|
+
var line = slot.closest('[data-composer-line]');
|
|
44
|
+
if (line) resize.observe(line);
|
|
45
|
+
var mutation = new MutationObserver(schedule);
|
|
46
|
+
if (editor) {
|
|
47
|
+
mutation.observe(editor, {
|
|
48
|
+
subtree: true,
|
|
49
|
+
childList: true,
|
|
50
|
+
characterData: true
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
window.addEventListener('resize', schedule);
|
|
54
|
+
return function () {
|
|
55
|
+
cancelAnimationFrame(frame);
|
|
56
|
+
resize.disconnect();
|
|
57
|
+
mutation.disconnect();
|
|
58
|
+
window.removeEventListener('resize', schedule);
|
|
59
|
+
};
|
|
60
|
+
}, [ref]);
|
|
61
|
+
return atLineStart;
|
|
62
|
+
}
|
|
63
|
+
export function ComposerSlot(_ref) {
|
|
64
|
+
var name = _ref.name,
|
|
65
|
+
value = _ref.value,
|
|
66
|
+
_ref$disabled = _ref.disabled,
|
|
67
|
+
disabled = _ref$disabled === void 0 ? false : _ref$disabled,
|
|
68
|
+
_onChange = _ref.onChange;
|
|
69
|
+
var filled = value.length > 0;
|
|
70
|
+
var ref = useRef(null);
|
|
71
|
+
var atLineStart = useAtLineStart(ref);
|
|
72
|
+
return /*#__PURE__*/React.createElement("span", {
|
|
73
|
+
ref: ref,
|
|
74
|
+
"data-odn-composer-slot": "",
|
|
75
|
+
"data-filled": filled ? 'true' : 'false',
|
|
76
|
+
"data-at-line-start": atLineStart ? 'true' : undefined
|
|
77
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
78
|
+
"data-odn-composer-slot-mirror": "",
|
|
79
|
+
"aria-hidden": "true"
|
|
80
|
+
}, filled ? value : name), /*#__PURE__*/React.createElement("textarea", {
|
|
81
|
+
"data-odn-composer-slot-input": "",
|
|
82
|
+
value: value,
|
|
83
|
+
placeholder: name,
|
|
84
|
+
"aria-label": name,
|
|
85
|
+
disabled: disabled,
|
|
86
|
+
rows: 1,
|
|
87
|
+
autoComplete: "off",
|
|
88
|
+
onChange: function onChange(event) {
|
|
89
|
+
return _onChange(event.target.value.replace(/[\r\n]+/g, ''));
|
|
90
|
+
},
|
|
91
|
+
onMouseDown: function onMouseDown(event) {
|
|
92
|
+
return event.stopPropagation();
|
|
93
|
+
},
|
|
94
|
+
onInput: function onInput(event) {
|
|
95
|
+
return event.stopPropagation();
|
|
96
|
+
},
|
|
97
|
+
onCompositionStart: function onCompositionStart(event) {
|
|
98
|
+
return event.stopPropagation();
|
|
99
|
+
},
|
|
100
|
+
onCompositionEnd: function onCompositionEnd(event) {
|
|
101
|
+
return event.stopPropagation();
|
|
102
|
+
},
|
|
103
|
+
onPaste: function onPaste(event) {
|
|
104
|
+
return event.stopPropagation();
|
|
105
|
+
},
|
|
106
|
+
onCopy: function onCopy(event) {
|
|
107
|
+
return event.stopPropagation();
|
|
108
|
+
},
|
|
109
|
+
onCut: function onCut(event) {
|
|
110
|
+
return event.stopPropagation();
|
|
111
|
+
},
|
|
112
|
+
onKeyDown: function onKeyDown(event) {
|
|
113
|
+
if (event.key === 'Enter') {
|
|
114
|
+
event.preventDefault();
|
|
115
|
+
if (!event.shiftKey) return;
|
|
116
|
+
}
|
|
117
|
+
event.stopPropagation();
|
|
118
|
+
}
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
export default ComposerSlot;
|