@flowgram.ai/form-materials 0.2.18 → 0.2.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowgram.ai/form-materials",
3
- "version": "0.2.18",
3
+ "version": "0.2.19",
4
4
  "homepage": "https://flowgram.ai/",
5
5
  "repository": "https://github.com/bytedance/flowgram.ai",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  "immer": "~10.1.1",
33
33
  "@coze-editor/editor": "0.1.0-alpha.8d7a30",
34
34
  "@codemirror/view": "~6.38.0",
35
- "@flowgram.ai/editor": "0.2.18"
35
+ "@flowgram.ai/editor": "0.2.19"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/lodash": "^4.14.137",
@@ -48,8 +48,8 @@
48
48
  "tsup": "^8.0.1",
49
49
  "typescript": "^5.0.4",
50
50
  "vitest": "^0.34.6",
51
- "@flowgram.ai/ts-config": "0.2.18",
52
- "@flowgram.ai/eslint-config": "0.2.18"
51
+ "@flowgram.ai/eslint-config": "0.2.19",
52
+ "@flowgram.ai/ts-config": "0.2.19"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "react": ">=16.8",
@@ -65,7 +65,12 @@ export function ConditionRow({ style, value, onChange, readonly }: PropTypes) {
65
65
  onChange={(v) => onChange({ ...value, right: v })}
66
66
  />
67
67
  ) : (
68
- <Input size="small" disabled value={opConfig?.rightDisplay || 'Empty'} />
68
+ <Input
69
+ size="small"
70
+ disabled
71
+ style={{ pointerEvents: 'none' }}
72
+ value={opConfig?.rightDisplay || 'Empty'}
73
+ />
69
74
  )}
70
75
  </UIRight>
71
76
  </UIValues>
