@opentiny/fluent-editor
Version:
A rich text editor based on Quill 2.0, which extends rich modules and formats on the basis of Quill. It's powerful and out-of-the-box.
171 lines (170 loc) • 5.24 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Quill = require("quill");
const Delta = Quill.import("delta");
function hexToRgbA(hex) {
let color;
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
color = hex.substring(1).split("");
if (color.length === 3) {
color = [color[0], color[0], color[1], color[1], color[2], color[2]];
}
color = `0x${color.join("")}`;
return `rgba(${[color >> 16 & 255, color >> 8 & 255, color & 255].join(",")},1)`;
}
}
function imageFileToUrl(imageFile) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(imageFile);
reader.onload = function(e) {
resolve(e.target.result);
};
reader.onerror = reject;
}).catch((error) => {
console.error("Error reading file:", error);
});
}
function imageUrlToFile(imageUrl, isErrorImage) {
return new Promise((resolve, reject) => {
fetch(imageUrl, {
method: "get",
mode: "no-cors"
}).then((res) => res.blob()).then((blob) => {
if (!blob.type.includes("image") || !blob.type) {
return reject();
}
const fileType = blob.type.replace(/^.*\//, "");
const fileName = isErrorImage ? "editorx-error-image.png" : `image-${(/* @__PURE__ */ new Date()).getTime()}.${fileType}`;
const file = new File([blob], fileName, blob);
resolve(file);
}).catch(reject);
});
}
function isNullOrUndefined(param) {
return param === null || param === void 0;
}
function omit(obj, uselessKeys) {
return obj && Object.keys(obj).reduce((acc, key) => {
return uselessKeys.includes(key) ? acc : { ...acc, [key]: obj[key] };
}, {});
}
function replaceDeltaImage(delta, imageUrls, imagePlaceholder) {
let imageIndex = 0;
return delta.reduce((newDelta, op) => {
if (op.insert.image && !op.insert.image.hasExisted) {
const attributes = imagePlaceholder[imageIndex] ? { ...op.attributes, width: "auto", height: 225 } : op.attributes;
newDelta.insert({ image: imageUrls[imageIndex] }, attributes);
imageIndex++;
} else {
newDelta.insert(op.insert, op.attributes);
}
return newDelta;
}, new Delta());
}
const WHITE_SPACE_CHAR_RE = /^[\u3000\u0020]$/;
const WHITE_SPACE_OR_NBSP_RE = /^[\u3000\u0020\u00A0]$/;
function replaceStrWhiteSpace(str) {
let textWithWhiteSpace = "";
let beginHasChar = false;
let consecutiveSpaces = 0;
for (const char of str) {
if (WHITE_SPACE_CHAR_RE.test(char)) {
consecutiveSpaces += 1;
if (!beginHasChar) {
textWithWhiteSpace += " ";
} else if (consecutiveSpaces === 1) {
textWithWhiteSpace += char;
} else {
textWithWhiteSpace += " ";
}
} else {
consecutiveSpaces = 0;
textWithWhiteSpace += char;
beginHasChar = true;
}
}
return textWithWhiteSpace;
}
function shouldInsertNbspOnSpace(prefix) {
if (!prefix) {
return true;
}
const lastChar = prefix.slice(-1);
return WHITE_SPACE_OR_NBSP_RE.test(lastChar);
}
function splitWithBreak(insertContent) {
const lines = [];
const insertStr = insertContent;
let start = 0;
for (let i = 0; i < insertContent.length; i++) {
if (insertStr.charAt(i) === "\n") {
if (i === 0) {
lines.push("\n");
} else {
lines.push(insertStr.substring(start, i));
lines.push("\n");
}
start = i + 1;
}
}
const tailStr = insertStr.substring(start);
if (tailStr) {
lines.push(tailStr);
}
return lines;
}
function getEventComposedPath(evt) {
let path;
path = evt.path || evt.composedPath && evt.composedPath();
if (path === void 0 && evt.target) {
path = [];
let target = evt.target;
path.push(target);
while (target && target.parentNode) {
target = target.parentNode;
path.push(target);
}
}
return path;
}
function sanitize(url, protocols) {
const anchor = document.createElement("a");
anchor.href = url;
const protocol = anchor.href.slice(0, anchor.href.indexOf(":"));
return protocols.includes(protocol);
}
function hadProtocol(url) {
if (!url || !/^(?:f|ht)tps?\:\/\//.test(url)) {
return false;
}
return true;
}
function isInside(position, dom) {
const areaPosition = dom.getBoundingClientRect();
const { pageX, pageY } = position;
const left = pageX - window.scrollX;
const top = pageY - window.scrollY;
const {
left: areaLeft,
top: areaTop,
width: areaWidth,
height: areaHeight
} = areaPosition;
const inside = left > areaLeft && left < areaLeft + areaWidth && top > areaTop && top < areaTop + areaHeight;
return inside;
}
exports.getEventComposedPath = getEventComposedPath;
exports.hadProtocol = hadProtocol;
exports.hexToRgbA = hexToRgbA;
exports.imageFileToUrl = imageFileToUrl;
exports.imageUrlToFile = imageUrlToFile;
exports.isInside = isInside;
exports.isNullOrUndefined = isNullOrUndefined;
exports.omit = omit;
exports.replaceDeltaImage = replaceDeltaImage;
exports.replaceStrWhiteSpace = replaceStrWhiteSpace;
exports.sanitize = sanitize;
exports.shouldInsertNbspOnSpace = shouldInsertNbspOnSpace;
exports.splitWithBreak = splitWithBreak;
//# sourceMappingURL=editor.utils.cjs.js.map