testomatio-editor-blocks 0.1.1 → 0.2.0

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 (41) hide show
  1. package/README.md +13 -11
  2. package/package/editor/blocks/markdown.d.ts +5 -0
  3. package/package/editor/blocks/markdown.js +160 -0
  4. package/package/editor/blocks/snippet.d.ts +38 -0
  5. package/package/editor/blocks/snippet.js +65 -0
  6. package/package/editor/blocks/step.d.ts +32 -0
  7. package/package/editor/blocks/step.js +97 -0
  8. package/package/editor/blocks/stepField.d.ts +26 -0
  9. package/package/editor/blocks/stepField.js +316 -0
  10. package/package/editor/customMarkdownConverter.js +111 -80
  11. package/package/editor/customSchema.d.ts +31 -45
  12. package/package/editor/customSchema.js +6 -616
  13. package/package/editor/snippetAutocomplete.d.ts +28 -0
  14. package/package/editor/snippetAutocomplete.js +94 -0
  15. package/package/editor/stepAutocomplete.d.ts +1 -1
  16. package/package/editor/stepAutocomplete.js +15 -2
  17. package/package/editor/stepImageUpload.d.ts +1 -1
  18. package/package/editor/stepImageUpload.js +4 -9
  19. package/package/index.d.ts +2 -2
  20. package/package/index.js +2 -2
  21. package/package/styles.css +57 -0
  22. package/package.json +1 -1
  23. package/src/App.tsx +161 -45
  24. package/src/editor/blocks/blocks.test.ts +22 -0
  25. package/src/editor/blocks/markdown.ts +199 -0
  26. package/src/editor/blocks/snippet.tsx +109 -0
  27. package/src/editor/blocks/step.tsx +175 -0
  28. package/src/editor/blocks/stepField.tsx +487 -0
  29. package/src/editor/customMarkdownConverter.test.ts +121 -36
  30. package/src/editor/customMarkdownConverter.ts +128 -85
  31. package/src/editor/customSchema.tsx +6 -935
  32. package/src/editor/snippetAutocomplete.test.ts +54 -0
  33. package/src/editor/snippetAutocomplete.ts +133 -0
  34. package/src/editor/stepAutocomplete.test.ts +20 -0
  35. package/src/editor/stepAutocomplete.tsx +15 -2
  36. package/src/editor/stepImageUpload.test.ts +25 -0
  37. package/src/editor/stepImageUpload.ts +11 -0
  38. package/src/editor/styles.css +57 -0
  39. package/src/index.ts +2 -2
  40. package/src/editor/customSchema.test.ts +0 -47
  41. package/src/editor/stepImageUpload.tsx +0 -19
