@flowgram.ai/form-materials 0.2.20 → 0.2.22

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.
Files changed (28) hide show
  1. package/dist/esm/index.js +683 -4
  2. package/dist/esm/index.js.map +1 -1
  3. package/dist/index.d.mts +42 -10
  4. package/dist/index.d.ts +42 -10
  5. package/dist/index.js +707 -48
  6. package/dist/index.js.map +1 -1
  7. package/package.json +6 -5
  8. package/src/components/code-editor/config.json +9 -0
  9. package/src/components/code-editor/index.tsx +59 -0
  10. package/src/components/code-editor/language-features.ts +24 -0
  11. package/src/components/code-editor/theme/dark.ts +119 -0
  12. package/src/components/code-editor/theme/index.ts +12 -0
  13. package/src/components/code-editor/theme/light.ts +119 -0
  14. package/src/components/code-editor/utils.ts +20 -0
  15. package/src/components/index.ts +3 -0
  16. package/src/components/json-editor-with-variables/config.json +13 -0
  17. package/src/components/json-editor-with-variables/extensions/variable-tag.tsx +173 -0
  18. package/src/components/json-editor-with-variables/extensions/variable-tree.tsx +83 -0
  19. package/src/components/json-editor-with-variables/index.tsx +19 -0
  20. package/src/components/json-editor-with-variables/styles.tsx +44 -0
  21. package/src/components/prompt-editor/config.json +1 -1
  22. package/src/components/prompt-editor/index.tsx +15 -2
  23. package/src/components/prompt-editor/types.tsx +2 -0
  24. package/src/components/prompt-editor-with-inputs/config.json +13 -0
  25. package/src/components/prompt-editor-with-inputs/extensions/inputs-tree.tsx +82 -0
  26. package/src/components/prompt-editor-with-inputs/index.tsx +22 -0
  27. package/src/components/prompt-editor-with-inputs/inputs-picker.tsx +100 -0
  28. package/src/components/prompt-editor-with-variables/config.json +4 -3
