testomatio-editor-blocks 0.4.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,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
  }
@@ -50,17 +69,16 @@ export const stepBlock = createReactBlockSpec(
50
69
  const stepData = (block.props.stepData as string) || "";
51
70
  const expectedResult = (block.props.expectedResult as string) || "";
52
71
  const expectedHasContent = expectedResult.trim().length > 0;
53
- const storedExpectedCollapsed = useMemo(
54
- () => readExpectedCollapsedPreference(),
55
- [],
56
- );
72
+ /* storedExpectedCollapsed removed — currently unused */
57
73
  const dataHasContent = stepData.trim().length > 0;
58
74
  const [isExpectedVisible, setIsExpectedVisible] = useState(
59
- expectedHasContent ? true : !storedExpectedCollapsed,
75
+ expectedHasContent,
60
76
  );
61
77
  const [isDataVisible, setIsDataVisible] = useState(dataHasContent);
62
78
  const [shouldFocusDataField, setShouldFocusDataField] = useState(false);
79
+ const [documentVersion, setDocumentVersion] = useState(0);
63
80
  const uploadImage = useStepImageUpload();
81
+ const [viewMode, setViewMode] = useState<StepViewMode>(() => readStepViewMode());
64
82
 
65
83
  // Calculate step number based on position in document
66
84
  const stepNumber = useMemo(() => {
@@ -68,7 +86,55 @@ export const stepBlock = createReactBlockSpec(
68
86
  const stepBlocks = allBlocks.filter((b) => b.type === "testStep");
69
87
  const index = stepBlocks.findIndex((b) => b.id === block.id);
70
88
  return index >= 0 ? index + 1 : 1;
71
- }, [editor.document, block.id]);
89
+ }, [block.id, documentVersion, editor.document]);
90
+
91
+ useEditorChange(() => {
92
+ setDocumentVersion((version) => version + 1);
93
+ }, editor);
94
+
95
+ useEffect(() => {
96
+ if (typeof window === "undefined") {
97
+ return;
98
+ }
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
+ );
72
138
 
73
139
  useEffect(() => {
74
140
  if (dataHasContent && !isDataVisible) {
@@ -149,9 +215,29 @@ export const stepBlock = createReactBlockSpec(
149
215
  }, [editor, block.id]);
150
216
 
151
217
  const handleFieldFocus = useCallback(() => {
152
- 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
+ }
153
230
  }, [editor, block.id]);
154
231
 
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
+
155
241
  const [dataFocusSignal] = useState(0);
156
242
  const [expectedFocusSignal, setExpectedFocusSignal] = useState(0);
157
243
 
@@ -175,123 +261,179 @@ export const stepBlock = createReactBlockSpec(
175
261
  const canToggleData = !dataHasContent;
176
262
  const canToggleExpected = !expectedHasContent;
177
263
 
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}
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}
188
274
  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
- }}
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>
291
+ }
212
292
  />
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"
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}
248
310
  >
249
- Step Data
250
- </span>
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>
251
320
  </div>
252
- )}
253
- {isExpectedVisible ? (
254
321
  <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
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}
271
331
  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();
332
+ enableImageUpload={false}
333
+ showFormattingButtons
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
+ });
284
350
  }
285
- }}
286
- aria-expanded="false"
287
- >
288
- Expected Result
289
- </span>
351
+ } catch (error) {
352
+ console.error("Failed to upload image to Step Data", error);
353
+ }
354
+ }}
355
+ />
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
+ )}
290
435
  </div>
291
- )}
292
- <button type="button" className="bn-step-add" onClick={handleInsertNextStep}>
293
- + Step
294
- </button>
436
+ </div>
295
437
  </div>
296
438
  );
297
439
  },