Skip to content

Commit 5fab7f0

Browse files
committed
move table into module
1 parent 6451f1d commit 5fab7f0

File tree

3 files changed

+62
-44
lines changed

3 files changed

+62
-44
lines changed

formats/table.js

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
1+
import Parchment from 'parchment';
12
import Block from '../blots/block';
23
import Container from '../blots/container';
3-
import Quill from '../core/quill';
44

5-
class TableContainer extends Container {
5+
class TableCell extends Block {
66
static create(value) {
7-
const node = super.create(value);
8-
node.setAttribute('contenteditable', false);
7+
const node = super.create();
8+
if (value && value.row) {
9+
node.setAttribute('data-row', value.row);
10+
} else {
11+
node.setAttribute('data-row', tableId());
12+
}
13+
node.setAttribute('contenteditable', true);
914
return node;
1015
}
11-
}
12-
TableContainer.blotName = 'table-container';
13-
TableContainer.tagName = 'TABLE';
1416

15-
class TableBody extends Container {}
16-
TableBody.blotName = 'table-body';
17-
TableBody.tagName = 'TBODY';
17+
static formats(domNode) {
18+
if (domNode.hasAttribute('data-row')) {
19+
return {
20+
row: domNode.getAttribute('data-row'),
21+
};
22+
}
23+
return undefined;
24+
}
25+
26+
table() {
27+
let cur = this.parent;
28+
while (cur != null && cur.statics.blotName !== 'table-container') {
29+
cur = cur.parent;
30+
}
31+
return cur;
32+
}
33+
}
34+
TableCell.blotName = 'table';
35+
TableCell.tagName = 'TD';
1836

1937
class TableRow extends Container {
2038
checkMerge() {
@@ -29,15 +47,14 @@ class TableRow extends Container {
2947
TableRow.blotName = 'table-row';
3048
TableRow.tagName = 'TR';
3149

32-
class TableCell extends Block {
50+
class TableBody extends Container {}
51+
TableBody.blotName = 'table-body';
52+
TableBody.tagName = 'TBODY';
53+
54+
class TableContainer extends Container {
3355
static create(value) {
34-
const node = super.create();
35-
if (value && value.row) {
36-
node.setAttribute('data-row', value.row);
37-
} else {
38-
node.setAttribute('data-row', tableId());
39-
}
40-
node.setAttribute('contenteditable', true);
56+
const node = super.create(value);
57+
node.setAttribute('contenteditable', false);
4158
return node;
4259
}
4360

@@ -57,31 +74,10 @@ class TableCell extends Block {
5774
blot.optimize(); // Add break blot
5875
});
5976
});
60-
static formats(domNode) {
61-
if (domNode.hasAttribute('data-row')) {
62-
return {
63-
row: domNode.getAttribute('data-row'),
64-
};
65-
}
66-
return undefined;
67-
}
68-
69-
static register() {
70-
Quill.register(TableRow);
71-
Quill.register(TableBody);
72-
Quill.register(TableContainer);
73-
}
74-
75-
table() {
76-
let cur = this.parent;
77-
while (cur != null && cur.statics.blotName !== 'table-container') {
78-
cur = cur.parent;
79-
}
80-
return cur;
8177
}
8278
}
83-
TableCell.blotName = 'table';
84-
TableCell.tagName = 'TD';
79+
TableContainer.blotName = 'table-container';
80+
TableContainer.tagName = 'TABLE';
8581

8682
TableContainer.allowedChildren = [TableBody];
8783
TableBody.requiredContainer = TableContainer;
@@ -98,4 +94,4 @@ function tableId() {
9894
.slice(4);
9995
}
10096

101-
export { TableCell as default, tableId };
97+
export { TableCell, TableRow, TableBody, TableContainer, tableId };

modules/table.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Parchment from 'parchment';
2+
import Block from '../blots/block';
3+
import Container from '../blots/container';
4+
import Quill from '../core/quill';
5+
import Module from '../core/module';
6+
import {
7+
TableCell,
8+
TableRow,
9+
TableBody,
10+
TableContainer,
11+
} from '../formats/table';
12+
13+
class Table extends Module {
14+
static register() {
15+
Quill.register(TableCell);
16+
Quill.register(TableRow);
17+
Quill.register(TableBody);
18+
Quill.register(TableContainer);
19+
}
20+
}
21+
22+
export default Table;

quill.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Indent from './formats/indent';
1111
import Blockquote from './formats/blockquote';
1212
import Header from './formats/header';
1313
import List from './formats/list';
14-
import Table from './formats/table';
1514

1615
import { BackgroundClass, BackgroundStyle } from './formats/background';
1716
import { ColorClass, ColorStyle } from './formats/color';
@@ -32,6 +31,7 @@ import Video from './formats/video';
3231
import CodeBlock, { Code as InlineCode } from './formats/code';
3332

3433
import Syntax from './modules/syntax';
34+
import Table from './modules/table';
3535
import Toolbar from './modules/toolbar';
3636

3737
import Icons from './ui/icons';
@@ -79,7 +79,6 @@ Quill.register(
7979
'formats/code-block': CodeBlock,
8080
'formats/header': Header,
8181
'formats/list': List,
82-
'formats/table': Table,
8382

8483
'formats/bold': Bold,
8584
'formats/code': InlineCode,
@@ -94,6 +93,7 @@ Quill.register(
9493
'formats/video': Video,
9594

9695
'modules/syntax': Syntax,
96+
'modules/table': Table,
9797
'modules/toolbar': Toolbar,
9898

9999
'themes/bubble': BubbleTheme,

0 commit comments

Comments
 (0)