@zohodesk/dot
Version:
In this Library, we Provide Some Basic Components to Build Your Application
193 lines (154 loc) • 5.16 kB
JavaScript
// editorUtils
export function editorContentValidate(editorObj) {
let content = editorObj && editorObj.getContent && editorObj.getContent() || '';
if (content == '' || !content.length || content == '<div><br></div>' || editorObj.mode !== 'plaintext' && !editorObj.doc.body.innerText.trim().length && !editorObj.doc.getElementsByTagName('img').length && !editorObj.doc.getElementsByTagName('iframe').length && !isValidSpace(editorObj)) {
return false;
}
return true;
}
function isValidSpace(editorObj) {
let uTags = editorObj.doc.body.getElementsByTagName('u');
let isValidContent = false;
for (let i = 0; i < uTags.length; i++) {
let uTag = uTags[i];
let contentLength = uTag.innerText.length;
if (contentLength) {
isValidContent = true;
break;
}
}
return isValidContent;
}
export function fixEncoding(str) {
if (/&lt;|&gt;/g.test(str) == true) {
str = str.replace(/&lt;/g, '<');
str = str.replace(/&gt;/g, '>');
}
return str;
}
export function encodeForHtml(str) {
if (/<|>|&|"|'|\\/g.test(str) == true) {
str = str.replace(/&/g, '&');
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
str = str.replace(/"/g, '"');
str = str.replace(/\\/g, '\');
str = str.replace(/'/g, ''');
}
return str;
} // attachment
export function isFileNameSizeInvalid(fileName, fileNameMaxCharacters) {
return fileName.length > fileNameMaxCharacters;
}
export function isImageFormat(fileName, allowedFileExtensions) {
let extension = getExtensionFromFileName(fileName);
const isValidExtension = allowedFileExtensions.includes(extension);
return isValidExtension;
}
export function getExtensionFromFileName(fileName) {
if (fileName && fileName.indexOf('.') != -1) {
return fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase();
}
return '';
} // common
export function triggerKeyup(elem) {
let keyup = new KeyboardEvent('keyup');
elem && elem.dispatchEvent && elem.dispatchEvent(keyup);
} // editor
export function getRichEditorContent(key) {
let richTextEditor = global.editor[key];
if (richTextEditor != undefined) {
let content = richTextEditor.getContent();
content = content.trim();
if (content == '<br>') {
return '';
}
if (richTextEditor.mode == 'plaintext') {
content = replaceNewLineCharacter(content);
}
return content;
}
return '';
}
function replaceNewLineCharacter(content) {
if (typeof content !== 'undefined') {
content = encodeForHtml(content);
content = content.replace(/\n/g, '<br/>');
}
return content;
}
export const changeContent = (id, value) => {
let editorObj = global.editor && global.editor[id];
if (editorObj) {
let content = fixEncoding(value);
if (editorObj.mode === 'plaintext') {
editorObj.squireInstance.setHTML(content);
editorObj.plainText(editorObj.squireInstance.getHTML());
} else {
editorObj.setContent(content);
}
}
};
export function validateEditorContentOnInsertHtml(node) {
if (node.nodeName === 'IFRAME') {
let iframeSrcRegex = /^(https?:)?\/\/((www\.youtube\.com)|(www\.youtube-nocookie\.com)|(([a-z\-]+\.)?flowplayer\.org)|(www\.useloom\.com)|(player\.vimeo\.com)|(www\.dailymotion\.com)|(w\.soundcloud\.com)|(whatfix\.com)|([a-z\-]+\.zoho\.com)|([a-z\-]+\.google\.com)|([a-z\-]+\.efficientforms\.com)|([a-z0-9\-]+\.microsoftstream\.com)).*$/;
if (!node.getAttribute('src') || !iframeSrcRegex.test(node.getAttribute('src'))) {
node.parentNode.removeChild(node);
}
}
} // loadresource
/* use promise and remove callback*/
export function loadResource(url, type, id, callback) {
return new Promise(resolve => {
let css, script;
if (id && document.getElementById(id)) {
resolve();
return;
}
if (type === 'css') {
css = document.createElement('link');
css.type = 'text/css';
css.rel = 'stylesheet';
css.href = url;
css.id = id || '';
css.onload = function () {
resolve();
};
css.onerror = function () {
resolve();
};
document.getElementsByTagName('head')[0].appendChild(css);
} else if (type === 'js') {
script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.defer = true;
script.onload = function () {
resolve();
if (id) {
script.id = id;
}
if (typeof callback === 'function') {
callback();
}
};
script.onerror = function () {
resolve();
};
document.getElementsByTagName('head')[0].appendChild(script);
}
});
} // bind
export function bind() {
for (var _len = arguments.length, handlers = new Array(_len), _key = 0; _key < _len; _key++) {
handlers[_key] = arguments[_key];
}
handlers.forEach(handler => {
if (__DEVELOPMENT__) {
if (!this[handler]) {
console.log('Handler method not defined: ', handler);
}
}
this[handler] = this[handler].bind(this);
});
}