@tiptap/extension-image 3.9.1 → 3.10.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.
package/dist/index.cjs CHANGED
@@ -35,7 +35,8 @@ var Image = import_core.Node.create({
35
35
  return {
36
36
  inline: false,
37
37
  allowBase64: false,
38
- HTMLAttributes: {}
38
+ HTMLAttributes: {},
39
+ resize: false
39
40
  };
40
41
  },
41
42
  inline() {
@@ -88,6 +89,69 @@ var Image = import_core.Node.create({
88
89
  const title = (_f = (_e = node.attrs) == null ? void 0 : _e.title) != null ? _f : "";
89
90
  return title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})`;
90
91
  },
92
+ addNodeView() {
93
+ if (!this.options.resize || !this.options.resize.enabled || typeof document === "undefined" || !this.editor.isEditable) {
94
+ return null;
95
+ }
96
+ const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
97
+ return ({ node, getPos, HTMLAttributes }) => {
98
+ const el = document.createElement("img");
99
+ Object.entries(HTMLAttributes).forEach(([key, value]) => {
100
+ if (value != null) {
101
+ switch (key) {
102
+ case "width":
103
+ case "height":
104
+ break;
105
+ default:
106
+ el.setAttribute(key, value);
107
+ break;
108
+ }
109
+ }
110
+ });
111
+ el.src = HTMLAttributes.src;
112
+ const nodeView = new import_core.ResizableNodeview({
113
+ element: el,
114
+ node,
115
+ getPos,
116
+ onResize: (width, height) => {
117
+ el.style.width = `${width}px`;
118
+ el.style.height = `${height}px`;
119
+ },
120
+ onCommit: (width, height) => {
121
+ const pos = getPos();
122
+ if (pos === void 0) {
123
+ return;
124
+ }
125
+ this.editor.chain().setNodeSelection(pos).updateAttributes(this.name, {
126
+ width,
127
+ height
128
+ }).run();
129
+ },
130
+ onUpdate: (updatedNode, _decorations, _innerDecorations) => {
131
+ if (updatedNode.type !== node.type) {
132
+ return false;
133
+ }
134
+ return true;
135
+ },
136
+ options: {
137
+ directions,
138
+ min: {
139
+ width: minWidth,
140
+ height: minHeight
141
+ },
142
+ preserveAspectRatio: alwaysPreserveAspectRatio === true
143
+ }
144
+ });
145
+ const dom = nodeView.dom;
146
+ dom.style.visibility = "hidden";
147
+ dom.style.pointerEvents = "none";
148
+ el.onload = () => {
149
+ dom.style.visibility = "";
150
+ dom.style.pointerEvents = "";
151
+ };
152
+ return nodeView;
153
+ };
154
+ },
91
155
  addCommands() {
92
156
  return {
93
157
  setImage: (options) => ({ commands }) => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/image.ts"],"sourcesContent":["import { Image } from './image.js'\n\nexport * from './image.js'\n\nexport default Image\n","import { mergeAttributes, Node, nodeInputRule } from '@tiptap/core'\n\nexport interface ImageOptions {\n /**\n * Controls if the image node should be inline or not.\n * @default false\n * @example true\n */\n inline: boolean\n\n /**\n * Controls if base64 images are allowed. Enable this if you want to allow\n * base64 image urls in the `src` attribute.\n * @default false\n * @example true\n */\n allowBase64: boolean\n\n /**\n * HTML attributes to add to the image element.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n}\n\nexport interface SetImageOptions {\n src: string\n alt?: string\n title?: string\n width?: number\n height?: number\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n image: {\n /**\n * Add an image\n * @param options The image attributes\n * @example\n * editor\n * .commands\n * .setImage({ src: 'https://tiptap.dev/logo.png', alt: 'tiptap', title: 'tiptap logo' })\n */\n setImage: (options: SetImageOptions) => ReturnType\n }\n }\n}\n\n/**\n * Matches an image to a ![image](src \"title\") on input.\n */\nexport const inputRegex = /(?:^|\\s)(!\\[(.+|:?)]\\((\\S+)(?:(?:\\s+)[\"'](\\S+)[\"'])?\\))$/\n\n/**\n * This extension allows you to insert images.\n * @see https://www.tiptap.dev/api/nodes/image\n */\nexport const Image = Node.create<ImageOptions>({\n name: 'image',\n\n addOptions() {\n return {\n inline: false,\n allowBase64: false,\n HTMLAttributes: {},\n }\n },\n\n inline() {\n return this.options.inline\n },\n\n group() {\n return this.options.inline ? 'inline' : 'block'\n },\n\n draggable: true,\n\n addAttributes() {\n return {\n src: {\n default: null,\n },\n alt: {\n default: null,\n },\n title: {\n default: null,\n },\n width: {\n default: null,\n },\n height: {\n default: null,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: this.options.allowBase64 ? 'img[src]' : 'img[src]:not([src^=\"data:\"])',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['img', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]\n },\n\n parseMarkdown: (token, helpers) => {\n return helpers.createNode('image', {\n src: token.href,\n title: token.title,\n alt: token.text,\n })\n },\n\n renderMarkdown: node => {\n const src = node.attrs?.src ?? ''\n const alt = node.attrs?.alt ?? ''\n const title = node.attrs?.title ?? ''\n\n return title ? `![${alt}](${src} \"${title}\")` : `![${alt}](${src})`\n },\n\n addCommands() {\n return {\n setImage:\n options =>\n ({ commands }) => {\n return commands.insertContent({\n type: this.name,\n attrs: options,\n })\n },\n }\n },\n\n addInputRules() {\n return [\n nodeInputRule({\n find: inputRegex,\n type: this.type,\n getAttributes: match => {\n const [, , alt, src, title] = match\n\n return { src, alt, title }\n },\n }),\n ]\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAqD;AAqD9C,IAAM,aAAa;AAMnB,IAAM,QAAQ,iBAAK,OAAqB;AAAA,EAC7C,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,aAAa;AAAA,MACb,gBAAgB,CAAC;AAAA,IACnB;AAAA,EACF;AAAA,EAEA,SAAS;AACP,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,QAAQ;AACN,WAAO,KAAK,QAAQ,SAAS,WAAW;AAAA,EAC1C;AAAA,EAEA,WAAW;AAAA,EAEX,gBAAgB;AACd,WAAO;AAAA,MACL,KAAK;AAAA,QACH,SAAS;AAAA,MACX;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,KAAK,KAAK,KAAK,IAAI;AAE9B,iBAAO,EAAE,KAAK,KAAK,MAAM;AAAA,QAC3B;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;ADtJD,IAAO,gBAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/image.ts"],"sourcesContent":["import { Image } from './image.js'\n\nexport * from './image.js'\n\nexport default Image\n","import { mergeAttributes, Node, nodeInputRule, ResizableNodeview } from '@tiptap/core'\nimport type { ResizableNodeViewDirection } from 'packages/core/src/lib'\n\nexport interface ImageOptions {\n /**\n * Controls if the image node should be inline or not.\n * @default false\n * @example true\n */\n inline: boolean\n\n /**\n * Controls if base64 images are allowed. Enable this if you want to allow\n * base64 image urls in the `src` attribute.\n * @default false\n * @example true\n */\n allowBase64: boolean\n\n /**\n * HTML attributes to add to the image element.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n\n /**\n * Controls if the image should be resizable and how the resize is configured.\n * @default false\n * @example { directions: { top: true, right: true, bottom: true, left: true, topLeft: true, topRight: true, bottomLeft: true, bottomRight: true }, minWidth: 100, minHeight: 100 }\n */\n resize:\n | {\n enabled: boolean\n directions?: ResizableNodeViewDirection[]\n minWidth?: number\n minHeight?: number\n alwaysPreserveAspectRatio?: boolean\n }\n | false\n}\n\nexport interface SetImageOptions {\n src: string\n alt?: string\n title?: string\n width?: number\n height?: number\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n image: {\n /**\n * Add an image\n * @param options The image attributes\n * @example\n * editor\n * .commands\n * .setImage({ src: 'https://tiptap.dev/logo.png', alt: 'tiptap', title: 'tiptap logo' })\n */\n setImage: (options: SetImageOptions) => ReturnType\n }\n }\n}\n\n/**\n * Matches an image to a ![image](src \"title\") on input.\n */\nexport const inputRegex = /(?:^|\\s)(!\\[(.+|:?)]\\((\\S+)(?:(?:\\s+)[\"'](\\S+)[\"'])?\\))$/\n\n/**\n * This extension allows you to insert images.\n * @see https://www.tiptap.dev/api/nodes/image\n */\nexport const Image = Node.create<ImageOptions>({\n name: 'image',\n\n addOptions() {\n return {\n inline: false,\n allowBase64: false,\n HTMLAttributes: {},\n resize: false,\n }\n },\n\n inline() {\n return this.options.inline\n },\n\n group() {\n return this.options.inline ? 'inline' : 'block'\n },\n\n draggable: true,\n\n addAttributes() {\n return {\n src: {\n default: null,\n },\n alt: {\n default: null,\n },\n title: {\n default: null,\n },\n width: {\n default: null,\n },\n height: {\n default: null,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: this.options.allowBase64 ? 'img[src]' : 'img[src]:not([src^=\"data:\"])',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['img', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]\n },\n\n parseMarkdown: (token, helpers) => {\n return helpers.createNode('image', {\n src: token.href,\n title: token.title,\n alt: token.text,\n })\n },\n\n renderMarkdown: node => {\n const src = node.attrs?.src ?? ''\n const alt = node.attrs?.alt ?? ''\n const title = node.attrs?.title ?? ''\n\n return title ? `![${alt}](${src} \"${title}\")` : `![${alt}](${src})`\n },\n\n addNodeView() {\n if (\n !this.options.resize ||\n !this.options.resize.enabled ||\n typeof document === 'undefined' ||\n !this.editor.isEditable\n ) {\n return null\n }\n\n const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize\n\n return ({ node, getPos, HTMLAttributes }) => {\n const el = document.createElement('img')\n\n Object.entries(HTMLAttributes).forEach(([key, value]) => {\n if (value != null) {\n switch (key) {\n case 'width':\n case 'height':\n break\n default:\n el.setAttribute(key, value)\n break\n }\n }\n })\n\n el.src = HTMLAttributes.src\n\n const nodeView = new ResizableNodeview({\n element: el,\n node,\n getPos,\n onResize: (width, height) => {\n el.style.width = `${width}px`\n el.style.height = `${height}px`\n },\n onCommit: (width, height) => {\n const pos = getPos()\n if (pos === undefined) {\n return\n }\n\n this.editor\n .chain()\n .setNodeSelection(pos)\n .updateAttributes(this.name, {\n width,\n height,\n })\n .run()\n },\n onUpdate: (updatedNode, _decorations, _innerDecorations) => {\n if (updatedNode.type !== node.type) {\n return false\n }\n\n return true\n },\n options: {\n directions,\n min: {\n width: minWidth,\n height: minHeight,\n },\n preserveAspectRatio: alwaysPreserveAspectRatio === true,\n },\n })\n\n const dom = nodeView.dom as HTMLElement\n\n // when image is loaded, show the node view to get the correct dimensions\n dom.style.visibility = 'hidden'\n dom.style.pointerEvents = 'none'\n el.onload = () => {\n dom.style.visibility = ''\n dom.style.pointerEvents = ''\n }\n\n return nodeView\n }\n },\n\n addCommands() {\n return {\n setImage:\n options =>\n ({ commands }) => {\n return commands.insertContent({\n type: this.name,\n attrs: options,\n })\n },\n }\n },\n\n addInputRules() {\n return [\n nodeInputRule({\n find: inputRegex,\n type: this.type,\n getAttributes: match => {\n const [, , alt, src, title] = match\n\n return { src, alt, title }\n },\n }),\n ]\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAwE;AAqEjE,IAAM,aAAa;AAMnB,IAAM,QAAQ,iBAAK,OAAqB;AAAA,EAC7C,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,aAAa;AAAA,MACb,gBAAgB,CAAC;AAAA,MACjB,QAAQ;AAAA,IACV;AAAA,EACF;AAAA,EAEA,SAAS;AACP,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,QAAQ;AACN,WAAO,KAAK,QAAQ,SAAS,WAAW;AAAA,EAC1C;AAAA,EAEA,WAAW;AAAA,EAEX,gBAAgB;AACd,WAAO;AAAA,MACL,KAAK;AAAA,QACH,SAAS;AAAA,MACX;AAAA,MACA,KAAK;AAAA,QACH,SAAS;AAAA,MACX;AAAA,MACA,OAAO;AAAA,QACL,SAAS;AAAA,MACX;AAAA,MACA,OAAO;AAAA,QACL,SAAS;AAAA,MACX;AAAA,MACA,QAAQ;AAAA,QACN,SAAS;A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M,KAAK;AAAA,QACX,eAAe,WAAS;AACtB,gBAAM,CAAC,EAAE,EAAE,KAAK,KAAK,KAAK,IAAI;AAE9B,iBAAO,EAAE,KAAK,KAAK,MAAM;AAAA,QAC3B;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;AD3PD,IAAO,gBAAQ;","names":[]}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,14 @@
1
1
  import { Node } from '@tiptap/core';
2
2
 
3
+ /**
4
+ * Directions where resize handles can be placed
5
+ *
6
+ * @example
7
+ * - `'top'` - Top edge handle
8
+ * - `'bottom-right'` - Bottom-right corner handle
9
+ */
10
+ type ResizableNodeViewDirection = 'top' | 'right' | 'bottom' | 'left' | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
11
+
3
12
  interface ImageOptions {
4
13
  /**
5
14
  * Controls if the image node should be inline or not.
@@ -20,6 +29,18 @@ interface ImageOptions {
20
29
  * @example { class: 'foo' }
21
30
  */
22
31
  HTMLAttributes: Record<string, any>;
32
+ /**
33
+ * Controls if the image should be resizable and how the resize is configured.
34
+ * @default false
35
+ * @example { directions: { top: true, right: true, bottom: true, left: true, topLeft: true, topRight: true, bottomLeft: true, bottomRight: true }, minWidth: 100, minHeight: 100 }
36
+ */
37
+ resize: {
38
+ enabled: boolean;
39
+ directions?: ResizableNodeViewDirection[];
40
+ minWidth?: number;
41
+ minHeight?: number;
42
+ alwaysPreserveAspectRatio?: boolean;
43
+ } | false;
23
44
  }
24
45
  interface SetImageOptions {
25
46
  src: string;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,14 @@
1
1
  import { Node } from '@tiptap/core';
2
2
 
3
+ /**
4
+ * Directions where resize handles can be placed
5
+ *
6
+ * @example
7
+ * - `'top'` - Top edge handle
8
+ * - `'bottom-right'` - Bottom-right corner handle
9
+ */
10
+ type ResizableNodeViewDirection = 'top' | 'right' | 'bottom' | 'left' | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
11
+
3
12
  interface ImageOptions {
4
13
  /**
5
14
  * Controls if the image node should be inline or not.
@@ -20,6 +29,18 @@ interface ImageOptions {
20
29
  * @example { class: 'foo' }
21
30
  */
22
31
  HTMLAttributes: Record<string, any>;
32
+ /**
33
+ * Controls if the image should be resizable and how the resize is configured.
34
+ * @default false
35
+ * @example { directions: { top: true, right: true, bottom: true, left: true, topLeft: true, topRight: true, bottomLeft: true, bottomRight: true }, minWidth: 100, minHeight: 100 }
36
+ */
37
+ resize: {
38
+ enabled: boolean;
39
+ directions?: ResizableNodeViewDirection[];
40
+ minWidth?: number;
41
+ minHeight?: number;
42
+ alwaysPreserveAspectRatio?: boolean;
43
+ } | false;
23
44
  }
24
45
  interface SetImageOptions {
25
46
  src: string;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/image.ts
2
- import { mergeAttributes, Node, nodeInputRule } from "@tiptap/core";
2
+ import { mergeAttributes, Node, nodeInputRule, ResizableNodeview } from "@tiptap/core";
3
3
  var inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/;
4
4
  var Image = Node.create({
5
5
  name: "image",
@@ -7,7 +7,8 @@ var Image = Node.create({
7
7
  return {
8
8
  inline: false,
9
9
  allowBase64: false,
10
- HTMLAttributes: {}
10
+ HTMLAttributes: {},
11
+ resize: false
11
12
  };
12
13
  },
13
14
  inline() {
@@ -60,6 +61,69 @@ var Image = Node.create({
60
61
  const title = (_f = (_e = node.attrs) == null ? void 0 : _e.title) != null ? _f : "";
61
62
  return title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})`;
62
63
  },
64
+ addNodeView() {
65
+ if (!this.options.resize || !this.options.resize.enabled || typeof document === "undefined" || !this.editor.isEditable) {
66
+ return null;
67
+ }
68
+ const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
69
+ return ({ node, getPos, HTMLAttributes }) => {
70
+ const el = document.createElement("img");
71
+ Object.entries(HTMLAttributes).forEach(([key, value]) => {
72
+ if (value != null) {
73
+ switch (key) {
74
+ case "width":
75
+ case "height":
76
+ break;
77
+ default:
78
+ el.setAttribute(key, value);
79
+ break;
80
+ }
81
+ }
82
+ });
83
+ el.src = HTMLAttributes.src;
84
+ const nodeView = new ResizableNodeview({
85
+ element: el,
86
+ node,
87
+ getPos,
88
+ onResize: (width, height) => {
89
+ el.style.width = `${width}px`;
90
+ el.style.height = `${height}px`;
91
+ },
92
+ onCommit: (width, height) => {
93
+ const pos = getPos();
94
+ if (pos === void 0) {
95
+ return;
96
+ }
97
+ this.editor.chain().setNodeSelection(pos).updateAttributes(this.name, {
98
+ width,
99
+ height
100
+ }).run();
101
+ },
102
+ onUpdate: (updatedNode, _decorations, _innerDecorations) => {
103
+ if (updatedNode.type !== node.type) {
104
+ return false;
105
+ }
106
+ return true;
107
+ },
108
+ options: {
109
+ directions,
110
+ min: {
111
+ width: minWidth,
112
+ height: minHeight
113
+ },
114
+ preserveAspectRatio: alwaysPreserveAspectRatio === true
115
+ }
116
+ });
117
+ const dom = nodeView.dom;
118
+ dom.style.visibility = "hidden";
119
+ dom.style.pointerEvents = "none";
120
+ el.onload = () => {
121
+ dom.style.visibility = "";
122
+ dom.style.pointerEvents = "";
123
+ };
124
+ return nodeView;
125
+ };
126
+ },
63
127
  addCommands() {
64
128
  return {
65
129
  setImage: (options) => ({ commands }) => {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/image.ts","../src/index.ts"],"sourcesContent":["import { mergeAttributes, Node, nodeInputRule } from '@tiptap/core'\n\nexport interface ImageOptions {\n /**\n * Controls if the image node should be inline or not.\n * @default false\n * @example true\n */\n inline: boolean\n\n /**\n * Controls if base64 images are allowed. Enable this if you want to allow\n * base64 image urls in the `src` attribute.\n * @default false\n * @example true\n */\n allowBase64: boolean\n\n /**\n * HTML attributes to add to the image element.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n}\n\nexport interface SetImageOptions {\n src: string\n alt?: string\n title?: string\n width?: number\n height?: number\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n image: {\n /**\n * Add an image\n * @param options The image attributes\n * @example\n * editor\n * .commands\n * .setImage({ src: 'https://tiptap.dev/logo.png', alt: 'tiptap', title: 'tiptap logo' })\n */\n setImage: (options: SetImageOptions) => ReturnType\n }\n }\n}\n\n/**\n * Matches an image to a ![image](src \"title\") on input.\n */\nexport const inputRegex = /(?:^|\\s)(!\\[(.+|:?)]\\((\\S+)(?:(?:\\s+)[\"'](\\S+)[\"'])?\\))$/\n\n/**\n * This extension allows you to insert images.\n * @see https://www.tiptap.dev/api/nodes/image\n */\nexport const Image = Node.create<ImageOptions>({\n name: 'image',\n\n addOptions() {\n return {\n inline: false,\n allowBase64: false,\n HTMLAttributes: {},\n }\n },\n\n inline() {\n return this.options.inline\n },\n\n group() {\n return this.options.inline ? 'inline' : 'block'\n },\n\n draggable: true,\n\n addAttributes() {\n return {\n src: {\n default: null,\n },\n alt: {\n default: null,\n },\n title: {\n default: null,\n },\n width: {\n default: null,\n },\n height: {\n default: null,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: this.options.allowBase64 ? 'img[src]' : 'img[src]:not([src^=\"data:\"])',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['img', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]\n },\n\n parseMarkdown: (token, helpers) => {\n return helpers.createNode('image', {\n src: token.href,\n title: token.title,\n alt: token.text,\n })\n },\n\n renderMarkdown: node => {\n const src = node.attrs?.src ?? ''\n const alt = node.attrs?.alt ?? ''\n const title = node.attrs?.title ?? ''\n\n return title ? `![${alt}](${src} \"${title}\")` : `![${alt}](${src})`\n },\n\n addCommands() {\n return {\n setImage:\n options =>\n ({ commands }) => {\n return commands.insertContent({\n type: this.name,\n attrs: options,\n })\n },\n }\n },\n\n addInputRules() {\n return [\n nodeInputRule({\n find: inputRegex,\n type: this.type,\n getAttributes: match => {\n const [, , alt, src, title] = match\n\n return { src, alt, title }\n },\n }),\n ]\n },\n})\n","import { Image } from './image.js'\n\nexport * from './image.js'\n\nexport default Image\n"],"mappings":";AAAA,SAAS,iBAAiB,MAAM,qBAAqB;AAqD9C,IAAM,aAAa;AAMnB,IAAM,QAAQ,KAAK,OAAqB;AAAA,EAC7C,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,aAAa;AAAA,MACb,gBAAgB,CAAC;AAAA,IACnB;AAAA,EACF;AAAA,EAEA,SAAS;AACP,WAAO,KAAK,QAAQ;AAAA,EACtB;AAAA,EAEA,QAAQ;AACN,WAAO,KAAK,QAAQ,SAAS,WAAW;AAAA,EAC1C;AAAA,EAEA,WAAW;AAAA,EAEX,gBAAgB;AACd,WAAO;AAAA,MACL,KAAK;AAAA,QACH,SAAS;AAAA,MACX;AAAA,MACA,KAAK;AAAA,QACH,SAAS;AAAA,MACX;AAAA,MACA,OAAO;AAAA,QACL,SAAS;AAAA,MACX;AAAA,MACA,OAAO;AAAA,QACL,SAAS;AAAA,MACX;AAAA,MACA,QAAQ;AAAA,QACN,SAAS;AAAA,MACX;AAAA,IACF;AAAA,EACF;AAAA,EAEA,YAAY;AACV,WAAO;AAAA,MACL;AAAA,QACE,KAAK,KAAK,QAAQ,cAAc,aAAa;AAAA,MAC/C;AAAA,IACF;AAAA,EACF;AAAA,EAEA,WAAW,EAAE,eAAe,GAAG;AAC7B,WAAO,CAAC,OAAO,gBAAgB,KAAK,QAAQ,gBAAgB,cAAc,CAAC;AAAA,EAC7E;AAAA,EAEA,eAAe,CAAC,OAAO,YAAY;AACjC,WAAO,QAAQ,WAAW,SAAS;AAAA,MACjC,KAAK,MAAM;AAAA,MACX,OAAO,MAAM;AAAA,MACb,KAAK,MAAM;AAAA,IACb,CAAC;AAAA,EACH;AAAA,EAEA,gBAAgB,UAAQ;AAxH1B;AAyHI,UAAM,OAAM,gBAAK,UAAL,mBAAY,QAAZ,YAAmB;AAC/B,UAAM,OAAM,gBAAK,UAAL,mBAAY,QAAZ,YAAmB;AAC/B,UAAM,SAAQ,gBAAK,UAAL,mBAAY,UAAZ,YAAqB;AAEnC,WAAO,QAAQ,KAAK,GAAG,KAAK,GAAG,KAAK,KAAK,OAAO,KAAK,GAAG,KAAK,GAAG;AAAA,EAClE;AAAA,EAEA,cAAc;AACZ,WAAO;AAAA,MACL,UACE,aACA,CAAC,EAAE,SAAS,MAAM;AAChB,eAAO,SAAS,cAAc;AAAA,UAC5B,MAAM,KAAK;AAAA,UACX,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,IACJ;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO;AAAA,MACL,cAAc;AAAA,QACZ,MAAM;AAAA,QACN,MAAM,KAAK;AAAA,QACX,eAAe,WAAS;AACtB,gBAAM,CAAC,EAAE,EAAE,KAAK,KAAK,KAAK,IAAI;AAE9B,iBAAO,EAAE,KAAK,KAAK,MAAM;AAAA,QAC3B;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;ACtJD,IAAO,gBAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/image.ts","../src/index.ts"],"sourcesContent":["import { mergeAttributes, Node, nodeInputRule, ResizableNodeview } from '@tiptap/core'\nimport type { ResizableNodeViewDirection } from 'packages/core/src/lib'\n\nexport interface ImageOptions {\n /**\n * Controls if the image node should be inline or not.\n * @default false\n * @example true\n */\n inline: boolean\n\n /**\n * Controls if base64 images are allowed. Enable this if you want to allow\n * base64 image urls in the `src` attribute.\n * @default false\n * @example true\n */\n allowBase64: boolean\n\n /**\n * HTML attributes to add to the image element.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n\n /**\n * Controls if the image should be resizable and how the resize is configured.\n * @default false\n * @example { directions: { top: true, right: true, bottom: true, left: true, topLeft: true, topRight: true, bottomLeft: true, bottomRight: true }, minWidth: 100, minHeight: 100 }\n */\n resize:\n | {\n enabled: boolean\n directions?: ResizableNodeViewDirection[]\n minWidth?: number\n minHeight?: number\n alwaysPreserveAspectRatio?: boolean\n }\n | false\n}\n\nexport interface SetImageOptions {\n src: string\n alt?: string\n title?: string\n width?: number\n height?: number\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n image: {\n /**\n * Add an image\n * @param options The image attributes\n * @example\n * editor\n * .commands\n * .setImage({ src: 'https://tiptap.dev/logo.png', alt: 'tiptap', title: 'tiptap logo' })\n */\n setImage: (options: SetImageOptions) => ReturnType\n }\n }\n}\n\n/**\n * Matches an image to a ![image](src \"title\") on input.\n */\nexport const inputRegex = /(?:^|\\s)(!\\[(.+|:?)]\\((\\S+)(?:(?:\\s+)[\"'](\\S+)[\"'])?\\))$/\n\n/**\n * This extension allows you to insert images.\n * @see https://www.tiptap.dev/api/nodes/image\n */\nexport const Image = Node.create<ImageOptions>({\n name: 'image',\n\n addOptions() {\n return {\n inline: false,\n allowBase64: false,\n HTMLAttributes: {},\n resize: false,\n }\n },\n\n inline() {\n return this.options.inline\n },\n\n group() {\n return this.options.inline ? 'inline' : 'block'\n },\n\n draggable: true,\n\n addAttributes() {\n return {\n src: {\n default: null,\n },\n alt: {\n default: null,\n },\n title: {\n default: null,\n },\n width: {\n default: null,\n },\n height: {\n default: null,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: this.options.allowBase64 ? 'img[src]' : 'img[src]:not([src^=\"data:\"])',\n },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return ['img', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]\n },\n\n parseMarkdown: (token, helpers) => {\n return helpers.createNode('image', {\n src: token.href,\n title: token.title,\n alt: token.text,\n })\n },\n\n renderMarkdown: node => {\n const src = node.attrs?.src ?? ''\n const alt = node.attrs?.alt ?? ''\n const title = node.attrs?.title ?? ''\n\n return title ? `![${alt}](${src} \"${title}\")` : `![${alt}](${src})`\n },\n\n addNodeView() {\n if (\n !this.options.resize ||\n !this.options.resize.enabled ||\n typeof document === 'undefined' ||\n !this.editor.isEditable\n ) {\n return null\n }\n\n const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize\n\n return ({ node, getPos, HTMLAttributes }) => {\n const el = document.createElement('img')\n\n Object.entries(HTMLAttributes).forEach(([key, value]) => {\n if (value != null) {\n switch (key) {\n case 'width':\n case 'height':\n break\n default:\n el.setAttribute(key, value)\n break\n }\n }\n })\n\n el.src = HTMLAttributes.src\n\n const nodeView = new ResizableNodeview({\n element: el,\n node,\n getPos,\n onResize: (width, height) => {\n el.style.width = `${width}px`\n el.style.height = `${height}px`\n },\n onCommit: (width, height) => {\n const pos = getPos()\n if (pos === undefined) {\n return\n }\n\n this.editor\n .chain()\n .setNodeSelection(pos)\n .updateAttributes(this.name, {\n width,\n height,\n })\n .run()\n },\n onUpdate: (updatedNode, _decorations, _innerDecorations) => {\n if (updatedNode.type !== node.type) {\n return false\n }\n\n return true\n },\n options: {\n directions,\n min: {\n width: minWidth,\n height: minHeight,\n },\n preserveAspectRatio: alwaysPreserveAspectRatio === true,\n },\n })\n\n const dom = nodeView.dom as HTMLElement\n\n // when image is loaded, show the node view to get the correct dimensions\n dom.style.visibility = 'hidden'\n dom.style.pointerEvents = 'none'\n el.onload = () => {\n dom.style.visibility = ''\n dom.style.pointerEvents = ''\n }\n\n return nodeView\n }\n },\n\n addCommands() {\n return {\n setImage:\n options =>\n ({ commands }) => {\n return commands.insertContent({\n type: this.name,\n attrs: options,\n })\n },\n }\n },\n\n addInputRules() {\n return [\n nodeInputRule({\n find: inputRegex,\n type: this.type,\n getAttributes: match => {\n const [, , alt, src, title] = match\n\n return { src, alt, title }\n },\n }),\n ]\n },\n})\n","import { Image } from './image.js'\n\nexport * from './image.js'\n\nexport default Image\n"],"mappings":";AAAA,SAAS,iBAAiB,MAAM,eAAe,yBAAyB;AAqEjE,IAAM,aAAa;AAMnB,IAAM,QAAQ,KAAK,OAAqB;AAAA,EAC7C,MAAM;AAAA,EAEN,aAAa;AAC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A,QACrD;AAAA,MACF,CAAC;AAED,YAAM,MAAM,SAAS;AAGrB,UAAI,MAAM,aAAa;AACvB,UAAI,MAAM,gBAAgB;AAC1B,SAAG,SAAS,MAAM;AAChB,YAAI,MAAM,aAAa;AACvB,YAAI,MAAM,gBAAgB;AAAA,MAC5B;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAEA,cAAc;AACZ,WAAO;AAAA,MACL,UACE,aACA,CAAC,EAAE,SAAS,MAAM;AAChB,eAAO,SAAS,cAAc;AAAA,UAC5B,MAAM,KAAK;AAAA,UACX,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,IACJ;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO;AAAA,MACL,cAAc;AAAA,QACZ,MAAM;AAAA,QACN,MAAM,KAAK;AAAA,QACX,eAAe,WAAS;AACtB,gBAAM,CAAC,EAAE,EAAE,KAAK,KAAK,KAAK,IAAI;AAE9B,iBAAO,EAAE,KAAK,KAAK,MAAM;AAAA,QAC3B;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;AC3PD,IAAO,gBAAQ;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-image",
3
3
  "description": "image extension for tiptap",
4
- "version": "3.9.1",
4
+ "version": "3.10.0",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -31,10 +31,10 @@
31
31
  "dist"
32
32
  ],
33
33
  "devDependencies": {
34
- "@tiptap/core": "^3.9.1"
34
+ "@tiptap/core": "^3.10.0"
35
35
  },
36
36
  "peerDependencies": {
37
- "@tiptap/core": "^3.9.1"
37
+ "@tiptap/core": "^3.10.0"
38
38
  },
39
39
  "repository": {
40
40
  "type": "git",
package/src/image.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { mergeAttributes, Node, nodeInputRule } from '@tiptap/core'
1
+ import { mergeAttributes, Node, nodeInputRule, ResizableNodeview } from '@tiptap/core'
2
+ import type { ResizableNodeViewDirection } from 'packages/core/src/lib'
2
3
 
3
4
  export interface ImageOptions {
4
5
  /**
@@ -22,6 +23,21 @@ export interface ImageOptions {
22
23
  * @example { class: 'foo' }
23
24
  */
24
25
  HTMLAttributes: Record<string, any>
26
+
27
+ /**
28
+ * Controls if the image should be resizable and how the resize is configured.
29
+ * @default false
30
+ * @example { directions: { top: true, right: true, bottom: true, left: true, topLeft: true, topRight: true, bottomLeft: true, bottomRight: true }, minWidth: 100, minHeight: 100 }
31
+ */
32
+ resize:
33
+ | {
34
+ enabled: boolean
35
+ directions?: ResizableNodeViewDirection[]
36
+ minWidth?: number
37
+ minHeight?: number
38
+ alwaysPreserveAspectRatio?: boolean
39
+ }
40
+ | false
25
41
  }
26
42
 
27
43
  export interface SetImageOptions {
@@ -65,6 +81,7 @@ export const Image = Node.create<ImageOptions>({
65
81
  inline: false,
66
82
  allowBase64: false,
67
83
  HTMLAttributes: {},
84
+ resize: false,
68
85
  }
69
86
  },
70
87
 
@@ -126,6 +143,90 @@ export const Image = Node.create<ImageOptions>({
126
143
  return title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})`
127
144
  },
128
145
 
146
+ addNodeView() {
147
+ if (
148
+ !this.options.resize ||
149
+ !this.options.resize.enabled ||
150
+ typeof document === 'undefined' ||
151
+ !this.editor.isEditable
152
+ ) {
153
+ return null
154
+ }
155
+
156
+ const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize
157
+
158
+ return ({ node, getPos, HTMLAttributes }) => {
159
+ const el = document.createElement('img')
160
+
161
+ Object.entries(HTMLAttributes).forEach(([key, value]) => {
162
+ if (value != null) {
163
+ switch (key) {
164
+ case 'width':
165
+ case 'height':
166
+ break
167
+ default:
168
+ el.setAttribute(key, value)
169
+ break
170
+ }
171
+ }
172
+ })
173
+
174
+ el.src = HTMLAttributes.src
175
+
176
+ const nodeView = new ResizableNodeview({
177
+ element: el,
178
+ node,
179
+ getPos,
180
+ onResize: (width, height) => {
181
+ el.style.width = `${width}px`
182
+ el.style.height = `${height}px`
183
+ },
184
+ onCommit: (width, height) => {
185
+ const pos = getPos()
186
+ if (pos === undefined) {
187
+ return
188
+ }
189
+
190
+ this.editor
191
+ .chain()
192
+ .setNodeSelection(pos)
193
+ .updateAttributes(this.name, {
194
+ width,
195
+ height,
196
+ })
197
+ .run()
198
+ },
199
+ onUpdate: (updatedNode, _decorations, _innerDecorations) => {
200
+ if (updatedNode.type !== node.type) {
201
+ return false
202
+ }
203
+
204
+ return true
205
+ },
206
+ options: {
207
+ directions,
208
+ min: {
209
+ width: minWidth,
210
+ height: minHeight,
211
+ },
212
+ preserveAspectRatio: alwaysPreserveAspectRatio === true,
213
+ },
214
+ })
215
+
216
+ const dom = nodeView.dom as HTMLElement
217
+
218
+ // when image is loaded, show the node view to get the correct dimensions
219
+ dom.style.visibility = 'hidden'
220
+ dom.style.pointerEvents = 'none'
221
+ el.onload = () => {
222
+ dom.style.visibility = ''
223
+ dom.style.pointerEvents = ''
224
+ }
225
+
226
+ return nodeView
227
+ }
228
+ },
229
+
129
230
  addCommands() {
130
231
  return {
131
232
  setImage: