tiptap-essentials 2.0.0-beta.3 → 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.
@@ -0,0 +1,15 @@
1
+ import type { ChainedCommands } from '@tiptap/core';
2
+ import type { MarkType } from '@tiptap/pm/model';
3
+ /**
4
+ * The group of the marks that shift text off its baseline, subscript and
5
+ * superscript. Each of them excludes the group, so text is lowered or raised
6
+ * but never both: setting one takes the other off.
7
+ */
8
+ export declare const BASELINE_SHIFT = "baselineShift";
9
+ /**
10
+ * Takes every other shift off the text, in a chain, ahead of setting `type`.
11
+ * ProseMirror would take them off as `type` went on, but Tiptap's `setMark`
12
+ * and `toggleMark` would answer that they had done nothing, which leaves
13
+ * their key to the browser: on a Mac, Cmd+, would open its settings as well.
14
+ */
15
+ export declare function unsetOtherShifts(chain: ChainedCommands, type: MarkType): ChainedCommands;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The group of the marks that shift text off its baseline, subscript and
3
+ * superscript. Each of them excludes the group, so text is lowered or raised
4
+ * but never both: setting one takes the other off.
5
+ */
6
+ export const BASELINE_SHIFT = 'baselineShift';
7
+ /**
8
+ * Takes every other shift off the text, in a chain, ahead of setting `type`.
9
+ * ProseMirror would take them off as `type` went on, but Tiptap's `setMark`
10
+ * and `toggleMark` would answer that they had done nothing, which leaves
11
+ * their key to the browser: on a Mac, Cmd+, would open its settings as well.
12
+ */
13
+ export function unsetOtherShifts(chain, type) {
14
+ for (const other of Object.values(type.schema.marks)) {
15
+ if (other !== type && type.excludes(other)) {
16
+ chain.unsetMark(other);
17
+ }
18
+ }
19
+ return chain;
20
+ }
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,10 +9,12 @@ 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';
16
+ export { Subscript } from './subscript.js';
17
+ export { Superscript } from './superscript.js';
16
18
  export { Table } from './table.js';
17
19
  export type { TableOptions } from './table.js';
18
20
  export { TextStyle } from './text-style.js';
package/dist/index.js CHANGED
@@ -6,6 +6,8 @@ export { Highlight } from './highlight.js';
6
6
  export { Image } from './image.js';
7
7
  export { LineHeight } from './line-height.js';
8
8
  export { PlainSpaces } from './plain-spaces.js';
9
+ export { Subscript } from './subscript.js';
10
+ export { Superscript } from './superscript.js';
9
11
  export { Table } from './table.js';
10
12
  export { TextStyle } from './text-style.js';
11
13
  export { Youtube } from './youtube.js';
package/dist/samples.d.ts CHANGED
@@ -10,3 +10,4 @@ export declare const size = "<p><span style=\"font-size: var(--lg-font-size);\">
10
10
  export declare const color = "<p><span style=\"color: var(--tiptap-text-red);\">Red</span> text</p>";
11
11
  export declare const highlight: string;
12
12
  export declare const spacing: string;
13
+ export declare const subscript = "<p>H<sub>2</sub>O</p>";
package/dist/samples.js CHANGED
@@ -20,3 +20,4 @@ export const highlight = '<p><mark data-color="var(--tiptap-highlight-yellow)" '
20
20
  'style="background-color: var(--tiptap-highlight-yellow); color: inherit;">Marked</mark> text</p>';
21
21
  export const spacing = '<p style="line-height: var(--tiptap-line-height-150); margin-top: var(--space-40); ' +
22
22
  'margin-bottom: var(--space-40);">Spaced</p>';
23
+ export const subscript = '<p>H<sub>2</sub>O</p>';
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Tiptap's subscript, taking superscript off the text it lowers.
3
+ */
4
+ export declare const Subscript: import("@tiptap/core").Mark<import("@tiptap/extension-subscript").SubscriptExtensionOptions, any>;
@@ -0,0 +1,16 @@
1
+ import { Subscript as TiptapSubscript } from '@tiptap/extension-subscript';
2
+ import { BASELINE_SHIFT, unsetOtherShifts } from './baseline-shift.js';
3
+ /**
4
+ * Tiptap's subscript, taking superscript off the text it lowers.
5
+ */
6
+ export const Subscript = TiptapSubscript.extend({
7
+ group: BASELINE_SHIFT,
8
+ excludes: BASELINE_SHIFT,
9
+ addCommands() {
10
+ return {
11
+ ...this.parent(),
12
+ setSubscript: () => ({ chain }) => unsetOtherShifts(chain(), this.type).setMark(this.type).run(),
13
+ toggleSubscript: () => ({ chain }) => unsetOtherShifts(chain(), this.type).toggleMark(this.type).run()
14
+ };
15
+ }
16
+ });
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Tiptap's superscript, taking subscript off the text it raises.
3
+ */
4
+ export declare const Superscript: import("@tiptap/core").Mark<import("@tiptap/extension-superscript").SuperscriptExtensionOptions, any>;
@@ -0,0 +1,16 @@
1
+ import { Superscript as TiptapSuperscript } from '@tiptap/extension-superscript';
2
+ import { BASELINE_SHIFT, unsetOtherShifts } from './baseline-shift.js';
3
+ /**
4
+ * Tiptap's superscript, taking subscript off the text it raises.
5
+ */
6
+ export const Superscript = TiptapSuperscript.extend({
7
+ group: BASELINE_SHIFT,
8
+ excludes: BASELINE_SHIFT,
9
+ addCommands() {
10
+ return {
11
+ ...this.parent(),
12
+ setSuperscript: () => ({ chain }) => unsetOtherShifts(chain(), this.type).setMark(this.type).run(),
13
+ toggleSuperscript: () => ({ chain }) => unsetOtherShifts(chain(), this.type).toggleMark(this.type).run()
14
+ };
15
+ }
16
+ });
@@ -1,6 +1,6 @@
1
1
  import { Extension } from '@tiptap/core';