@@ -0,0 +1,54 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { renderToStaticMarkup } from "react-dom/server";
3
+ import React from "react";
4
+ import {
5
+ setSnippetFetcher,
6
+ useSnippetAutocomplete,
7
+ parseSnippetsFromJsonApi,
8
+ } from "./snippetAutocomplete";
9
+
10
+ describe("snippet autocomplete", () => {
11
+ it("parses JSON:API snippet resources", () => {
12
+ const suggestions = parseSnippetsFromJsonApi({
13
+ data: [
14
+ {
15
+ id: "501",
16
+ type: "snippet",
17
+ attributes: {
18
+ title: "Login setup",
19
+ body: "Open /login",
20
+ description: "Ready state",
21
+ "usage-count": 4,
22
+ },
23
+ },
24
+ ],
25
+ });
26
+
27
+ expect(suggestions).toEqual([
28
+ {
29
+ id: "501",
30
+ title: "Login setup",
31
+ body: "Open /login",
32
+ description: "Ready state",
33
+ usageCount: 4,
34
+ isSnippet: true,
35
+ },
36
+ ]);
37
+ });
38
+
39
+ it("reads suggestions from the global snippet fetcher via useSnippetAutocomplete", () => {
40
+ setSnippetFetcher(() => [{ id: "1", title: "Reusable snippet" }]);
41
+
42
+ let seen: any[] = [];
43
+ const Probe = () => {
44
+ seen = useSnippetAutocomplete();
45
+ return null;
46
+ };
47
+
48
+ renderToStaticMarkup(React.createElement(Probe));
49
+
50
+ expect(seen).toEqual([{ id: "1", title: "Reusable snippet" }]);
51
+
52
+ setSnippetFetcher(null);
53
+ });
54
+ });
@@ -0,0 +1,133 @@
1
+ import { useEffect, useState } from "react";
2
+
3
+ export type SnippetSuggestion = {
4
+ id: string;
5
+ title: string;
6
+ body?: string | null;
7
+ description?: string | null;
8
+ usageCount?: number | null;
9
+ isSnippet?: boolean | null;
10
+ };
11
+
12
+ export type SnippetJsonApiAttributes = {
13
+ title?: string | null;
14
+ body?: string | null;
15
+ description?: string | null;
16
+ "usage-count"?: number | string | null;
17
+ };
18
+
19
+ export type SnippetJsonApiResource = {
20
+ id?: string | number | null;
21
+ type?: string | null;
22
+ attributes?: SnippetJsonApiAttributes | null;
23
+ };
24
+
25
+ export type SnippetJsonApiDocument = {
26
+ data?: SnippetJsonApiResource[] | null;
27
+ };
28
+
29
+ export type SnippetSuggestionsFetcher = () => Promise<SnippetInput> | SnippetInput;
30
+
31
+ type SnippetInput = SnippetSuggestion[] | SnippetJsonApiDocument | SnippetJsonApiResource[] | null | undefined;
32
+
33
+ let globalFetcher: SnippetSuggestionsFetcher | null = null;
34
+ let cachedSuggestions: SnippetSuggestion[] = [];
35
+
36
+ export function setSnippetFetcher(fetcher: SnippetSuggestionsFetcher | null) {
37
+ globalFetcher = fetcher;
38
+ cachedSuggestions = [];
39
+ }
40
+
41
+ export function useSnippetAutocomplete(): SnippetSuggestion[] {
42
+ const [suggestions, setSuggestions] = useState<SnippetSuggestion[]>(() => {
43
+ if (cachedSuggestions.length > 0) {
44
+ return cachedSuggestions;
45
+ }
46
+ if (globalFetcher) {
47
+ const result = globalFetcher();
48
+ if (!result || typeof (result as Promise<unknown>).then !== "function") {
49
+ const normalized = normalizeSnippetSuggestions(result as SnippetInput);
50
+ cachedSuggestions = normalized;
51
+ return normalized;
52
+ }
53
+ }
54
+ return [];
55
+ });
56
+
57
+ useEffect(() => {
58
+ if (suggestions.length > 0) {
59
+ return;
60
+ }
61
+ if (!globalFetcher) {
62
+ return;
63
+ }
64
+
65
+ let cancelled = false;
66
+ Promise.resolve(globalFetcher())
67
+ .then((result) => normalizeSnippetSuggestions(result))
68
+ .then((items) => {
69
+ if (cancelled) return;
70
+ cachedSuggestions = items;
71
+ setSuggestions(items);
72
+ })
73
+ .catch((error) => console.error("Failed to fetch snippet suggestions", error));
74
+
75
+ return () => {
76
+ cancelled = true;
77
+ };
78
+ }, [suggestions.length]);
79
+
80
+ return suggestions;
81
+ }
82
+
83
+ export function parseSnippetsFromJsonApi(
84
+ document: SnippetJsonApiDocument | SnippetJsonApiResource[] | null | undefined,
85
+ ): SnippetSuggestion[] {
86
+ const resources = Array.isArray(document) ? document : document?.data;
87
+ if (!Array.isArray(resources) || resources.length === 0) {
88
+ return [];
89
+ }
90
+
91
+ return resources
92
+ .map((resource) => normalizeJsonApiResource(resource))
93
+ .filter((value): value is SnippetSuggestion => Boolean(value));
94
+ }
95
+
96
+ function normalizeSnippetSuggestions(snippets?: SnippetInput): SnippetSuggestion[] {
97
+ if (!snippets) return [];
98
+
99
+ if (Array.isArray(snippets)) {
100
+ if (snippets.length === 0) return [];
101
+ if (isSnippetSuggestionArray(snippets)) return snippets;
102
+ return parseSnippetsFromJsonApi(snippets);
103
+ }
104
+
105
+ return parseSnippetsFromJsonApi(snippets);
106
+ }
107
+
108
+ function normalizeJsonApiResource(resource: SnippetJsonApiResource | null | undefined): SnippetSuggestion | null {
109
+ if (!resource) return null;
110
+ const attrs = resource.attributes;
111
+ const id = resource.id;
112
+ const title = attrs?.title ?? "";
113
+ if (!id || !title) return null;
114
+
115
+ return {
116
+ id: String(id),
117
+ title: String(title),
118
+ body: attrs?.body ?? null,
119
+ description: attrs?.description ?? null,
120
+ usageCount: coerceNumber(attrs?.["usage-count"]),
121
+ isSnippet: true,
122
+ };
123
+ }
124
+
125
+ function isSnippetSuggestionArray(value: SnippetInput): value is SnippetSuggestion[] {
126
+ return Array.isArray(value) && value.length > 0 && typeof (value[0] as SnippetSuggestion | any)?.title === "string";
127
+ }
128
+
129
+ function coerceNumber(value: string | number | null | undefined): number | null {
130
+ if (value === null || value === undefined) return null;
131
+ const num = Number(value);
132
+ return Number.isFinite(num) ? num : null;
133
+ }
@@ -1,5 +1,8 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import { parseStepsFromJsonApi } from "./stepAutocomplete";
3
+ import { renderToStaticMarkup } from "react-dom/server";
4
+ import React from "react";
5
+ import { setStepsFetcher, useStepAutocomplete } from "./stepAutocomplete";
3
6
 
