tiptap-essentials 2.0.0-beta.5 → 2.0.0-beta.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.
package/dist/samples.d.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  */
6
6
  export declare const picture: string;
7
7
  export declare const table: string;
8
+ export declare const captionedTable: string;
8
9
  export declare const video: string;
9
10
  export declare const size = "<p><span style=\"font-size: var(--lg-font-size);\">Large</span> text</p>";
10
11
  export declare const color = "<p><span style=\"color: var(--tiptap-text-red);\">Red</span> text</p>";
package/dist/samples.js CHANGED
@@ -12,6 +12,11 @@ export const table = '<div class="table-responsive" role="region" tabindex="0" a
12
12
  '<tr><th><p>Port</p></th><th><p>Ships</p></th></tr>' +
13
13
  '<tr><td><p>Chattogram</p></td><td><p>12</p></td></tr>' +
14
14
  '</tbody></table></div>';
15
+ export const captionedTable = '<div class="table-responsive" role="region" tabindex="0" aria-label="Ships by port">' +
16
+ '<table class="tiptap-table"><caption>Ships by port</caption><tbody>' +
17
+ '<tr><th><p>Port</p></th><th><p>Ships</p></th></tr>' +
18
+ '<tr><td><p>Chattogram</p></td><td><p>12</p></td></tr>' +
19
+ '</tbody></table></div>';
15
20
  export const video = '<div data-youtube-video=""><iframe width="640" height="480" allowfullscreen="true" ' +
16
21
  'src="https://www.youtube.com/embed/M7lc1UVf-VE?start=90&amp;rel=1" title="A talk"></iframe></div>';
17
22
  export const size = '<p><span style="font-size: var(--lg-font-size);">Large</span> text</p>';
package/dist/table.d.ts CHANGED
@@ -2,15 +2,31 @@ import { type TableOptions as TiptapTableOptions } from '@tiptap/extension-table
2
2
  export interface TableOptions extends TiptapTableOptions {
3
3
  /**
4
4
  * What a screen reader calls the region a table too wide for the page
5
- * scrolls in, which a keyboard can then reach. Without one, the table
6
- * scrolls in a plain box.
5
+ * scrolls in, which a keyboard can then reach, where the table has no
6
+ * caption to call it by. Without one, the table scrolls in a plain box.
7
7
  */
8
8
  region: string | null;
9
9
  }
10
+ declare module '@tiptap/core' {
11
+ interface Commands<ReturnType> {
12
+ tableCaption: {
13
+ /**
14
+ * Captions the table the selection is in. A caption of spaces alone
15
+ * takes its caption off.
16
+ */
17
+ setTableCaption: (caption: string) => ReturnType;
18
+ unsetTableCaption: () => ReturnType;
19
+ };
20
+ }
21
+ }
10
22
  /**
11
23
  * Tiptap's table, written inside a box it scrolls in when it is too wide for
12
24
  * the page, and without the column widths Tiptap gives every table. The
13
25
  * editor draws its own copy without the region's tab stop: inside the editable
14
26
  * area, one would take the focus from the editor whenever a cell is clicked.
27
+ *
28
+ * A table's caption is a line of plain text the table holds, set by a command
29
+ * rather than typed in place: ProseMirror's tables read every child of a table
30
+ * as a row. It is written first in the table, and names the region.
15
31
  */
16
32
  export declare const Table: import("@tiptap/core").Node<TableOptions, any>;
package/dist/table.js CHANGED
@@ -1,10 +1,14 @@
1
- import { mergeAttributes } from '@tiptap/core';
1
+ import { findParentNode, mergeAttributes } from '@tiptap/core';
2
2
  import { Table as TiptapTable } from '@tiptap/extension-table';
