@vev/silke 1.1.9 → 1.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 (189) hide show
  1. package/.turbo/turbo-build.log +162 -77
  2. package/CHANGELOG.md +40 -0
  3. package/dist/index.js +47 -40
  4. package/dist/index.js.LEGAL.txt +1 -1
  5. package/dist/index.js.map +7 -0
  6. package/dist/styles.css +1596 -1013
  7. package/dist/types/components/index.d.ts +2 -0
  8. package/dist/types/components/silke-autocomplete-field/autocomplete-item.d.ts +1 -1
  9. package/dist/types/components/silke-autocomplete-field/silke-autocomplete-field-native.d.ts +5 -0
  10. package/dist/types/components/silke-box/silke-box.d.ts +2 -0
  11. package/dist/types/components/silke-button/silke-dropdown-button.d.ts +3 -1
  12. package/dist/types/components/silke-card/silke-card-grid.d.ts +1 -0
  13. package/dist/types/components/silke-card/silke-card.d.ts +1 -0
  14. package/dist/types/components/silke-command-text-field/index.d.ts +1 -1
  15. package/dist/types/components/silke-command-text-field/silke-command-text-field.d.ts +16 -0
  16. package/dist/types/components/silke-command-text-field/utils.d.ts +25 -0
  17. package/dist/types/components/silke-css-number-field/silke-css-number-field.spec.d.ts +1 -0
  18. package/dist/types/components/silke-expression-input/index.d.ts +3 -0
  19. package/dist/types/components/silke-expression-input/silke-expression-input-tag.d.ts +6 -0
  20. package/dist/types/components/silke-expression-input/silke-expression-input.d.ts +29 -0
  21. package/dist/types/components/silke-expression-input/utils.d.ts +18 -0
  22. package/dist/types/components/silke-form-schema/fields/array-field-inline.d.ts +2 -1
  23. package/dist/types/components/silke-form-schema/fields/array-field-modal.d.ts +2 -1
  24. package/dist/types/components/silke-form-schema/fields/array-field.d.ts +1 -1
  25. package/dist/types/components/silke-form-schema/fields/group-field.d.ts +1 -1
  26. package/dist/types/components/silke-form-schema/fields/layout-field.d.ts +1 -1
  27. package/dist/types/components/silke-form-schema/fields/object-field.d.ts +1 -1
  28. package/dist/types/components/silke-form-schema/fields/popover-field.d.ts +1 -1
  29. package/dist/types/components/silke-form-schema/fields/string-field.d.ts +1 -1
  30. package/dist/types/components/silke-form-schema/index.d.ts +1 -0
  31. package/dist/types/components/silke-form-schema/silke-form-schema.d.ts +4 -1
  32. package/dist/types/components/silke-form-schema/silke-schema-decorator.d.ts +17 -0
  33. package/dist/types/components/silke-form-schema/silke-schema-field.d.ts +2 -1
  34. package/dist/types/components/silke-form-schema/silke-schema-fields.d.ts +2 -0
  35. package/dist/types/components/silke-form-schema/silke-schema-path.spec.d.ts +1 -0
  36. package/dist/types/components/silke-gradient-picker/silke-gradient-utils.d.ts +2 -0
  37. package/dist/types/components/silke-icon-picker/color-swatch-section.d.ts +6 -0
  38. package/dist/types/components/silke-icon-picker/emoji-tab.d.ts +1 -1
  39. package/dist/types/components/silke-icon-picker/icons-tab.d.ts +1 -1
  40. package/dist/types/components/silke-icon-picker/silke-icon-picker.d.ts +2 -0
  41. package/dist/types/components/silke-icon-picker/utils.d.ts +2 -0
  42. package/dist/types/components/silke-list/silke-list-item.d.ts +5 -1
  43. package/dist/types/components/silke-list/types.d.ts +1 -0
  44. package/dist/types/components/silke-navigation-link/silke-navigation-link.d.ts +15 -0
  45. package/dist/types/components/silke-popover/popover-context.spec.d.ts +1 -0
  46. package/dist/types/components/silke-rich-text-field/blockquote.d.ts +21 -0
  47. package/dist/types/components/silke-rich-text-field/blockquote.spec.d.ts +1 -0
  48. package/dist/types/components/silke-rich-text-field/dropcap.d.ts +27 -0
  49. package/dist/types/components/silke-rich-text-field/dropcap.spec.d.ts +1 -0
  50. package/dist/types/components/silke-rich-text-field/floating-layer.d.ts +12 -0
  51. package/dist/types/components/silke-rich-text-field/font-size.d.ts +2 -0
  52. package/dist/types/components/silke-rich-text-field/formatting-toolbar.d.ts +9 -0
  53. package/dist/types/components/silke-rich-text-field/index.d.ts +1 -0
  54. package/dist/types/components/silke-rich-text-field/jsdom-rects.d.ts +10 -0
  55. package/dist/types/components/silke-rich-text-field/link-menu.d.ts +26 -0
  56. package/dist/types/components/silke-rich-text-field/link-menu.spec.d.ts +1 -0
  57. package/dist/types/components/silke-rich-text-field/link-suggestions.d.ts +48 -0
  58. package/dist/types/components/silke-rich-text-field/link-suggestions.spec.d.ts +1 -0
  59. package/dist/types/components/silke-rich-text-field/link-tooltip.d.ts +15 -0
  60. package/dist/types/components/silke-rich-text-field/list-item.d.ts +15 -0
  61. package/dist/types/components/silke-rich-text-field/list-link-fontsize.spec.d.ts +1 -0
  62. package/dist/types/components/silke-rich-text-field/lorem-ipsum.d.ts +10 -0
  63. package/dist/types/components/silke-rich-text-field/lorem-ipsum.spec.d.ts +1 -0
  64. package/dist/types/components/silke-rich-text-field/paste.d.ts +1 -0
  65. package/dist/types/components/silke-rich-text-field/paste.spec.d.ts +1 -0
  66. package/dist/types/components/silke-rich-text-field/plugins.d.ts +18 -0
  67. package/dist/types/components/silke-rich-text-field/plugins.spec.d.ts +1 -0
  68. package/dist/types/components/silke-rich-text-field/silke-rich-text-field.d.ts +60 -0
  69. package/dist/types/components/silke-rich-text-field/slash-menu-items.d.ts +10 -0
  70. package/dist/types/components/silke-rich-text-field/slash-menu.d.ts +11 -0
  71. package/dist/types/components/silke-rich-text-field/table-content-sync.spec.d.ts +1 -0
  72. package/dist/types/components/silke-rich-text-field/table-toolbar.d.ts +17 -0
  73. package/dist/types/components/silke-rich-text-field/table-toolbar.spec.d.ts +1 -0
  74. package/dist/types/components/silke-rich-text-field/table.spec.d.ts +1 -0
  75. package/dist/types/components/silke-rich-text-field/use-link-hover.d.ts +15 -0
  76. package/dist/types/components/silke-rich-text-field/use-slash-menu.d.ts +13 -0
  77. package/dist/types/components/silke-rich-text-field/utils.d.ts +13 -0
  78. package/dist/types/components/silke-sortable-list/index.d.ts +1 -1
  79. package/dist/types/components/silke-sortable-list/silke-nested-sortable-list.d.ts +8 -1
  80. package/dist/types/components/silke-theme-card/silke-theme-card.d.ts +13 -6
  81. package/dist/types/components/silke-theme-swatch/silke-theme-swatch.d.ts +6 -1
  82. package/dist/types/icons.d.ts +18 -0
  83. package/esbuild.config.mjs +9 -4
  84. package/package.json +22 -9
  85. package/src/components/index.ts +2 -0
  86. package/src/components/silke-alert-banner/silke-alert-banner.tsx +1 -1
  87. package/src/components/silke-autocomplete-field/autocomplete-item.tsx +47 -61
  88. package/src/components/silke-autocomplete-field/silke-autocomplete-field-native.tsx +13 -2
  89. package/src/components/silke-autocomplete-field/silke-autocomplete-field.tsx +8 -5
  90. package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +2 -1
  91. package/src/components/silke-box/silke-box.scss +4 -0
  92. package/src/components/silke-box/silke-box.tsx +4 -0
  93. package/src/components/silke-button/silke-dropdown-button.tsx +4 -1
  94. package/src/components/silke-card/silke-card.scss +4 -0
  95. package/src/components/silke-card/silke-card.tsx +3 -0
  96. package/src/components/silke-command-text-field/index.ts +1 -0
  97. package/src/components/silke-command-text-field/silke-command-parser.spec.ts +55 -0
  98. package/src/components/silke-command-text-field/silke-command-parser.ts +33 -2
  99. package/src/components/silke-command-text-field/silke-command-text-field.module.scss +3 -0
  100. package/src/components/silke-command-text-field/silke-command-text-field.tsx +53 -48
  101. package/src/components/silke-command-text-field/utils.spec.ts +192 -0
  102. package/src/components/silke-command-text-field/utils.ts +99 -16
  103. package/src/components/silke-css-number-field/silke-css-number-field.spec.tsx +115 -0
  104. package/src/components/silke-css-number-field/silke-css-number-field.tsx +33 -0
  105. package/src/components/silke-expression-input/index.ts +3 -0
  106. package/src/components/silke-expression-input/silke-expression-input-tag.tsx +37 -0
  107. package/src/components/silke-expression-input/silke-expression-input.mdx +40 -0
  108. package/src/components/silke-expression-input/silke-expression-input.scss +24 -0
  109. package/src/components/silke-expression-input/silke-expression-input.tsx +343 -0
  110. package/src/components/silke-expression-input/utils.ts +144 -0
  111. package/src/components/silke-form-schema/fields/array-field-inline.tsx +4 -0
  112. package/src/components/silke-form-schema/fields/array-field-modal.tsx +4 -0
  113. package/src/components/silke-form-schema/fields/array-field.tsx +6 -11
  114. package/src/components/silke-form-schema/fields/group-field.tsx +2 -0
  115. package/src/components/silke-form-schema/fields/layout-field.tsx +2 -0
  116. package/src/components/silke-form-schema/fields/number-field.tsx +114 -4
  117. package/src/components/silke-form-schema/fields/object-field.tsx +2 -0
  118. package/src/components/silke-form-schema/fields/popover-field.tsx +2 -0
  119. package/src/components/silke-form-schema/fields/string-field.tsx +27 -1
  120. package/src/components/silke-form-schema/index.ts +1 -0
  121. package/src/components/silke-form-schema/silke-form-schema.mdx +30 -1
  122. package/src/components/silke-form-schema/silke-form-schema.tsx +12 -6
  123. package/src/components/silke-form-schema/silke-schema-decorator.tsx +32 -0
  124. package/src/components/silke-form-schema/silke-schema-field.tsx +19 -2
  125. package/src/components/silke-form-schema/silke-schema-fields.tsx +30 -4
  126. package/src/components/silke-form-schema/silke-schema-path.spec.tsx +160 -0
  127. package/src/components/silke-gradient-picker/silke-gradient-picker.tsx +3 -1
  128. package/src/components/silke-gradient-picker/silke-gradient-utils.ts +23 -3
  129. package/src/components/silke-icon/silke-shape.tsx +1 -1
  130. package/src/components/silke-icon-picker/color-swatch-section.tsx +61 -0
  131. package/src/components/silke-icon-picker/emoji-tab.tsx +8 -6
  132. package/src/components/silke-icon-picker/icons-tab.tsx +8 -7
  133. package/src/components/silke-icon-picker/silke-icon-picker.tsx +52 -25
  134. package/src/components/silke-icon-picker/styles.scss +66 -1
  135. package/src/components/silke-icon-picker/utils.ts +2 -0
  136. package/src/components/silke-list/silke-list-item.scss +10 -0
  137. package/src/components/silke-list/silke-list-item.tsx +16 -3
  138. package/src/components/silke-list/silke-list.mdx +18 -0
  139. package/src/components/silke-list/types.ts +1 -0
  140. package/src/components/silke-navigation-link/silke-navigation-link.scss +162 -42
  141. package/src/components/silke-navigation-link/silke-navigation-link.tsx +99 -21
  142. package/src/components/silke-popover/popover-context.spec.tsx +44 -0
  143. package/src/components/silke-popover-native/silke-popover-native.scss +5 -3
  144. package/src/components/silke-rich-text-field/blockquote.spec.ts +151 -0
  145. package/src/components/silke-rich-text-field/blockquote.ts +90 -0
  146. package/src/components/silke-rich-text-field/dropcap.spec.ts +67 -0
  147. package/src/components/silke-rich-text-field/dropcap.ts +59 -0
  148. package/src/components/silke-rich-text-field/floating-layer.tsx +15 -0
  149. package/src/components/silke-rich-text-field/font-size.ts +62 -0
  150. package/src/components/silke-rich-text-field/formatting-toolbar.tsx +145 -0
  151. package/src/components/silke-rich-text-field/index.ts +1 -0
  152. package/src/components/silke-rich-text-field/jsdom-rects.ts +15 -0
  153. package/src/components/silke-rich-text-field/link-menu.spec.tsx +502 -0
  154. package/src/components/silke-rich-text-field/link-menu.tsx +553 -0
  155. package/src/components/silke-rich-text-field/link-suggestions.spec.ts +203 -0
  156. package/src/components/silke-rich-text-field/link-suggestions.ts +188 -0
  157. package/src/components/silke-rich-text-field/link-tooltip.tsx +60 -0
  158. package/src/components/silke-rich-text-field/list-item.ts +64 -0
  159. package/src/components/silke-rich-text-field/list-link-fontsize.spec.ts +135 -0
  160. package/src/components/silke-rich-text-field/lorem-ipsum.spec.ts +131 -0
  161. package/src/components/silke-rich-text-field/lorem-ipsum.ts +51 -0
  162. package/src/components/silke-rich-text-field/paste.spec.ts +93 -0
  163. package/src/components/silke-rich-text-field/paste.ts +39 -0
  164. package/src/components/silke-rich-text-field/plugins.spec.ts +143 -0
  165. package/src/components/silke-rich-text-field/plugins.ts +230 -0
  166. package/src/components/silke-rich-text-field/silke-rich-text-field.mdx +180 -0
  167. package/src/components/silke-rich-text-field/silke-rich-text-field.scss +343 -0
  168. package/src/components/silke-rich-text-field/silke-rich-text-field.tsx +505 -0
  169. package/src/components/silke-rich-text-field/slash-menu-items.ts +112 -0
  170. package/src/components/silke-rich-text-field/slash-menu.tsx +50 -0
  171. package/src/components/silke-rich-text-field/table-content-sync.spec.tsx +61 -0
  172. package/src/components/silke-rich-text-field/table-toolbar.spec.tsx +168 -0
  173. package/src/components/silke-rich-text-field/table-toolbar.tsx +121 -0
  174. package/src/components/silke-rich-text-field/table.spec.ts +87 -0
  175. package/src/components/silke-rich-text-field/use-link-hover.ts +63 -0
  176. package/src/components/silke-rich-text-field/use-slash-menu.ts +130 -0
  177. package/src/components/silke-rich-text-field/utils.ts +55 -0
  178. package/src/components/silke-sortable-list/index.ts +1 -1
  179. package/src/components/silke-sortable-list/silke-nested-sortable-list.scss +11 -1
  180. package/src/components/silke-sortable-list/silke-nested-sortable-list.tsx +9 -0
  181. package/src/components/silke-sortable-list/silke-sortable-list.mdx +13 -0
  182. package/src/components/silke-tag/silke-tag.scss +12 -3
  183. package/src/components/silke-theme-card/silke-theme-card.module.scss +141 -45
  184. package/src/components/silke-theme-card/silke-theme-card.tsx +76 -47
  185. package/src/components/silke-theme-swatch/silke-theme-swatch.mdx +56 -0
  186. package/src/components/silke-theme-swatch/silke-theme-swatch.module.scss +53 -63
  187. package/src/components/silke-theme-swatch/silke-theme-swatch.tsx +99 -14
  188. package/src/icons.tsx +65 -4
  189. package/tsconfig.build.tsbuildinfo +0 -1
