Skip to content

Commit 87c35dd

Browse files
committed
chore(table): use upstream table cell align attribute
Cell alignment support was added upstream in ueberdosis/tiptap#7610 Signed-off-by: Jonas <jonas@freesources.org>
1 parent a4bda84 commit 87c35dd

7 files changed

Lines changed: 34 additions & 88 deletions

File tree

src/nodes/Table/Table.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ export default Table.extend({
101101
for (let i = 0; i < lastRow.childCount; i++) {
102102
tr.setNodeAttribute(
103103
pos,
104-
'textAlign',
105-
lastRow.child(i).attrs.textAlign,
104+
'align',
105+
lastRow.child(i).attrs.align,
106106
)
107107
pos += newRow.child(i).nodeSize
108108
}
@@ -129,8 +129,8 @@ export default Table.extend({
129129
for (let i = 0; i < lastRow.childCount; i++) {
130130
tr.setNodeAttribute(
131131
pos,
132-
'textAlign',
133-
lastRow.child(i).attrs.textAlign,
132+
'align',
133+
lastRow.child(i).attrs.align,
134134
)
135135
pos += newRow.child(i).nodeSize
136136
}

src/nodes/Table/TableCell.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,6 @@ export default TableCell.extend({
3131
]
3232
},
3333

34-
addAttributes() {
35-
return {
36-
...this.parent?.(),
37-
textAlign: {
38-
rendered: true,
39-
default: null,
40-
renderHTML: (attributes) => {
41-
if (!attributes.textAlign) {
42-
return {}
43-
}
44-
return {
45-
style: `text-align: ${attributes.textAlign}`,
46-
}
47-
},
48-
parseHTML: (element) => element.style.textAlign || null,
49-
},
50-
}
51-
},
52-
5334
renderHTML({ HTMLAttributes }) {
5435
const attributes = mergeAttributes(
5536
this.options.HTMLAttributes,

src/nodes/Table/TableCellView.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
-->
55

66
<template>
7-
<NodeViewWrapper data-text-el="table-cell" as="td" :dir="dir" :style="textAlign">
7+
<NodeViewWrapper data-text-el="table-cell" as="td" :dir="dir" :style="align">
88
<div class="container">
99
<NodeViewContent class="content" />
1010
<NcActions v-if="isEditable" data-text-table-actions="row" size="small">
@@ -78,8 +78,8 @@ export default {
7878
}
7979
},
8080
computed: {
81-
textAlign() {
82-
return { 'text-align': this.node.attrs.textAlign }
81+
align() {
82+
return { 'text-align': this.node.attrs.align }
8383
},
8484
dir() {
8585
return this.node.attrs.dir || ''

src/nodes/Table/TableHeader.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,4 @@ export default TableHeader.extend({
3737
}
3838
return ['th', attributes, 0]
3939
},
40-
41-
addAttributes() {
42-
return {
43-
...this.parent?.(),
44-
textAlign: {
45-
rendered: true,
46-
default: null,
47-
renderHTML: (attributes) => {
48-
if (!attributes.textAlign) {
49-
return {}
50-
}
51-
return {
52-
style: `text-align: ${attributes.textAlign}`,
53-
}
54-
},
55-
parseHTML: (element) => element.style.textAlign || null,
56-
},
57-
}
58-
},
5940
})

src/nodes/Table/TableHeaderView.vue

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
-->
55

66
<template>
7-
<NodeViewWrapper
8-
data-text-el="table-header"
9-
as="th"
10-
:dir="dir"
11-
:style="textAlign">
7+
<NodeViewWrapper data-text-el="table-header" as="th" :dir="dir" :style="align">
128
<div>
139
<NodeViewContent class="content" />
1410
<NcActions
@@ -23,8 +19,8 @@
2319
:aria-label="t('text', 'Left align column')"
2420
type="radio"
2521
value="left"
26-
:model-value="node.attrs.textAlign"
27-
@click="alignLeft">
22+
:model-value="node.attrs.align"
23+
@click="setAlignLeft">
2824
<template #icon>
2925
<AlignHorizontalLeft />
3026
</template>
@@ -34,8 +30,8 @@
3430
:aria-label="t('text', 'Center align column')"
3531
type="radio"
3632
value="center"
37-
:model-value="node.attrs.textAlign"
38-
@click="alignCenter">
33+
:model-value="node.attrs.align"
34+
@click="setAlignCenter">
3935
<template #icon>
4036
<AlignHorizontalCenter />
4137
</template>
@@ -45,8 +41,8 @@
4541
:aria-label="t('text', 'Right align column')"
4642
type="radio"
4743
value="right"
48-
:model-value="node.attrs.textAlign"
49-
@click="alignRight">
44+
:model-value="node.attrs.align"
45+
@click="setAlignRight">
5046
<template #icon>
5147
<AlignHorizontalRight />
5248
</template>
@@ -134,8 +130,8 @@ export default {
134130
}
135131
},
136132
computed: {
137-
textAlign() {
138-
return { 'text-align': this.node.attrs.textAlign }
133+
align() {
134+
return { 'text-align': this.node.attrs.align }
139135
},
140136
dir() {
141137
return this.node.attrs.dir || ''
@@ -148,24 +144,24 @@ export default {
148144
})
149145
},
150146
methods: {
151-
alignCenter() {
152-
this.align('center')
147+
setAlignCenter() {
148+
this.setAlign('center')
153149
},
154-
alignLeft() {
155-
this.align('left')
150+
setAlignLeft() {
151+
this.setAlign('left')
156152
},
157-
alignRight() {
158-
this.align('right')
153+
setAlignRight() {
154+
this.setAlign('right')
159155
},
160-
align(textAlign) {
156+
setAlign(align) {
161157
this.editor
162158
.chain()
163159
.focus()
164160
.setTextSelection(this.getPos())
165-
.setCellAttribute('textAlign', textAlign)
161+
.setCellAttribute('align', align)
166162
.run()
167163
while (this.editor.commands.goToNextRow()) {
168-
this.editor.commands.setCellAttribute('textAlign', textAlign)
164+
this.editor.commands.setCellAttribute('align', align)
169165
}
170166
// Set focus back to first row
171167
this.editor.chain().setTextSelection(this.getPos()).focus().run()

src/nodes/Table/markdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ function tableToMarkdown(state: MarkdownSerializerState, node: Node) {
198198
row.length = Math.max(row.length, lines.length)
199199
const lineLength = Math.max(...lines.map((line) => line.length))
200200
columnWidths[cellIdx] = Math.max(columnWidths[cellIdx], lineLength)
201-
const align = node.attrs?.textAlign ?? ''
201+
const align = node.attrs?.align ?? ''
202202
row.cells.push({ md, lines, nodeTypes, align })
203203
})
204204
})

src/tests/nodes/Table.spec.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,13 @@ describe('Table extension', () => {
114114
editor.state.doc,
115115
table(
116116
thead(
117-
th({ dir: 'ltr', textAlign: 'center' }, 'heading'),
118-
th({ dir: 'ltr', textAlign: 'right' }, 'heading 2'),
117+
th({ dir: 'ltr', align: 'center' }, 'heading'),
118+
th({ dir: 'ltr', align: 'right' }, 'heading 2'),
119119
th({ dir: 'ltr' }, 'heading 3'),
120120
),
121121
tr(
122-
td(
123-
{ dir: 'ltr', textAlign: 'center' },
124-
p({ dir: 'ltr' }, 'center'),
125-
),
126-
td(
127-
{ dir: 'ltr', textAlign: 'right' },
128-
p({ dir: 'ltr' }, 'right'),
129-
),
122+
td({ dir: 'ltr', align: 'center' }, p({ dir: 'ltr' }, 'center')),
123+
td({ dir: 'ltr', align: 'right' }, p({ dir: 'ltr' }, 'right')),
130124
td(
131125
{ dir: 'ltr' },
132126
p(
@@ -148,19 +142,13 @@ describe('Table extension', () => {
148142
editor.state.doc,
149143
table(
150144
thead(
151-
th({ dir: 'ltr', textAlign: 'center' }, 'heading'),
152-
th({ dir: 'ltr', textAlign: 'right' }, 'heading 2'),
145+
th({ dir: 'ltr', align: 'center' }, 'heading'),
146+
th({ dir: 'ltr', align: 'right' }, 'heading 2'),
153147
th({ dir: 'ltr' }, 'heading 3'),
154148
),
155149
tr(
156-
td(
157-
{ dir: 'ltr', textAlign: 'center' },
158-
p({ dir: 'ltr' }, 'center'),
159-
),
160-
td(
161-
{ dir: 'ltr', textAlign: 'right' },
162-
p({ dir: 'ltr' }, 'right'),
163-
),
150+
td({ dir: 'ltr', align: 'center' }, p({ dir: 'ltr' }, 'center')),
151+
td({ dir: 'ltr', align: 'right' }, p({ dir: 'ltr' }, 'right')),
164152
td(
165153
{ dir: 'ltr' },
166154
p(

0 commit comments

Comments
 (0)