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'; import { FolderUserStore } from 'Stores/User/Folder'; import { SettingsUserStore } from 'Stores/User/Settings'; /** * @param {(string|number)} value * @param {boolean=} includeZero = true * @returns {boolean} */ export function isPosNumeric(value) { return null != value && /^[0-9]*$/.test(value.toString()); } /** * @param {string} html * @returns {string} */ export function htmlToPlain(html) { let pos = 0, limit = 800, iP1 = 0, iP2 = 0, iP3 = 0, text = ''; const tpl = createElement('template'), 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() + ' ' : '' ); }, convertDivs = (...args) => { let divText = 1 < args.length ? args[1].trim() : ''; if (divText.length) { divText = '\n' + 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 = tpl.content.textContent; if (text) { text = text .replace(/\n[ \t]+/gm, '\n') .replace(/[\n]{3,}/gm, '\n\n') .replace(/>/gi, '>') .replace(/</gi, '<') .replace(/&/gi, '&') // wordwrap max line length 100 .match(/.{1,100}(\s|$)|\S+?(\s|$)/g).join('\n'); } while (0 < --limit) { iP1 = text.indexOf('__bq__start__', pos); if (0 > iP1) { break; } 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; } } 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, '