@tiptap/extension-image
Version:
image extension for tiptap
187 lines (184 loc) • 5.15 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
Image: () => Image,
default: () => index_default,
inputRegex: () => inputRegex
});
module.exports = __toCommonJS(index_exports);
// src/image.ts
var import_core = require("@tiptap/core");
var inputRegex = /(?:^|\s)(!\[(.+|:?)]\((\S+)(?:(?:\s+)["'](\S+)["'])?\))$/;
var Image = import_core.Node.create({
name: "image",
addOptions() {
return {
inline: false,
allowBase64: false,
HTMLAttributes: {},
resize: false
};
},
inline() {
return this.options.inline;
},
group() {
return this.options.inline ? "inline" : "block";
},
draggable: true,
addAttributes() {
return {
src: {
default: null
},
alt: {
default: null
},
title: {
default: null
},
width: {
default: null
},
height: {
default: null
}
};
},
parseHTML() {
return [
{
tag: this.options.allowBase64 ? "img[src]" : 'img[src]:not([src^="data:"])'
}
];
},
renderHTML({ HTMLAttributes }) {
return ["img", (0, import_core.mergeAttributes)(this.options.HTMLAttributes, HTMLAttributes)];
},
parseMarkdown: (token, helpers) => {
return helpers.createNode("image", {
src: token.href,
title: token.title,
alt: token.text
});
},
renderMarkdown: (node) => {
var _a, _b, _c, _d, _e, _f;
const src = (_b = (_a = node.attrs) == null ? void 0 : _a.src) != null ? _b : "";
const alt = (_d = (_c = node.attrs) == null ? void 0 : _c.alt) != null ? _d : "";
const title = (_f = (_e = node.attrs) == null ? void 0 : _e.title) != null ? _f : "";
return title ? `` : ``;
},
addNodeView() {
if (!this.options.resize || !this.options.resize.enabled || typeof document === "undefined") {
return null;
}
const { directions, minWidth, minHeight, alwaysPreserveAspectRatio } = this.options.resize;
return ({ node, getPos, HTMLAttributes, editor }) => {
const el = document.createElement("img");
Object.entries(HTMLAttributes).forEach(([key, value]) => {
if (value != null) {
switch (key) {
case "width":
case "height":
break;
default:
el.setAttribute(key, value);
break;
}
}
});
el.src = HTMLAttributes.src;
const nodeView = new import_core.ResizableNodeView({
element: el,
editor,
node,
getPos,
onResize: (width, height) => {
el.style.width = `${width}px`;
el.style.height = `${height}px`;
},
onCommit: (width, height) => {
const pos = getPos();
if (pos === void 0) {
return;
}
this.editor.chain().setNodeSelection(pos).updateAttributes(this.name, {
width,
height
}).run();
},
onUpdate: (updatedNode, _decorations, _innerDecorations) => {
if (updatedNode.type !== node.type) {
return false;
}
return true;
},
options: {
directions,
min: {
width: minWidth,
height: minHeight
},
preserveAspectRatio: alwaysPreserveAspectRatio === true
}
});
const dom = nodeView.dom;
dom.style.visibility = "hidden";
dom.style.pointerEvents = "none";
el.onload = () => {
dom.style.visibility = "";
dom.style.pointerEvents = "";
};
return nodeView;
};
},
addCommands() {
return {
setImage: (options) => ({ commands }) => {
return commands.insertContent({
type: this.name,
attrs: options
});
}
};
},
addInputRules() {
return [
(0, import_core.nodeInputRule)({
find: inputRegex,
type: this.type,
getAttributes: (match) => {
const [, , alt, src, title] = match;
return { src, alt, title };
}
})
];
}
});
// src/index.ts
var index_default = Image;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Image,
inputRegex
});
//# sourceMappingURL=index.cjs.map