@eccenca/gui-elements 24.0.1 → 24.1.0-rc.1
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/CHANGELOG.md +15 -6
- package/dist/cjs/cmem/react-flow/StickyNoteModal/StickyNoteModal.js +3 -3
- package/dist/cjs/cmem/react-flow/StickyNoteModal/StickyNoteModal.js.map +1 -1
- package/dist/cjs/components/AutoSuggestion/ExtendedCodeEditor.js +3 -3
- package/dist/cjs/components/AutoSuggestion/ExtendedCodeEditor.js.map +1 -1
- package/dist/cjs/components/ContentGroup/ContentGroup.js +95 -0
- package/dist/cjs/components/ContentGroup/ContentGroup.js.map +1 -0
- package/dist/cjs/components/index.js +1 -0
- package/dist/cjs/components/index.js.map +1 -1
- package/dist/cjs/extensions/codemirror/CodeMirror.js +58 -6
- package/dist/cjs/extensions/codemirror/CodeMirror.js.map +1 -1
- package/dist/cjs/extensions/codemirror/debouncedLinter.js +18 -0
- package/dist/cjs/extensions/codemirror/debouncedLinter.js.map +1 -0
- package/dist/cjs/extensions/codemirror/hooks/useCodemirrorModeExtension.hooks.js +9 -18
- package/dist/cjs/extensions/codemirror/hooks/useCodemirrorModeExtension.hooks.js.map +1 -1
- package/dist/cjs/extensions/codemirror/linters/jsLinter.js +36 -0
- package/dist/cjs/extensions/codemirror/linters/jsLinter.js.map +1 -0
- package/dist/cjs/extensions/codemirror/linters/turtleLinter.js +81 -0
- package/dist/cjs/extensions/codemirror/linters/turtleLinter.js.map +1 -0
- package/dist/cjs/extensions/codemirror/types.js +3 -0
- package/dist/cjs/extensions/codemirror/types.js.map +1 -0
- package/dist/esm/cmem/react-flow/StickyNoteModal/StickyNoteModal.js +4 -4
- package/dist/esm/cmem/react-flow/StickyNoteModal/StickyNoteModal.js.map +1 -1
- package/dist/esm/components/AutoSuggestion/ExtendedCodeEditor.js +14 -3
- package/dist/esm/components/AutoSuggestion/ExtendedCodeEditor.js.map +1 -1
- package/dist/esm/components/ContentGroup/ContentGroup.js +100 -0
- package/dist/esm/components/ContentGroup/ContentGroup.js.map +1 -0
- package/dist/esm/components/index.js +1 -0
- package/dist/esm/components/index.js.map +1 -1
- package/dist/esm/extensions/codemirror/CodeMirror.js +58 -7
- package/dist/esm/extensions/codemirror/CodeMirror.js.map +1 -1
- package/dist/esm/extensions/codemirror/debouncedLinter.js +15 -0
- package/dist/esm/extensions/codemirror/debouncedLinter.js.map +1 -0
- package/dist/esm/extensions/codemirror/hooks/useCodemirrorModeExtension.hooks.js +6 -15
- package/dist/esm/extensions/codemirror/hooks/useCodemirrorModeExtension.hooks.js.map +1 -1
- package/dist/esm/extensions/codemirror/linters/jsLinter.js +32 -0
- package/dist/esm/extensions/codemirror/linters/jsLinter.js.map +1 -0
- package/dist/esm/extensions/codemirror/linters/turtleLinter.js +77 -0
- package/dist/esm/extensions/codemirror/linters/turtleLinter.js.map +1 -0
- package/dist/esm/extensions/codemirror/types.js +2 -0
- package/dist/esm/extensions/codemirror/types.js.map +1 -0
- package/dist/types/cmem/react-flow/StickyNoteModal/StickyNoteModal.d.ts +5 -1
- package/dist/types/components/AutoSuggestion/ExtendedCodeEditor.d.ts +11 -6
- package/dist/types/components/ContentGroup/ContentGroup.d.ts +78 -0
- package/dist/types/components/index.d.ts +1 -0
- package/dist/types/extensions/codemirror/CodeMirror.d.ts +23 -5
- package/dist/types/extensions/codemirror/debouncedLinter.d.ts +4 -0
- package/dist/types/extensions/codemirror/hooks/useCodemirrorModeExtension.hooks.d.ts +3 -5
- package/dist/types/extensions/codemirror/linters/jsLinter.d.ts +5 -0
- package/dist/types/extensions/codemirror/linters/turtleLinter.d.ts +5 -0
- package/dist/types/extensions/codemirror/types.d.ts +5 -0
- package/package.json +7 -5
- package/src/cmem/react-flow/StickyNoteModal/StickyNoteModal.tsx +16 -2
- package/src/components/AutoSuggestion/ExtendedCodeEditor.tsx +29 -6
- package/src/components/ContentGroup/ContentGroup.stories.tsx +47 -0
- package/src/components/ContentGroup/ContentGroup.tsx +256 -0
- package/src/components/ContentGroup/_contentgroup.scss +56 -0
- package/src/components/TextField/stories/TextField.stories.tsx +2 -1
- package/src/components/index.scss +1 -0
- package/src/components/index.ts +1 -0
- package/src/extensions/codemirror/CodeMirror.stories.tsx +30 -0
- package/src/extensions/codemirror/CodeMirror.tsx +86 -9
- package/src/extensions/codemirror/_codemirror.scss +96 -0
- package/src/extensions/codemirror/debouncedLinter.ts +26 -0
- package/src/extensions/codemirror/hooks/useCodemirrorModeExtension.hooks.ts +6 -16
- package/src/extensions/codemirror/linters/jsLinter.ts +38 -0
- package/src/extensions/codemirror/linters/turtleLinter.ts +102 -0
- package/src/extensions/codemirror/types.ts +7 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import classNames from "classnames";
|
|
3
|
+
import Color from "color";
|
|
4
|
+
|
|
5
|
+
import { TestableComponent } from "../../components/interfaces";
|
|
6
|
+
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
|
|
7
|
+
import {
|
|
8
|
+
Divider,
|
|
9
|
+
Icon,
|
|
10
|
+
IconButton,
|
|
11
|
+
OverflowText,
|
|
12
|
+
Section,
|
|
13
|
+
SectionHeader,
|
|
14
|
+
Spacing,
|
|
15
|
+
StickyTarget,
|
|
16
|
+
StickyTargetProps,
|
|
17
|
+
Toolbar,
|
|
18
|
+
ToolbarSection,
|
|
19
|
+
Tooltip,
|
|
20
|
+
} from "../index";
|
|
21
|
+
|
|
22
|
+
export interface ContentGroupProps extends Omit<React.HTMLAttributes<HTMLElement>, "title">, TestableComponent {
|
|
23
|
+
/**
|
|
24
|
+
* Title of the content group.
|
|
25
|
+
*/
|
|
26
|
+
title?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Level of the content group.
|
|
29
|
+
*/
|
|
30
|
+
level?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Context information to display in the header.
|
|
33
|
+
*/
|
|
34
|
+
contextInfo?: React.ReactElement | React.ReactElement[];
|
|
35
|
+
/**
|
|
36
|
+
* Annotation to display in the content.
|
|
37
|
+
*/
|
|
38
|
+
annotation?: React.ReactElement | React.ReactElement[];
|
|
39
|
+
/**
|
|
40
|
+
* Action options to display in the header.
|
|
41
|
+
*/
|
|
42
|
+
actionOptions?: React.ReactElement | React.ReactElement[];
|
|
43
|
+
/**
|
|
44
|
+
* Flag to collapse the content group.
|
|
45
|
+
*/
|
|
46
|
+
isCollapsed?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Text to display when the callapse button is hovered.
|
|
49
|
+
* If not set then it uses "Show more" or "Show less".
|
|
50
|
+
*/
|
|
51
|
+
textToggleCollapse?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Event handler to toggle the collapse state.
|
|
54
|
+
*/
|
|
55
|
+
handlerToggleCollapse?: () => void;
|
|
56
|
+
/**
|
|
57
|
+
* Use a border on the left side to visually connect the whole content content group.
|
|
58
|
+
*/
|
|
59
|
+
borderMainConnection?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Use a border on the left side to visually emphase the content group.
|
|
62
|
+
* If it is set to an array of color codes then the border is multi colored.
|
|
63
|
+
*/
|
|
64
|
+
borderSubConnection?: boolean | string[];
|
|
65
|
+
/**
|
|
66
|
+
* Whitespace size between header and the content.
|
|
67
|
+
*/
|
|
68
|
+
whitespaceSize?: "tiny" | "small" | "medium" | "large" | "xlarge";
|
|
69
|
+
/**
|
|
70
|
+
* Title minimum headline level.
|
|
71
|
+
*/
|
|
72
|
+
minimumHeadlineLevel?: 1 | 2 | 3 | 4 | 5 | 6;
|
|
73
|
+
/**
|
|
74
|
+
* Props to pass to `StickyTarget`.
|
|
75
|
+
*/
|
|
76
|
+
stickyHeaderProps?: Omit<StickyTargetProps, "children">;
|
|
77
|
+
/**
|
|
78
|
+
* Description of the content group.
|
|
79
|
+
* Added as tooltip to an info icon placed in the content group header.
|
|
80
|
+
*/
|
|
81
|
+
description?: string;
|
|
82
|
+
/**
|
|
83
|
+
* Flag to hide the group divider.
|
|
84
|
+
*/
|
|
85
|
+
hideGroupDivider?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Additional props to pass to the content container.
|
|
88
|
+
*/
|
|
89
|
+
contentProps?: Omit<React.HTMLAttributes<HTMLDivElement>, "children">;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Manage display of a grouped content section.
|
|
94
|
+
* Add info, actions and context annotations by using its properties.
|
|
95
|
+
* Can be nested into each other.
|
|
96
|
+
*/
|
|
97
|
+
export const ContentGroup = ({
|
|
98
|
+
children,
|
|
99
|
+
className = "",
|
|
100
|
+
title,
|
|
101
|
+
contextInfo,
|
|
102
|
+
annotation,
|
|
103
|
+
actionOptions,
|
|
104
|
+
isCollapsed = false,
|
|
105
|
+
textToggleCollapse,
|
|
106
|
+
handlerToggleCollapse,
|
|
107
|
+
borderMainConnection = false,
|
|
108
|
+
borderSubConnection = false,
|
|
109
|
+
level = 1,
|
|
110
|
+
minimumHeadlineLevel = 3,
|
|
111
|
+
whitespaceSize = "small",
|
|
112
|
+
style,
|
|
113
|
+
stickyHeaderProps,
|
|
114
|
+
description,
|
|
115
|
+
hideGroupDivider,
|
|
116
|
+
contentProps,
|
|
117
|
+
...otherContentWrapperProps
|
|
118
|
+
}: ContentGroupProps) => {
|
|
119
|
+
const displayHeader = title || handlerToggleCollapse;
|
|
120
|
+
|
|
121
|
+
let borderGradient: string[] | undefined = undefined;
|
|
122
|
+
if (typeof borderSubConnection === "object") {
|
|
123
|
+
const borderColors: string[] = Array.isArray(borderSubConnection) ? borderSubConnection : [borderSubConnection];
|
|
124
|
+
borderGradient = borderColors.reduce((acc: string[], borderColor: string, index: number): string[] => {
|
|
125
|
+
try {
|
|
126
|
+
const color = Color(borderColor);
|
|
127
|
+
|
|
128
|
+
acc.push(
|
|
129
|
+
`${color.rgb().toString()} ` +
|
|
130
|
+
`${(index / borderColors.length) * 100}% ` +
|
|
131
|
+
`${((index + 1) / borderColors.length) * 100}%`
|
|
132
|
+
);
|
|
133
|
+
} catch {
|
|
134
|
+
// eslint-disable-next-line no-console
|
|
135
|
+
console.warn("Received invalid background color for tag: " + borderColor);
|
|
136
|
+
}
|
|
137
|
+
return acc;
|
|
138
|
+
}, []);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const contextInfoElements = Array.isArray(contextInfo) ? contextInfo : [contextInfo];
|
|
142
|
+
const { className: contentClassName, ...otherContentProps } = contentProps ?? {};
|
|
143
|
+
|
|
144
|
+
const headerContent = displayHeader ? (
|
|
145
|
+
<>
|
|
146
|
+
<SectionHeader className={`${eccgui}-contentgroup__header`}>
|
|
147
|
+
<Toolbar>
|
|
148
|
+
{handlerToggleCollapse && (
|
|
149
|
+
<ToolbarSection>
|
|
150
|
+
<IconButton
|
|
151
|
+
className={`${eccgui}-contentgroup__header__toggler`}
|
|
152
|
+
name={isCollapsed ? "toggler-showmore" : "toggler-showless"}
|
|
153
|
+
text={textToggleCollapse ?? (isCollapsed ? "Show more" : "Show less")}
|
|
154
|
+
onClick={handlerToggleCollapse}
|
|
155
|
+
/>
|
|
156
|
+
<Spacing vertical size="small" />
|
|
157
|
+
</ToolbarSection>
|
|
158
|
+
)}
|
|
159
|
+
{title && (
|
|
160
|
+
<ToolbarSection canShrink>
|
|
161
|
+
{React.createElement(
|
|
162
|
+
"h" +
|
|
163
|
+
Math.min(
|
|
164
|
+
Math.max(minimumHeadlineLevel, level + minimumHeadlineLevel),
|
|
165
|
+
6
|
|
166
|
+
).toString(),
|
|
167
|
+
{
|
|
168
|
+
children: <OverflowText>{title}</OverflowText>,
|
|
169
|
+
className: `${eccgui}-contentgroup__header__title`,
|
|
170
|
+
}
|
|
171
|
+
)}
|
|
172
|
+
{description && (
|
|
173
|
+
<>
|
|
174
|
+
<Spacing vertical size="tiny" />
|
|
175
|
+
<Tooltip content={description}>
|
|
176
|
+
<Icon name="item-info" small className="dmapp--text-info" />
|
|
177
|
+
</Tooltip>
|
|
178
|
+
</>
|
|
179
|
+
)}
|
|
180
|
+
</ToolbarSection>
|
|
181
|
+
)}
|
|
182
|
+
{contextInfoElements &&
|
|
183
|
+
contextInfoElements[0]?.props &&
|
|
184
|
+
Object.values(contextInfoElements[0].props).every((v) => v !== undefined) && (
|
|
185
|
+
<ToolbarSection className={`${eccgui}-contentgroup__header__context`} canGrow>
|
|
186
|
+
<div className={`${eccgui}-contentgroup__content `}>
|
|
187
|
+
<Spacing vertical size="tiny" />
|
|
188
|
+
{contextInfoElements}
|
|
189
|
+
</div>
|
|
190
|
+
</ToolbarSection>
|
|
191
|
+
)}
|
|
192
|
+
{!isCollapsed && handlerToggleCollapse && actionOptions && (
|
|
193
|
+
<ToolbarSection className={`${eccgui}-contentgroup__header__options`}>
|
|
194
|
+
<Spacing vertical size="small" />
|
|
195
|
+
{actionOptions}
|
|
196
|
+
</ToolbarSection>
|
|
197
|
+
)}
|
|
198
|
+
</Toolbar>
|
|
199
|
+
</SectionHeader>
|
|
200
|
+
{(!isCollapsed || !handlerToggleCollapse) && (
|
|
201
|
+
<>
|
|
202
|
+
{!hideGroupDivider && <Divider addSpacing="small" />}
|
|
203
|
+
<Spacing size={whitespaceSize} />
|
|
204
|
+
</>
|
|
205
|
+
)}
|
|
206
|
+
</>
|
|
207
|
+
) : (
|
|
208
|
+
<></>
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
<Section
|
|
213
|
+
className={
|
|
214
|
+
`${eccgui}-contentgroup` +
|
|
215
|
+
(className ? ` ${className}` : "") +
|
|
216
|
+
(whitespaceSize ? ` ${eccgui}-contentgroup--padding-${whitespaceSize}` : "") +
|
|
217
|
+
(borderMainConnection ? ` ${eccgui}-contentgroup--border-main` : "") +
|
|
218
|
+
(borderSubConnection ? ` ${eccgui}-contentgroup--border-sub` : "")
|
|
219
|
+
}
|
|
220
|
+
style={
|
|
221
|
+
borderGradient
|
|
222
|
+
? ({
|
|
223
|
+
...(style ?? {}),
|
|
224
|
+
[`--${eccgui}-color-contentgroup-border-sub`]: borderGradient.join(", "),
|
|
225
|
+
} as React.CSSProperties)
|
|
226
|
+
: style
|
|
227
|
+
}
|
|
228
|
+
{...otherContentWrapperProps}
|
|
229
|
+
>
|
|
230
|
+
{headerContent && stickyHeaderProps ? (
|
|
231
|
+
<StickyTarget {...stickyHeaderProps}>{headerContent}</StickyTarget>
|
|
232
|
+
) : (
|
|
233
|
+
headerContent
|
|
234
|
+
)}
|
|
235
|
+
{(!isCollapsed || !handlerToggleCollapse) && (
|
|
236
|
+
<>
|
|
237
|
+
<div className={`${eccgui}-contentgroup__content`}>
|
|
238
|
+
<div
|
|
239
|
+
className={classNames(`${eccgui}-contentgroup__content__body`, contentClassName)}
|
|
240
|
+
{...otherContentProps}
|
|
241
|
+
>
|
|
242
|
+
{children}
|
|
243
|
+
</div>
|
|
244
|
+
{contextInfo && !displayHeader && (
|
|
245
|
+
<div className={`${eccgui}-contentgroup__content__context`}>{contextInfoElements}</div>
|
|
246
|
+
)}
|
|
247
|
+
{annotation && <div>{annotation}</div>}
|
|
248
|
+
{actionOptions && !displayHeader && (
|
|
249
|
+
<div className={`${eccgui}-contentgroup__content__options`}>{actionOptions}</div>
|
|
250
|
+
)}
|
|
251
|
+
</div>
|
|
252
|
+
</>
|
|
253
|
+
)}
|
|
254
|
+
</Section>
|
|
255
|
+
);
|
|
256
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
$eccgui-color-scontentgroup-border-main: rgba($eccgui-color-workspace-text, $eccgui-opacity-muted) !default;
|
|
2
|
+
$eccgui-color-scontentgroup-border-sub: rgba($eccgui-color-workspace-text, $eccgui-opacity-disabled) !default;
|
|
3
|
+
|
|
4
|
+
.#{$eccgui}-contentgroup {
|
|
5
|
+
--#{$eccgui}-color-contentgroup-border-main: #{$eccgui-color-scontentgroup-border-main};
|
|
6
|
+
--#{$eccgui}-color-contentgroup-border-sub: #{$eccgui-color-scontentgroup-border-sub};
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.#{$eccgui}-contentgroup--border-main {
|
|
10
|
+
border-left: 0.25 * $eccgui-size-block-whitespace solid
|
|
11
|
+
var(--#{$eccgui}-color-contentgroup-border-main, #{$eccgui-color-scontentgroup-border-main});
|
|
12
|
+
|
|
13
|
+
&.#{$eccgui}-contentgroup--padding-small {
|
|
14
|
+
padding-left: 0.5 * $eccgui-size-block-whitespace;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.#{$eccgui}-contentgroup--border-sub {
|
|
19
|
+
position: relative;
|
|
20
|
+
border-right: 0.25 * $eccgui-size-block-whitespace solid transparent;
|
|
21
|
+
|
|
22
|
+
&::after {
|
|
23
|
+
position: absolute;
|
|
24
|
+
top: 0;
|
|
25
|
+
bottom: 0;
|
|
26
|
+
left: 100%;
|
|
27
|
+
width: 0.25 * $eccgui-size-block-whitespace;
|
|
28
|
+
content: " ";
|
|
29
|
+
background-color: var(--#{$eccgui}-color-contentgroup-border-sub, #{$eccgui-color-scontentgroup-border-sub});
|
|
30
|
+
background-image: linear-gradient(to bottom, var(--#{$eccgui}-color-contentgroup-border-sub));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
&.#{$eccgui}-contentgroup--padding-small {
|
|
34
|
+
padding-right: 0.5 * $eccgui-size-block-whitespace;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.#{$eccgui}-contentgroup--padding-small {
|
|
39
|
+
+ .#{$eccgui}-contentgroup {
|
|
40
|
+
margin-top: 0.5 * $eccgui-size-block-whitespace;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
> .#{$eccgui}-contentgroup__content {
|
|
44
|
+
column-gap: 0.5 * $eccgui-size-block-whitespace;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.#{$eccgui}-contentgroup__content {
|
|
49
|
+
display: flex;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.#{$eccgui}-contentgroup__content__body {
|
|
53
|
+
flex-grow: 1;
|
|
54
|
+
flex-shrink: 1;
|
|
55
|
+
width: 100%;
|
|
56
|
+
}
|
|
@@ -31,6 +31,7 @@ Default.args = {
|
|
|
31
31
|
fullWidth: false,
|
|
32
32
|
placeholder: "placeholder text",
|
|
33
33
|
readOnly: false,
|
|
34
|
+
disabled: false,
|
|
34
35
|
};
|
|
35
36
|
|
|
36
37
|
/** Text field with default value that contains a zero width/invisible character.
|
|
@@ -46,7 +47,7 @@ const invisibleCharacterWarningProps: TextFieldProps = {
|
|
|
46
47
|
const codePointsString = [...Array.from(codePoints)]
|
|
47
48
|
.map((n) => {
|
|
48
49
|
const info = characters.invisibleZeroWidthCharacters.codePointMap.get(n);
|
|
49
|
-
return info
|
|
50
|
+
return info?.fullLabel;
|
|
50
51
|
})
|
|
51
52
|
.join(", ");
|
|
52
53
|
alert("Invisible character detected in input string. Code points: " + codePointsString);
|
package/src/components/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { Meta, StoryFn } from "@storybook/react";
|
|
3
3
|
|
|
4
|
+
import { helpersArgTypes } from "../../../.storybook/helpers";
|
|
5
|
+
|
|
4
6
|
import { CodeEditor } from "./CodeMirror";
|
|
5
7
|
|
|
6
8
|
export default {
|
|
@@ -11,6 +13,9 @@ export default {
|
|
|
11
13
|
onChange: {
|
|
12
14
|
action: "value changed",
|
|
13
15
|
},
|
|
16
|
+
intent: {
|
|
17
|
+
...helpersArgTypes.exampleIntent,
|
|
18
|
+
},
|
|
14
19
|
},
|
|
15
20
|
} as Meta<typeof CodeEditor>;
|
|
16
21
|
|
|
@@ -22,3 +27,28 @@ BasicExample.args = {
|
|
|
22
27
|
mode: "markdown",
|
|
23
28
|
defaultValue: "**test me**",
|
|
24
29
|
};
|
|
30
|
+
|
|
31
|
+
export const LinterExample = TemplateFull.bind({});
|
|
32
|
+
LinterExample.args = {
|
|
33
|
+
name: "codeinput",
|
|
34
|
+
defaultValue: "**test me**",
|
|
35
|
+
mode: "javascript",
|
|
36
|
+
useLinting: true,
|
|
37
|
+
autoFocus: true,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const DisabledExample = TemplateFull.bind({});
|
|
41
|
+
DisabledExample.args = {
|
|
42
|
+
name: "codeinput",
|
|
43
|
+
defaultValue: "**test me**",
|
|
44
|
+
mode: "javascript",
|
|
45
|
+
disabled: true,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const IntentExample = TemplateFull.bind({});
|
|
49
|
+
IntentExample.args = {
|
|
50
|
+
name: "codeinput",
|
|
51
|
+
defaultValue: "**test me**",
|
|
52
|
+
mode: "javascript",
|
|
53
|
+
intent: "warning",
|
|
54
|
+
};
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import React, { useRef } from "react";
|
|
1
|
+
import React, { useMemo, useRef } from "react";
|
|
2
2
|
import { defaultKeymap, indentWithTab } from "@codemirror/commands";
|
|
3
3
|
import { foldKeymap } from "@codemirror/language";
|
|
4
|
+
import { lintGutter } from "@codemirror/lint";
|
|
4
5
|
import { EditorState, Extension } from "@codemirror/state";
|
|
5
6
|
import { DOMEventHandlers, EditorView, KeyBinding, keymap, Rect, ViewUpdate } from "@codemirror/view";
|
|
6
7
|
import { minimalSetup } from "codemirror";
|
|
7
8
|
|
|
9
|
+
import { IntentTypes } from "../../common/Intent";
|
|
8
10
|
import { markField } from "../../components/AutoSuggestion/extensions/markText";
|
|
11
|
+
import { TestableComponent } from "../../components/interfaces";
|
|
9
12
|
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
|
|
10
13
|
|
|
11
14
|
//hooks
|
|
@@ -14,6 +17,8 @@ import {
|
|
|
14
17
|
supportedCodeEditorModes,
|
|
15
18
|
useCodeMirrorModeExtension,
|
|
16
19
|
} from "./hooks/useCodemirrorModeExtension.hooks";
|
|
20
|
+
import { jsLinter } from "./linters/jsLinter";
|
|
21
|
+
import { turtleLinter } from "./linters/turtleLinter";
|
|
17
22
|
//adaptations
|
|
18
23
|
import {
|
|
19
24
|
adaptedCodeFolding,
|
|
@@ -25,10 +30,11 @@ import {
|
|
|
25
30
|
adaptedLineNumbers,
|
|
26
31
|
adaptedPlaceholder,
|
|
27
32
|
} from "./tests/codemirrorTestHelper";
|
|
33
|
+
import { ExtensionCreator } from "./types";
|
|
28
34
|
|
|
29
|
-
export interface CodeEditorProps {
|
|
35
|
+
export interface CodeEditorProps extends TestableComponent {
|
|
30
36
|
// Is called with the editor instance that allows access via the CodeMirror API
|
|
31
|
-
setEditorView?: (editor: EditorView | undefined) =>
|
|
37
|
+
setEditorView?: (editor: EditorView | undefined) => void;
|
|
32
38
|
/**
|
|
33
39
|
* `name` attribute of connected textarea element.
|
|
34
40
|
*/
|
|
@@ -50,7 +56,7 @@ export interface CodeEditorProps {
|
|
|
50
56
|
/**
|
|
51
57
|
* Called when the focus status changes
|
|
52
58
|
*/
|
|
53
|
-
onFocusChange?: (focused: boolean) =>
|
|
59
|
+
onFocusChange?: (focused: boolean) => void;
|
|
54
60
|
/**
|
|
55
61
|
* Called when the user presses a key
|
|
56
62
|
*/
|
|
@@ -62,7 +68,7 @@ export interface CodeEditorProps {
|
|
|
62
68
|
/**
|
|
63
69
|
* Called when the user selects text
|
|
64
70
|
*/
|
|
65
|
-
onSelection?: (ranges: { from: number; to: number }[]) =>
|
|
71
|
+
onSelection?: (ranges: { from: number; to: number }[]) => void;
|
|
66
72
|
/**
|
|
67
73
|
* Called when the cursor position changes
|
|
68
74
|
*/
|
|
@@ -133,6 +139,23 @@ export interface CodeEditorProps {
|
|
|
133
139
|
* If the <Tab> key is enabled as normal input, i.e. it won't have the behavior of changing to the next input element, expected in a web app.
|
|
134
140
|
*/
|
|
135
141
|
enableTab?: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Enables linting feature in the editor ("turtle" and "javascript" modes can use linting currently).
|
|
144
|
+
*/
|
|
145
|
+
useLinting?: boolean;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Autofocus the editor when it is rendered
|
|
149
|
+
*/
|
|
150
|
+
autoFocus?: boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Intent state of the code editor.
|
|
153
|
+
*/
|
|
154
|
+
intent?: IntentTypes | "edited" | "removed";
|
|
155
|
+
/**
|
|
156
|
+
* Disables the editor.
|
|
157
|
+
*/
|
|
158
|
+
disabled?: boolean;
|
|
136
159
|
}
|
|
137
160
|
|
|
138
161
|
const addExtensionsFor = (flag: boolean, ...extensions: Extension[]) => (flag ? [...extensions] : []);
|
|
@@ -140,6 +163,11 @@ const addToKeyMapConfigFor = (flag: boolean, ...keys: any) => (flag ? [...keys]
|
|
|
140
163
|
const addHandlersFor = (flag: boolean, handlerName: string, handler: any) =>
|
|
141
164
|
flag ? ({ [handlerName]: handler } as DOMEventHandlers<any>) : {};
|
|
142
165
|
|
|
166
|
+
const ModeLinterMap: ReadonlyMap<SupportedCodeEditorModes, ReadonlyArray<ExtensionCreator>> = new Map([
|
|
167
|
+
["turtle", [turtleLinter]],
|
|
168
|
+
["javascript", [jsLinter]],
|
|
169
|
+
]);
|
|
170
|
+
|
|
143
171
|
/**
|
|
144
172
|
* Includes a code editor, currently we use CodeMirror library as base.
|
|
145
173
|
*/
|
|
@@ -170,9 +198,30 @@ export const CodeEditor = ({
|
|
|
170
198
|
tabForceSpaceForModes = ["python", "yaml"],
|
|
171
199
|
enableTab = false,
|
|
172
200
|
height,
|
|
201
|
+
useLinting = false,
|
|
202
|
+
"data-test-id": dataTestId,
|
|
203
|
+
autoFocus = false,
|
|
204
|
+
disabled = false,
|
|
205
|
+
intent,
|
|
206
|
+
...otherCodeEditorProps
|
|
173
207
|
}: CodeEditorProps) => {
|
|
174
208
|
const parent = useRef<any>(undefined);
|
|
175
209
|
|
|
210
|
+
const linters = useMemo(() => {
|
|
211
|
+
if (!mode) {
|
|
212
|
+
return [];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const values = [lintGutter()];
|
|
216
|
+
|
|
217
|
+
const linters = ModeLinterMap.get(mode);
|
|
218
|
+
if (linters) {
|
|
219
|
+
values.push(...linters.map((linter) => linter()));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return values;
|
|
223
|
+
}, [mode]);
|
|
224
|
+
|
|
176
225
|
const onKeyDownHandler = (event: KeyboardEvent, view: EditorView) => {
|
|
177
226
|
if (onKeyDown && !onKeyDown(event)) {
|
|
178
227
|
if (event.key === "Enter") {
|
|
@@ -219,13 +268,22 @@ export const CodeEditor = ({
|
|
|
219
268
|
keymap?.of(keyMapConfigs),
|
|
220
269
|
EditorState?.tabSize.of(tabIntentSize),
|
|
221
270
|
EditorState?.readOnly.of(readOnly),
|
|
271
|
+
EditorView?.editable.of(!disabled),
|
|
222
272
|
AdaptedEditorViewDomEventHandlers(domEventHandlers) as Extension,
|
|
223
273
|
EditorView?.updateListener.of((v: ViewUpdate) => {
|
|
224
|
-
|
|
274
|
+
if (disabled) return;
|
|
275
|
+
|
|
276
|
+
if (onChange) {
|
|
277
|
+
onChange(v.state.doc.toString());
|
|
278
|
+
}
|
|
225
279
|
|
|
226
280
|
if (onSelection)
|
|
227
281
|
onSelection(v.state.selection.ranges.filter((r) => !r.empty).map(({ from, to }) => ({ from, to })));
|
|
228
282
|
|
|
283
|
+
if (onFocusChange) {
|
|
284
|
+
v.view.dom.className += ` ${eccgui}-intent--${intent}`;
|
|
285
|
+
}
|
|
286
|
+
|
|
229
287
|
if (onCursorChange) {
|
|
230
288
|
const cursorPosition = v.state.selection.main.head ?? 0;
|
|
231
289
|
const editorRect = v.view.dom.getBoundingClientRect();
|
|
@@ -250,6 +308,7 @@ export const CodeEditor = ({
|
|
|
250
308
|
addExtensionsFor(shouldHighlightActiveLine, adaptedHighlightActiveLine()),
|
|
251
309
|
addExtensionsFor(wrapLines, EditorView?.lineWrapping),
|
|
252
310
|
addExtensionsFor(supportCodeFolding, adaptedFoldGutter(), adaptedCodeFolding()),
|
|
311
|
+
addExtensionsFor(useLinting, ...linters),
|
|
253
312
|
additionalExtensions,
|
|
254
313
|
];
|
|
255
314
|
|
|
@@ -265,11 +324,27 @@ export const CodeEditor = ({
|
|
|
265
324
|
view.dom.style.height = typeof height === "string" ? height : `${height}px`;
|
|
266
325
|
}
|
|
267
326
|
|
|
268
|
-
|
|
327
|
+
if (disabled) {
|
|
328
|
+
view.dom.className += ` ${eccgui}-disabled`;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (intent) {
|
|
332
|
+
view.dom.className += ` ${eccgui}-intent--${intent}`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (autoFocus) {
|
|
336
|
+
view.focus();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (setEditorView) {
|
|
340
|
+
setEditorView(view);
|
|
341
|
+
}
|
|
269
342
|
|
|
270
343
|
return () => {
|
|
271
344
|
view.destroy();
|
|
272
|
-
|
|
345
|
+
if (setEditorView) {
|
|
346
|
+
setEditorView(undefined);
|
|
347
|
+
}
|
|
273
348
|
};
|
|
274
349
|
}, [parent.current, mode, preventLineNumbers]);
|
|
275
350
|
|
|
@@ -279,11 +354,13 @@ export const CodeEditor = ({
|
|
|
279
354
|
// overwrite/extend some attributes
|
|
280
355
|
id={id ? id : name ? `codemirror-${name}` : undefined}
|
|
281
356
|
ref={parent}
|
|
282
|
-
|
|
357
|
+
// @deprecated (v25) fallback with static test id will be removed
|
|
358
|
+
data-test-id={dataTestId ? dataTestId : "codemirror-wrapper"}
|
|
283
359
|
className={
|
|
284
360
|
`${eccgui}-codeeditor ${eccgui}-codeeditor--mode-${mode}` +
|
|
285
361
|
(outerDivAttributes?.className ? ` ${outerDivAttributes?.className}` : "")
|
|
286
362
|
}
|
|
363
|
+
{...otherCodeEditorProps}
|
|
287
364
|
/>
|
|
288
365
|
);
|
|
289
366
|
};
|