forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Block from '../blots/block'; | ||
import Container from '../blots/container'; | ||
|
||
class TableCell extends Block { | ||
static create(value) { | ||
const node = super.create(value); | ||
if (value && value.row) { | ||
node.setAttribute('data-row', value.row); | ||
} else { | ||
node.setAttribute( | ||
'data-row', | ||
Math.random() | ||
.toString(36) | ||
.slice(4), | ||
); | ||
} | ||
return node; | ||
} | ||
|
||
static formats(domNode) { | ||
if (domNode.hasAttribute('data-row')) { | ||
return { | ||
row: domNode.getAttribute('data-row'), | ||
}; | ||
} | ||
return null; | ||
} | ||
} | ||
TableCell.blotName = 'table'; | ||
TableCell.tagName = 'TD'; | ||
|
||
class TableBody extends Container {} | ||
TableBody.blotName = 'table-body'; | ||
TableBody.tagName = 'TBODY'; | ||
|
||
class TableRow extends Container { | ||
checkMerge() { | ||
if (super.checkMerge()) { | ||
const thisCell = this.children.tail.formats(); | ||
const nextCell = this.next.children.head.formats(); | ||
return thisCell.table.row === nextCell.table.row; | ||
} | ||
return false; | ||
} | ||
} | ||
TableRow.blotName = 'table-row'; | ||
TableRow.tagName = 'TR'; | ||
|
||
class TableContainer extends Container {} | ||
TableContainer.blotName = 'table-container'; | ||
TableContainer.tagName = 'TABLE'; | ||
|
||
TableContainer.defaultChild = TableBody; | ||
TableContainer.allowedChildren = [TableBody]; | ||
TableBody.requiredParent = TableContainer; | ||
|
||
TableBody.defaultChild = TableRow; | ||
TableBody.allowedChildren = [TableRow]; | ||
TableRow.requiredParent = TableBody; | ||
|
||
TableRow.defaultChild = TableCell; | ||
TableRow.allowedChildren = [TableCell]; | ||
TableCell.requiredParent = TableRow; | ||
|
||
TableCell.requiredParent = TableRow; | ||
|
||
export { TableContainer, TableBody, TableRow, TableCell }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Delta from 'quill-delta'; | ||
import Editor from '../../../core/editor'; | ||
|
||
const tableDelta = new Delta() | ||
.insert('A1') | ||
.insert('\n', { table: { row: 'a' } }) | ||
.insert('A2') | ||
.insert('\n', { table: { row: 'a' } }) | ||
.insert('A3') | ||
.insert('\n', { table: { row: 'a' } }) | ||
.insert('B1') | ||
.insert('\n', { table: { row: 'b' } }) | ||
.insert('B2') | ||
.insert('\n', { table: { row: 'b' } }) | ||
.insert('B3') | ||
.insert('\n', { table: { row: 'b' } }) | ||
.insert('C1') | ||
.insert('\n', { table: { row: 'c' } }) | ||
.insert('C2') | ||
.insert('\n', { table: { row: 'c' } }) | ||
.insert('C3') | ||
.insert('\n', { table: { row: 'c' } }); | ||
|
||
const tableHTML = ` | ||
<table> | ||
<tr> | ||
<td data-row="a">A1</td> | ||
<td data-row="a">A2</td> | ||
<td data-row="a">A3</td> | ||
</tr> | ||
<tr> | ||
<td data-row="b">B1</td> | ||
<td data-row="b">B2</td> | ||
<td data-row="b">B3</td> | ||
</tr> | ||
<tr> | ||
<td data-row="c">C1</td> | ||
<td data-row="c">C2</td> | ||
<td data-row="c">C3</td> | ||
</tr> | ||
</table>`; | ||
|
||
describe('Table', function() { | ||
it('initialize', function() { | ||
const editor = this.initialize(Editor, tableHTML); | ||
expect(editor.getDelta()).toEqual(tableDelta); | ||
expect(this.container).toEqualHTML(tableHTML); | ||
}); | ||
|
||
it('add', function() { | ||
const editor = this.initialize(Editor, ''); | ||
editor.applyDelta(tableDelta.delete(1)); | ||
expect(this.container).toEqualHTML(tableHTML); | ||
}); | ||
}); | ||
|