refactoring

This commit is contained in:
Philipp Kühn
2021-02-06 20:10:49 +01:00
parent 05be43ac54
commit 9a65d5a7d6

View File

@@ -3,11 +3,6 @@ import { Node } from 'prosemirror-model'
export default function (doc: Node): DecorationSet {
const hexColor = /(#[0-9a-f]{3,6})\b/ig
const results: {
color: string,
from: number,
to: number,
}[] = []
const decorations: Decoration[] = []
doc.descendants((node, position) => {
@@ -18,21 +13,17 @@ export default function (doc: Node): DecorationSet {
Array
.from(node.text.matchAll(hexColor))
.forEach(match => {
const color = match[0]
const index = match.index || 0
results.push({
color: match[0],
from: position + index,
to: position + index + match[0].length,
})
})
})
results.forEach(issue => {
decorations.push(Decoration.inline(issue.from, issue.to, {
const from = position + index
const to = position + index + match[0].length
const decoration = Decoration.inline(from, to, {
class: 'color',
style: `--color: ${issue.color}`,
}))
style: `--color: ${color}`,
})
decorations.push(decoration)
})
})
return DecorationSet.create(doc, decorations)