@tiptap/extension-image 3.20.3 → 3.20.4

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 ADDED
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Image: () => Image,
24
+ default: () => index_default,
25
+ inputRegex: () => inputRegex
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/image.ts
30
+ var import_core = require("@tiptap/core");
31
+ var inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/;
32
+ var Image = import_core.Node.create({
33
+ name: "image",
34
+ addOptions() {
35
+ return {
36
+ inline: false,
37
+ allowBase64: false,
38
+ HTMLAttributes: {},
39
+ resize: false
40
+ };
41
+ },
42
+ inline() {
43
+ return this.options.inline;
44
+ },
45
+ group() {
46
+ return this.options.inline ? "inline" : "block";
47
+ },
48
+ draggable: true,
49
+ addAttributes() {
50
+ return {
51
+ src: {
52
+ default: null
53
+ },
54
+ alt: {
55
+ default: null
56
+ },
57
+ title: {
58
+ default: null
59
+ },
60
+ width: {
61
+ default: null
62
+ },
63
+ height: {
64
+ default: null
65
+ }
66
+ };
67
+ },
68
+ parseHTML() {
69
+ return [
70
+ {
71
+ tag: this.options.allowBase64 ? "img[src]" : 'img[src]:not([src^="data:"])'
72
+ }
73
+ ];
74
+ },
75
+ renderHTML({ HTMLAttributes }) {
76
+ return ["img", (0, import_core.mergeAttributes)(this.options.HTMLAttributes, HTMLAttributes)];
77
+ },
78
+ parseMarkdown: (token, helpers) => {
79
+ return helpers.createNode("image", {
80
+ src: token.href,
81
+ title: token.title,
82
+ alt: token.text
83
+ });
84
+ },
85
+ renderMarkdown: (node) => {
86
+ var _a, _b, _c, _d, _e, _f;
87
+ const src = (_b = (_a = node.attrs) == null ? void 0 : _a.src) != null ? _b : "";
88
+ const alt = (_d = (_c = node.attrs) == null ? void 0 : _c.alt) != null ? _d : "";
89
+ const title = (_f = (_e = node.attrs) == null ? void 0 : _e.title) != null ? _f : "";
90
+ return title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})`;
91
+ },
92
+ addNodeView() {
93
+ if (!this.options.resize || !this.options.resize.enabled || typeof document === "undefined") {
94
+ return null;
95
+ }
96
+ const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
97
+ return ({ node, getPos, HTMLAttributes, editor }) => {
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
+ editor,
115
+ node,
116
+ getPos,
117
+ onResize: (width, height) => {
118
+ el.style.width = `${width}px`;
119
+ el.style.height = `${height}px`;
120
+ },
121
+ onCommit: (width, height) => {
122
+ const pos = getPos();
123
+ if (pos === void 0) {
124
+ return;
125
+ }
126
+ this.editor.chain().setNodeSelection(pos).updateAttributes(this.name, {
127
+ width,
128
+ height
129
+ }).run();
130
+ },
131
+ onUpdate: (updatedNode, _decorations, _innerDecorations) => {
132
+ if (updatedNode.type !== node.type) {
133
+ return false;
134
+ }
135
+ return true;
136
+ },
137
+ options: {
138
+ directions,
139
+ min: {
140
+ width: minWidth,
141
+ height: minHeight
142
+ },
143
+ preserveAspectRatio: alwaysPreserveAspectRatio === true
144
+ }
145
+ });
146
+ const dom = nodeView.dom;
147
+ dom.style.visibility = "hidden";
148
+ dom.style.pointerEvents = "none";
149
+ el.onload = () => {
150
+ dom.style.visibility = "";
151
+ dom.style.pointerEvents = "";
152
+ };
153
+ return nodeView;
154
+ };
155
+ },
156
+ addCommands() {
157
+ return {
158
+ setImage: (options) => ({ commands }) => {
159
+ return commands.insertContent({
160
+ type: this.name,
161
+ attrs: options
162
+ });
163
+ }
164
+ };
165
+ },
166
+ addInputRules() {
167
+ return [
168
+ (0, import_core.nodeInputRule)({
169
+ find: inputRegex,
170
+ type: this.type,
171
+ getAttributes: (match) => {
172
+ const [, , alt, src, title] = match;
173
+ return { src, alt, title };
174
+ }
175
+ })
176
+ ];
177
+ }
178
+ });
179
+
180
+ // src/index.ts
181
+ var index_default = Image;
182
+ // Annotate the CommonJS export names for ESM import in node:
183
+ 0 && (module.exports = {
184
+ Image,
185
+ inputRegex
186
+ });
187
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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\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 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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AAI,YAAY,SAAS,KAAK,MAAM;AAClC,mBAAO;AAAA,UACT;AAEA,iBAAO;AAAA,QACT;AAAA,QACA,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,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;;;ADvPD,IAAO,gBAAQ;","names":[]}
@@ -0,0 +1,68 @@
1
+ import { Node, ResizableNodeViewDirection } from '@tiptap/core';
2
+
3
+ interface ImageOptions {
4
+ /**
5
+ * Controls if the image node should be inline or not.
6
+ * @default false
7
+ * @example true
8
+ */
9
+ inline: boolean;
10
+ /**
11
+ * Controls if base64 images are allowed. Enable this if you want to allow
12
+ * base64 image urls in the `src` attribute.
13
+ * @default false
14
+ * @example true
15
+ */
16
+ allowBase64: boolean;
17
+ /**
18
+ * HTML attributes to add to the image element.
19
+ * @default {}
20
+ * @example { class: 'foo' }
21
+ */
22
+ HTMLAttributes: Record<string, any>;
23
+ /**
24
+ * Controls if the image should be resizable and how the resize is configured.
25
+ * @default false
26
+ * @example { directions: { top: true, right: true, bottom: true, left: true, topLeft: true, topRight: true, bottomLeft: true, bottomRight: true }, minWidth: 100, minHeight: 100 }
27
+ */
28
+ resize: {
29
+ enabled: boolean;
30
+ directions?: ResizableNodeViewDirection[];
31
+ minWidth?: number;
32
+ minHeight?: number;
33
+ alwaysPreserveAspectRatio?: boolean;
34
+ } | false;
35
+ }
36
+ interface SetImageOptions {
37
+ src: string;
38
+ alt?: string;
39
+ title?: string;
40
+ width?: number;
41
+ height?: number;
42
+ }
43
+ declare module '@tiptap/core' {
44
+ interface Commands<ReturnType> {
45
+ image: {
46
+ /**
47
+ * Add an image
48
+ * @param options The image attributes
49
+ * @example
50
+ * editor
51
+ * .commands
52
+ * .setImage({ src: 'https://tiptap.dev/logo.png', alt: 'tiptap', title: 'tiptap logo' })
53
+ */
54
+ setImage: (options: SetImageOptions) => ReturnType;
55
+ };
56
+ }
57
+ }
58
+ /**
59
+ * Matches an image to a ![image](src "title") on input.
60
+ */
61
+ declare const inputRegex: RegExp;
62
+ /**
63
+ * This extension allows you to insert images.
64
+ * @see https://www.tiptap.dev/api/nodes/image
65
+ */
66
+ declare const Image: Node<ImageOptions, any>;
67
+
68
+ export { Image, type ImageOptions, type SetImageOptions, Image as default, inputRegex };
@@ -0,0 +1,68 @@
1
+ import { Node, ResizableNodeViewDirection } from '@tiptap/core';
2
+
3
+ interface ImageOptions {
4
+ /**
5
+ * Controls if the image node should be inline or not.
6
+ * @default false
7
+ * @example true
8
+ */
9
+ inline: boolean;
10
+ /**
11
+ * Controls if base64 images are allowed. Enable this if you want to allow
12
+ * base64 image urls in the `src` attribute.
13
+ * @default false
14
+ * @example true
15
+ */
16
+ allowBase64: boolean;
17
+ /**
18
+ * HTML attributes to add to the image element.
19
+ * @default {}
20
+ * @example { class: 'foo' }
21
+ */
22
+ HTMLAttributes: Record<string, any>;
23
+ /**
24
+ * Controls if the image should be resizable and how the resize is configured.
25
+ * @default false
26
+ * @example { directions: { top: true, right: true, bottom: true, left: true, topLeft: true, topRight: true, bottomLeft: true, bottomRight: true }, minWidth: 100, minHeight: 100 }
27
+ */
28
+ resize: {
29
+ enabled: boolean;
30
+ directions?: ResizableNodeViewDirection[];
31
+ minWidth?: number;
32
+ minHeight?: number;
33
+ alwaysPreserveAspectRatio?: boolean;
34
+ } | false;
35
+ }
36
+ interface SetImageOptions {
37
+ src: string;
38
+ alt?: string;
39
+ title?: string;
40
+ width?: number;
41
+ height?: number;
42
+ }
43
+ declare module '@tiptap/core' {
44
+ interface Commands<ReturnType> {
45
+ image: {
46
+ /**
47
+ * Add an image
48
+ * @param options The image attributes
49
+ * @example
50
+ * editor
51
+ * .commands
52
+ * .setImage({ src: 'https://tiptap.dev/logo.png', alt: 'tiptap', title: 'tiptap logo' })
53
+ */
54
+ setImage: (options: SetImageOptions) => ReturnType;
55
+ };
56
+ }
57
+ }
58
+ /**
59
+ * Matches an image to a ![image](src "title") on input.
60
+ */
61
+ declare const inputRegex: RegExp;
62
+ /**
63
+ * This extension allows you to insert images.
64
+ * @see https://www.tiptap.dev/api/nodes/image
65
+ */
66
+ declare const Image: Node<ImageOptions, any>;
67
+
68
+ export { Image, type ImageOptions, type SetImageOptions, Image as default, inputRegex };
package/dist/index.js ADDED
@@ -0,0 +1,159 @@
1
+ // src/image.ts
2
+ import { mergeAttributes, Node, nodeInputRule, ResizableNodeView } from "@tiptap/core";
3
+ var inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/;
4
+ var Image = Node.create({
5
+ name: "image",
6
+ addOptions() {
7
+ return {
8
+ inline: false,
9
+ allowBase64: false,
10
+ HTMLAttributes: {},
11
+ resize: false
12
+ };
13
+ },
14
+ inline() {
15
+ return this.options.inline;
16
+ },
17
+ group() {
18
+ return this.options.inline ? "inline" : "block";
19
+ },
20
+ draggable: true,
21
+ addAttributes() {
22
+ return {
23
+ src: {
24
+ default: null
25
+ },
26
+ alt: {
27
+ default: null
28
+ },
29
+ title: {
30
+ default: null
31
+ },
32
+ width: {
33
+ default: null
34
+ },
35
+ height: {
36
+ default: null
37
+ }
38
+ };
39
+ },
40
+ parseHTML() {
41
+ return [
42
+ {
43
+ tag: this.options.allowBase64 ? "img[src]" : 'img[src]:not([src^="data:"])'
44
+ }
45
+ ];
46
+ },
47
+ renderHTML({ HTMLAttributes }) {
48
+ return ["img", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)];
49
+ },
50
+ parseMarkdown: (token, helpers) => {
51
+ return helpers.createNode("image", {
52
+ src: token.href,
53
+ title: token.title,
54
+ alt: token.text
55
+ });
56
+ },
57
+ renderMarkdown: (node) => {
58
+ var _a, _b, _c, _d, _e, _f;
59
+ const src = (_b = (_a = node.attrs) == null ? void 0 : _a.src) != null ? _b : "";
60
+ const alt = (_d = (_c = node.attrs) == null ? void 0 : _c.alt) != null ? _d : "";
61
+ const title = (_f = (_e = node.attrs) == null ? void 0 : _e.title) != null ? _f : "";
62
+ return title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})`;
63
+ },
64
+ addNodeView() {
65
+ if (!this.options.resize || !this.options.resize.enabled || typeof document === "undefined") {
66
+ return null;
67
+ }
68
+ const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
69
+ return ({ node, getPos, HTMLAttributes, editor }) => {
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
+ editor,
87
+ node,
88
+ getPos,
89
+ onResize: (width, height) => {
90
+ el.style.width = `${width}px`;
91
+ el.style.height = `${height}px`;
92
+ },
93
+ onCommit: (width, height) => {
94
+ const pos = getPos();
95
+ if (pos === void 0) {
96
+ return;
97
+ }
98
+ this.editor.chain().setNodeSelection(pos).updateAttributes(this.name, {
99
+ width,
100
+ height
101
+ }).run();
102
+ },
103
+ onUpdate: (updatedNode, _decorations, _innerDecorations) => {
104
+ if (updatedNode.type !== node.type) {
105
+ return false;
106
+ }
107
+ return true;
108
+ },
109
+ options: {
110
+ directions,
111
+ min: {
112
+ width: minWidth,
113
+ height: minHeight
114
+ },
115
+ preserveAspectRatio: alwaysPreserveAspectRatio === true
116
+ }
117
+ });
118
+ const dom = nodeView.dom;
119
+ dom.style.visibility = "hidden";
120
+ dom.style.pointerEvents = "none";
121
+ el.onload = () => {
122
+ dom.style.visibility = "";
123
+ dom.style.pointerEvents = "";
124
+ };
125
+ return nodeView;
126
+ };
127
+ },
128
+ addCommands() {
129
+ return {
130
+ setImage: (options) => ({ commands }) => {
131
+ return commands.insertContent({
132
+ type: this.name,
133
+ attrs: options
134
+ });
135
+ }
136
+ };
137
+ },
138
+ addInputRules() {
139
+ return [
140
+ nodeInputRule({
141
+ find: inputRegex,
142
+ type: this.type,
143
+ getAttributes: (match) => {
144
+ const [, , alt, src, title] = match;
145
+ return { src, alt, title };
146
+ }
147
+ })
148
+ ];
149
+ }
150
+ });
151
+
152
+ // src/index.ts
153
+ var index_default = Image;
154
+ export {
155
+ Image,
156
+ index_default as default,
157
+ inputRegex
158
+ };
159
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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\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 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,OAAO,MAAM;AAAA,MACb,KAAK,MAAM;AAAA,IACb,CAAC;AAAA,EACH;AAAA,EAEA,gBAAgB,UAAQ;AAzI1B;AA0II,UAAM,OAAM,gBAAK,UAAL,mBAAY,QAAZ,YAA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cAAc,sBAAsB;AAC1D,cAAI,YAAY,SAAS,KAAK,MAAM;AAClC,mBAAO;AAAA,UACT;AAEA,iBAAO;AAAA,QACT;AAAA,QACA,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;;;ACvPD,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.20.3",
4
+ "version": "3.20.4",
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.20.3"
34
+ "@tiptap/core": "^3.20.4"
35
35
  },
36
36
  "peerDependencies": {
37
- "@tiptap/core": "^3.20.3"
37
+ "@tiptap/core": "^3.20.4"
38
38
  },
39
39
  "repository": {
40
40
  "type": "git",