@tiptap/extension-image 3.27.4 → 3.29.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
@@ -94,6 +94,7 @@ var Image = import_core.Node.create({
94
94
  return null;
95
95
  }
96
96
  const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
97
+ const resizeManagedAttributes = /* @__PURE__ */ new Set(["src", "width", "height"]);
97
98
  return ({ node, getPos, HTMLAttributes, editor }) => {
98
99
  const el = document.createElement("img");
99
100
  el.draggable = false;
@@ -101,6 +102,7 @@ var Image = import_core.Node.create({
101
102
  Object.entries(mergedAttributes).forEach(([key, value]) => {
102
103
  if (value != null) {
103
104
  switch (key) {
105
+ case "src":
104
106
  case "width":
105
107
  case "height":
106
108
  break;
@@ -113,6 +115,49 @@ var Image = import_core.Node.create({
113
115
  if (mergedAttributes.src !== null) {
114
116
  el.src = mergedAttributes.src;
115
117
  }
118
+ let previousHTMLAttributes = { ...HTMLAttributes };
119
+ const syncImageSource = (src) => {
120
+ if (typeof src === "string" && src !== "") {
121
+ if (el.getAttribute("src") !== src) {
122
+ el.src = src;
123
+ }
124
+ return;
125
+ }
126
+ if (el.hasAttribute("src")) {
127
+ el.removeAttribute("src");
128
+ }
129
+ if (el.src !== "") {
130
+ el.src = "";
131
+ }
132
+ };
133
+ syncImageSource(HTMLAttributes.src);
134
+ const onUpdate = (updatedNode) => {
135
+ if (updatedNode.type !== node.type) {
136
+ return false;
137
+ }
138
+ const extensionAttributes = editor.extensionManager.attributes.filter(
139
+ (attribute) => attribute.type === updatedNode.type.name
140
+ );
141
+ const newHTMLAttributes = (0, import_core.getRenderedAttributes)(updatedNode, extensionAttributes);
142
+ Object.keys(previousHTMLAttributes).forEach((key) => {
143
+ if (!resizeManagedAttributes.has(key) && !(key in newHTMLAttributes)) {
144
+ el.removeAttribute(key);
145
+ }
146
+ });
147
+ Object.entries(newHTMLAttributes).forEach(([key, value]) => {
148
+ if (resizeManagedAttributes.has(key)) {
149
+ return;
150
+ }
151
+ if (value != null) {
152
+ el.setAttribute(key, value);
153
+ } else {
154
+ el.removeAttribute(key);
155
+ }
156
+ });
157
+ syncImageSource(newHTMLAttributes.src);
158
+ previousHTMLAttributes = newHTMLAttributes;
159
+ return true;
160
+ };
116
161
  const nodeView = new import_core.ResizableNodeView({
117
162
  element: el,
118
163
  editor,
@@ -132,12 +177,7 @@ var Image = import_core.Node.create({
132
177
  height
133
178
  }).run();
134
179
  },
135
- onUpdate: (updatedNode, _decorations, _innerDecorations) => {
136
- if (updatedNode.type !== node.type) {
137
- return false;
138
- }
139
- return true;
140
- },
180
+ onUpdate,
141
181
  options: {
142
182
  directions,
143
183
  min: {
@@ -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 type { ResizableNodeViewDirection } from '@tiptap/core'\nimport { mergeAttributes, Node, nodeInputRule, ResizableNodeView } 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 /**\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 (!this.options.resize || !this.options.resize.enabled || typeof document === 'undefined') {\n return null\n }\n\n const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize\n\n return ({ node, getPos, HTMLAttributes, editor }) => {\n const el = document.createElement('img')\n el.draggable = false\n\n const mergedAttributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)\n\n Object.entries(mergedAttributes).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 if (mergedAttributes.src !== null) {\n el.src = mergedAttributes.src\n }\n\n const nodeView = new ResizableNodeView({\n element: el,\n editor,\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;;;ACCA,kBAAwE;AAoEjE,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,EAE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8B;AAAA,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,UACL,2BAAc;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;;;AD5PD,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 type { ResizableNodeViewDirection, ResizableNodeViewOptions } from '@tiptap/core'\nimport {\n getRenderedAttributes,\n mergeAttributes,\n Node,\n nodeInputRule,\n ResizableNodeView,\n} 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 /**\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 (!this.options.resize || !this.options.resize.enabled || typeof document === 'undefined') {\n return null\n }\n\n const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize\n const resizeManagedAttributes = new Set(['src', 'width', 'height'])\n\n return ({ node, getPos, HTMLAttributes, editor }) => {\n const el = document.createElement('img')\n el.draggable = false\n\n const mergedAttributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)\n\n Object.entries(mergedAttributes).forEach(([key, value]) => {\n if (value != null) {\n switch (key) {\n case 'src':\n case 'width':\n case 'height':\n break\n default:\n el.setAttribute(key, value)\n break\n }\n }\n })\n\n if (mergedAttributes.src !== null) {\n el.src = mergedAttributes.src\n }\n\n let previousHTMLAttributes = { ...HTMLAttributes }\n const syncImageSource = (src: unknown) => {\n if (typeof src === 'string' && src !== '') {\n if (el.getAttribute('src') !== src) {\n el.src = src\n }\n\n return\n }\n\n if (el.hasAttribute('src')) {\n el.removeAttribute('src')\n }\n\n if (el.src !== '') {\n el.src = ''\n }\n }\n\n syncImageSource(HTMLAttributes.src)\n\n const onUpdate: ResizableNodeViewOptions['onUpdate'] = (\n updatedNode: Parameters<typeof getRenderedAttributes>[0],\n ) => {\n if (updatedNode.type !== node.type) {\n return false\n }\n\n const extensionAttributes = editor.extensionManager.attributes.filter(\n attribute => attribute.type === updatedNode.type.name,\n )\n const newHTMLAttributes = getRenderedAttributes(updatedNode, extensionAttributes)\n\n // Remove attributes that were previously rendered but are no longer present\n Object.keys(previousHTMLAttributes).forEach(key => {\n if (!resizeManagedAttributes.has(key) && !(key in newHTMLAttributes)) {\n el.removeAttribute(key)\n }\n })\n\n Object.entries(newHTMLAttributes).forEach(([key, value]) => {\n if (resizeManagedAttributes.has(key)) {\n return\n }\n\n if (value != null) {\n el.setAttribute(key, value)\n } else {\n el.removeAttribute(key)\n }\n })\n\n syncImageSource(newHTMLAttributes.src)\n\n previousHTMLAttributes = newHTMLAttributes\n\n return true\n }\n\n const nodeView = new ResizableNodeView({\n element: el,\n editor,\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,\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;;;ACCA,kBAMO;AAoEA,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,SA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AAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,IACJ;AAAA,EACF;AAAA,EAEA,gBAAgB;AACd,WAAO;AAAA,UACL,2BAAc;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;;;ADzTD,IAAO,gBAAQ;","names":[]}
package/dist/index.js CHANGED
@@ -1,5 +1,11 @@
1
1
  // src/image.ts
2
- import { mergeAttributes, Node, nodeInputRule, ResizableNodeView } from "@tiptap/core";
2
+ import {
3
+ getRenderedAttributes,
4
+ mergeAttributes,
5
+ Node,
6
+ nodeInputRule,
7
+ ResizableNodeView
8
+ } from "@tiptap/core";
3
9
  var inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/;
4
10
  var Image = Node.create({
5
11
  name: "image",
@@ -66,6 +72,7 @@ var Image = Node.create({
66
72
  return null;
67
73
  }
68
74
  const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
75
+ const resizeManagedAttributes = /* @__PURE__ */ new Set(["src", "width", "height"]);
69
76
  return ({ node, getPos, HTMLAttributes, editor }) => {
70
77
  const el = document.createElement("img");
71
78
  el.draggable = false;
@@ -73,6 +80,7 @@ var Image = Node.create({
73
80
  Object.entries(mergedAttributes).forEach(([key, value]) => {
74
81
  if (value != null) {
75
82
  switch (key) {
83
+ case "src":
76
84
  case "width":
77
85
  case "height":
78
86
  break;
@@ -85,6 +93,49 @@ var Image = Node.create({
85
93
  if (mergedAttributes.src !== null) {
86
94
  el.src = mergedAttributes.src;
87
95
  }
96
+ let previousHTMLAttributes = { ...HTMLAttributes };
97
+ const syncImageSource = (src) => {
98
+ if (typeof src === "string" && src !== "") {
99
+ if (el.getAttribute("src") !== src) {
100
+ el.src = src;
101
+ }
102
+ return;
103
+ }
104
+ if (el.hasAttribute("src")) {
105
+ el.removeAttribute("src");
106
+ }
107
+ if (el.src !== "") {
108
+ el.src = "";
109
+ }
110
+ };
111
+ syncImageSource(HTMLAttributes.src);
112
+ const onUpdate = (updatedNode) => {
113
+ if (updatedNode.type !== node.type) {
114
+ return false;
115
+ }
116
+ const extensionAttributes = editor.extensionManager.attributes.filter(
117
+ (attribute) => attribute.type === updatedNode.type.name
118
+ );
119
+ const newHTMLAttributes = getRenderedAttributes(updatedNode, extensionAttributes);
120
+ Object.keys(previousHTMLAttributes).forEach((key) => {
121
+ if (!resizeManagedAttributes.has(key) && !(key in newHTMLAttributes)) {
122
+ el.removeAttribute(key);
123
+ }
124
+ });
125
+ Object.entries(newHTMLAttributes).forEach(([key, value]) => {
126
+ if (resizeManagedAttributes.has(key)) {
127
+ return;
128
+ }
129
+ if (value != null) {
130
+ el.setAttribute(key, value);
131
+ } else {
132
+ el.removeAttribute(key);
133
+ }
134
+ });
135
+ syncImageSource(newHTMLAttributes.src);
136
+ previousHTMLAttributes = newHTMLAttributes;
137
+ return true;
138
+ };
88
139
  const nodeView = new ResizableNodeView({
89
140
  element: el,
90
141
  editor,
@@ -104,12 +155,7 @@ var Image = Node.create({
104
155
  height
105
156
  }).run();
106
157
  },
107
- onUpdate: (updatedNode, _decorations, _innerDecorations) => {
108
- if (updatedNode.type !== node.type) {
109
- return false;
110
- }
111
- return true;
112
- },
158
+ onUpdate,
113
159
  options: {
114
160
  directions,
115
161
  min: {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/image.ts","../src/index.ts"],"sourcesContent":["import type { ResizableNodeViewDirection } from '@tiptap/core'\nimport { mergeAttributes, Node, nodeInputRule, ResizableNodeView } 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 /**\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 (!this.options.resize || !this.options.resize.enabled || typeof document === 'undefined') {\n return null\n }\n\n const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize\n\n return ({ node, getPos, HTMLAttributes, editor }) => {\n const el = document.createElement('img')\n el.draggable = false\n\n const mergedAttributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)\n\n Object.entries(mergedAttributes).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 if (mergedAttributes.src !== null) {\n el.src = mergedAttributes.src\n }\n\n const nodeView = new ResizableNodeView({\n element: el,\n editor,\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":";AACA,SAAS,iBAAiB,MAAM,eAAe,yBAAyB;AAoEjE,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,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;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,OAA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ts","../src/index.ts"],"sourcesContent":["import type { ResizableNodeViewDirection, ResizableNodeViewOptions } from '@tiptap/core'\nimport {\n getRenderedAttributes,\n mergeAttributes,\n Node,\n nodeInputRule,\n ResizableNodeView,\n} 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 /**\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 (!this.options.resize || !this.options.resize.enabled || typeof document === 'undefined') {\n return null\n }\n\n const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize\n const resizeManagedAttributes = new Set(['src', 'width', 'height'])\n\n return ({ node, getPos, HTMLAttributes, editor }) => {\n const el = document.createElement('img')\n el.draggable = false\n\n const mergedAttributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)\n\n Object.entries(mergedAttributes).forEach(([key, value]) => {\n if (value != null) {\n switch (key) {\n case 'src':\n case 'width':\n case 'height':\n break\n default:\n el.setAttribute(key, value)\n break\n }\n }\n })\n\n if (mergedAttributes.src !== null) {\n el.src = mergedAttributes.src\n }\n\n let previousHTMLAttributes = { ...HTMLAttributes }\n const syncImageSource = (src: unknown) => {\n if (typeof src === 'string' && src !== '') {\n if (el.getAttribute('src') !== src) {\n el.src = src\n }\n\n return\n }\n\n if (el.hasAttribute('src')) {\n el.removeAttribute('src')\n }\n\n if (el.src !== '') {\n el.src = ''\n }\n }\n\n syncImageSource(HTMLAttributes.src)\n\n const onUpdate: ResizableNodeViewOptions['onUpdate'] = (\n updatedNode: Parameters<typeof getRenderedAttributes>[0],\n ) => {\n if (updatedNode.type !== node.type) {\n return false\n }\n\n const extensionAttributes = editor.extensionManager.attributes.filter(\n attribute => attribute.type === updatedNode.type.name,\n )\n const newHTMLAttributes = getRenderedAttributes(updatedNode, extensionAttributes)\n\n // Remove attributes that were previously rendered but are no longer present\n Object.keys(previousHTMLAttributes).forEach(key => {\n if (!resizeManagedAttributes.has(key) && !(key in newHTMLAttributes)) {\n el.removeAttribute(key)\n }\n })\n\n Object.entries(newHTMLAttributes).forEach(([key, value]) => {\n if (resizeManagedAttributes.has(key)) {\n return\n }\n\n if (value != null) {\n el.setAttribute(key, value)\n } else {\n el.removeAttribute(key)\n }\n })\n\n syncImageSource(newHTMLAttributes.src)\n\n previousHTMLAttributes = newHTMLAttributes\n\n return true\n }\n\n const nodeView = new ResizableNodeView({\n element: el,\n editor,\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,\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":";AAC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A,SAAS;AAAA,UACP;AAAA,UACA,KAAK;AAAA,YACH,OAAO;AAAA,YACP,QAAQ;AAAA,UACV;AAAA,UACA,qBAAqB,8BAA8B;AAAA,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;;;ACzTD,IAAO,gBAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiptap/extension-image",
3
- "version": "3.27.4",
3
+ "version": "3.29.0",
4
4
  "description": "image extension for tiptap",
5
5
  "keywords": [
6
6
  "tiptap",
@@ -39,10 +39,10 @@
39
39
  }
40
40
  },
41
41
  "devDependencies": {
42
- "@tiptap/core": "^3.27.4"
42
+ "@tiptap/core": "^3.29.0"
43
43
  },
44
44
  "peerDependencies": {
45
- "@tiptap/core": "3.27.4"
45
+ "@tiptap/core": "3.29.0"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup"
package/src/image.ts CHANGED
@@ -1,5 +1,11 @@
1
- import type { ResizableNodeViewDirection } from '@tiptap/core'
2
- import { mergeAttributes, Node, nodeInputRule, ResizableNodeView } from '@tiptap/core'
1
+ import type { ResizableNodeViewDirection, ResizableNodeViewOptions } from '@tiptap/core'
2
+ import {
3
+ getRenderedAttributes,
4
+ mergeAttributes,
5
+ Node,
6
+ nodeInputRule,
7
+ ResizableNodeView,
8
+ } from '@tiptap/core'
3
9
 
4
10
  export interface ImageOptions {
5
11
  /**
@@ -149,6 +155,7 @@ export const Image = Node.create<ImageOptions>({
149
155
  }
150
156
 
151
157
  const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize
158
+ const resizeManagedAttributes = new Set(['src', 'width', 'height'])
152
159
 
153
160
  return ({ node, getPos, HTMLAttributes, editor }) => {
154
161
  const el = document.createElement('img')
@@ -159,6 +166,7 @@ export const Image = Node.create<ImageOptions>({
159
166
  Object.entries(mergedAttributes).forEach(([key, value]) => {
160
167
  if (value != null) {
161
168
  switch (key) {
169
+ case 'src':
162
170
  case 'width':
163
171
  case 'height':
164
172
  break
@@ -173,6 +181,65 @@ export const Image = Node.create<ImageOptions>({
173
181
  el.src = mergedAttributes.src
174
182
  }
175
183
 
184
+ let previousHTMLAttributes = { ...HTMLAttributes }
185
+ const syncImageSource = (src: unknown) => {
186
+ if (typeof src === 'string' && src !== '') {
187
+ if (el.getAttribute('src') !== src) {
188
+ el.src = src
189
+ }
190
+
191
+ return
192
+ }
193
+
194
+ if (el.hasAttribute('src')) {
195
+ el.removeAttribute('src')
196
+ }
197
+
198
+ if (el.src !== '') {
199
+ el.src = ''
200
+ }
201
+ }
202
+
203
+ syncImageSource(HTMLAttributes.src)
204
+
205
+ const onUpdate: ResizableNodeViewOptions['onUpdate'] = (
206
+ updatedNode: Parameters<typeof getRenderedAttributes>[0],
207
+ ) => {
208
+ if (updatedNode.type !== node.type) {
209
+ return false
210
+ }
211
+
212
+ const extensionAttributes = editor.extensionManager.attributes.filter(
213
+ attribute => attribute.type === updatedNode.type.name,
214
+ )
215
+ const newHTMLAttributes = getRenderedAttributes(updatedNode, extensionAttributes)
216
+
217
+ // Remove attributes that were previously rendered but are no longer present
218
+ Object.keys(previousHTMLAttributes).forEach(key => {
219
+ if (!resizeManagedAttributes.has(key) && !(key in newHTMLAttributes)) {
220
+ el.removeAttribute(key)
221
+ }
222
+ })
223
+
224
+ Object.entries(newHTMLAttributes).forEach(([key, value]) => {
225
+ if (resizeManagedAttributes.has(key)) {
226
+ return
227
+ }
228
+
229
+ if (value != null) {
230
+ el.setAttribute(key, value)
231
+ } else {
232
+ el.removeAttribute(key)
233
+ }
234
+ })
235
+
236
+ syncImageSource(newHTMLAttributes.src)
237
+
238
+ previousHTMLAttributes = newHTMLAttributes
239
+
240
+ return true
241
+ }
242
+
176
243
  const nodeView = new ResizableNodeView({
177
244
  element: el,
178
245
  editor,
@@ -197,13 +264,7 @@ export const Image = Node.create<ImageOptions>({
197
264
  })
198
265
  .run()
199
266
  },
200
- onUpdate: (updatedNode, _decorations, _innerDecorations) => {
201
- if (updatedNode.type !== node.type) {
202
- return false
203
- }
204
-
205
- return true
206
- },
267
+ onUpdate,
207
268
  options: {
208
269
  directions,
209
270
  min: {