@tiptap/extension-image 3.30.1 → 3.30.3

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
@@ -1,231 +1,182 @@
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
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
26
4
  });
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
- const resizeManagedAttributes = /* @__PURE__ */ new Set(["src", "width", "height"]);
98
- return ({ node, getPos, HTMLAttributes, editor }) => {
99
- const el = document.createElement("img");
100
- el.draggable = false;
101
- const mergedAttributes = (0, import_core.mergeAttributes)(this.options.HTMLAttributes, HTMLAttributes);
102
- Object.entries(mergedAttributes).forEach(([key, value]) => {
103
- if (value != null) {
104
- switch (key) {
105
- case "src":
106
- case "width":
107
- case "height":
108
- break;
109
- default:
110
- el.setAttribute(key, value);
111
- break;
112
- }
113
- }
114
- });
115
- if (mergedAttributes.src !== null) {
116
- el.src = mergedAttributes.src;
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
- };
161
- const nodeView = new import_core.ResizableNodeView({
162
- element: el,
163
- editor,
164
- node,
165
- getPos,
166
- onResize: (width, height) => {
167
- el.style.width = `${width}px`;
168
- el.style.height = `${height}px`;
169
- },
170
- onCommit: (width, height) => {
171
- const pos = getPos();
172
- if (pos === void 0) {
173
- return;
174
- }
175
- this.editor.chain().setNodeSelection(pos).updateAttributes(this.name, {
176
- width,
177
- height
178
- }).run();
179
- },
180
- onUpdate,
181
- options: {
182
- directions,
183
- min: {
184
- width: minWidth,
185
- height: minHeight
186
- },
187
- preserveAspectRatio: alwaysPreserveAspectRatio === true
188
- }
189
- });
190
- const dom = nodeView.dom;
191
- dom.style.visibility = "hidden";
192
- dom.style.pointerEvents = "none";
193
- el.onload = () => {
194
- dom.style.visibility = "";
195
- dom.style.pointerEvents = "";
196
- };
197
- return nodeView;
198
- };
199
- },
200
- addCommands() {
201
- return {
202
- setImage: (options) => ({ commands }) => {
203
- return commands.insertContent({
204
- type: this.name,
205
- attrs: options
206
- });
207
- }
208
- };
209
- },
210
- addInputRules() {
211
- return [
212
- (0, import_core.nodeInputRule)({
213
- find: inputRegex,
214
- type: this.type,
215
- getAttributes: (match) => {
216
- const [, , alt, src, title] = match;
217
- return { src, alt, title };
218
- }
219
- })
220
- ];
221
- }
5
+ let _tiptap_core = require("@tiptap/core");
6
+ //#region src/image.ts
7
+ /**
8
+ * Matches an image to a ![image](src "title") on input.
9
+ */
10
+ const inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/;
11
+ /**
12
+ * This extension allows you to insert images.
13
+ * @see https://www.tiptap.dev/api/nodes/image
14
+ */
15
+ const Image = _tiptap_core.Node.create({
16
+ name: "image",
17
+ addOptions() {
18
+ return {
19
+ inline: false,
20
+ allowBase64: false,
21
+ HTMLAttributes: {},
22
+ resize: false
23
+ };
24
+ },
25
+ inline() {
26
+ return this.options.inline;
27
+ },
28
+ group() {
29
+ return this.options.inline ? "inline" : "block";
30
+ },
31
+ draggable: true,
32
+ addAttributes() {
33
+ return {
34
+ src: { default: null },
35
+ alt: { default: null },
36
+ title: { default: null },
37
+ width: { default: null },
38
+ height: { default: null }
39
+ };
40
+ },
41
+ parseHTML() {
42
+ return [{ tag: this.options.allowBase64 ? "img[src]" : "img[src]:not([src^=\"data:\"])" }];
43
+ },
44
+ renderHTML({ HTMLAttributes }) {
45
+ return ["img", (0, _tiptap_core.mergeAttributes)(this.options.HTMLAttributes, HTMLAttributes)];
46
+ },
47
+ parseMarkdown: (token, helpers) => {
48
+ return helpers.createNode("image", {
49
+ src: token.href,
50
+ title: token.title,
51
+ alt: token.text
52
+ });
53
+ },
54
+ renderMarkdown: (node) => {
55
+ var _node$attrs$src, _node$attrs, _node$attrs$alt, _node$attrs2, _node$attrs$title, _node$attrs3;
56
+ const src = (_node$attrs$src = (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.src) !== null && _node$attrs$src !== void 0 ? _node$attrs$src : "";
57
+ const alt = (_node$attrs$alt = (_node$attrs2 = node.attrs) === null || _node$attrs2 === void 0 ? void 0 : _node$attrs2.alt) !== null && _node$attrs$alt !== void 0 ? _node$attrs$alt : "";
58
+ const title = (_node$attrs$title = (_node$attrs3 = node.attrs) === null || _node$attrs3 === void 0 ? void 0 : _node$attrs3.title) !== null && _node$attrs$title !== void 0 ? _node$attrs$title : "";
59
+ return title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})`;
60
+ },
61
+ addNodeView() {
62
+ if (!this.options.resize || !this.options.resize.enabled || typeof document === "undefined") return null;
63
+ const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
64
+ const resizeManagedAttributes = /* @__PURE__ */ new Set([
65
+ "src",
66
+ "width",
67
+ "height"
68
+ ]);
69
+ return ({ node, getPos, HTMLAttributes, editor }) => {
70
+ const el = document.createElement("img");
71
+ el.draggable = false;
72
+ const mergedAttributes = (0, _tiptap_core.mergeAttributes)(this.options.HTMLAttributes, HTMLAttributes);
73
+ Object.entries(mergedAttributes).forEach(([key, value]) => {
74
+ if (value != null) switch (key) {
75
+ case "src":
76
+ case "width":
77
+ case "height": break;
78
+ default:
79
+ el.setAttribute(key, value);
80
+ break;
81
+ }
82
+ });
83
+ if (mergedAttributes.src !== null) el.src = mergedAttributes.src;
84
+ let previousHTMLAttributes = { ...HTMLAttributes };
85
+ const syncImageSource = (src) => {
86
+ if (typeof src === "string" && src !== "") {
87
+ if (el.getAttribute("src") !== src) el.src = src;
88
+ return;
89
+ }
90
+ if (el.hasAttribute("src")) el.removeAttribute("src");
91
+ if (el.src !== "") el.src = "";
92
+ };
93
+ syncImageSource(HTMLAttributes.src);
94
+ const onUpdate = (updatedNode) => {
95
+ if (updatedNode.type !== node.type) return false;
96
+ const newHTMLAttributes = (0, _tiptap_core.getRenderedAttributes)(updatedNode, editor.extensionManager.attributes.filter((attribute) => attribute.type === updatedNode.type.name));
97
+ Object.keys(previousHTMLAttributes).forEach((key) => {
98
+ if (!resizeManagedAttributes.has(key) && !(key in newHTMLAttributes)) el.removeAttribute(key);
99
+ });
100
+ Object.entries(newHTMLAttributes).forEach(([key, value]) => {
101
+ if (resizeManagedAttributes.has(key)) return;
102
+ if (value != null) el.setAttribute(key, value);
103
+ else el.removeAttribute(key);
104
+ });
105
+ syncImageSource(newHTMLAttributes.src);
106
+ previousHTMLAttributes = newHTMLAttributes;
107
+ return true;
108
+ };
109
+ const nodeView = new _tiptap_core.ResizableNodeView({
110
+ element: el,
111
+ editor,
112
+ node,
113
+ getPos,
114
+ onResize: (width, height) => {
115
+ el.style.width = `${width}px`;
116
+ el.style.height = `${height}px`;
117
+ },
118
+ onCommit: (width, height) => {
119
+ const pos = getPos();
120
+ if (pos === void 0) return;
121
+ this.editor.chain().setNodeSelection(pos).updateAttributes(this.name, {
122
+ width,
123
+ height
124
+ }).run();
125
+ },
126
+ onUpdate,
127
+ options: {
128
+ directions,
129
+ min: {
130
+ width: minWidth,
131
+ height: minHeight
132
+ },
133
+ preserveAspectRatio: alwaysPreserveAspectRatio === true
134
+ }
135
+ });
136
+ const dom = nodeView.dom;
137
+ const showNodeView = () => {
138
+ dom.style.visibility = "";
139
+ dom.style.pointerEvents = "";
140
+ };
141
+ dom.style.visibility = "hidden";
142
+ dom.style.pointerEvents = "none";
143
+ if (el.complete && el.naturalWidth > 0) showNodeView();
144
+ else {
145
+ el.onload = showNodeView;
146
+ el.onerror = showNodeView;
147
+ }
148
+ return nodeView;
149
+ };
150
+ },
151
+ addCommands() {
152
+ return { setImage: (options) => ({ commands }) => {
153
+ return commands.insertContent({
154
+ type: this.name,
155
+ attrs: options
156
+ });
157
+ } };
158
+ },
159
+ addInputRules() {
160
+ return [(0, _tiptap_core.nodeInputRule)({
161
+ find: inputRegex,
162
+ type: this.type,
163
+ getAttributes: (match) => {
164
+ const [, , alt, src, title] = match;
165
+ return {
166
+ src,
167
+ alt,
168
+ title
169
+ };
170
+ }
171
+ })];
172
+ }
222
173
  });
174
+ //#endregion
175
+ //#region src/index.ts
176
+ var src_default = Image;
177
+ //#endregion
178
+ exports.Image = Image;
179
+ exports.default = src_default;
180
+ exports.inputRegex = inputRegex;
223
181
 
224
- // src/index.ts
225
- var index_default = Image;
226
- // Annotate the CommonJS export names for ESM import in node:
227
- 0 && (module.exports = {
228
- Image,
229
- inputRegex
230
- });
231
182
  //# sourceMappingURL=index.cjs.map
@@ -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, 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,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,WAAO,6BAAgB,KAAK,QAAQ,gBAAgB,cAAc,CAAC;AAAA,EAC7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AAG,KAAK;AACzB,aAAG,MAAM,SAAS,GAAG,MAAM;AAAA,QAC7B;AAAA,QACA,UAAU,CAAC,OAAO,WAAW;AAC3B,gBAAM,MAAM,OAAO;AACnB,cAAI,QAAQ,QAAW;AACrB;AAAA,UACF;AAEA,eAAK,OACF,MAAM,EACN,iBAAiB,GAAG,EACpB,iBAAiB,KAAK,MAAM;AAAA,YAC3B;AAAA,YACA;AAAA,UACF,CAAC,EACA,IAAI;AAAA,QACT;AAAA,QACA;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;;;ADzTD,IAAO,gBAAQ;","names":[]}
1
+ {"version":3,"file":"index.cjs","names":["Node","ResizableNodeView"],"sources":["../src/image.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 const showNodeView = () => {\n dom.style.visibility = ''\n dom.style.pointerEvents = ''\n }\n\n dom.style.visibility = 'hidden'\n dom.style.pointerEvents = 'none'\n\n if (el.complete && el.naturalWidth > 0) {\n // A cached image is already complete before this node view mounts, so\n // `load` never fires again and the image would stay hidden forever.\n showNodeView()\n } else {\n el.onload = showNodeView\n // Reveal on error too, so a broken src renders as an inspectable broken\n // image instead of an invisible node.\n el.onerror = showNodeView\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":";;;;;;;;;AA2EA,MAAa,aAAa;;;;;AAM1B,MAAa,QAAQA,aAAAA,KAAK,OAAqB;CAC7C,MAAM;CAEN,aAAa;EACX,OAAO;GACL,QAAQ;GACR,aAAa;GACb,gBAAgB,CAAC;GACjB,QAAQ;EACV;CACF;CAEA,SAAS;EACP,OAAO,KAAK,QAAQ;CACtB;CAEA,QAAQ;EACN,OAAO,KAAK,QAAQ,SAAS,WAAW;CAC1C;CAEA,WAAW;CAEX,gBAAgB;EACd,OAAO;GACL,KAAK,EACH,SAAS,KACX;GACA,KAAK,EACH,SAAS,KACX;GACA,OAAO,EACL,SAAS,KACX;GACA,OAAO,EACL,SAAS,KACX;GACA,QAAQ,EACN,SAAS,KACX;EACF;CACF;CAEA,YAAY;EACV,OAAO,CACL,EACE,KAAK,KAAK,QAAQ,cAAc,aAAa,iCAC/C,CACF;CACF;CAEA,WAAW,EAAE,kBAAkB;EAC7B,OAAO,CAAC,QAAA,GAAA,aAAA,gBAAA,CAAuB,KAAK,QAAQ,gBAAgB,cAAc,CAAC;CAC7E;CAEA,gBAAgB,OAAO,YAAY;EACjC,OAAO,QAAQ,WAAW,SAAS;GACjC,KAAK,MAAM;GACX,OAAO,MAAM;GACb,KAAK,MAAM;EACb,CAAC;CACH;CAEA,iBAAgB,SAAQ;;EACtB,MAAM,OAAA,mBAAA,cAAM,KAAK,WAAA,QAAA,gBAAA,KAAA,IAAA,KAAA,IAAA,YAAO,SAAA,QAAA,oBAAA,KAAA,IAAA,kBAAO;EAC/B,MAAM,OAAA,mBAAA,eAAM,KAAK,WAAA,QAAA,iBAAA,KAAA,IAAA,KAAA,IAAA,aAAO,SAAA,QAAA,oBAAA,KAAA,IAAA,kBAAO;EAC/B,MAAM,SAAA,qBAAA,eAAQ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ACf;GAEA,OAAO;EACT;CACF;CAEA,cAAc;EACZ,OAAO,EACL,WACE,aACC,EAAE,eAAe;GAChB,OAAO,SAAS,cAAc;IAC5B,MAAM,KAAK;IACX,OAAO;GACT,CAAC;EACH,EACJ;CACF;CAEA,gBAAgB;EACd,OAAO,EAAA,GAAA,aAAA,cAAA,CACS;GACZ,MAAM;GACN,MAAM,KAAK;GACX,gBAAe,UAAS;IACtB,MAAM,KAAK,KAAK,KAAK,SAAS;IAE9B,OAAO;KAAE;KAAK;KAAK;IAAM;GAC3B;EACF,CAAC,CACH;CACF;AACF,CAAC;;;ACrUD,IAAA,cAAe"}
package/dist/index.d.cts CHANGED
@@ -1,59 +1,59 @@
1
- import { Node, ResizableNodeViewDirection } from '@tiptap/core';
2
-
1
+ import { Node, ResizableNodeViewDirection } from "@tiptap/core";
2
+ //#region src/image.d.ts
3
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;
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
35
  }
36
36
  interface SetImageOptions {
37
- src: string;
38
- alt?: string;
39
- title?: string;
40
- width?: number;
41
- height?: number;
37
+ src: string;
38
+ alt?: string;
39
+ title?: string;
40
+ width?: number;
41
+ height?: number;
42
42
  }
43
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
- }
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
57
  }
58
58
  /**
59
59
  * Matches an image to a ![image](src "title") on input.
@@ -64,5 +64,6 @@ declare const inputRegex: RegExp;
64
64
  * @see https://www.tiptap.dev/api/nodes/image
65
65
  */
66
66
  declare const Image: Node<ImageOptions, any>;
67
-
68
- export { Image, type ImageOptions, type SetImageOptions, Image as default, inputRegex };
67
+ //#endregion
68
+ export { Image, Image as default, ImageOptions, SetImageOptions, inputRegex };
69
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/image.ts"],"mappings":";;UASiB;;;;;;EAMf;;;;;;;EAQA;;;;;;EAOA,gBAAgB;;;;;;EAOhB;IAEM;IACA,aAAa;IACb;IACA;IACA;;;UAKS;EACf;EACA;EACA;EACA;EACA;;;YAIU,SAAS;IACjB;;;;;;;;;MASE,WAAW,SAAS,oBAAoB;;;;;;;cAQjC,YAAU;;;;;cAMV,OAAK,KAAA"}
package/dist/index.d.ts CHANGED
@@ -1,59 +1,59 @@
1
- import { Node, ResizableNodeViewDirection } from '@tiptap/core';
2
-
1
+ import { Node, ResizableNodeViewDirection } from "@tiptap/core";
2
+ //#region src/image.d.ts
3
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;
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
35
  }
36
36
  interface SetImageOptions {
37
- src: string;
38
- alt?: string;
39
- title?: string;
40
- width?: number;
41
- height?: number;
37
+ src: string;
38
+ alt?: string;
39
+ title?: string;
40
+ width?: number;
41
+ height?: number;
42
42
  }
43
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
- }
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
57
  }
58
58
  /**
59
59
  * Matches an image to a ![image](src "title") on input.
@@ -64,5 +64,6 @@ declare const inputRegex: RegExp;
64
64
  * @see https://www.tiptap.dev/api/nodes/image
65
65
  */
66
66
  declare const Image: Node<ImageOptions, any>;
67
-
68
- export { Image, type ImageOptions, type SetImageOptions, Image as default, inputRegex };
67
+ //#endregion
68
+ export { Image, Image as default, ImageOptions, SetImageOptions, inputRegex };
69
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/image.ts"],"mappings":";;UASiB;;;;;;EAMf;;;;;;;EAQA;;;;;;EAOA,gBAAgB;;;;;;EAOhB;IAEM;IACA,aAAa;IACb;IACA;IACA;;;UAKS;EACf;EACA;EACA;EACA;EACA;;;YAIU,SAAS;IACjB;;;;;;;;;MASE,WAAW,SAAS,oBAAoB;;;;;;;cAQjC,YAAU;;;;;cAMV,OAAK,KAAA"}
package/dist/index.js CHANGED
@@ -1,209 +1,176 @@
1
- // src/image.ts
2
- import {
3
- getRenderedAttributes,
4
- mergeAttributes,
5
- Node,
6
- nodeInputRule,
7
- ResizableNodeView
8
- } from "@tiptap/core";
9
- var inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/;
10
- var Image = Node.create({
11
- name: "image",
12
- addOptions() {
13
- return {
14
- inline: false,
15
- allowBase64: false,
16
- HTMLAttributes: {},
17
- resize: false
18
- };
19
- },
20
- inline() {
21
- return this.options.inline;
22
- },
23
- group() {
24
- return this.options.inline ? "inline" : "block";
25
- },
26
- draggable: true,
27
- addAttributes() {
28
- return {
29
- src: {
30
- default: null
31
- },
32
- alt: {
33
- default: null
34
- },
35
- title: {
36
- default: null
37
- },
38
- width: {
39
- default: null
40
- },
41
- height: {
42
- default: null
43
- }
44
- };
45
- },
46
- parseHTML() {
47
- return [
48
- {
49
- tag: this.options.allowBase64 ? "img[src]" : 'img[src]:not([src^="data:"])'
50
- }
51
- ];
52
- },
53
- renderHTML({ HTMLAttributes }) {
54
- return ["img", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)];
55
- },
56
- parseMarkdown: (token, helpers) => {
57
- return helpers.createNode("image", {
58
- src: token.href,
59
- title: token.title,
60
- alt: token.text
61
- });
62
- },
63
- renderMarkdown: (node) => {
64
- var _a, _b, _c, _d, _e, _f;
65
- const src = (_b = (_a = node.attrs) == null ? void 0 : _a.src) != null ? _b : "";
66
- const alt = (_d = (_c = node.attrs) == null ? void 0 : _c.alt) != null ? _d : "";
67
- const title = (_f = (_e = node.attrs) == null ? void 0 : _e.title) != null ? _f : "";
68
- return title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})`;
69
- },
70
- addNodeView() {
71
- if (!this.options.resize || !this.options.resize.enabled || typeof document === "undefined") {
72
- return null;
73
- }
74
- const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
75
- const resizeManagedAttributes = /* @__PURE__ */ new Set(["src", "width", "height"]);
76
- return ({ node, getPos, HTMLAttributes, editor }) => {
77
- const el = document.createElement("img");
78
- el.draggable = false;
79
- const mergedAttributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes);
80
- Object.entries(mergedAttributes).forEach(([key, value]) => {
81
- if (value != null) {
82
- switch (key) {
83
- case "src":
84
- case "width":
85
- case "height":
86
- break;
87
- default:
88
- el.setAttribute(key, value);
89
- break;
90
- }
91
- }
92
- });
93
- if (mergedAttributes.src !== null) {
94
- el.src = mergedAttributes.src;
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
- };
139
- const nodeView = new ResizableNodeView({
140
- element: el,
141
- editor,
142
- node,
143
- getPos,
144
- onResize: (width, height) => {
145
- el.style.width = `${width}px`;
146
- el.style.height = `${height}px`;
147
- },
148
- onCommit: (width, height) => {
149
- const pos = getPos();
150
- if (pos === void 0) {
151
- return;
152
- }
153
- this.editor.chain().setNodeSelection(pos).updateAttributes(this.name, {
154
- width,
155
- height
156
- }).run();
157
- },
158
- onUpdate,
159
- options: {
160
- directions,
161
- min: {
162
- width: minWidth,
163
- height: minHeight
164
- },
165
- preserveAspectRatio: alwaysPreserveAspectRatio === true
166
- }
167
- });
168
- const dom = nodeView.dom;
169
- dom.style.visibility = "hidden";
170
- dom.style.pointerEvents = "none";
171
- el.onload = () => {
172
- dom.style.visibility = "";
173
- dom.style.pointerEvents = "";
174
- };
175
- return nodeView;
176
- };
177
- },
178
- addCommands() {
179
- return {
180
- setImage: (options) => ({ commands }) => {
181
- return commands.insertContent({
182
- type: this.name,
183
- attrs: options
184
- });
185
- }
186
- };
187
- },
188
- addInputRules() {
189
- return [
190
- nodeInputRule({
191
- find: inputRegex,
192
- type: this.type,
193
- getAttributes: (match) => {
194
- const [, , alt, src, title] = match;
195
- return { src, alt, title };
196
- }
197
- })
198
- ];
199
- }
1
+ import { Node, ResizableNodeView, getRenderedAttributes, mergeAttributes, nodeInputRule } from "@tiptap/core";
2
+ //#region src/image.ts
3
+ /**
4
+ * Matches an image to a ![image](src "title") on input.
5
+ */
6
+ const inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/;
7
+ /**
8
+ * This extension allows you to insert images.
9
+ * @see https://www.tiptap.dev/api/nodes/image
10
+ */
11
+ const Image = Node.create({
12
+ name: "image",
13
+ addOptions() {
14
+ return {
15
+ inline: false,
16
+ allowBase64: false,
17
+ HTMLAttributes: {},
18
+ resize: false
19
+ };
20
+ },
21
+ inline() {
22
+ return this.options.inline;
23
+ },
24
+ group() {
25
+ return this.options.inline ? "inline" : "block";
26
+ },
27
+ draggable: true,
28
+ addAttributes() {
29
+ return {
30
+ src: { default: null },
31
+ alt: { default: null },
32
+ title: { default: null },
33
+ width: { default: null },
34
+ height: { default: null }
35
+ };
36
+ },
37
+ parseHTML() {
38
+ return [{ tag: this.options.allowBase64 ? "img[src]" : "img[src]:not([src^=\"data:\"])" }];
39
+ },
40
+ renderHTML({ HTMLAttributes }) {
41
+ return ["img", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)];
42
+ },
43
+ parseMarkdown: (token, helpers) => {
44
+ return helpers.createNode("image", {
45
+ src: token.href,
46
+ title: token.title,
47
+ alt: token.text
48
+ });
49
+ },
50
+ renderMarkdown: (node) => {
51
+ var _node$attrs$src, _node$attrs, _node$attrs$alt, _node$attrs2, _node$attrs$title, _node$attrs3;
52
+ const src = (_node$attrs$src = (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.src) !== null && _node$attrs$src !== void 0 ? _node$attrs$src : "";
53
+ const alt = (_node$attrs$alt = (_node$attrs2 = node.attrs) === null || _node$attrs2 === void 0 ? void 0 : _node$attrs2.alt) !== null && _node$attrs$alt !== void 0 ? _node$attrs$alt : "";
54
+ const title = (_node$attrs$title = (_node$attrs3 = node.attrs) === null || _node$attrs3 === void 0 ? void 0 : _node$attrs3.title) !== null && _node$attrs$title !== void 0 ? _node$attrs$title : "";
55
+ return title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})`;
56
+ },
57
+ addNodeView() {
58
+ if (!this.options.resize || !this.options.resize.enabled || typeof document === "undefined") return null;
59
+ const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
60
+ const resizeManagedAttributes = /* @__PURE__ */ new Set([
61
+ "src",
62
+ "width",
63
+ "height"
64
+ ]);
65
+ return ({ node, getPos, HTMLAttributes, editor }) => {
66
+ const el = document.createElement("img");
67
+ el.draggable = false;
68
+ const mergedAttributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes);
69
+ Object.entries(mergedAttributes).forEach(([key, value]) => {
70
+ if (value != null) switch (key) {
71
+ case "src":
72
+ case "width":
73
+ case "height": break;
74
+ default:
75
+ el.setAttribute(key, value);
76
+ break;
77
+ }
78
+ });
79
+ if (mergedAttributes.src !== null) el.src = mergedAttributes.src;
80
+ let previousHTMLAttributes = { ...HTMLAttributes };
81
+ const syncImageSource = (src) => {
82
+ if (typeof src === "string" && src !== "") {
83
+ if (el.getAttribute("src") !== src) el.src = src;
84
+ return;
85
+ }
86
+ if (el.hasAttribute("src")) el.removeAttribute("src");
87
+ if (el.src !== "") el.src = "";
88
+ };
89
+ syncImageSource(HTMLAttributes.src);
90
+ const onUpdate = (updatedNode) => {
91
+ if (updatedNode.type !== node.type) return false;
92
+ const newHTMLAttributes = getRenderedAttributes(updatedNode, editor.extensionManager.attributes.filter((attribute) => attribute.type === updatedNode.type.name));
93
+ Object.keys(previousHTMLAttributes).forEach((key) => {
94
+ if (!resizeManagedAttributes.has(key) && !(key in newHTMLAttributes)) el.removeAttribute(key);
95
+ });
96
+ Object.entries(newHTMLAttributes).forEach(([key, value]) => {
97
+ if (resizeManagedAttributes.has(key)) return;
98
+ if (value != null) el.setAttribute(key, value);
99
+ else el.removeAttribute(key);
100
+ });
101
+ syncImageSource(newHTMLAttributes.src);
102
+ previousHTMLAttributes = newHTMLAttributes;
103
+ return true;
104
+ };
105
+ const nodeView = new ResizableNodeView({
106
+ element: el,
107
+ editor,
108
+ node,
109
+ getPos,
110
+ onResize: (width, height) => {
111
+ el.style.width = `${width}px`;
112
+ el.style.height = `${height}px`;
113
+ },
114
+ onCommit: (width, height) => {
115
+ const pos = getPos();
116
+ if (pos === void 0) return;
117
+ this.editor.chain().setNodeSelection(pos).updateAttributes(this.name, {
118
+ width,
119
+ height
120
+ }).run();
121
+ },
122
+ onUpdate,
123
+ options: {
124
+ directions,
125
+ min: {
126
+ width: minWidth,
127
+ height: minHeight
128
+ },
129
+ preserveAspectRatio: alwaysPreserveAspectRatio === true
130
+ }
131
+ });
132
+ const dom = nodeView.dom;
133
+ const showNodeView = () => {
134
+ dom.style.visibility = "";
135
+ dom.style.pointerEvents = "";
136
+ };
137
+ dom.style.visibility = "hidden";
138
+ dom.style.pointerEvents = "none";
139
+ if (el.complete && el.naturalWidth > 0) showNodeView();
140
+ else {
141
+ el.onload = showNodeView;
142
+ el.onerror = showNodeView;
143
+ }
144
+ return nodeView;
145
+ };
146
+ },
147
+ addCommands() {
148
+ return { setImage: (options) => ({ commands }) => {
149
+ return commands.insertContent({
150
+ type: this.name,
151
+ attrs: options
152
+ });
153
+ } };
154
+ },
155
+ addInputRules() {
156
+ return [nodeInputRule({
157
+ find: inputRegex,
158
+ type: this.type,
159
+ getAttributes: (match) => {
160
+ const [, , alt, src, title] = match;
161
+ return {
162
+ src,
163
+ alt,
164
+ title
165
+ };
166
+ }
167
+ })];
168
+ }
200
169
  });
170
+ //#endregion
171
+ //#region src/index.ts
172
+ var src_default = Image;
173
+ //#endregion
174
+ export { Image, src_default as default, inputRegex };
201
175
 
202
- // src/index.ts
203
- var index_default = Image;
204
- export {
205
- Image,
206
- index_default as default,
207
- inputRegex
208
- };
209
176
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/image.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":";AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAoEA,IAAM,aAAa;AAMnB,IAAM,QAAQ,KAAK,OAAqB;AAAA,EAC7C,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,aAAa;AAAA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AgB;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":[]}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/image.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 const showNodeView = () => {\n dom.style.visibility = ''\n dom.style.pointerEvents = ''\n }\n\n dom.style.visibility = 'hidden'\n dom.style.pointerEvents = 'none'\n\n if (el.complete && el.naturalWidth > 0) {\n // A cached image is already complete before this node view mounts, so\n // `load` never fires again and the image would stay hidden forever.\n showNodeView()\n } else {\n el.onload = showNodeView\n // Reveal on error too, so a broken src renders as an inspectable broken\n // image instead of an invisible node.\n el.onerror = showNodeView\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":";;;;;AA2EA,MAAa,aAAa;;;;;AAM1B,MAAa,QAAQ,KAAK,OAAqB;CAC7C,MAAM;CAEN,aAAa;EACX,OAAO;GACL,QAAQ;GACR,aAAa;GACb,gBAAgB,CAAC;GACjB,QAAQ;EACV;CACF;CAEA,SAAS;EACP,OAAO,KAAK,QAAQ;CACtB;CAEA,QAAQ;EACN,OAAO,KAAK,QAAQ,SAAS,WAAW;CAC1C;CAEA,WAAW;CAEX,gBAAgB;EACd,OAAO;GACL,KAAK,EACH,SAAS,KACX;GACA,KAAK,EACH,SAAS,KACX;GACA,OAAO,EACL,SAAS,KACX;GACA,OAAO,EACL,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AG,MAAM,QAAQ,GAAG,MAAM;KAC1B,GAAG,MAAM,SAAS,GAAG,OAAO;IAC9B;IACA,WAAW,OAAO,WAAW;KAC3B,MAAM,MAAM,OAAO;KACnB,IAAI,QAAQ,KAAA,GACV;KAGF,KAAK,OACF,MAAM,CAAC,CACP,iBAAiB,GAAG,CAAC,CACrB,iBAAiB,KAAK,MAAM;MAC3B;MACA;KACF,CAAC,CAAC,CACD,IAAI;IACT;IACA;IACA,SAAS;KACP;KACA,KAAK;MACH,OAAO;MACP,QAAQ;KACV;KACA,qBAAqB,8BAA8B;IACrD;GACF,CAAC;GAED,MAAM,MAAM,SAAS;GAGrB,MAAM,qBAAqB;IACzB,IAAI,MAAM,aAAa;IACvB,IAAI,MAAM,gBAAgB;GAC5B;GAEA,IAAI,MAAM,aAAa;GACvB,IAAI,MAAM,gBAAgB;GAE1B,IAAI,GAAG,YAAY,GAAG,eAAe,GAGnC,aAAa;QACR;IACL,GAAG,SAAS;IAGZ,GAAG,UAAU;GACf;GAEA,OAAO;EACT;CACF;CAEA,cAAc;EACZ,OAAO,EACL,WACE,aACC,EAAE,eAAe;GAChB,OAAO,SAAS,cAAc;IAC5B,MAAM,KAAK;IACX,OAAO;GACT,CAAC;EACH,EACJ;CACF;CAEA,gBAAgB;EACd,OAAO,CACL,cAAc;GACZ,MAAM;GACN,MAAM,KAAK;GACX,gBAAe,UAAS;IACtB,MAAM,KAAK,KAAK,KAAK,SAAS;IAE9B,OAAO;KAAE;KAAK;KAAK;IAAM;GAC3B;EACF,CAAC,CACH;CACF;AACF,CAAC;;;ACrUD,IAAA,cAAe"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiptap/extension-image",
3
- "version": "3.30.1",
3
+ "version": "3.30.3",
4
4
  "description": "image extension for tiptap",
5
5
  "keywords": [
6
6
  "tiptap",
@@ -39,12 +39,12 @@
39
39
  }
40
40
  },
41
41
  "devDependencies": {
42
- "@tiptap/core": "^3.30.1"
42
+ "@tiptap/core": "^3.30.3"
43
43
  },
44
44
  "peerDependencies": {
45
- "@tiptap/core": "3.30.1"
45
+ "@tiptap/core": "3.30.3"
46
46
  },
47
47
  "scripts": {
48
- "build": "tsup"
48
+ "build": "vp pack"
49
49
  }
50
50
  }
package/src/image.ts CHANGED
@@ -278,13 +278,25 @@ export const Image = Node.create<ImageOptions>({
278
278
  const dom = nodeView.dom as HTMLElement
279
279
 
280
280
  // when image is loaded, show the node view to get the correct dimensions
281
- dom.style.visibility = 'hidden'
282
- dom.style.pointerEvents = 'none'
283
- el.onload = () => {
281
+ const showNodeView = () => {
284
282
  dom.style.visibility = ''
285
283
  dom.style.pointerEvents = ''
286
284
  }
287
285
 
286
+ dom.style.visibility = 'hidden'
287
+ dom.style.pointerEvents = 'none'
288
+
289
+ if (el.complete && el.naturalWidth > 0) {
290
+ // A cached image is already complete before this node view mounts, so
291
+ // `load` never fires again and the image would stay hidden forever.
292
+ showNodeView()
293
+ } else {
294
+ el.onload = showNodeView
295
+ // Reveal on error too, so a broken src renders as an inspectable broken
296
+ // image instead of an invisible node.
297
+ el.onerror = showNodeView
298
+ }
299
+
288
300
  return nodeView
289
301
  }
290
302
  },