2
- import type { ImageOptions } from '@tiptap/extension-image';
3
- import { type SuperscriptExtensionOptions } from '@tiptap/extension-superscript';
2
+ import type { SubscriptExtensionOptions } from '@tiptap/extension-subscript';
3
+ import type { SuperscriptExtensionOptions } from '@tiptap/extension-superscript';
4
4
  import { type TableKitOptions } from '@tiptap/extension-table';
5
5
  import { type TextAlignOptions } from '@tiptap/extension-text-align';
6
6
  import { type TextStyleKitOptions } from '@tiptap/extension-text-style';
@@ -10,6 +10,7 @@ import { type BlockSpacingOptions } from './block-spacing.js';
10
10
  import { type ColorOptions } from './color.js';
11
11
  import { type FontSizeOptions } from './font-size.js';
12
12
  import { type HighlightOptions } from './highlight.js';
13
+ import { type ImageOptions } from './image.js';
13
14
  import { type LineHeightOptions } from './line-height.js';
14
15
  import { type TableOptions } from './table.js';
15
16
  type Part<Options> = Partial<Options> | false;
@@ -32,6 +33,7 @@ export interface TiptapEssentialsOptions {
32
33
  blockSpacing: Part<BlockSpacingOptions>;
33
34
  textAlign: Part<TextAlignOptions>;
34
35
  superscript: Part<SuperscriptExtensionOptions>;
36
+ subscript: Part<SubscriptExtensionOptions>;
35
37
  image: Part<ImageOptions>;
36
38
  youtube: Part<YoutubeOptions>;
37
39
  plainSpaces: Record<string, never> | false;
@@ -1,5 +1,4 @@
1
1
  import { Extension } from '@tiptap/core';
2
- import Superscript, {} from '@tiptap/extension-superscript';
3
2
  import { TableKit } from '@tiptap/extension-table';
4
3
  import TextAlign, {} from '@tiptap/extension-text-align';
5
4
  import { TextStyleKit } from '@tiptap/extension-text-style';
@@ -11,6 +10,8 @@ import { Highlight } from './highlight.js';
11
10
  import { Image } from './image.js';
12
11
  import { LineHeight } from './line-height.js';
13
12
  import { PlainSpaces } from './plain-spaces.js';
13
+ import { Subscript } from './subscript.js';
14
+ import { Superscript } from './superscript.js';
14
15
  import { Table } from './table.js';
15
16
  import { TextStyle } from './text-style.js';
16
17
  import { Youtube } from './youtube.js';
@@ -32,13 +33,14 @@ export const TiptapEssentials = Extension.create({
32
33
  highlight: {},
33
34
  blockSpacing: {},
34
35
  superscript: {},
36
+ subscript: {},
35
37
  image: {},
36
38
  youtube: {},
37
39
  plainSpaces: {}
38
40
  };
39
41
  },
40
42
  addExtensions() {
41
- const { starterKit, tableKit, textStyleKit, highlight, blockSpacing, textAlign, superscript, image, youtube, plainSpaces } = this.options;
43
+ const { starterKit, tableKit, textStyleKit, highlight, blockSpacing, textAlign, superscript, subscript, image, youtube, plainSpaces } = this.options;
42
44
  const extensions = [];
43
45
  if (starterKit !== false) {
44
46
  extensions.push(StarterKit.configure(starterKit));
@@ -84,6 +86,9 @@ export const TiptapEssentials = Extension.create({
84
86
  if (superscript !== false) {
85
87
  extensions.push(Superscript.configure(superscript));
86
88
  }
89
+ if (subscript !== false) {
90
+ extensions.push(Subscript.configure(subscript));
91
+ }
87
92
  if (image !== false) {
88
93
  extensions.push(Image.configure(image));
89
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiptap-essentials",
3
- "version": "2.0.0-beta.3",
3
+ "version": "2.0.0-beta.5",
4
4
  "description": "Essential extensions of Tiptap",
5
5
  "type": "module",
6
6
  "exports": {
@@ -35,6 +35,7 @@
35
35
  "dependencies": {
36
36
  "@tiptap/extension-highlight": "^3.35.0",
37
37
  "@tiptap/extension-image": "^3.35.0",
38
+ "@tiptap/extension-subscript": "^3.35.0",
38
39
  "@tiptap/extension-superscript": "^3.35.0",
39
40
  "@tiptap/extension-table": "^3.35.0",
40
41
  "@tiptap/extension-text-align": "^3.35.0",