testomatio-editor-blocks 0.3.0 → 0.4.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.
@@ -1,9 +1,52 @@
1
- import { createReactBlockSpec } from "@blocknote/react";
2
- import { useCallback, useEffect, useState } from "react";
1
+ import { createReactBlockSpec, useEditorChange } from "@blocknote/react";
2
+ import { useCallback, useEffect, useMemo, useState } from "react";
3
3
  import { StepField } from "./stepField";
4
+ import { StepHorizontalView } from "./stepHorizontalView";
4
5
  import { useStepImageUpload } from "../stepImageUpload";
5
6
  import type { StepSuggestion } from "../stepAutocomplete";
6
7
 
8
+ const EXPECTED_COLLAPSED_KEY = "bn-expected-collapsed";
9
+ const VIEW_MODE_KEY = "bn-step-view-mode";
10
+ const STEP_TITLE_PLACEHOLDER = "Enter step title...";
11
+ const STEP_DATA_PLACEHOLDER = "Enter step data...";
12
+ const EXPECTED_RESULT_PLACEHOLDER = "Enter expected result...";
13
+ type StepViewMode = "vertical" | "horizontal";
14
+
15
+ /* readExpectedCollapsedPreference removed — currently unused */
16
+
17
+ const writeExpectedCollapsedPreference = (collapsed: boolean) => {
18
+ if (typeof window === "undefined") {
19
+ return;
20
+ }
21
+ try {
22
+ window.localStorage.setItem(EXPECTED_COLLAPSED_KEY, collapsed ? "true" : "false");
23
+ } catch {
24
+ //
25
+ }
26
+ };
27
+
28
+ const readStepViewMode = (): StepViewMode => {
29
+ if (typeof window === "undefined") {
30
+ return "vertical";
31
+ }
32
+ try {
33
+ return window.localStorage.getItem(VIEW_MODE_KEY) === "horizontal" ? "horizontal" : "vertical";
34
+ } catch {
35
+ return "vertical";
36
+ }
37
+ };
38
+
39
+ const writeStepViewMode = (mode: StepViewMode) => {
40
+ if (typeof window === "undefined") {
41
+ return;
42
+ }
43
+ try {
44
+ window.localStorage.setItem(VIEW_MODE_KEY, mode);
45
+ } catch {
46
+ //
47
+ }
48
+ };
49
+
7
50
  export const stepBlock = createReactBlockSpec(
8
51
  {
9
52
  type: "testStep",
@@ -25,25 +68,79 @@ export const stepBlock = createReactBlockSpec(
25
68
  const stepTitle = (block.props.stepTitle as string) || "";
26
69
  const stepData = (block.props.stepData as string) || "";
27
70
  const expectedResult = (block.props.expectedResult as string) || "";
28
- const showExpectedField =
29
- stepTitle.trim().length > 0 || stepData.trim().length > 0 || expectedResult.trim().length > 0;
30
- const [isDataVisible, setIsDataVisible] = useState(() => stepData.trim().length > 0);
71
+ const expectedHasContent = expectedResult.trim().length > 0;
72
+ /* storedExpectedCollapsed removed currently unused */
73
+ const dataHasContent = stepData.trim().length > 0;
74
+ const [isExpectedVisible, setIsExpectedVisible] = useState(
75
+ expectedHasContent,
76
+ );
77
+ const [isDataVisible, setIsDataVisible] = useState(dataHasContent);
31
78
  const [shouldFocusDataField, setShouldFocusDataField] = useState(false);
79
+ const [documentVersion, setDocumentVersion] = useState(0);
32
80
  const uploadImage = useStepImageUpload();
81
+ const [viewMode, setViewMode] = useState<StepViewMode>(() => readStepViewMode());
82
+
83
+ // Calculate step number based on position in document
84
+ const stepNumber = useMemo(() => {
85
+ const allBlocks = editor.document;
86
+ const stepBlocks = allBlocks.filter((b) => b.type === "testStep");
87
+ const index = stepBlocks.findIndex((b) => b.id === block.id);
88
+ return index >= 0 ? index + 1 : 1;
89
+ }, [block.id, documentVersion, editor.document]);
90
+
91
+ useEditorChange(() => {
92
+ setDocumentVersion((version) => version + 1);
93
+ }, editor);
33
94
 
34
95
  useEffect(() => {
35
- if (stepData.trim().length > 0 && !isDataVisible) {
36
- setIsDataVisible(true);
96
+ if (typeof window === "undefined") {
97
+ return;
37
98
  }
38
- }, [isDataVisible, stepData]);
99
+ const handleStorage = (event: StorageEvent) => {
100
+ if (event.key === VIEW_MODE_KEY) {
101
+ setViewMode(readStepViewMode());
102
+ }
103
+ };
104
+ const handleLocal = () => {
105
+ setViewMode(readStepViewMode());
106
+ };
107
+ window.addEventListener("storage", handleStorage);
108
+ window.addEventListener("bn-step-view-mode", handleLocal as EventListener);
109
+ return () => {
110
+ window.removeEventListener("storage", handleStorage);
111
+ window.removeEventListener("bn-step-view-mode", handleLocal as EventListener);
112
+ };
113
+ }, []);
114
+
115
+ const combinedStepValue = useMemo(() => {
116
+ if (!stepData) {
117
+ return stepTitle;
118
+ }
119
+ return stepTitle ? `${stepTitle}\n${stepData}` : stepData;
120
+ }, [stepData, stepTitle]);
121
+
122
+ const handleCombinedStepChange = useCallback(
123
+ (next: string) => {
124
+ if (next === combinedStepValue) {
125
+ return;
126
+ }
127
+ const [nextTitle = "", ...rest] = next.split("\n");
128
+ const nextData = rest.join("\n");
129
+ editor.updateBlock(block.id, {
130
+ props: {
131
+ stepTitle: nextTitle,
132
+ stepData: nextData,
133
+ },
134
+ });
135
+ },
136
+ [block.id, combinedStepValue, editor],
137
+ );
39
138
 
40
139
  useEffect(() => {
41
- if (shouldFocusDataField && isDataVisible) {
42
- const timer = setTimeout(() => setShouldFocusDataField(false), 0);
43
- return () => clearTimeout(timer);
140
+ if (dataHasContent && !isDataVisible) {
141
+ setIsDataVisible(true);
44
142
  }
45
- return undefined;
46
- }, [isDataVisible, shouldFocusDataField]);
143
+ }, [dataHasContent, isDataVisible]);
47
144
 
48
145
  const handleStepTitleChange = useCallback(
49
146
  (next: string) => {
@@ -75,11 +172,15 @@ export const stepBlock = createReactBlockSpec(
75
172
  [editor, block.id, stepData],
76
173
  );
77
174
 
78
- const handleShowDataField = useCallback(() => {
175
+ const handleShowData = useCallback(() => {
79
176
  setIsDataVisible(true);
80
177
  setShouldFocusDataField(true);
81
178
  }, []);
82
179
 
180
+ const handleHideData = useCallback(() => {
181
+ setIsDataVisible(false);
182
+ }, []);
183
+
83
184
  const handleExpectedChange = useCallback(
84
185
  (next: string) => {
85
186
  if (next === expectedResult) {
@@ -114,88 +215,225 @@ export const stepBlock = createReactBlockSpec(
114
215
  }, [editor, block.id]);
115
216
 
116
217
  const handleFieldFocus = useCallback(() => {
117
- editor.setSelection(block.id, block.id);
218
+ const selection = editor.getSelection();
219
+ const blocks = selection?.blocks ?? [];
220
+ const firstId = blocks[0]?.id;
221
+ const lastId = blocks[blocks.length - 1]?.id;
222
+ if (firstId === block.id && lastId === block.id) {
223
+ return;
224
+ }
225
+ try {
226
+ editor.setSelection(block.id, block.id);
227
+ } catch {
228
+ //
229
+ }
118
230
  }, [editor, block.id]);
119
231
 
120
- return (
121
- <div className="bn-teststep" data-block-id={block.id}>
122
- <StepField
123
- label="Step Title"
124
- value={stepTitle}
125
- placeholder="Describe the action to perform"
126
- onChange={handleStepTitleChange}
127
- autoFocus={stepTitle.length === 0}
128
- enableAutocomplete
129
- fieldName="title"
130
- suggestionFilter={(suggestion) => (suggestion as StepSuggestion).isSnippet !== true}
232
+ const handleToggleView = useCallback(() => {
233
+ const next = viewMode === "horizontal" ? "vertical" : "horizontal";
234
+ writeStepViewMode(next);
235
+ setViewMode(next);
236
+ if (typeof window !== "undefined") {
237
+ window.dispatchEvent(new Event("bn-step-view-mode"));
238
+ }
239
+ }, [viewMode]);
240
+
241
+ const [dataFocusSignal] = useState(0);
242
+ const [expectedFocusSignal, setExpectedFocusSignal] = useState(0);
243
+
244
+ const handleShowExpected = useCallback(() => {
245
+ setIsExpectedVisible(true);
246
+ setExpectedFocusSignal((value) => value + 1);
247
+ writeExpectedCollapsedPreference(false);
248
+ }, []);
249
+
250
+ const handleHideExpected = useCallback(() => {
251
+ setIsExpectedVisible(false);
252
+ writeExpectedCollapsedPreference(true);
253
+ }, []);
254
+
255
+ useEffect(() => {
256
+ if (expectedHasContent && !isExpectedVisible) {
257
+ setIsExpectedVisible(true);
258
+ }
259
+ }, [expectedHasContent, isExpectedVisible]);
260
+
261
+ const canToggleData = !dataHasContent;
262
+ const canToggleExpected = !expectedHasContent;
263
+
264
+ if (viewMode === "horizontal") {
265
+ return (
266
+ <StepHorizontalView
267
+ blockId={block.id}
268
+ stepNumber={stepNumber}
269
+ stepValue={combinedStepValue}
270
+ expectedResult={expectedResult}
271
+ onStepChange={handleCombinedStepChange}
272
+ onExpectedChange={handleExpectedChange}
273
+ onInsertNextStep={handleInsertNextStep}
131
274
  onFieldFocus={handleFieldFocus}
132
- rightAction={
133
- !isDataVisible ? (
134
- <button
135
- type="button"
136
- className="bn-teststep__toggle"
137
- onClick={handleShowDataField}
138
- aria-expanded="false"
139
- tabIndex={-1}
140
- >
141
- + Step Data
142
- </button>
143
- ) : null
275
+ viewToggle={
276
+ <button
277
+ type="button"
278
+ className="bn-teststep__view-toggle bn-teststep__view-toggle--horizontal"
279
+ aria-label="Switch step view"
280
+ onClick={handleToggleView}
281
+ >
282
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
283
+ <mask id="mask-toggle" style={{maskType: "alpha"}} maskUnits="userSpaceOnUse" x="0" y="0" width="16" height="16">
284
+ <rect width="16" height="16" fill="#D9D9D9"/>
285
+ </mask>
286
+ <g mask="url(#mask-toggle)">
287
+ <path d="M12.6667 2C13.0333 2 13.3472 2.13056 13.6083 2.39167C13.8694 2.65278 14 2.96667 14 3.33333L14 12.6667C14 13.0333 13.8694 13.3472 13.6083 13.6083C13.3472 13.8694 13.0333 14 12.6667 14L10 14C9.63333 14 9.31944 13.8694 9.05833 13.6083C8.79722 13.3472 8.66667 13.0333 8.66667 12.6667L8.66667 3.33333C8.66667 2.96667 8.79722 2.65278 9.05833 2.39167C9.31945 2.13055 9.63333 2 10 2L12.6667 2ZM6 2C6.36667 2 6.68056 2.13055 6.94167 2.39167C7.20278 2.65278 7.33333 2.96667 7.33333 3.33333L7.33333 12.6667C7.33333 13.0333 7.20278 13.3472 6.94167 13.6083C6.68055 13.8694 6.36667 14 6 14L3.33333 14C2.96667 14 2.65278 13.8694 2.39167 13.6083C2.13056 13.3472 2 13.0333 2 12.6667L2 3.33333C2 2.96667 2.13056 2.65278 2.39167 2.39167C2.65278 2.13055 2.96667 2 3.33333 2L6 2ZM3.33333 12.6667L6 12.6667L6 3.33333L3.33333 3.33333L3.33333 12.6667Z" fill="currentColor"/>
288
+ </g>
289
+ </svg>
290
+ </button>
144
291
  }
145
- enableImageUpload={false}
146
- showFormattingButtons
147
- onImageFile={async (file) => {
148
- if (!uploadImage) {
149
- return;
150
- }
151
-
152
- setIsDataVisible(true);
153
- setShouldFocusDataField(true);
154
- try {
155
- const result = await uploadImage(file);
156
- if (result?.url) {
157
- const nextValue = stepData.trim().length > 0 ? `${stepData}\n![](${result.url})` : `![](${result.url})`;
158
- editor.updateBlock(block.id, {
159
- props: {
160
- stepData: nextValue,
161
- },
162
- });
163
- }
164
- } catch (error) {
165
- console.error("Failed to upload image to Step Data", error);
166
- }
167
- }}
168
292
  />
169
- {isDataVisible && (
293
+ );
294
+ }
295
+
296
+ return (
297
+ <div className="bn-teststep" data-block-id={block.id}>
298
+ <div className="bn-teststep__timeline">
299
+ <span className="bn-teststep__number">{stepNumber}</span>
300
+ <div className="bn-teststep__line" />
301
+ </div>
302
+ <div className="bn-teststep__content">
303
+ <div className="bn-teststep__header">
304
+ <span className="bn-teststep__title">Step</span>
305
+ <button
306
+ type="button"
307
+ className="bn-teststep__view-toggle"
308
+ aria-label="Switch step view"
309
+ onClick={handleToggleView}
310
+ >
311
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
312
+ <mask id="mask-toggle" style={{maskType: "alpha"}} maskUnits="userSpaceOnUse" x="0" y="0" width="16" height="16">
313
+ <rect width="16" height="16" fill="#D9D9D9"/>
314
+ </mask>
315
+ <g mask="url(#mask-toggle)">
316
+ <path d="M12.6667 2C13.0333 2 13.3472 2.13056 13.6083 2.39167C13.8694 2.65278 14 2.96667 14 3.33333L14 12.6667C14 13.0333 13.8694 13.3472 13.6083 13.6083C13.3472 13.8694 13.0333 14 12.6667 14L10 14C9.63333 14 9.31944 13.8694 9.05833 13.6083C8.79722 13.3472 8.66667 13.0333 8.66667 12.6667L8.66667 3.33333C8.66667 2.96667 8.79722 2.65278 9.05833 2.39167C9.31945 2.13055 9.63333 2 10 2L12.6667 2ZM6 2C6.36667 2 6.68056 2.13055 6.94167 2.39167C7.20278 2.65278 7.33333 2.96667 7.33333 3.33333L7.33333 12.6667C7.33333 13.0333 7.20278 13.3472 6.94167 13.6083C6.68055 13.8694 6.36667 14 6 14L3.33333 14C2.96667 14 2.65278 13.8694 2.39167 13.6083C2.13056 13.3472 2 13.0333 2 12.6667L2 3.33333C2 2.96667 2.13056 2.65278 2.39167 2.39167C2.65278 2.13055 2.96667 2 3.33333 2L6 2ZM3.33333 12.6667L6 12.6667L6 3.33333L3.33333 3.33333L3.33333 12.6667Z" fill="currentColor"/>
317
+ </g>
318
+ </svg>
319
+ </button>
320
+ </div>
170
321
  <StepField
171
- label="Step Data"
172
- value={stepData}
173
- placeholder="Provide additional data about the step"
174
- onChange={handleStepDataChange}
175
- autoFocus={shouldFocusDataField}
176
- multiline
177
- enableImageUpload
178
- showFormattingButtons
179
- showImageButton
322
+ label="Step"
323
+ showLabel={false}
324
+ value={stepTitle}
325
+ placeholder={STEP_TITLE_PLACEHOLDER}
326
+ onChange={handleStepTitleChange}
327
+ autoFocus={stepTitle.length === 0}
328
+ enableAutocomplete
329
+ fieldName="title"
330
+ suggestionFilter={(suggestion) => (suggestion as StepSuggestion).isSnippet !== true}
180
331
  onFieldFocus={handleFieldFocus}
181
- />
182
- )}
183
- {showExpectedField && (
184
- <StepField
185
- label="Expected Result"
186
- value={expectedResult}
187
- placeholder="What should happen?"
188
- onChange={handleExpectedChange}
189
- multiline
190
- enableImageUpload
332
+ enableImageUpload={false}
191
333
  showFormattingButtons
192
- showImageButton
193
- onFieldFocus={handleFieldFocus}
334
+ onImageFile={async (file) => {
335
+ if (!uploadImage) {
336
+ return;
337
+ }
338
+
339
+ setIsDataVisible(true);
340
+ setShouldFocusDataField(true);
341
+ try {
342
+ const result = await uploadImage(file);
343
+ if (result?.url) {
344
+ const nextValue = stepData.trim().length > 0 ? `${stepData}\n![](${result.url})` : `![](${result.url})`;
345
+ editor.updateBlock(block.id, {
346
+ props: {
347
+ stepData: nextValue,
348
+ },
349
+ });
350
+ }
351
+ } catch (error) {
352
+ console.error("Failed to upload image to Step Data", error);
353
+ }
354
+ }}
194
355
  />
195
- )}
196
- <button type="button" className="bn-step-add" onClick={handleInsertNextStep}>
197
- + Step
198
- </button>
356
+ {isDataVisible ? (
357
+ <StepField
358
+ label="Step data"
359
+ placeholder={STEP_DATA_PLACEHOLDER}
360
+ labelAction={
361
+ canToggleData ? (
362
+ <button
363
+ type="button"
364
+ className="bn-step-field__dismiss"
365
+ onClick={handleHideData}
366
+ aria-label="Hide step data"
367
+ >
368
+ ×
369
+ </button>
370
+ ) : undefined
371
+ }
372
+ value={stepData}
373
+ onChange={handleStepDataChange}
374
+ autoFocus={shouldFocusDataField}
375
+ focusSignal={dataFocusSignal}
376
+ multiline
377
+ enableAutocomplete
378
+ enableImageUpload
379
+ showFormattingButtons
380
+ showImageButton
381
+ onFieldFocus={handleFieldFocus}
382
+ />
383
+ ) : null}
384
+ {isExpectedVisible ? (
385
+ <StepField
386
+ label="Expected result"
387
+ placeholder={EXPECTED_RESULT_PLACEHOLDER}
388
+ labelAction={
389
+ canToggleExpected ? (
390
+ <button
391
+ type="button"
392
+ className="bn-step-field__dismiss"
393
+ onClick={handleHideExpected}
394
+ tabIndex={-1}
395
+ aria-label="Hide expected result"
396
+ >
397
+ ×
398
+ </button>
399
+ ) : undefined
400
+ }
401
+ value={expectedResult}
402
+ onChange={handleExpectedChange}
403
+ multiline
404
+ focusSignal={expectedFocusSignal}
405
+ enableAutocomplete
406
+ enableImageUpload
407
+ showFormattingButtons
408
+ showImageButton
409
+ onFieldFocus={handleFieldFocus}
410
+ />
411
+ ) : null}
412
+ <div className="bn-step-actions">
413
+ <button type="button" className="bn-step-action-btn" onClick={handleInsertNextStep}>
414
+ <svg className="bn-step-action-btn__icon" width="16" height="16" viewBox="0 0 13.334 13.334" fill="none" aria-hidden="true">
415
+ <path d="M6.667 0a6.667 6.667 0 1 1 0 13.334A6.667 6.667 0 0 1 6.667 0Zm0 1.334a5.333 5.333 0 1 0 0 10.666 5.333 5.333 0 0 0 0-10.666ZM7.334 3.334V6H10v1.334H7.334V10H6V7.334H3.334V6H6V3.334h1.334Z" fill="currentColor"/>
416
+ </svg>
417
+ Add new step
418
+ </button>
419
+ {!isDataVisible && (
420
+ <button type="button" className="bn-step-action-btn" onClick={handleShowData}>
421
+ <svg className="bn-step-action-btn__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
422
+ <path fillRule="evenodd" clipRule="evenodd" d="M8.666 7.333H12.666V8.667H8.666V12.667H7.332V8.667H3.332V7.333H7.332V3.333H8.666V7.333Z" fill="currentColor"/>
423
+ </svg>
424
+ Step data
425
+ </button>
426
+ )}
427
+ {!isExpectedVisible && (
428
+ <button type="button" className="bn-step-action-btn" onClick={handleShowExpected}>
429
+ <svg className="bn-step-action-btn__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
430
+ <path fillRule="evenodd" clipRule="evenodd" d="M8.666 7.333H12.666V8.667H8.666V12.667H7.332V8.667H3.332V7.333H7.332V3.333H8.666V7.333Z" fill="currentColor"/>
431
+ </svg>
432
+ Expected result
433
+ </button>
434
+ )}
435
+ </div>
436
+ </div>
199
437
  </div>
200
438
  );
201
439
  },