Skip to content

Commit 80d09bc

Browse files
committed
feat(renderer): support textStyle color and backgroundColor\n\n- Map attrs.color and attrs.backgroundColor to inline styles\n- Add tests for color and background color rendering\n\nCloses # if applicable
1 parent 549a969 commit 80d09bc

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

src/components/JsonRenderer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ function stylesFromAttrs(attrs?: JSONContent['attrs']) {
1515
case 'textAlign':
1616
style.textAlign = value
1717
break
18+
case 'color':
19+
if (value)
20+
style.color = value
21+
break
22+
case 'backgroundColor':
23+
if (value)
24+
style.backgroundColor = value
25+
break
1826
case 'fontFamily':
1927
if (value)
2028
style.fontFamily = value
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { mount } from '@vue/test-utils'
3+
import { defineComponent } from 'vue'
4+
import type { JSONContent } from '../../src'
5+
import { JsonRenderer } from '../../src'
6+
7+
const WrappedJsonRenderer = defineComponent({
8+
components: { JsonRenderer },
9+
props: {
10+
content: {
11+
type: Object as () => JSONContent,
12+
required: true,
13+
},
14+
},
15+
template: '<JsonRenderer v-bind="$props" />',
16+
})
17+
18+
describe('jsonRenderer background color support', () => {
19+
it('renders text with inline background color from textStyle mark', () => {
20+
const wrapper = mount(WrappedJsonRenderer, {
21+
props: {
22+
content: {
23+
type: 'doc',
24+
content: [
25+
{
26+
type: 'paragraph',
27+
content: [
28+
{
29+
type: 'text',
30+
text: 'highlighted',
31+
marks: [
32+
{
33+
type: 'textStyle',
34+
attrs: { backgroundColor: '#ffee99' },
35+
},
36+
],
37+
},
38+
],
39+
},
40+
],
41+
},
42+
},
43+
})
44+
45+
const el = wrapper.find('span').element as HTMLElement
46+
const styleAttr = el.getAttribute('style') || ''
47+
expect(styleAttr).toMatch(/background-color:\s*(#ffee99|rgb\()/)
48+
})
49+
})
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { mount } from '@vue/test-utils'
3+
import { defineComponent } from 'vue'
4+
import type { JSONContent } from '../../src'
5+
import { JsonRenderer } from '../../src'
6+
7+
const WrappedJsonRenderer = defineComponent({
8+
components: { JsonRenderer },
9+
props: {
10+
content: {
11+
type: Object as () => JSONContent,
12+
required: true,
13+
},
14+
},
15+
template: '<JsonRenderer v-bind="$props" />',
16+
})
17+
18+
describe('jsonRenderer color support', () => {
19+
it('renders text with inline color from textStyle mark', () => {
20+
const wrapper = mount(WrappedJsonRenderer, {
21+
props: {
22+
content: {
23+
type: 'doc',
24+
content: [
25+
{
26+
type: 'paragraph',
27+
content: [
28+
{
29+
type: 'text',
30+
text: 'colored',
31+
marks: [
32+
{
33+
type: 'textStyle',
34+
attrs: { color: '#5dc2da' },
35+
},
36+
],
37+
},
38+
],
39+
},
40+
],
41+
},
42+
},
43+
})
44+
45+
const el = wrapper.find('span').element as HTMLElement
46+
// Check that color style is applied (jsdom may normalize to rgb)
47+
const styleAttr = el.getAttribute('style') || ''
48+
expect(styleAttr).toMatch(/color:\s*(#5dc2da|rgb\()/)
49+
})
50+
})

0 commit comments

Comments
 (0)