Files
libredesk/frontend/src/utils/strings.js
Abhinav Raut b1e1dff3eb feat: replace quill editor with tiptap editor, removes the stupid hack as both editors handle new lines and empty content differently.
Quill adds <p><br></p> for new lines, while Tiptap uses <br> for Shift + Enter and <p> for Enter.

This commit fixes this hack I had added, now all editors in Libredesk are tiptap editors.

fix: Typography for agent and contact message bubbles and macro preview, as tailwind removes browser defaults. Introduces new class `native-html` for this.

fix: removes hardcoded classes in tiptap starter kit configuration as the new class `native-html` takes care of it and has to be just applied.

fix: Form validation for automations and macro form.

fix: automation list padding between items.

feat: adds bullet list and ordered list menu options to tiptap editor.
2025-03-02 01:42:17 +05:30

60 lines
2.0 KiB
JavaScript

// Adds titleCase property to string.
String.prototype.titleCase = function () {
return this.toLowerCase()
.split(' ')
.map(function (word) {
return word.charAt(0).toUpperCase() + word.slice(1)
})
.join(' ')
}
/**
* Replaces the `src` attribute of all <img> tags with the class `inline-image`
* to use the value of the `title` attribute as a Content-ID (cid).
* The resulting `src` will be in the format `cid:content_id`
*
* @param {string} htmlString - The input HTML string.
* @returns {string} - The updated HTML string with `src` replaced by `cid:title`.
*/
export function transformImageSrcToCID (htmlString) {
return htmlString.replace(/(<img\s+class="inline-image"[^>]*?src=")[^"]*(".*?title=")([^"]*)("[^>]*?>)/g, '$1cid:$3$2$3$4');
}
/**
* Reverts the `src` attribute of all <img> tags with the class `inline-image`
* from the `cid:filename` format to `/uploads/filename`, where the filename is stored in the `title` attribute.
*
* @param {string} htmlString - The input HTML string.
* @returns {string} - The updated HTML string with `cid:title` replaced by `/uploads/title`.
*/
export function revertCIDToImageSrc (htmlString) {
return htmlString.replace(/(<img\s+class="inline-image"[^>]*?src=")cid:([^"]*)(".*?title=")\2("[^>]*?>)/g, '$1/uploads/$2$3$2$4');
}
export function validateEmail (email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return emailRegex.test(email)
}
export const isGoDuration = (value) => {
const regex = /^[0-9]+[smh]$/
return regex.test(value)
}
export const isGoHourMinuteDuration = (value) => {
const regex = /^([0-9]+h|[0-9]+m)$/
return regex.test(value)
}
const template = document.createElement('template')
export function getTextFromHTML(htmlString) {
try {
template.innerHTML = htmlString
const text = template.content.textContent || template.content.innerText || ''
template.innerHTML = ''
return text.trim()
} catch (error) {
console.error('Error converting HTML to text:', error)
return ''
}
}