tiptap-essentials 2.0.0-beta.4 → 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/image.d.ts CHANGED
@@ -1,8 +1,20 @@
1
+ import { type ImageOptions as TiptapImageOptions } from '@tiptap/extension-image';
1
2
  /** A format a picture offers its image in, and the widths stored in it. */
2
3
  export interface PictureSource {
3
4
  type: string;
4
5
  srcset: string;
5
6
  }
7
+ export interface ImageOptions extends TiptapImageOptions {
8
+ /**
9
+ * Where the site's images are served from, `https://static.example`. Given
10
+ * one, an image is read only where every address it is fetched from is
11
+ * there, any other dropped as it is read; without one, every address is
12
+ * kept.
13
+ */
14
+ host: string | null;
15
+ /** Told how many images a paste lost for being from anywhere else. */
16
+ onImagesRefused: ((count: number) => void) | null;
17
+ }
6
18
  /** What an image is inserted with. */
7
19
  export interface ImageAttributes {
8
20
  src: string;
@@ -30,4 +42,4 @@ declare module '@tiptap/core' {
30
42
  * ahead of the `<img>` a browser drawing none of them is given. Read back,
31
43
  * the picture is that image again.
32
44
  */
33
- export declare const Image: import("@tiptap/core").Node<import("@tiptap/extension-image").ImageOptions, any>;
45
+ export declare const Image: import("@tiptap/core").Node<ImageOptions, any>;
package/dist/image.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { mergeAttributes } from '@tiptap/core';
2
- import TiptapImage from '@tiptap/extension-image';
2
+ import { Plugin, PluginKey } from '@tiptap/pm/state';
3
+ import TiptapImage, {} from '@tiptap/extension-image';
3
4
  /**
4
5
  * Tiptap's image, taking the attributes a responsive image is written with.
5
6
  * Given sources, it is written as a `<picture>`: a `<source>` for each,
@@ -7,6 +8,9 @@ import TiptapImage from '@tiptap/extension-image';
7
8
  * the picture is that image again.
8
9
  */
9
10
  export const Image = TiptapImage.extend({
11
+ addOptions() {
12
+ return { ...this.parent(), host: null, onImagesRefused: null };
13
+ },
10
14
  addAttributes() {
11
15
  return {
12
16
  ...this.parent?.(),
@@ -22,9 +26,43 @@ export const Image = TiptapImage.extend({
22
26
  };
23
27
  },
24
28
  parseHTML() {
29
+ const { host } = this.options;
25
30
  return [
26
- { tag: 'picture', getAttrs: pictureAttributes },
27
- ...(this.parent?.() ?? [])
31
+ {
32
+ tag: 'picture',
33
+ getAttrs: (picture) => isFromHost(picture, host) && pictureAttributes(picture)
34
+ },
35
+ ...(this.parent?.() ?? []).map((rule) => ({
36
+ ...rule,
37
+ // An img inside a picture is read with its picture's sources, where
38
+ // the picture's own rule turned them down.
39
+ getAttrs: (img) => isFromHost(img.closest('picture') ?? img, host)
40
+ ? (rule.getAttrs?.(img) ?? null)
41
+ : false
42
+ }))
43
+ ];
44
+ },
45
+ addProseMirrorPlugins() {
46
+ const { host, onImagesRefused } = this.options;
47
+ if (host === null || onImagesRefused === null) {
48
+ return [];
49
+ }
50
+ return [
51
+ new Plugin({
52
+ key: new PluginKey('imagesRefused'),
53
+ props: {
54
+ // Read into a document of its own: in one the page's document
55
+ // made, a pasted image would load, and run its `onerror`.
56
+ transformPastedHTML(html) {
57
+ const pasted = new DOMParser().parseFromString(html, 'text/html');
58
+ const refused = Array.from(pasted.querySelectorAll('img')).filter((img) => !isFromHost(img.closest('picture') ?? img, host)).length;
59
+ if (refused > 0) {
60
+ onImagesRefused(refused);
61
+ }
62
+ return html;
63
+ }
64
+ }
65
+ })
28
66
  ];
29
67
  },
30
68
  renderHTML({ node, HTMLAttributes }) {
@@ -55,6 +93,20 @@ function pictureAttributes(picture) {
55
93
  }
56
94
  return Object.fromEntries(Array.from(img.attributes, ({ name, value }) => [name, value]));
57
95
  }
96
+ // Every address an image is fetched from: its img's src, and each URL of the
97
+ // srcsets it and its picture's sources offer.
98
+ function addressesOf(element) {
99
+ const img = element.tagName === 'PICTURE' ? element.querySelector('img') : element;
100
+ const offers = [img, ...Array.from(element.querySelectorAll('source'))];
101
+ const srcsetUrls = offers.flatMap((offer) => (offer?.getAttribute('srcset') ?? '')
102
+ .split(',')
103
+ .map((candidate) => candidate.trim().split(/\s+/)[0] ?? ''));
104
+ return [img?.getAttribute('src') ?? '', ...srcsetUrls].filter(Boolean);
105
+ }
106
+ function isFromHost(element, host) {
107
+ return (host === null ||
108
+ addressesOf(element).every((address) => address.startsWith(`${host}/`)));
109
+ }
58
110
  function sourcesOf(element) {
59
111
  if (element.tagName !== 'PICTURE') {
60
112
  return [];
package/dist/index.d.ts CHANGED
@@ -9,7 +9,7 @@ export type { FontSizeOptions } from './font-size.js';
9
9
  export { Highlight } from './highlight.js';
10
10
  export type { HighlightOptions } from './highlight.js';
11
11
  export { Image } from './image.js';
12
- export type { ImageAttributes, PictureSource } from './image.js';
12
+ export type { ImageAttributes, ImageOptions, PictureSource } from './image.js';
13
13
  export { LineHeight } from './line-height.js';
14
14
  export type { LineHeightOptions } from './line-height.js';
15
15
  export { PlainSpaces } from './plain-spaces.js';
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
+ }
@@ -1,5 +1,4 @@
1
1
  import { Extension } from '@tiptap/core';
2
- import type { ImageOptions } from '@tiptap/extension-image';
3
2
  import type { SubscriptExtensionOptions } from '@tiptap/extension-subscript';
4
3
  import type { SuperscriptExtensionOptions } from '@tiptap/extension-superscript';
5
4
  import { type TableKitOptions } from '@tiptap/extension-table';
@@ -11,6 +10,7 @@ import { type BlockSpacingOptions } from './block-spacing.js';
11
10
  import { type ColorOptions } from './color.js';
12
11
  import { type FontSizeOptions } from './font-size.js';
13
12
  import { type HighlightOptions } from './highlight.js';
13
+ import { type ImageOptions } from './image.js';
14
14
  import { type LineHeightOptions } from './line-height.js';
15
15
  import { type TableOptions } from './table.js';
16
16
  type Part<Options> = Partial<Options> | false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiptap-essentials",
3
- "version": "2.0.0-beta.4",
3
+ "version": "2.0.0-beta.6",
4
4
  "description": "Essential extensions of Tiptap",
5
5
  "type": "module",
6
6
  "exports": {