This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
colour.js
63 lines (57 loc) · 1.56 KB
/
colour.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { createElement } = window.wp.element
const { registerFormatType, applyFormat, removeFormat, getActiveFormat } = window.wp.richText
const { InspectorControls, PanelColorSettings } = window.wp.editor;
[
{
name: 'color',
title: 'Inline Text Colour'
},
{
name: 'background-color',
title: 'Inline Background Colour'
}
].forEach(({ name, title }) => {
const type = `advanced/${name}`
registerFormatType(type, {
title,
tagName: 'span',
className: name,
attributes: {
style: 'style'
},
edit ({ isActive, value, onChange }) {
let activeColor
if (isActive) {
const activeFormat = getActiveFormat(value, type)
const style = activeFormat.attributes.style
activeColor = style.replace(new RegExp(`^${name}:\\s*`), '')
}
return (
createElement(InspectorControls, null,
createElement(PanelColorSettings, {
title,
initialOpen: true,
colorSettings: [
{
value: activeColor,
onChange: (color) => {
if (color) {
onChange(applyFormat(value, {
type,
attributes: {
style: `${name}:${color}`
}
}))
return
}
onChange(removeFormat(value, type))
},
label: 'Apply colour to the selected text.'
}
]
})
)
)
}
})
})