refactoring
This commit is contained in:
@@ -1,26 +1,29 @@
|
|||||||
import { Decoration, DecorationSet } from 'prosemirror-view'
|
import { Decoration, DecorationSet } from 'prosemirror-view'
|
||||||
import { Node } from 'prosemirror-model'
|
import { Node } from 'prosemirror-model'
|
||||||
|
|
||||||
export default function (doc: Node) {
|
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: any[] = []
|
const results: {
|
||||||
const decorations: [any?] = []
|
color: string,
|
||||||
|
from: number,
|
||||||
|
to: number,
|
||||||
|
}[] = []
|
||||||
|
const decorations: Decoration[] = []
|
||||||
|
|
||||||
doc.descendants((node: any, position: any) => {
|
doc.descendants((node, position) => {
|
||||||
if (!node.isText) {
|
if (!node.text) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let matches
|
[...node.text.matchAll(hexColor)].forEach(match => {
|
||||||
|
const index = match.index || 0
|
||||||
|
|
||||||
// eslint-disable-next-line
|
|
||||||
while (matches = hexColor.exec(node.text)) {
|
|
||||||
results.push({
|
results.push({
|
||||||
color: matches[0],
|
color: match[0],
|
||||||
from: position + matches.index,
|
from: position + index,
|
||||||
to: position + matches.index + matches[0].length,
|
to: position + index + match[0].length,
|
||||||
})
|
})
|
||||||
}
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
results.forEach(issue => {
|
results.forEach(issue => {
|
||||||
|
|||||||
Reference in New Issue
Block a user