testomatio-editor-blocks 0.4.0 → 0.4.6

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,28 +1,47 @@
1
- import { createReactBlockSpec } from "@blocknote/react";
1
+ import { createReactBlockSpec, useEditorChange } from "@blocknote/react";
2
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
 
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";
8
14
 
9
- const readExpectedCollapsedPreference = (): boolean => {
15
+ /* readExpectedCollapsedPreference removed currently unused */
16
+
17
+ const writeExpectedCollapsedPreference = (collapsed: boolean) => {
10
18
  if (typeof window === "undefined") {
11
- return false;
19
+ return;
12
20
  }
13
21
  try {
14
- return window.localStorage.getItem(EXPECTED_COLLAPSED_KEY) === "true";
22
+ window.localStorage.setItem(EXPECTED_COLLAPSED_KEY, collapsed ? "true" : "false");
15
23
  } catch {
16
- return false;
24
+ //
17
25
  }
18
26
  };
19
27
 
20
- const writeExpectedCollapsedPreference = (collapsed: boolean) => {
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) => {
21
40
  if (typeof window === "undefined") {
22
41
  return;
23
42
  }
24
43
  try {
25
- window.localStorage.setItem(EXPECTED_COLLAPSED_KEY, collapsed ? "true" : "false");
44
+ window.localStorage.setItem(VIEW_MODE_KEY, mode);
26
45
  } catch {
27
46
  //
28
47
  }
@@ -42,6 +61,9 @@ export const stepBlock = createReactBlockSpec(
42
61
  expectedResult: {
43
62
  default: "",
44
63
  },
64
+ listStyle: {
65
+ default: "bullet",
66
+ },
45
67
  },
46
68
  },
47
69
  {
@@ -50,17 +72,16 @@ export const stepBlock = createReactBlockSpec(
50
72
  const stepData = (block.props.stepData as string) || "";
51
73
  const expectedResult = (block.props.expectedResult as string) || "";
52
74
  const expectedHasContent = expectedResult.trim().length > 0;
53
- const storedExpectedCollapsed = useMemo(
54
- () => readExpectedCollapsedPreference(),
55
- [],
56
- );
75
+ /* storedExpectedCollapsed removed — currently unused */
57
76
  const dataHasContent = stepData.trim().length > 0;
58
77
  const [isExpectedVisible, setIsExpectedVisible] = useState(
59
- expectedHasContent ? true : !storedExpectedCollapsed,
78
+ expectedHasContent,
60
79
  );
61
80
  const [isDataVisible, setIsDataVisible] = useState(dataHasContent);
62
81
  const [shouldFocusDataField, setShouldFocusDataField] = useState(false);
82
+ const [documentVersion, setDocumentVersion] = useState(0);
63
83
  const uploadImage = useStepImageUpload();
84
+ const [viewMode, setViewMode] = useState<StepViewMode>(() => readStepViewMode());
64
85
 
65
86
  // Calculate step number based on position in document
66
87
  const stepNumber = useMemo(() => {
@@ -68,7 +89,55 @@ export const stepBlock = createReactBlockSpec(
68
89
  const stepBlocks = allBlocks.filter((b) => b.type === "testStep");
69
90
  const index = stepBlocks.findIndex((b) => b.id === block.id);
70
91
  return index >= 0 ? index + 1 : 1;
71
- }, [editor.document, block.id]);
92
+ }, [block.id, documentVersion, editor.document]);
93
+
94
+ useEditorChange(() => {
95
+ setDocumentVersion((version) => version + 1);
96
+ }, editor);
97
+
98
+ useEffect(() => {
99
+ if (typeof window === "undefined") {
100
+ return;
101
+ }
102
+ const handleStorage = (event: StorageEvent) => {
103
+ if (event.key === VIEW_MODE_KEY) {
104
+ setViewMode(readStepViewMode());
105
+ }
106
+ };
107
+ const handleLocal = () => {
108
+ setViewMode(readStepViewMode());
109
+ };
110
+ window.addEventListener("storage", handleStorage);
111
+ window.addEventListener("bn-step-view-mode", handleLocal as EventListener);
112
+ return () => {
113
+ window.removeEventListener("storage", handleStorage);
114
+ window.removeEventListener("bn-step-view-mode", handleLocal as EventListener);
115
+ };
116
+ }, []);
117
+
118
+ const combinedStepValue = useMemo(() => {
119
+ if (!stepData) {
120
+ return stepTitle;
121
+ }
122
+ return stepTitle ? `${stepTitle}\n${stepData}` : stepData;
123
+ }, [stepData, stepTitle]);
124
+
125
+ const handleCombinedStepChange = useCallback(
126
+ (next: string) => {
127
+ if (next === combinedStepValue) {
128
+ return;
129
+ }
130
+ const [nextTitle = "", ...rest] = next.split("\n");
131
+ const nextData = rest.join("\n");
132
+ editor.updateBlock(block.id, {
133
+ props: {
134
+ stepTitle: nextTitle,
135
+ stepData: nextData,
136
+ },
137
+ });
138
+ },
139
+ [block.id, combinedStepValue, editor],
140
+ );
72
141
 
73
142
  useEffect(() => {
74
143
  if (dataHasContent && !isDataVisible) {
@@ -149,9 +218,29 @@ export const stepBlock = createReactBlockSpec(
149
218
  }, [editor, block.id]);
150
219
 
151
220
  const handleFieldFocus = useCallback(() => {
152
- editor.setSelection(block.id, block.id);
221
+ const selection = editor.getSelection();
222
+ const blocks = selection?.blocks ?? [];
223
+ const firstId = blocks[0]?.id;
224
+ const lastId = blocks[blocks.length - 1]?.id;
225
+ if (firstId === block.id && lastId === block.id) {
226
+ return;
227
+ }
228
+ try {
229
+ editor.setSelection(block.id, block.id);
230
+ } catch {
231
+ //
232
+ }
153
233
  }, [editor, block.id]);
154
234
 
235
+ const handleToggleView = useCallback(() => {
236
+ const next = viewMode === "horizontal" ? "vertical" : "horizontal";
237
+ writeStepViewMode(next);
238
+ setViewMode(next);
239
+ if (typeof window !== "undefined") {
240
+ window.dispatchEvent(new Event("bn-step-view-mode"));
241
+ }
242
+ }, [viewMode]);
243
+
155
244
  const [dataFocusSignal] = useState(0);
156
245
  const [expectedFocusSignal, setExpectedFocusSignal] = useState(0);
157
246
 
@@ -175,123 +264,179 @@ export const stepBlock = createReactBlockSpec(
175
264
  const canToggleData = !dataHasContent;
176
265
  const canToggleExpected = !expectedHasContent;
177
266
 
178
- return (
179
- <div className="bn-teststep" data-block-id={block.id}>
180
- <StepField
181
- label={`Step ${stepNumber}`}
182
- value={stepTitle}
183
- onChange={handleStepTitleChange}
184
- autoFocus={stepTitle.length === 0}
185
- enableAutocomplete
186
- fieldName="title"
187
- suggestionFilter={(suggestion) => (suggestion as StepSuggestion).isSnippet !== true}
267
+ if (viewMode === "horizontal") {
268
+ return (
269
+ <StepHorizontalView
270
+ blockId={block.id}
271
+ stepNumber={stepNumber}
272
+ stepValue={combinedStepValue}
273
+ expectedResult={expectedResult}
274
+ onStepChange={handleCombinedStepChange}
275
+ onExpectedChange={handleExpectedChange}
276
+ onInsertNextStep={handleInsertNextStep}
188
277
  onFieldFocus={handleFieldFocus}
189
- enableImageUpload={false}
190
- showFormattingButtons
191
- onImageFile={async (file) => {
192
- if (!uploadImage) {
193
- return;
194
- }
195
-
196
- setIsDataVisible(true);
197
- setShouldFocusDataField(true);
198
- try {
199
- const result = await uploadImage(file);
200
- if (result?.url) {
201
- const nextValue = stepData.trim().length > 0 ? `${stepData}\n![](${result.url})` : `![](${result.url})`;
202
- editor.updateBlock(block.id, {
203
- props: {
204
- stepData: nextValue,
205
- },
206
- });
207
- }
208
- } catch (error) {
209
- console.error("Failed to upload image to Step Data", error);
210
- }
211
- }}
278
+ viewToggle={
279
+ <button
280
+ type="button"
281
+ className="bn-teststep__view-toggle bn-teststep__view-toggle--horizontal"
282
+ aria-label="Switch step view"
283
+ onClick={handleToggleView}
284
+ >
285
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
286
+ <mask id="mask-toggle" style={{maskType: "alpha"}} maskUnits="userSpaceOnUse" x="0" y="0" width="16" height="16">
287
+ <rect width="16" height="16" fill="#D9D9D9"/>
288
+ </mask>
289
+ <g mask="url(#mask-toggle)">
290
+ <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"/>
291
+ </g>
292
+ </svg>
293
+ </button>
294
+ }
212
295
  />
213
- {isDataVisible ? (
214
- <StepField
215
- label="Step Data"
216
- labelToggle={
217
- canToggleData
218
- ? {
219
- onClick: handleHideData,
220
- expanded: true,
221
- }
222
- : undefined
223
- }
224
- value={stepData}
225
- onChange={handleStepDataChange}
226
- autoFocus={shouldFocusDataField}
227
- focusSignal={dataFocusSignal}
228
- multiline
229
- enableImageUpload
230
- showFormattingButtons
231
- showImageButton
232
- onFieldFocus={handleFieldFocus}
233
- />
234
- ) : (
235
- <div className="bn-step-field bn-step-field--collapsed">
236
- <span
237
- className="bn-step-field__label bn-step-field__label--toggle"
238
- role="button"
239
- tabIndex={-1}
240
- onClick={handleShowData}
241
- onKeyDown={(event) => {
242
- if (event.key === "Enter" || event.key === " ") {
243
- event.preventDefault();
244
- handleShowData();
245
- }
246
- }}
247
- aria-expanded="false"
296
+ );
297
+ }
298
+
299
+ return (
300
+ <div className="bn-teststep" data-block-id={block.id}>
301
+ <div className="bn-teststep__timeline">
302
+ <span className="bn-teststep__number">{stepNumber}</span>
303
+ <div className="bn-teststep__line" />
304
+ </div>
305
+ <div className="bn-teststep__content">
306
+ <div className="bn-teststep__header">
307
+ <span className="bn-teststep__title">Step</span>
308
+ <button
309
+ type="button"
310
+ className="bn-teststep__view-toggle"
311
+ aria-label="Switch step view"
312
+ onClick={handleToggleView}
248
313
  >
249
- Step Data
250
- </span>
314
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
315
+ <mask id="mask-toggle" style={{maskType: "alpha"}} maskUnits="userSpaceOnUse" x="0" y="0" width="16" height="16">
316
+ <rect width="16" height="16" fill="#D9D9D9"/>
317
+ </mask>
318
+ <g mask="url(#mask-toggle)">
319
+ <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"/>
320
+ </g>
321
+ </svg>
322
+ </button>
251
323
  </div>
252
- )}
253
- {isExpectedVisible ? (
254
324
  <StepField
255
- label="Expected Result"
256
- labelToggle={
257
- canToggleExpected
258
- ? {
259
- onClick: handleHideExpected,
260
- expanded: true,
261
- }
262
- : undefined
263
- }
264
- value={expectedResult}
265
- onChange={handleExpectedChange}
266
- multiline
267
- focusSignal={expectedFocusSignal}
268
- enableImageUpload
269
- showFormattingButtons
270
- showImageButton
325
+ label="Step"
326
+ showLabel={false}
327
+ value={stepTitle}
328
+ placeholder={STEP_TITLE_PLACEHOLDER}
329
+ onChange={handleStepTitleChange}
330
+ autoFocus={stepTitle.length === 0}
331
+ enableAutocomplete
332
+ fieldName="title"
333
+ suggestionFilter={(suggestion) => (suggestion as StepSuggestion).isSnippet !== true}
271
334
  onFieldFocus={handleFieldFocus}
272
- />
273
- ) : (
274
- <div className="bn-step-field bn-step-field--collapsed">
275
- <span
276
- className="bn-step-field__label bn-step-field__label--toggle"
277
- role="button"
278
- tabIndex={-1}
279
- onClick={handleShowExpected}
280
- onKeyDown={(event) => {
281
- if (event.key === "Enter" || event.key === " ") {
282
- event.preventDefault();
283
- handleShowExpected();
335
+ enableImageUpload={false}
336
+ showFormattingButtons
337
+ onImageFile={async (file) => {
338
+ if (!uploadImage) {
339
+ return;
340
+ }
341
+
342
+ setIsDataVisible(true);
343
+ setShouldFocusDataField(true);
344
+ try {
345
+ const result = await uploadImage(file);
346
+ if (result?.url) {
347
+ const nextValue = stepData.trim().length > 0 ? `${stepData}\n![](${result.url})` : `![](${result.url})`;
348
+ editor.updateBlock(block.id, {
349
+ props: {
350
+ stepData: nextValue,
351
+ },
352
+ });
284
353
  }
285
- }}
286
- aria-expanded="false"
287
- >
288
- Expected Result
289
- </span>
354
+ } catch (error) {
355
+ console.error("Failed to upload image to Step Data", error);
356
+ }
357
+ }}
358
+ />
359
+ {isDataVisible ? (
360
+ <StepField
361
+ label="Step data"
362
+ placeholder={STEP_DATA_PLACEHOLDER}
363
+ labelAction={
364
+ canToggleData ? (
365
+ <button
366
+ type="button"
367
+ className="bn-step-field__dismiss"
368
+ onClick={handleHideData}
369
+ aria-label="Hide step data"
370
+ >
371
+ ×
372
+ </button>
373
+ ) : undefined
374
+ }
375
+ value={stepData}
376
+ onChange={handleStepDataChange}
377
+ autoFocus={shouldFocusDataField}
378
+ focusSignal={dataFocusSignal}
379
+ multiline
380
+ enableAutocomplete
381
+ enableImageUpload
382
+ showFormattingButtons
383
+ showImageButton
384
+ onFieldFocus={handleFieldFocus}
385
+ />
386
+ ) : null}
387
+ {isExpectedVisible ? (
388
+ <StepField
389
+ label="Expected result"
390
+ placeholder={EXPECTED_RESULT_PLACEHOLDER}
391
+ labelAction={
392
+ canToggleExpected ? (
393
+ <button
394
+ type="button"
395
+ className="bn-step-field__dismiss"
396
+ onClick={handleHideExpected}
397
+ tabIndex={-1}
398
+ aria-label="Hide expected result"
399
+ >
400
+ ×
401
+ </button>
402
+ ) : undefined
403
+ }
404
+ value={expectedResult}
405
+ onChange={handleExpectedChange}
406
+ multiline
407
+ focusSignal={expectedFocusSignal}
408
+ enableAutocomplete
409
+ enableImageUpload
410
+ showFormattingButtons
411
+ showImageButton
412
+ onFieldFocus={handleFieldFocus}
413
+ />
414
+ ) : null}
415
+ <div className="bn-step-actions">
416
+ <button type="button" className="bn-step-action-btn" onClick={handleInsertNextStep}>
417
+ <svg className="bn-step-action-btn__icon" width="16" height="16" viewBox="0 0 13.334 13.334" fill="none" aria-hidden="true">
418
+ <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"/>
419
+ </svg>
420
+ Add new step
421
+ </button>
422
+ {!isDataVisible && (
423
+ <button type="button" className="bn-step-action-btn" onClick={handleShowData}>
424
+ <svg className="bn-step-action-btn__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
425
+ <path fillRule="evenodd" clipRule="evenodd" d="M8.666 7.333H12.666V8.667H8.666V12.667H7.332V8.667H3.332V7.333H7.332V3.333H8.666V7.333Z" fill="currentColor"/>
426
+ </svg>
427
+ Step data
428
+ </button>
429
+ )}
430
+ {!isExpectedVisible && (
431
+ <button type="button" className="bn-step-action-btn" onClick={handleShowExpected}>
432
+ <svg className="bn-step-action-btn__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
433
+ <path fillRule="evenodd" clipRule="evenodd" d="M8.666 7.333H12.666V8.667H8.666V12.667H7.332V8.667H3.332V7.333H7.332V3.333H8.666V7.333Z" fill="currentColor"/>
434
+ </svg>
435
+ Expected result
436
+ </button>
437
+ )}
290
438
  </div>
291
- )}
292
- <button type="button" className="bn-step-add" onClick={handleInsertNextStep}>
293
- + Step
294
- </button>
439
+ </div>
295
440
  </div>
296
441
  );
297
442
  },