tiptap-essentials 2.0.0-beta.4 → 2.0.0-beta.5
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 +13 -1
- package/dist/image.js +55 -3
- package/dist/index.d.ts +1 -1
- package/dist/tiptap-essentials.d.ts +1 -1
- package/package.json +1 -1
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<
|
|
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
|
|
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
|
-
{
|
|
27
|
-
|
|
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';
|
|
@@ -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;
|