@@ -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
+ `;
@@ -2,7 +2,7 @@
2
2
  "name": "prompt-editor",
3
3
  "depMaterials": [],
4
4
  "depPackages": [
5
- "@coze-editor/editor@0.1.0-alpha.8d7a30",
5
+ "@coze-editor/editor@0.1.0-alpha.879fbb",
6
6
  "@codemirror/view",
7
7
  "styled-components"
8
8
  ]
@@ -5,7 +5,7 @@
5
5
 
6
6
  import React from 'react';
7
7
 
8
- import { Renderer, EditorProvider } from '@coze-editor/editor/react';
8
+ import { Renderer, EditorProvider, ActiveLinePlaceholder } from '@coze-editor/editor/react';
9
9
  import preset from '@coze-editor/editor/preset-prompt';
10
10
 
11
11
  import { PropsType } from './types';
@@ -17,7 +17,16 @@ import JinjaHighlight from './extensions/jinja';
17
17
  export type PromptEditorPropsType = PropsType;
18
18
 
19
19
  export function PromptEditor(props: PropsType) {
20
- const { value, onChange, readonly, style, hasError, children } = props || {};
20
+ const {
21
+ value,
22
+ onChange,
23
+ readonly,
24
+ placeholder,
25
+ activeLinePlaceholder,
26
+ style,
27
+ hasError,
28
+ children,
29
+ } = props || {};
21
30
 
22
31
  return (
23
32
  <UIContainer $hasError={hasError} style={style}>
@@ -28,11 +37,15 @@ export function PromptEditor(props: PropsType) {
28
37
  options={{
29
38
  readOnly: readonly,
30
39
  editable: !readonly,
40
+ placeholder,
31
41
  }}
32
42
  onChange={(e) => {
33
43
  onChange({ type: 'template', content: e.value });
34
44
  }}
35
45
  />
46
+ {activeLinePlaceholder && (
47
+ <ActiveLinePlaceholder>{activeLinePlaceholder}</ActiveLinePlaceholder>
48
+ )}
36
49
  <MarkdownHighlight />
37
50
  <LanguageSupport />
38
51
  <JinjaHighlight />
@@ -12,5 +12,7 @@ export type PropsType = React.PropsWithChildren<{
12
12
  onChange: (value?: IFlowTemplateValue) => void;
13
13
  readonly?: boolean;
14
14
  hasError?: boolean;
15
+ placeholder?: string;
16
+ activeLinePlaceholder?: string;
15
17
  style?: React.CSSProperties;
16
18
  }>;
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "prompt-editor-with-inputs",
3
+ "depMaterials": [
4
+ "prompt-editor",
5
+ "flow-value"
6
+ ],
7
+ "depPackages": [
8
+ "@coze-editor/editor@0.1.0-alpha.879fbb",
9
+ "@codemirror/view",
10
+ "styled-components",
11
+ "@douyinfe/semi-ui"
12
+ ]
13
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React, { useEffect, useState } from 'react';
7
+
8
+ import { Popover } from '@douyinfe/semi-ui';
9
+ import {
10
+ Mention,
11
+ MentionOpenChangeEvent,
12
+ getCurrentMentionReplaceRange,
13
+ useEditor,
14
+ PositionMirror,
15
+ } from '@coze-editor/editor/react';
16
+ import { EditorAPI } from '@coze-editor/editor/preset-prompt';
17
+
18
+ import { InputsPicker } from '../inputs-picker';
19
+ import { IFlowValue } from '../../../typings';
20
+
21
+ export function InputsTree({ inputsValues }: { inputsValues: Record<string, IFlowValue> }) {
22
+ const [posKey, setPosKey] = useState('');
23
+ const [visible, setVisible] = useState(false);
24
+ const [position, setPosition] = useState(-1);
25
+ const editor = useEditor<EditorAPI>();
26
+
27
+ function insert(variablePath: string) {
28
+ const range = getCurrentMentionReplaceRange(editor.$view.state);
29
+
30
+ if (!range) {
31
+ return;
32
+ }
33
+
34
+ editor.replaceText({
35
+ ...range,
36
+ text: '{{' + variablePath + '}}',
37
+ });
38
+
39
+ setVisible(false);
40
+ }
41
+
42
+ function handleOpenChange(e: MentionOpenChangeEvent) {
43
+ setPosition(e.state.selection.main.head);
44
+ setVisible(e.value);
45
+ }
46
+
47
+ useEffect(() => {
48
+ if (!editor) {
49
+ return;
50
+ }
51
+ }, [editor, visible]);
52
+
53
+ return (
54
+ <>
55
+ <Mention triggerCharacters={['{', '{}', '@']} onOpenChange={handleOpenChange} />
56
+
57
+ <Popover
58
+ visible={visible}
59
+ trigger="custom"
60
+ position="topLeft"
61
+ rePosKey={posKey}
62
+ content={
63
+ <div style={{ width: 300 }}>
64
+ <InputsPicker
65
+ inputsValues={inputsValues}
66
+ onSelect={(v) => {
67
+ insert(v);
68
+ }}
69
+ />
70
+ </div>
71
+ }
72
+ >
73
+ {/* PositionMirror allows the Popover to appear at the specified cursor position */}
74
+ <PositionMirror
75
+ position={position}
76
+ // When Doc scroll, update position
77
+ onChange={() => setPosKey(String(Math.random()))}
78
+ />
79
+ </Popover>
80
+ </>
81
+ );
82
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React from 'react';
7
+
8
+ import { InputsTree } from './extensions/inputs-tree';
9
+ import { PromptEditor, PromptEditorPropsType } from '../prompt-editor';
10
+ import { IFlowValue } from '../../typings';
11
+
12
+ interface PropsType extends PromptEditorPropsType {
13
+ inputsValues: Record<string, IFlowValue>;
14
+ }
15
+
16
+ export function PromptEditorWithInputs({ inputsValues, ...restProps }: PropsType) {
17
+ return (
18
+ <PromptEditor {...restProps}>
19
+ <InputsTree inputsValues={inputsValues} />
20
+ </PromptEditor>
21
+ );
22
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React, { useMemo } from 'react';
7
+
8
+ import { last } from 'lodash';
9
+ import {
10
+ type ArrayType,
11
+ ASTMatch,
12
+ type BaseType,
13
+ type BaseVariableField,
14
+ useScopeAvailable,
15
+ } from '@flowgram.ai/editor';
16
+ import { TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree';
17
+ import { Tree } from '@douyinfe/semi-ui';
18
+
19
+ import { IFlowValue } from '../../typings';
20
+
21
+ type VariableField = BaseVariableField<{ icon?: string | JSX.Element; title?: string }>;
22
+
23
+ export function InputsPicker({
24
+ inputsValues,
25
+ onSelect,
26
+ }: {
27
+ inputsValues: Record<string, IFlowValue>;
28
+ onSelect: (v: string) => void;
29
+ }) {
30
+ const available = useScopeAvailable();
31
+
32
+ const getArrayDrilldown = (type: ArrayType, depth = 1): { type: BaseType; depth: number } => {
33
+ if (ASTMatch.isArray(type.items)) {
34
+ return getArrayDrilldown(type.items, depth + 1);
35
+ }
36
+
37
+ return { type: type.items, depth: depth };
38
+ };
39
+
40
+ const renderVariable = (variable: VariableField, keyPath: string[]): TreeNodeData => {
41
+ let type = variable?.type;
42
+
43
+ let children: TreeNodeData[] | undefined;
44
+
45
+ if (ASTMatch.isObject(type)) {
46
+ children = (type.properties || [])
47
+ .map((_property) => renderVariable(_property as VariableField, [...keyPath, _property.key]))
48
+ .filter(Boolean) as TreeNodeData[];
49
+ }
50
+
51
+ if (ASTMatch.isArray(type)) {
52
+ const drilldown = getArrayDrilldown(type);
53
+
54
+ if (ASTMatch.isObject(drilldown.type)) {
55
+ children = (drilldown.type.properties || [])
56
+ .map((_property) =>
57
+ renderVariable(_property as VariableField, [
58
+ ...keyPath,
59
+ ...new Array(drilldown.depth).fill('[0]'),
60
+ _property.key,
61
+ ])
62
+ )
63
+ .filter(Boolean) as TreeNodeData[];
64
+ }
65
+ }
66
+
67
+ const key = keyPath
68
+ .map((_key, idx) => (_key === '[0]' || idx === 0 ? _key : `.${_key}`))
69
+ .join('');
70
+
71
+ return {
72
+ key: key,
73
+ label: last(keyPath),
74
+ value: key,
75
+ children,
76
+ };
77
+ };
78
+
79
+ const treeData: TreeNodeData[] = useMemo(
80
+ () =>
81
+ Object.entries(inputsValues).map(([key, value]) => {
82
+ if (value.type === 'ref') {
83
+ const variable = available.getByKeyPath(value.content || []);
84
+
85
+ if (variable) {
86
+ return renderVariable(variable, [key]);
87
+ }
88
+ }
89
+
90
+ return {
91
+ key,
92
+ value: key,
93
+ label: key,
94
+ };
95
+ }),
96
+ []
97
+ );
98
+
99
+ return <Tree treeData={treeData} onSelect={(v) => onSelect(v)} />;
100
+ }
@@ -1,10 +1,11 @@
1
1
  {
2
- "name": "prompt-editor",
2
+ "name": "prompt-editor-with-variables",
3
3
  "depMaterials": [
4
- "variable-selector"
4
+ "variable-selector",
5
+ "prompt-editor"
5
6
  ],
6
7
  "depPackages": [
7
- "@coze-editor/editor@0.1.0-alpha.8d7a30",
8
+ "@coze-editor/editor@0.1.0-alpha.879fbb",
8
9
  "@codemirror/view",
9
10
  "styled-components",
10
11
  "@douyinfe/semi-ui"