4
7
  describe("parseStepsFromJsonApi", () => {
5
8
  it("converts JSON:API resources into step suggestions", () => {
@@ -80,4 +83,21 @@ describe("parseStepsFromJsonApi", () => {
80
83
  "Click submit",
81
84
  ]);
82
85
  });
86
+
87
+ it("reads suggestions from the global fetcher via useStepAutocomplete", () => {
88
+ setStepsFetcher(() => [{ id: "1", title: "Global step" }]);
89
+
90
+ let seen: any[] = [];
91
+ const Probe = () => {
92
+ seen = useStepAutocomplete();
93
+ return null;
94
+ };
95
+
96
+ renderToStaticMarkup(React.createElement(Probe));
97
+
98
+ expect(seen).toEqual([{ id: "1", title: "Global step" }]);
99
+
100
+ // reset for other tests
101
+ setStepsFetcher(null);
102
+ });
83
103
  });
@@ -40,13 +40,26 @@ type StepInput = StepSuggestion[] | StepJsonApiDocument | StepJsonApiResource[]
40
40
  let globalFetcher: StepSuggestionsFetcher | null = null;
41
41
  let cachedSuggestions: StepSuggestion[] = [];
42
42
 
43
- export function setGlobalStepSuggestionsFetcher(fetcher: StepSuggestionsFetcher | null) {
43
+ export function setStepsFetcher(fetcher: StepSuggestionsFetcher | null) {
44
44
  globalFetcher = fetcher;
45
45
  cachedSuggestions = [];
46
46
  }
47
47
 
48
48
  export function useStepAutocomplete(): StepSuggestion[] {
49
- const [suggestions, setSuggestions] = useState<StepSuggestion[]>(cachedSuggestions);
49
+ const [suggestions, setSuggestions] = useState<StepSuggestion[]>(() => {
50
+ if (cachedSuggestions.length > 0) {
51
+ return cachedSuggestions;
52
+ }
53
+ if (globalFetcher) {
54
+ const result = globalFetcher();
55
+ if (!result || typeof (result as Promise<unknown>).then !== "function") {
56
+ const normalized = normalizeStepSuggestions(result as StepInput);
57
+ cachedSuggestions = normalized;
58
+ return normalized;
59
+ }
60
+ }
61
+ return [];
62
+ });
50
63
 
51
64
  useEffect(() => {
52
65
  if (suggestions.length > 0) {
@@ -0,0 +1,25 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { renderToStaticMarkup } from "react-dom/server";
3
+ import React from "react";
4
+ import { setImageUploadHandler, useStepImageUpload, type StepImageUploadHandler } from "./stepImageUpload";
5
+
6
+ describe("image upload handler hook", () => {
7
+ it("returns the configured upload handler", async () => {
8
+ const handler: StepImageUploadHandler = async () => ({ url: "https://example.com/image.png" });
9
+ setImageUploadHandler(handler);
10
+
11
+ let seen: any;
12
+ const Probe = () => {
13
+ seen = useStepImageUpload();
14
+ return null;
15
+ };
16
+
17
+ renderToStaticMarkup(React.createElement(Probe));
18
+
19
+ expect(seen).toBe(handler);
20
+ const result = await (seen as StepImageUploadHandler)(new Blob(["demo"], { type: "image/png" }));
21
+ expect(result).toEqual({ url: "https://example.com/image.png" });
22
+
23
+ setImageUploadHandler(null);
24
+ });
25
+ });
@@ -0,0 +1,11 @@
1
+ export type StepImageUploadHandler = (image: Blob) => Promise<{ url: string }>;
2
+
3
+ let imageUploadHandler: StepImageUploadHandler | null = null;
4
+
5
+ export function setImageUploadHandler(handler: StepImageUploadHandler | null) {
6
+ imageUploadHandler = handler;
7
+ }
8
+
9
+ export function useStepImageUpload(): StepImageUploadHandler | null {
10
+ return imageUploadHandler;
11
+ }
@@ -112,6 +112,63 @@
112
112
  box-shadow: 0 0 0 1px rgba(37, 99, 235, 0.08);
113
113
  }
114
114
 
115
+ .bn-snippet {
116
+ border-left-color: rgba(16, 185, 129, 0.75);
117
+ background: rgba(16, 185, 129, 0.12);
118
+ }
119
+
120
+ .bn-snippet::after {
121
+ box-shadow: 0 0 0 1px rgba(16, 185, 129, 0.16);
122
+ }
123
+
124
+ .bn-snippet .bn-teststep__toggle {
125
+ border-color: rgba(16, 185, 129, 0.45);
126
+ background: rgba(16, 185, 129, 0.1);
127
+ color: #0f766e;
128
+ }
129
+
130
+ .bn-snippet .bn-teststep__toggle:hover {
131
+ background: rgba(16, 185, 129, 0.18);
132
+ border-color: rgba(16, 185, 129, 0.55);
133
+ box-shadow: 0 0 0 1px rgba(16, 185, 129, 0.2);
134
+ }
135
+
136
+ .bn-snippet .bn-step-field__label {
137
+ color: #0f766e;
138
+ }
139
+
140
+ .bn-snippet .bn-step-toolbar__button {
141
+ border-color: rgba(16, 185, 129, 0.35);
142
+ color: #0f766e;
143
+ }
144
+
145
+ .bn-snippet .bn-step-toolbar__button:hover {
146
+ background: rgba(16, 185, 129, 0.12);
147
+ border-color: rgba(16, 185, 129, 0.55);
148
+ }
149
+
150
+ .bn-snippet .bn-step-editor {
151
+ border-color: rgba(16, 185, 129, 0.25);
152
+ }
153
+
154
+ .bn-snippet .bn-step-editor:focus-visible {
155
+ border-color: rgba(16, 185, 129, 0.7);
156
+ box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.2);
157
+ }
158
+
159
+ .bn-snippet .bn-step-suggestions {
160
+ border-color: rgba(16, 185, 129, 0.25);
161
+ }
162
+
163
+ .bn-snippet .bn-step-suggestion:hover,
164
+ .bn-snippet .bn-step-suggestion--active {
165
+ background: rgba(16, 185, 129, 0.1);
166
+ }
167
+
168
+ .bn-snippet .bn-step-suggestion__meta {
169
+ color: rgba(15, 118, 110, 0.65);
170
+ }
171
+
115
172
  .bn-teststep__toggle {
116
173
  align-self: flex-start;
117
174
  padding: 0.35rem 0.6rem;
package/src/index.ts CHANGED
@@ -15,7 +15,7 @@ export {
15
15
  export {
16
16
  useStepAutocomplete,
17
17
  parseStepsFromJsonApi,
18
- setGlobalStepSuggestionsFetcher,
18
+ setStepsFetcher,
19
19
  type StepSuggestion,
20
20
  type StepJsonApiDocument,
21
21
  type StepJsonApiResource,
@@ -23,7 +23,7 @@ export {
23
23
 
24
24
  export {
25
25
  useStepImageUpload,
26
- setGlobalStepImageUploadHandler,
26
+ setImageUploadHandler,
27
27
  type StepImageUploadHandler,
28
28
  } from "./editor/stepImageUpload";
29
29
 
@@ -1,47 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { __markdownTestUtils } from "./customSchema";
3
-
4
- describe("customSchema markdown helpers", () => {
5
- it("renders markdown images as <img> tags and round-trips back to markdown", () => {
6
- const markdown = "![](/attachments/example.png)";
7
- const html = __markdownTestUtils.markdownToHtml(markdown);
8
-
9
- expect(html).toContain('<img src="/attachments/example.png" alt="" class="bn-inline-image" contenteditable="false" draggable="false" />');
10
-
11
- const roundTrip = __markdownTestUtils.htmlToMarkdown(html);
12
- expect(roundTrip).toBe(markdown);
13
- });
14
-
15
- it("handles images mixed with surrounding text", () => {
16
- const markdown = [
17
- "Success screenshot:",
18
- "![](/attachments/success.png)",
19
- "Please archive it.",
20
- ].join("\n");
21
-
22
- const html = __markdownTestUtils.markdownToHtml(markdown);
23
- expect(html).toContain('<img src="/attachments/success.png" alt="" class="bn-inline-image" contenteditable="false" draggable="false" />');
24
-
25
- const roundTrip = __markdownTestUtils.htmlToMarkdown(html);
26
- expect(roundTrip).toBe("Success screenshot:\n![](/attachments/success.png)\nPlease archive it.");
27
- });
28
-
29
- it("renders expected-result style markdown with an image", () => {
30
- const markdown = [
31
- "Login should look like this",
32
- "![](/login.png)",
33
- ].join("\n");
34
-
35
- const html = __markdownTestUtils.markdownToHtml(markdown);
36
- expect(html).toContain('<img src="/login.png" alt="" class="bn-inline-image" contenteditable="false" draggable="false" />');
37
- expect(__markdownTestUtils.htmlToMarkdown(html)).toBe(markdown);
38
- });
39
-
40
- it("keeps inline images when mixed inside a single line of markdown text", () => {
41
- const markdown = "* asdsadsad aaaaa asd ![](https://placehold.co/600x400?text=Uploaded+1763329962213)";
42
- const html = __markdownTestUtils.markdownToHtml(markdown);
43
-
44
- expect(html).toContain('<img src="https://placehold.co/600x400?text=Uploaded+1763329962213" alt="" class="bn-inline-image" contenteditable="false" draggable="false" />');
45
- expect(__markdownTestUtils.htmlToMarkdown(html)).toBe(markdown);
46
- });
47
- });
@@ -1,19 +0,0 @@
1
- import { useEffect, useState } from "react";
2
-
3
- export type StepImageUploadHandler = (image: Blob) => Promise<{ url: string }>;
4
-
5
- let globalUploadHandler: StepImageUploadHandler | null = null;
6
-
7
- export function setGlobalStepImageUploadHandler(handler: StepImageUploadHandler | null) {
8
- globalUploadHandler = handler;
9
- }
10
-
11
- export function useStepImageUpload(): StepImageUploadHandler | null {
12
- const [handler, setHandler] = useState<StepImageUploadHandler | null>(globalUploadHandler);
13
-
14
- useEffect(() => {
15
- setHandler(globalUploadHandler);
16
- }, []);
17
-
18
- return handler;
19
- }