import { ComposeType, FolderType } from 'Common/EnumsUser'; import { EmailModel } from 'Model/Email'; import { encodeHtml } from 'Common/Html'; import { isArray } from 'Common/Utils'; import { createElement } from 'Common/Globals'; /** * @param {(string|number)} value * @param {boolean=} includeZero = true * @returns {boolean} */ export function isPosNumeric(value, includeZero = true) { return null != value && (includeZero ? /^[0-9]*$/ : /^[1-9]+[0-9]*$/).test(value.toString()); } /** * @param {string} text * @param {number=} len = 100 * @returns {string} */ function splitPlainText(text, len = 100) { let prefix = '', subText = '', result = text, spacePos = 0, newLinePos = 0; while (result.length > len) { subText = result.substr(0, len); spacePos = subText.lastIndexOf(' '); newLinePos = subText.lastIndexOf('\n'); if (-1 !== newLinePos) { spacePos = newLinePos; } if (-1 === spacePos) { spacePos = len; } prefix += subText.substr(0, spacePos) + '\n'; result = result.substr(spacePos + 1); } return prefix + result; } /** * @param {string} html * @returns {string} */ export function htmlToPlain(html) { let pos = 0, limit = 0, iP1 = 0, iP2 = 0, iP3 = 0, text = ''; const convertBlockquote = (blockquoteText) => { blockquoteText = '> ' + blockquoteText.trim().replace(/\n/gm, '\n> '); return blockquoteText.replace(/(^|\n)([> ]+)/gm, (...args) => args && 2 < args.length ? args[1] + args[2].replace(/[\s]/g, '').trim() + ' ' : '' ); }; const convertDivs = (...args) => { if (args && 1 < args.length) { let divText = args[1].trim(); if (divText.length) { divText = divText.replace(/
]*><\/p>/gi, '') .replace(/
]*>([\s\S\r\n\t]*)<\/pre>/gim, convertPre) .replace(/[\s]+/gm, ' ') .replace(/((?:href|data)\s?=\s?)("[^"]+?"|'[^']+?')/gim, fixAttibuteValue) .replace(/
]*>/gim, '\n') .replace(/<\/h[\d]>/gi, '\n') .replace(/<\/p>/gi, '\n\n') .replace(/
]*>/gim, '\n__bq__start__\n') .replace(/<\/blockquote>/gim, '\n__bq__end__\n') .replace(/]*>([\s\S\r\n]*?)<\/a>/gim, convertLinks) .replace(/<\/div>/gi, '\n') .replace(/ /gi, ' ') .replace(/"/gi, '"') .replace(/<[^>]*>/gm, ''); text = splitPlainText(tpl.content.textContent .replace(/\n[ \t]+/gm, '\n') .replace(/[\n]{3,}/gm, '\n\n') .replace(/>/gi, '>') .replace(/</gi, '<') .replace(/&/gi, '&') ); pos = 0; limit = 800; while (0 < limit) { --limit; iP1 = text.indexOf('__bq__start__', pos); if (-1 < iP1) { iP2 = text.indexOf('__bq__start__', iP1 + 5); iP3 = text.indexOf('__bq__end__', iP1 + 5); if ((-1 === iP2 || iP3 < iP2) && iP1 < iP3) { text = text.substr(0, iP1) + convertBlockquote(text.substring(iP1 + 13, iP3)) + text.substr(iP3 + 11); pos = 0; } else if (-1 < iP2 && iP2 < iP3) { pos = iP2 - 1; } else { pos = 0; } } else { break; } } return text.replace(/__bq__start__|__bq__end__/gm, '').trim(); } /** * @param {string} plain * @param {boolean} findEmailAndLinksInText = false * @returns {string} */ export function plainToHtml(plain) { plain = plain.toString().replace(/\r/g, ''); plain = plain.replace(/^>[> ]>+/gm, ([match]) => (match ? match.replace(/[ ]+/g, '') : match)); let bIn = false, bDo = true, bStart = true, aNextText = [], aText = plain.split('\n'); do { bDo = false; aNextText = []; aText.forEach(sLine => { bStart = '>' === sLine.substr(0, 1); if (bStart && !bIn) { bDo = true; bIn = true; aNextText.push('~~~blockquote~~~'); aNextText.push(sLine.substr(1)); } else if (!bStart && bIn) { if (sLine) { bIn = false; aNextText.push('~~~/blockquote~~~'); aNextText.push(sLine); } else { aNextText.push(sLine); } } else if (bStart && bIn) { aNextText.push(sLine.substr(1)); } else { aNextText.push(sLine); } }); if (bIn) { bIn = false; aNextText.push('~~~/blockquote~~~'); } aText = aNextText; } while (bDo); return aText.join('\n') // .replace(/~~~\/blockquote~~~\n~~~blockquote~~~/g, '\n') .replace(/&/g, '&') .replace(/>/g, '>') .replace(/') .replace(/[\s]*~~~\/blockquote~~~/g, '') .replace(/\n/g, '