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