Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(extension-highlight): add paste rule #5527

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/extension-highlight/src/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,49 @@ export const Highlight = Mark.create<HighlightOptions>({
find: pasteRegex,
type: this.type,
}),
markPasteRule({
find: (_, event) => {
const htmlStr = event?.clipboardData?.getData('text/html')

if (!htmlStr) {
return
}

const doc = new DOMParser().parseFromString(htmlStr, 'text/html')

let indexAcc = 0

// skip if there are any paragraphs as it will make the character counting a lot harder
if (doc.querySelectorAll('p').length > 0) {
return
}

return [...doc.querySelectorAll('span')].map(el => {
const color = el.style.backgroundColor
const text = el.textContent
const index = indexAcc

if (!text) {
return
}

// we only want to count text for elements which have the text node as a direct child
if (el.childNodes.length === 1 && el.childNodes[0].nodeType === Node.TEXT_NODE) {
indexAcc += text.length
}
if (!color || color === 'rgb(255, 255, 255)' || color === 'transparent') {
return
}

return { text, data: { color }, index }
}).filter(m => !!m)
},
type: this.type,
getAttributes: match => ({
'data-color': match.data?.color,
style: `background-color: ${match.data?.color}; color: inherit`,
}),
}),
]
},
})