@tiptap/extension-image 3.30.2 → 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 +178 -233
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +54 -53
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +54 -53
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +173 -212
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1,237 +1,182 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
dom.style.visibility = "hidden";
|
|
196
|
-
dom.style.pointerEvents = "none";
|
|
197
|
-
if (el.complete && el.naturalWidth > 0) {
|
|
198
|
-
showNodeView();
|
|
199
|
-
} else {
|
|
200
|
-
el.onload = showNodeView;
|
|
201
|
-
el.onerror = showNodeView;
|
|
202
|
-
}
|
|
203
|
-
return nodeView;
|
|
204
|
-
};
|
|
205
|
-
},
|
|
206
|
-
addCommands() {
|
|
207
|
-
return {
|
|
208
|
-
setImage: (options) => ({ commands }) => {
|
|
209
|
-
return commands.insertContent({
|
|
210
|
-
type: this.name,
|
|
211
|
-
attrs: options
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
},
|
|
216
|
-
addInputRules() {
|
|
217
|
-
return [
|
|
218
|
-
(0, import_core.nodeInputRule)({
|
|
219
|
-
find: inputRegex,
|
|
220
|
-
type: this.type,
|
|
221
|
-
getAttributes: (match) => {
|
|
222
|
-
const [, , alt, src, title] = match;
|
|
223
|
-
return { src, alt, title };
|
|
224
|
-
}
|
|
225
|
-
})
|
|
226
|
-
];
|
|
227
|
-
}
|
|
5
|
+
let _tiptap_core = require("@tiptap/core");
|
|
6
|
+
//#region src/image.ts
|
|
7
|
+
/**
|
|
8
|
+
* Matches an image to a  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 ? `` : ``;
|
|
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
|
+
}
|
|
228
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;
|
|
229
181
|
|
|
230
|
-
// src/index.ts
|
|
231
|
-
var index_default = Image;
|
|
232
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
233
|
-
0 && (module.exports = {
|
|
234
|
-
Image,
|
|
235
|
-
inputRegex
|
|
236
|
-
});
|
|
237
182
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -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  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 ? `` : ``\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"],"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;A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;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;;;ADrUD,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  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 ? `` : ``\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,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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,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
|
|
2
|
-
|
|
1
|
+
import { Node, ResizableNodeViewDirection } from "@tiptap/core";
|
|
2
|
+
//#region src/image.d.ts
|
|
3
3
|
interface ImageOptions {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
src: string;
|
|
38
|
+
alt?: string;
|
|
39
|
+
title?: string;
|
|
40
|
+
width?: number;
|
|
41
|
+
height?: number;
|
|
42
42
|
}
|
|
43
43
|
declare module '@tiptap/core' {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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  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,
|
|
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
|
|
2
|
-
|
|
1
|
+
import { Node, ResizableNodeViewDirection } from "@tiptap/core";
|
|
2
|
+
//#region src/image.d.ts
|
|
3
3
|
interface ImageOptions {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
src: string;
|
|
38
|
+
alt?: string;
|
|
39
|
+
title?: string;
|
|
40
|
+
width?: number;
|
|
41
|
+
height?: number;
|
|
42
42
|
}
|
|
43
43
|
declare module '@tiptap/core' {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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  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,
|
|
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,215 +1,176 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const showNodeView = () => {
|
|
170
|
-
dom.style.visibility = "";
|
|
171
|
-
dom.style.pointerEvents = "";
|
|
172
|
-
};
|
|
173
|
-
dom.style.visibility = "hidden";
|
|
174
|
-
dom.style.pointerEvents = "none";
|
|
175
|
-
if (el.complete && el.naturalWidth > 0) {
|
|
176
|
-
showNodeView();
|
|
177
|
-
} else {
|
|
178
|
-
el.onload = showNodeView;
|
|
179
|
-
el.onerror = showNodeView;
|
|
180
|
-
}
|
|
181
|
-
return nodeView;
|
|
182
|
-
};
|
|
183
|
-
},
|
|
184
|
-
addCommands() {
|
|
185
|
-
return {
|
|
186
|
-
setImage: (options) => ({ commands }) => {
|
|
187
|
-
return commands.insertContent({
|
|
188
|
-
type: this.name,
|
|
189
|
-
attrs: options
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
},
|
|
194
|
-
addInputRules() {
|
|
195
|
-
return [
|
|
196
|
-
nodeInputRule({
|
|
197
|
-
find: inputRegex,
|
|
198
|
-
type: this.type,
|
|
199
|
-
getAttributes: (match) => {
|
|
200
|
-
const [, , alt, src, title] = match;
|
|
201
|
-
return { src, alt, title };
|
|
202
|
-
}
|
|
203
|
-
})
|
|
204
|
-
];
|
|
205
|
-
}
|
|
1
|
+
import { Node, ResizableNodeView, getRenderedAttributes, mergeAttributes, nodeInputRule } from "@tiptap/core";
|
|
2
|
+
//#region src/image.ts
|
|
3
|
+
/**
|
|
4
|
+
* Matches an image to a  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 ? `` : ``;
|
|
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
|
+
}
|
|
206
169
|
});
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/index.ts
|
|
172
|
+
var src_default = Image;
|
|
173
|
+
//#endregion
|
|
174
|
+
export { Image, src_default as default, inputRegex };
|
|
207
175
|
|
|
208
|
-
// src/index.ts
|
|
209
|
-
var index_default = Image;
|
|
210
|
-
export {
|
|
211
|
-
Image,
|
|
212
|
-
index_default as default,
|
|
213
|
-
inputRegex
|
|
214
|
-
};
|
|
215
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  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 ? `` : ``\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":";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,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;A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ACH;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;;;ACrUD,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  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 ? `` : ``\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,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,OAAO,gBAAgB,KAAK,QAAQ,gBAAgB,cAAc,CAAC;CAC7E;CAEA,gBAAgB,OAAO,YAAY;EACjC,OAAO,QAAQ,WAAW,SAAS;GACjC,KAAK,MAAM;GACX,OAAO,MAAM;GAC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AQ;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.
|
|
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.
|
|
42
|
+
"@tiptap/core": "^3.30.3"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@tiptap/core": "3.30.
|
|
45
|
+
"@tiptap/core": "3.30.3"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
|
-
"build": "
|
|
48
|
+
"build": "vp pack"
|
|
49
49
|
}
|
|
50
50
|
}
|