search/app/utils/textUtilities.ts

53 lines
1.5 KiB
TypeScript

/* Copy to Clipboard */
export async function copyToClipboard(doc: ItemObject) {
const toast = useToast()
const selection = window.getSelection()
const container = document.createElement('div')
const range = selection?.getRangeAt(0)
container.appendChild(range.cloneContents())
const htmlOutput = container.innerHTML + '<br/>' + doc.title + '<br/>' + formatSignature(doc)
const textOutput = selection?.toString() + '\n\n' + doc.title + '\n' + formatSignature(doc)
try {
// Modern Clipboard API requires ClipboardItem
const clipboardItem = new ClipboardItem({
'text/html': new Blob([htmlOutput], { type: 'text/html' }),
'text/plain': new Blob([textOutput], { type: 'text/plain' })
})
await navigator.clipboard.write([clipboardItem])
toast.add({
title: 'Texto copiado!',
description: `${truncateAtWord(textOutput, 150)}`,
icon: 'ph-clipboard-text',
color: doc.type == 'activities' ? 'success' : 'info'
})
} catch (err) {
console.error('Failed to copy: ', err)
}
}
const truncateAtWord = (str: string, limit: number) => {
if (str.length <= limit) return str
const subString = str.slice(0, limit)
return subString.slice(0, subString.lastIndexOf('')) + '...'
}
export function debugDocument(json: []) {
let data = ''
// Iterate through keys and values
for (const key in json) {
if (json.hasOwnProperty(key)) {
data += `${key}: ${json[key]}\n\r`
}
}
return data
}
export function addNote() {
return 'yay'
}