@@ -1,6 +1,8 @@
1
1
  {
2
2
  "name": "prompt-editor",
3
- "depMaterials": [],
3
+ "depMaterials": [
4
+ "variable-selector"
5
+ ],
4
6
  "depPackages": [
5
7
  "@coze-editor/editor@0.1.0-alpha.8d7a30",
6
8
  "@codemirror/view",
@@ -0,0 +1,173 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React, { useLayoutEffect } from 'react';
7
+
8
+ import { createRoot, Root } from 'react-dom/client';
9
+ import { isEqual, last } from 'lodash';
10
+ import {
11
+ BaseVariableField,
12
+ Disposable,
13
+ DisposableCollection,
14
+ Scope,
15
+ useCurrentScope,
16
+ } from '@flowgram.ai/editor';
17
+ import { Popover } from '@douyinfe/semi-ui';
18
+ import { IconIssueStroked } from '@douyinfe/semi-icons';
19
+ import { useInjector } from '@coze-editor/editor/react';
20
+ import {
21
+ Decoration,
22
+ DecorationSet,
23
+ EditorView,
24
+ MatchDecorator,
25
+ ViewPlugin,
26
+ WidgetType,
27
+ } from '@codemirror/view';
28
+
29
+ import { UIPopoverContent, UIRootTitle, UITag, UIVarName } from '../styles';
30
+
31
+ class VariableTagWidget extends WidgetType {
32
+ keyPath?: string[];
33
+
34
+ toDispose = new DisposableCollection();
35
+
36
+ scope: Scope;
37
+
38
+ root: Root;
39
+
40
+ constructor({ keyPath, scope }: { keyPath?: string[]; scope: Scope }) {
41
+ super();
42
+
43
+ this.keyPath = keyPath;
44
+ this.scope = scope;
45
+ }
46
+
47
+ renderIcon = (icon: string | JSX.Element) => {
48
+ if (typeof icon === 'string') {
49
+ return <img style={{ marginRight: 8 }} width={12} height={12} src={icon} />;
50
+ }
51
+
52
+ return icon;
53
+ };
54
+
55
+ renderVariable(v?: BaseVariableField) {
56
+ if (!v) {
57
+ this.root.render(
58
+ <UITag prefixIcon={<IconIssueStroked />} color="amber">
59
+ Unknown
60
+ </UITag>
61
+ );
62
+ return;
63
+ }
64
+
65
+ const rootField = last(v.parentFields);
66
+
67
+ const rootTitle = (
68
+ <UIRootTitle>{rootField?.meta.title ? `${rootField.meta.title} -` : ''}</UIRootTitle>
69
+ );
70
+ const rootIcon = this.renderIcon(rootField?.meta.icon);
71
+
72
+ this.root.render(
73
+ <Popover
74
+ content={
75
+ <UIPopoverContent>
76
+ {rootIcon}
77
+ {rootTitle}
78
+ <UIVarName>{v?.keyPath.slice(1).join('.')}</UIVarName>
79
+ </UIPopoverContent>
80
+ }
81
+ >
82
+ <UITag prefixIcon={rootIcon}>
83
+ {rootTitle}
84
+ <UIVarName>{v?.key}</UIVarName>
85
+ </UITag>
86
+ </Popover>
87
+ );
88
+ }
89
+
90
+ toDOM(view: EditorView): HTMLElement {
91
+ const dom = document.createElement('span');
92
+
93
+ this.root = createRoot(dom);
94
+
95
+ this.toDispose.push(
96
+ Disposable.create(() => {
97
+ this.root.unmount();
98
+ })
99
+ );
100
+
101
+ this.toDispose.push(
102
+ this.scope.available.trackByKeyPath(
103
+ this.keyPath,
104
+ (v) => {
105
+ this.renderVariable(v);
106
+ },
107
+ { triggerOnInit: false }
108
+ )
109
+ );
110
+
111
+ this.renderVariable(this.scope.available.getByKeyPath(this.keyPath));
112
+
113
+ return dom;
114
+ }
115
+
116
+ eq(other: VariableTagWidget) {
117
+ return isEqual(this.keyPath, other.keyPath);
118
+ }
119
+
120
+ ignoreEvent(): boolean {
121
+ return false;
122
+ }
123
+
124
+ destroy(dom: HTMLElement): void {
125
+ this.toDispose.dispose();
126
+ }
127
+ }
128
+
129
+ export function VariableTagInject() {
130
+ const injector = useInjector();
131
+
132
+ const scope = useCurrentScope();
133
+
134
+ // 基于 {{var}} 的正则进行匹配,匹配后进行自定义渲染
135
+ useLayoutEffect(() => {
136
+ const atMatcher = new MatchDecorator({
137
+ regexp: /\{\{([^\}]+)\}\}/g,
138
+ decoration: (match) =>
139
+ Decoration.replace({
140
+ widget: new VariableTagWidget({
141
+ keyPath: match[1]?.split('.') ?? [],
142
+ scope,
143
+ }),
144
+ }),
145
+ });
146
+
147
+ return injector.inject([
148
+ ViewPlugin.fromClass(
149
+ class {
150
+ decorations: DecorationSet;
151
+
152
+ constructor(private view: EditorView) {
153
+ this.decorations = atMatcher.createDeco(view);
154
+ }
155
+
156
+ update() {
157
+ this.decorations = atMatcher.createDeco(this.view);
158
+ }
159
+ },
160
+ {
161
+ decorations: (p) => p.decorations,
162
+ provide(p) {
163
+ return EditorView.atomicRanges.of(
164
+ (view) => view.plugin(p)?.decorations ?? Decoration.none
165
+ );
166
+ },
167
+ }
168
+ ),
169
+ ]);
170
+ }, [injector]);
171
+
172
+ return null;
173
+ }
@@ -17,7 +17,7 @@ import { EditorAPI } from '@coze-editor/editor/preset-prompt';
17
17
 
18
18
  import { useVariableTree } from '../../variable-selector';
19
19
 
20
- function Variable() {
20
+ export function VariableTree() {
21
21
  const [posKey, setPosKey] = useState('');
22
22
  const [visible, setVisible] = useState(false);
23
23
  const [position, setPosition] = useState(-1);
@@ -53,7 +53,7 @@ function Variable() {
53
53
 
54
54
  return (
55
55
  <>
56
- <Mention triggerCharacters={['{', '{}']} onOpenChange={handleOpenChange} />
56
+ <Mention triggerCharacters={['{', '{}', '@']} onOpenChange={handleOpenChange} />
57
57
 
58
58
  <Popover
59
59
  visible={visible}
@@ -81,5 +81,3 @@ function Variable() {
81
81
  </>
82
82
  );
83
83
  }
84
-
85
- export default Variable;
@@ -5,13 +5,15 @@
5
5
 
6
6
  import React from 'react';
7
7
 
8
- import Variable from './extensions/variable';
8
+ import { VariableTree } from './extensions/variable-tree';
9
+ import { VariableTagInject } from './extensions/variable-tag';
9
10
  import { PromptEditor, PromptEditorPropsType } from '../prompt-editor';
10
11
 
11
12
  export function PromptEditorWithVariables(props: PromptEditorPropsType) {
12
13
  return (
13
14
  <PromptEditor {...props}>
14
- <Variable />
15
+ <VariableTree />
16
+ <VariableTagInject />
15
17
  </PromptEditor>
16
18
  );
17
19
  }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import styled from 'styled-components';
7
+ import { Tag } from '@douyinfe/semi-ui';
8
+
9
+ export const UIRootTitle = styled.div`
10
+ margin-right: 4px;
11
+ min-width: 20px;
12
+ overflow: hidden;
13
+ text-overflow: ellipsis;
14
+ white-space: nowrap;
15
+ color: var(--semi-color-text-2);
16
+ `;
17
+
18
+ export const UIVarName = styled.div`
19
+ overflow: hidden;
20
+ text-overflow: ellipsis;
21
+ white-space: nowrap;
22
+ `;
23
+
24
+ export const UITag = styled(Tag)`
25
+ display: inline-flex;
26
+ align-items: center;
27
+ justify-content: flex-start;
28
+ max-width: 300px;
29
+
30
+ & .semi-tag-content-center {
31
+ justify-content: flex-start;
32
+ }
33
+
34
+ &.semi-tag {
35
+ margin: 0 5px;
36
+ }
37
+ `;
38
+
39
+ export const UIPopoverContent = styled.div`
40
+ padding: 10px;
41
+ display: inline-flex;
42
+ align-items: center;
43
+ justify-content: flex-start;
44
+ `;