@@ -0,0 +1,61 @@
1
+ import { render, screen } from '@testing-library/react';
2
+ import React from 'react';
3
+ import { stubProseMirrorRects } from './jsdom-rects';
4
+ import { SilkeRichTextField } from './silke-rich-text-field';
5
+
6
+ /**
7
+ * Regression tests for "tables break in the text editor".
8
+ *
9
+ * Two things had to be true for a production table to be destroyed:
10
+ * 1. the schema had no table nodes, so parsing flattened the table, and
11
+ * 2. the value-sync effect called setContent, which emits an update by default,
12
+ * so the flattened HTML went back out through onChange and was saved.
13
+ *
14
+ * (1) is covered in table.spec.ts. These tests cover (2): loading content must
15
+ * never call onChange, whatever the schema does to it on parse.
16
+ */
17
+
18
+ const TABLE_HTML =
19
+ '<table><tbody><tr><th><p>Name</p></th><th><p>Qty</p></th></tr>' +
20
+ '<tr><td><p>Apples</p></td><td><p>3</p></td></tr></tbody></table>';
21
+
22
+ beforeAll(stubProseMirrorRects);
23
+
24
+ describe('rich text field content sync', () => {
25
+ it('renders a table from the value instead of flattening it', () => {
26
+ render(<SilkeRichTextField value={TABLE_HTML} onChange={jest.fn()} />);
27
+
28
+ const table = document.querySelector('.tiptap table');
29
+ expect(table).toBeTruthy();
30
+ expect(table!.querySelectorAll('tr')).toHaveLength(2);
31
+ expect(table!.querySelectorAll('th')).toHaveLength(2);
32
+ expect(screen.getByText('Apples')).toBeTruthy();
33
+ });
34
+
35
+ it('does not call onChange while loading a value', () => {
36
+ const onChange = jest.fn();
37
+ const { rerender } = render(<SilkeRichTextField value={TABLE_HTML} onChange={onChange} />);
38
+ expect(onChange).not.toHaveBeenCalled();
39
+
40
+ // An external update (the host writing new content into the field) must also
41
+ // stay silent — it is not an edit the user made.
42
+ rerender(
43
+ <SilkeRichTextField value={`${TABLE_HTML}<p>appended</p>`} onChange={onChange} />,
44
+ );
45
+ expect(onChange).not.toHaveBeenCalled();
46
+ });
47
+
48
+ it('does not call onChange for content the schema normalizes on parse', () => {
49
+ const onChange = jest.fn();
50
+ // <thead> has no node in the schema and <marquee> is not supported at all, so
51
+ // getHTML() will never match this input — exactly the case that used to trigger
52
+ // a silent save of the normalized document.
53
+ render(
54
+ <SilkeRichTextField
55
+ value={'<table><thead><tr><td><p>a</p></td></tr></thead></table><marquee>b</marquee>'}
56
+ onChange={onChange}
57
+ />,
58
+ );
59
+ expect(onChange).not.toHaveBeenCalled();
60
+ });
61
+ });
@@ -0,0 +1,168 @@
1
+ import { render, screen, waitFor } from '@testing-library/react';
2
+ import userEvent from '@testing-library/user-event';
3
+ import React from 'react';
4
+ import type { Editor } from '@tiptap/react';
5
+ import { stubProseMirrorRects } from './jsdom-rects';
6
+ import { SilkeRichTextField } from './silke-rich-text-field';
7
+ import { getTableElement } from './utils';
8
+
9
+ const TABLE_HTML =
10
+ '<table><tbody><tr><th><p>Name</p></th><th><p>Qty</p></th></tr>' +
11
+ '<tr><td><p>Apples</p></td><td><p>3</p></td></tr></tbody></table>';
12
+
13
+ beforeAll(stubProseMirrorRects);
14
+
15
+ /** Document position just inside the given text, for putting the caret in a known cell. */
16
+ function posOfText(editor: Editor, text: string) {
17
+ let pos = 0;
18
+ editor.state.doc.descendants((node, nodePos) => {
19
+ if (node.isText && node.text?.includes(text)) {
20
+ pos = nodePos + 1;
21
+ return false;
22
+ }
23
+ return true;
24
+ });
25
+ return pos;
26
+ }
27
+
28
+ /** Put the caret in a cell, which is what reveals the toolbar. */
29
+ const focusCell = (editor: Editor, text = 'Apples') =>
30
+ editor.chain().focus().setTextSelection(posOfText(editor, text)).run();
31
+
32
+ function renderField(content = TABLE_HTML) {
33
+ const editorRef = React.createRef<Editor>() as React.MutableRefObject<Editor>;
34
+ const result = render(
35
+ <SilkeRichTextField value={content} onChange={jest.fn()} editorRef={editorRef} autoFocus />,
36
+ );
37
+ return { ...result, editorRef };
38
+ }
39
+
40
+ /**
41
+ * BubbleMenu keeps its element mounted and toggles `visibility`, so presence in the DOM
42
+ * says nothing about whether the toolbar is showing.
43
+ *
44
+ * Coordinates are deliberately not asserted: they come from Floating UI, which needs real
45
+ * layout, and jsdom reports every rect as zero. An earlier hand-rolled version was unit
46
+ * tested against stubbed rects and still shipped broken — those tests only proved the
47
+ * arithmetic. What is testable is the anchor element the placement is derived from.
48
+ */
49
+ const isShowing = () => {
50
+ // Absent entirely until the menu has shown at least once.
51
+ const button = screen.queryByTitle('Add row');
52
+ if (!button) return false;
53
+ // BubbleMenu owns an ancestor of the rendered children, and how many wrappers sit in
54
+ // between is SilkeBox's business — walk up to whichever one it sets visibility on.
55
+ let el = button.closest('[data-rich-text-menu]') as HTMLElement | null;
56
+ while (el && !el.style.visibility) el = el.parentElement;
57
+ return el?.style.visibility === 'visible';
58
+ };
59
+
60
+ describe('table toolbar', () => {
61
+ it('stays hidden when the caret is not in a table', async () => {
62
+ const { editorRef } = renderField('<p>plain text</p>');
63
+ editorRef.current.chain().focus().setTextSelection(2).run();
64
+
65
+ await waitFor(() => expect(isShowing()).toBe(false));
66
+ });
67
+
68
+ it('shows the row and column controls when the caret is inside a cell', async () => {
69
+ const { editorRef } = renderField();
70
+ focusCell(editorRef.current);
71
+
72
+ await waitFor(() => expect(isShowing()).toBe(true));
73
+ for (const title of [
74
+ 'Toggle table header',
75
+ 'Add column',
76
+ 'Remove column',
77
+ 'Add row',
78
+ 'Remove row',
79
+ 'Delete table',
80
+ ]) {
81
+ expect(screen.getByTitle(title)).toBeTruthy();
82
+ }
83
+ });
84
+
85
+ it('hides again when the selection is not collapsed', async () => {
86
+ // A text selection belongs to the formatting toolbar.
87
+ const { editorRef } = renderField();
88
+ const from = posOfText(editorRef.current, 'Apples');
89
+ focusCell(editorRef.current);
90
+ await waitFor(() => expect(isShowing()).toBe(true));
91
+
92
+ editorRef.current.chain().focus().setTextSelection({ from, to: from + 4 }).run();
93
+ await waitFor(() => expect(isShowing()).toBe(false));
94
+ });
95
+
96
+ it('resolves the table element it anchors to', async () => {
97
+ // The toolbar is placed against the table's left edge, so a null or wrong element here
98
+ // is what sends it to the corner of the screen. Position itself needs a browser, but
99
+ // whether the anchor resolves at all does not.
100
+ const { editorRef } = renderField();
101
+ focusCell(editorRef.current);
102
+ await waitFor(() => expect(isShowing()).toBe(true));
103
+
104
+ const anchor = getTableElement(editorRef.current);
105
+ expect(anchor).toBe(document.querySelector('.tiptap table'));
106
+ expect(anchor!.tagName).toBe('TABLE');
107
+ });
108
+
109
+ it('has no anchor when the caret is outside a table', () => {
110
+ const { editorRef } = renderField('<p>plain text</p>');
111
+ editorRef.current.chain().focus().setTextSelection(2).run();
112
+
113
+ expect(getTableElement(editorRef.current)).toBeNull();
114
+ });
115
+
116
+ it('adds and removes rows and columns', async () => {
117
+ const rowCount = () => document.querySelectorAll('.tiptap table tr').length;
118
+ const columnCount = () => document.querySelectorAll('.tiptap table tr:first-child > *').length;
119
+
120
+ const { editorRef } = renderField();
121
+ focusCell(editorRef.current);
122
+ await waitFor(() => expect(isShowing()).toBe(true));
123
+
124
+ expect(rowCount()).toBe(2);
125
+ expect(columnCount()).toBe(2);
126
+
127
+ await userEvent.click(screen.getByTitle('Add row'));
128
+ await waitFor(() => expect(rowCount()).toBe(3));
129
+
130
+ await userEvent.click(screen.getByTitle('Add column'));
131
+ await waitFor(() => expect(columnCount()).toBe(3));
132
+
133
+ await userEvent.click(screen.getByTitle('Remove row'));
134
+ await waitFor(() => expect(rowCount()).toBe(2));
135
+
136
+ await userEvent.click(screen.getByTitle('Remove column'));
137
+ await waitFor(() => expect(columnCount()).toBe(2));
138
+ });
139
+
140
+ it('deletes the whole table', async () => {
141
+ const { editorRef } = renderField();
142
+ focusCell(editorRef.current);
143
+ await waitFor(() => expect(isShowing()).toBe(true));
144
+
145
+ await userEvent.click(screen.getByTitle('Delete table'));
146
+ await waitFor(() => expect(document.querySelector('.tiptap table')).toBeNull());
147
+ });
148
+
149
+ it('marks itself as a rich text menu so clicking it does not blur the field', async () => {
150
+ const onBlur = jest.fn();
151
+ const editorRef = React.createRef<Editor>() as React.MutableRefObject<Editor>;
152
+ render(
153
+ <SilkeRichTextField
154
+ value={TABLE_HTML}
155
+ onChange={jest.fn()}
156
+ onBlur={onBlur}
157
+ editorRef={editorRef}
158
+ autoFocus
159
+ />,
160
+ );
161
+ focusCell(editorRef.current);
162
+ await waitFor(() => expect(isShowing()).toBe(true));
163
+
164
+ // The editor's blur handler and the Vev editor's canvas mousedown guard both key
165
+ // off this attribute; without it the toolbar closes on its own first click.
166
+ expect(screen.getByTitle('Add row').closest('[data-rich-text-menu]')).toBeTruthy();
167
+ });
168
+ });
@@ -0,0 +1,121 @@
1
+ import { posToDOMRect } from '@tiptap/core';
2
+ import type { Editor } from '@tiptap/react';
3
+ import { BubbleMenu } from '@tiptap/react/menus';
4
+ import React from 'react';
5
+ import { SilkeBox } from '../silke-box';
6
+ import { SilkeButton } from '../silke-button';
7
+ import { SilkeDivider } from '../silke-divider';
8
+ import { getTableElement, useLatest } from './utils';
9
+
10
+ interface TableToolbarProps {
11
+ editor: Editor;
12
+ /** Hide while the slash or link menu owns the area around the caret. */
13
+ suppressed?: boolean;
14
+ }
15
+
16
+ /**
17
+ * Row/column controls for the cell the caret sits in — the replacement for the table
18
+ * editor the old Plate-based editor had, and placed the way that one was: in the gutter to
19
+ * the left of the table, vertically level with the row being edited. Over the table it
20
+ * covered the cell above the caret, which made editing the top rows unworkable.
21
+ *
22
+ * The anchor is a zero-width box at (table's left edge, caret's line), so Floating UI puts
23
+ * the toolbar just outside the table without it having to know the toolbar's own size.
24
+ */
25
+ export function TableToolbar({ editor, suppressed }: TableToolbarProps) {
26
+ const suppressedRef = useLatest(suppressed);
27
+
28
+ return (
29
+ <BubbleMenu
30
+ editor={editor}
31
+ // Must differ from the formatting toolbar's default key, or the two plugins collide.
32
+ pluginKey="tableToolbar"
33
+ // Appended to the body and fixed to the viewport so the toolbar is not trapped
34
+ // under panels by whatever stacking context the host puts the editor in.
35
+ appendTo={() => document.body}
36
+ getReferencedVirtualElement={() => {
37
+ const table = getTableElement(editor);
38
+ if (!table) return null;
39
+
40
+ const tableRect = table.getBoundingClientRect();
41
+ // An unmeasurable table would anchor everything at 0,0 — the corner-of-the-screen
42
+ // failure this toolbar had twice. Returning null falls back to BubbleMenu's own
43
+ // caret rect, which is merely in the wrong place rather than off in the corner.
44
+ if (!tableRect.width && !tableRect.height) return null;
45
+
46
+ const { from, to } = editor.state.selection;
47
+ const caretRect = posToDOMRect(editor.view, from, to);
48
+ const rect = new DOMRect(tableRect.left, caretRect.top, 0, caretRect.height);
49
+ return { getBoundingClientRect: () => rect };
50
+ }}
51
+ // Only while the selection is collapsed — a text selection belongs to the formatting
52
+ // toolbar, and two menus over the same caret would overlap.
53
+ shouldShow={({ editor: e, state }) =>
54
+ !suppressedRef.current && e.isActive('table') && state.selection.empty
55
+ }
56
+ // flip/shift keep it on screen when the table is hard against the left edge or the
57
+ // caret is near the top or bottom of the viewport.
58
+ options={{ strategy: 'fixed', placement: 'left-start', offset: 8, flip: true, shift: true }}
59
+ style={{ zIndex: 999 }}
60
+ >
61
+ <SilkeBox
62
+ data-rich-text-menu
63
+ column
64
+ gap="xxs"
65
+ hAlign="center"
66
+ bg="neutral-10"
67
+ rounded="small"
68
+ shadow="level3"
69
+ border
70
+ pad="xxs"
71
+ >
72
+ <SilkeButton
73
+ kind="ghost"
74
+ size="base"
75
+ selected={editor.isActive('tableHeader')}
76
+ onClick={() => editor.chain().focus().toggleHeaderRow().run()}
77
+ icon="text.table.header"
78
+ title="Toggle table header"
79
+ />
80
+ <SilkeDivider dark />
81
+ <SilkeButton
82
+ kind="ghost"
83
+ size="base"
84
+ onClick={() => editor.chain().focus().addColumnAfter().run()}
85
+ icon="text.column.add"
86
+ title="Add column"
87
+ />
88
+ <SilkeButton
89
+ kind="ghost"
90
+ size="base"
91
+ onClick={() => editor.chain().focus().deleteColumn().run()}
92
+ icon="text.column.remove"
93
+ title="Remove column"
94
+ />
95
+ <SilkeButton
96
+ kind="ghost"
97
+ size="base"
98
+ onClick={() => editor.chain().focus().addRowAfter().run()}
99
+ icon="text.row.add"
100
+ title="Add row"
101
+ />
102
+ <SilkeButton
103
+ kind="ghost"
104
+ size="base"
105
+ onClick={() => editor.chain().focus().deleteRow().run()}
106
+ icon="text.row.remove"
107
+ title="Remove row"
108
+ />
109
+ <SilkeDivider dark />
110
+ <SilkeButton
111
+ kind="ghost"
112
+ size="base"
113
+ onClick={() => editor.chain().focus().deleteTable().run()}
114
+ icon="delete"
115
+ title="Delete table"
116
+ data-cy="rich-text-delete-table"
117
+ />
118
+ </SilkeBox>
119
+ </BubbleMenu>
120
+ );
121
+ }
@@ -0,0 +1,87 @@
1
+ import { Editor } from '@tiptap/react';
2
+ import { buildExtensions } from './plugins';
3
+
4
+ /**
5
+ * Regression tests for "tables break in the text editor".
6
+ *
7
+ * The rich text field shipped without table nodes in the schema. ProseMirror does
8
+ * not keep elements it has no node for — it unwraps them and keeps the inline
9
+ * content — so every table in existing content parsed down to a flat run of
10
+ * paragraphs. The value-sync effect then wrote that flattened HTML back through
11
+ * onChange, destroying the table on disk without the user typing anything.
12
+ */
13
+
14
+ const createEditor = (content: string) =>
15
+ new Editor({ extensions: buildExtensions({ current: '' }), content });
16
+
17
+ const TABLE_HTML =
18
+ '<table><tbody><tr><th><p>Name</p></th><th><p>Qty</p></th></tr>' +
19
+ '<tr><td><p>Apples</p></td><td><p>3</p></td></tr></tbody></table>';
20
+
21
+ describe('rich text field tables', () => {
22
+ it('registers the four table nodes exactly once', () => {
23
+ const editor = createEditor('<p>hi</p>');
24
+ const names = editor.extensionManager.extensions.map((e) => e.name);
25
+ for (const name of ['table', 'tableRow', 'tableHeader', 'tableCell']) {
26
+ expect(names.filter((n) => n === name)).toHaveLength(1);
27
+ }
28
+ editor.destroy();
29
+ });
30
+
31
+ it('keeps a table as a table instead of flattening it to paragraphs', () => {
32
+ const editor = createEditor(TABLE_HTML);
33
+ const html = editor.getHTML();
34
+ expect(html).toContain('<table>');
35
+ expect(html).toContain('<th');
36
+ expect(html).toContain('Apples');
37
+ // The pre-fix output was '<p>Name</p><p>Qty</p><p>Apples</p><p>3</p>'.
38
+ expect(html).not.toBe('<p>Name</p><p>Qty</p><p>Apples</p><p>3</p>');
39
+ editor.destroy();
40
+ });
41
+
42
+ it('round-trips a table without changing the stored HTML', () => {
43
+ // The value-sync effect compares getHTML() to the saved value, so any
44
+ // normalization diff makes the field rewrite host content on load.
45
+ const editor = createEditor(TABLE_HTML);
46
+ expect(editor.getHTML()).toBe(TABLE_HTML);
47
+ editor.destroy();
48
+ });
49
+
50
+ it('preserves colspan and rowspan', () => {
51
+ const html =
52
+ '<table><tbody><tr><td colspan="2"><p>Wide</p></td></tr>' +
53
+ '<tr><td rowspan="2"><p>Tall</p></td><td><p>x</p></td></tr>' +
54
+ '<tr><td><p>y</p></td></tr></tbody></table>';
55
+ const editor = createEditor(html);
56
+ const out = editor.getHTML();
57
+ expect(out).toContain('colspan="2"');
58
+ expect(out).toContain('rowspan="2"');
59
+ editor.destroy();
60
+ });
61
+
62
+ it('wraps bare cell text in a paragraph (pasted-table shape)', () => {
63
+ const editor = createEditor('<table><tbody><tr><td>bare</td></tr></tbody></table>');
64
+ expect(editor.getHTML()).toContain('<td><p>bare</p></td>');
65
+ editor.destroy();
66
+ });
67
+
68
+ it('supports insertTable and the row/column commands', () => {
69
+ const editor = createEditor('<p></p>');
70
+ editor.commands.insertTable({ rows: 2, cols: 2, withHeaderRow: true });
71
+ expect(editor.getHTML()).toContain('<table>');
72
+ expect(editor.getHTML().match(/<tr>/g)).toHaveLength(2);
73
+
74
+ editor.commands.addRowAfter();
75
+ expect(editor.getHTML().match(/<tr>/g)).toHaveLength(3);
76
+
77
+ editor.commands.addColumnAfter();
78
+ expect(editor.getHTML().match(/<td>|<td /g)?.length).toBeGreaterThan(2);
79
+
80
+ editor.commands.deleteRow();
81
+ expect(editor.getHTML().match(/<tr>/g)).toHaveLength(2);
82
+
83
+ editor.commands.deleteTable();
84
+ expect(editor.getHTML()).not.toContain('<table>');
85
+ editor.destroy();
86
+ });
87
+ });
@@ -0,0 +1,63 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import styles from './silke-rich-text-field.scss';
3
+
4
+ interface LinkHoverState {
5
+ href: string;
6
+ /** Viewport coordinates — the tooltip renders in the floating layer. */
7
+ rect: { left: number; top: number };
8
+ }
9
+
10
+ export function useLinkHover(editorAreaRef: React.RefObject<HTMLDivElement | null>) {
11
+ const [hoveredLink, setHoveredLink] = useState<LinkHoverState | null>(null);
12
+ const anchorRef = useRef<HTMLAnchorElement | null>(null);
13
+ const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
14
+
15
+ useEffect(() => {
16
+ const editorEl = editorAreaRef.current;
17
+ if (!editorEl) return;
18
+
19
+ const onMouseOver = (e: MouseEvent) => {
20
+ const anchor = (e.target as HTMLElement).closest('a') as HTMLAnchorElement | null;
21
+ if (!anchor || !editorEl.contains(anchor)) return;
22
+ if (anchorRef.current === anchor) return;
23
+
24
+ if (timeoutRef.current) clearTimeout(timeoutRef.current);
25
+ anchorRef.current = anchor;
26
+
27
+ const anchorRect = anchor.getBoundingClientRect();
28
+ setHoveredLink({
29
+ href: anchor.getAttribute('href') || '',
30
+ rect: { left: anchorRect.left, top: anchorRect.bottom + 4 },
31
+ });
32
+ };
33
+
34
+ const onMouseOut = (e: MouseEvent) => {
35
+ const related = e.relatedTarget as HTMLElement | null;
36
+ if (related?.closest(`.${styles.linkTooltip}`)) return;
37
+ if (timeoutRef.current) clearTimeout(timeoutRef.current);
38
+ timeoutRef.current = setTimeout(() => {
39
+ anchorRef.current = null;
40
+ setHoveredLink(null);
41
+ }, 150);
42
+ };
43
+
44
+ editorEl.addEventListener('mouseover', onMouseOver);
45
+ editorEl.addEventListener('mouseout', onMouseOut);
46
+ return () => {
47
+ editorEl.removeEventListener('mouseover', onMouseOver);
48
+ editorEl.removeEventListener('mouseout', onMouseOut);
49
+ if (timeoutRef.current) clearTimeout(timeoutRef.current);
50
+ };
51
+ }, [editorAreaRef]);
52
+
53
+ const cancelDismiss = () => {
54
+ if (timeoutRef.current) clearTimeout(timeoutRef.current);
55
+ };
56
+
57
+ const dismiss = () => {
58
+ anchorRef.current = null;
59
+ setHoveredLink(null);
60
+ };
61
+
62
+ return { hoveredLink, hoveredAnchor: anchorRef, cancelDismiss, dismiss };
63
+ }
@@ -0,0 +1,130 @@
1
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+ import type { Editor } from '@tiptap/react';
3
+ import { SLASH_MENU_ITEMS, SlashMenuItem } from './slash-menu-items';
4
+
5
+ interface SlashMenuState {
6
+ open: boolean;
7
+ query: string;
8
+ index: number;
9
+ }
10
+
11
+ export function useSlashMenu(editor: Editor | null, isFocused: boolean) {
12
+ const [state, setState] = useState<SlashMenuState>({ open: false, query: '', index: 0 });
13
+ const openRef = useRef(false);
14
+ const focusedRef = useRef(false);
15
+ const startPosRef = useRef<number | null>(null);
16
+ const toolbarOpenRef = useRef(false);
17
+
18
+ focusedRef.current = isFocused;
19
+
20
+ // Watch for slash trigger via transactions
21
+ useEffect(() => {
22
+ if (!editor) return;
23
+
24
+ const handleTransaction = () => {
25
+ if (!focusedRef.current) return;
26
+
27
+ const { $from } = editor.state.selection;
28
+ const textBefore = $from.parent.textContent.slice(0, $from.parentOffset);
29
+ const match = textBefore.match(/(?:^|\s)\/([^\s]*)$/);
30
+
31
+ if (match) {
32
+ if (!openRef.current) {
33
+ startPosRef.current = $from.pos - match[1].length - 1;
34
+ }
35
+ openRef.current = true;
36
+ // Bail out when nothing changed. A fresh object here re-rendered on *every*
37
+ // transaction, and the BubbleMenu in the formatting toolbar dispatches one
38
+ // transaction per render — so the two fed each other into an infinite loop as
39
+ // soon as the menu opened. Returning `prev` also stops unrelated transactions
40
+ // from resetting `index` and clobbering arrow-key navigation.
41
+ setState((prev) =>
42
+ prev.open && prev.query === match[1] ? prev : { open: true, query: match[1], index: 0 },
43
+ );
44
+ } else if (openRef.current && !toolbarOpenRef.current) {
45
+ openRef.current = false;
46
+ setState({ open: false, query: '', index: 0 });
47
+ startPosRef.current = null;
48
+ }
49
+ };
50
+
51
+ editor.on('transaction', handleTransaction);
52
+ return () => {
53
+ editor.off('transaction', handleTransaction);
54
+ };
55
+ }, [editor]);
56
+
57
+ const close = useCallback(() => {
58
+ if (!openRef.current) return;
59
+ openRef.current = false;
60
+ toolbarOpenRef.current = false;
61
+ setState({ open: false, query: '', index: 0 });
62
+ startPosRef.current = null;
63
+ }, []);
64
+
65
+ const selectItem = useCallback(
66
+ (item: SlashMenuItem) => {
67
+ if (!editor) return;
68
+ // Only delete the slash text if the menu was opened by typing "/"
69
+ if (startPosRef.current !== null) {
70
+ const { $from } = editor.state.selection;
71
+ editor
72
+ .chain()
73
+ .focus()
74
+ .deleteRange({ from: startPosRef.current, to: $from.pos })
75
+ .run();
76
+ }
77
+ item.command(editor);
78
+ close();
79
+ },
80
+ [editor, close],
81
+ );
82
+
83
+ const filteredItems = useMemo(() => {
84
+ const q = state.query.toLowerCase();
85
+ return SLASH_MENU_ITEMS.filter(
86
+ (item) =>
87
+ item.label.toLowerCase().includes(q) ||
88
+ item.keywords.some((kw) => kw.includes(q)),
89
+ );
90
+ }, [state.query]);
91
+
92
+ const setIndex = useCallback((index: number) => {
93
+ setState((prev) => ({ ...prev, index }));
94
+ }, []);
95
+
96
+ const navigateUp = useCallback(() => {
97
+ setState((prev) => ({ ...prev, index: Math.max(prev.index - 1, 0) }));
98
+ }, []);
99
+
100
+ const navigateDown = useCallback(() => {
101
+ setState((prev) => ({
102
+ ...prev,
103
+ index: Math.min(prev.index + 1, filteredItems.length - 1),
104
+ }));
105
+ }, [filteredItems.length]);
106
+
107
+ const openFromToolbar = useCallback(() => {
108
+ if (!editor) return;
109
+ openRef.current = true;
110
+ toolbarOpenRef.current = true;
111
+ startPosRef.current = null;
112
+ setState({ open: true, query: '', index: 0 });
113
+ // Collapse selection to caret at the start so the BubbleMenu hides
114
+ // and the command menu appears at the start of the selection
115
+ const { from } = editor.state.selection;
116
+ editor.chain().focus().setTextSelection(from).run();
117
+ }, [editor]);
118
+
119
+ return {
120
+ open: state.open,
121
+ index: state.index,
122
+ filteredItems,
123
+ close,
124
+ selectItem,
125
+ setIndex,
126
+ navigateUp,
127
+ navigateDown,
128
+ openFromToolbar,
129
+ };
130
+ }
@@ -0,0 +1,55 @@
1
+ import type { ResolvedPos } from '@tiptap/pm/model';
2
+ import type { Editor } from '@tiptap/react';
3
+ import { useRef } from 'react';
4
+
5
+ /** Viewport position of the cursor, for the menus that render in the floating layer */
6
+ export function getCursorPosition(editor: Editor | null): { left: number; top: number } {
7
+ if (!editor) return { left: 0, top: 0 };
8
+
9
+ try {
10
+ const { $from } = editor.state.selection;
11
+ const coords = editor.view.coordsAtPos($from.pos);
12
+
13
+ return { left: coords.left, top: coords.bottom + 12 };
14
+ } catch {
15
+ return { left: 0, top: 0 };
16
+ }
17
+ }
18
+
19
+ /** The <table> element the caret sits in, for anchoring the table toolbar to its edge. */
20
+ export function getTableElement(editor: Editor | null): HTMLElement | null {
21
+ if (!editor) return null;
22
+
23
+ try {
24
+ const { $from } = editor.state.selection;
25
+ const { depth: caretDepth } = $from;
26
+
27
+ for (let depth = caretDepth; depth > 0; depth--) {
28
+ if ($from.node(depth).type.name !== 'table') continue;
29
+ const dom = editor.view.nodeDOM($from.before(depth)) as HTMLElement | null;
30
+ // Duck-typed rather than `instanceof HTMLElement`: the editor's DOM can live in
31
+ // another realm (the canvas renders content inside custom elements), and there
32
+ // instanceof against this bundle's constructor is false for a perfectly good node.
33
+ return dom && typeof dom.getBoundingClientRect === 'function' ? dom : null;
34
+ }
35
+ return null;
36
+ } catch {
37
+ return null;
38
+ }
39
+ }
40
+
41
+ /** Whether the caret sits inside a table cell. */
42
+ export function isInTable($from: ResolvedPos): boolean {
43
+ const { depth: caretDepth } = $from;
44
+ for (let depth = caretDepth; depth > 0; depth--) {
45
+ if ($from.node(depth).type.spec.tableRole) return true;
46
+ }
47
+ return false;
48
+ }
49
+
50
+ /** Stable ref that always points to the latest value. Avoids stale closures in useEditor callbacks. */
51
+ export function useLatest<T>(value: T) {
52
+ const ref = useRef(value);
53
+ ref.current = value;
54
+ return ref;
55
+ }