mirror of
https://github.com/zammad/zammad
synced 2026-05-24 09:48:36 +00:00
34 lines
837 B
TypeScript
34 lines
837 B
TypeScript
// Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/
|
|
|
|
export interface SelectionData {
|
|
text: string
|
|
html: string
|
|
selection: Selection | null
|
|
}
|
|
|
|
export const getCurrentSelectionData = (): SelectionData => {
|
|
let text = ''
|
|
let html = ''
|
|
let sel: Selection | null = null
|
|
if (window.getSelection) {
|
|
sel = window.getSelection()
|
|
text = sel?.toString() || ''
|
|
} else if (document.getSelection) {
|
|
sel = document.getSelection()
|
|
text = sel?.toString() || ''
|
|
}
|
|
|
|
if (sel && sel.rangeCount) {
|
|
const container = document.createElement('div')
|
|
for (let i = 1; i <= sel.rangeCount; i += 1) {
|
|
container.appendChild(sel.getRangeAt(i - 1).cloneContents())
|
|
}
|
|
html = container.innerHTML
|
|
}
|
|
|
|
return {
|
|
text: text.toString().trim() || '',
|
|
html,
|
|
selection: sel,
|
|
}
|
|
}
|