3
3
  /**
4
4
  * Tiptap's table, written inside a box it scrolls in when it is too wide for
5
5
  * the page, and without the column widths Tiptap gives every table. The
6
6
  * editor draws its own copy without the region's tab stop: inside the editable
7
7
  * area, one would take the focus from the editor whenever a cell is clicked.
8
+ *
9
+ * A table's caption is a line of plain text the table holds, set by a command
10
+ * rather than typed in place: ProseMirror's tables read every child of a table
11
+ * as a row. It is written first in the table, and names the region.
8
12
  */
9
13
  export const Table = TiptapTable.extend({
10
14
  addOptions() {
@@ -14,10 +18,25 @@ export const Table = TiptapTable.extend({
14
18
  region: null
15
19
  };
16
20
  },
17
- renderHTML({ HTMLAttributes }) {
21
+ addAttributes() {
22
+ return {
23
+ ...this.parent?.(),
24
+ caption: {
25
+ default: null,
26
+ parseHTML: (element) => captionText(Array.from(element.children).find((child) => child.localName === 'caption')?.textContent),
27
+ rendered: false
28
+ }
29
+ };
30
+ },
31
+ // A caption is read as the table's, and read as content would make a row.
32
+ parseHTML() {
33
+ return [...(this.parent?.() ?? []), { tag: 'caption', ignore: true }];
34
+ },
35
+ renderHTML({ node, HTMLAttributes }) {
36
+ const caption = node.attrs['caption'];
18
37
  const { region } = this.options;
19
38
  const box = region
20
- ? { role: 'region', tabindex: '0', 'aria-label': region }
39
+ ? { role: 'region', tabindex: '0', 'aria-label': caption ?? region }
21
40
  : {};
22
41
  return [
23
42
  'div',
@@ -25,12 +44,13 @@ export const Table = TiptapTable.extend({
25
44
  [
26
45
  'table',
27
46
  mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
47
+ ...(caption ? [['caption', caption]] : []),
28
48
  ['tbody', 0]
29
49
  ]
30
50
  ];
31
51
  },
32
52
  addNodeView() {
33
- return ({ HTMLAttributes }) => {
53
+ return ({ node, HTMLAttributes }) => {
34
54
  const dom = document.createElement('div');
35
55
  dom.className = 'table-responsive';
36
56
  const table = dom.appendChild(document.createElement('table'));
@@ -38,8 +58,39 @@ export const Table = TiptapTable.extend({
38
58
  for (const [name, value] of Object.entries(attributes)) {
39
59
  table.setAttribute(name, String(value));
40
60
  }
61
+ const caption = node.attrs['caption'];
62
+ if (caption) {
63
+ const element = table.appendChild(document.createElement('caption'));
64
+ element.textContent = caption;
65
+ element.setAttribute('contenteditable', 'false');
66
+ }
41
67
  const contentDOM = table.appendChild(document.createElement('tbody'));
42
68
  return { dom, contentDOM };
43
69
  };
70
+ },
71
+ addCommands() {
72
+ const caption = (text) => ({ tr, dispatch }) => {
73
+ const table = findParentNode((node) => node.type === this.type)(tr.selection);
74
+ if (!table) {
75
+ return false;
76
+ }
77
+ if (dispatch) {
78
+ tr.setNodeMarkup(table.pos, undefined, {
79
+ ...table.node.attrs,
80
+ caption: text
81
+ });
82
+ }
83
+ return true;
84
+ };
85
+ return {
86
+ ...this.parent?.(),
87
+ setTableCaption: (text) => caption(captionText(text)),
88
+ unsetTableCaption: () => caption(null)
89
+ };
44
90
  }
45
91
  });
92
+ // A caption as a table holds it: each run of spaces and line breaks one
93
+ // space, none at either end, and spaces alone no caption.
94
+ function captionText(text) {
95
+ return text?.replace(/\s+/g, ' ').trim() || null;
96
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiptap-essentials",
3
- "version": "2.0.0-beta.5",
3
+ "version": "2.0.0-beta.6",
4
4
  "description": "Essential extensions of Tiptap",
5
5
  "type": "module",
6
6
  "exports": {