Skip to content

Commit

Permalink
fix: cannot exit table in wysiwyg (#2250)
Browse files Browse the repository at this point in the history
* fix: add exitTable command to exit table in wysiwyg

* chore: add test case(table keymap)

* chore: remove only keyword in test
  • Loading branch information
js87zz authored Feb 8, 2022
1 parent c689091 commit 9ecc097
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
29 changes: 29 additions & 0 deletions apps/editor/src/__test__/unit/wysiwyg/keymap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,35 @@ describe('keymap', () => {

expect(wwe.getHTML()).toBe(expected);
});

describe('exitTable keymap', () => {
it('should exit the table node and add paragraph', () => {
wwe.setSelection(5, 5); // in 'foo' cell

forceKeymapFn('table', 'exitTable');

const expected = oneLineTrim`
<table>
<thead>
<tr>
<th><p>foo</p></th>
<th><p>bar</p></th>
</tr>
</thead>
<tbody>
<tr>
<td><p>baz</p></td>
<td><p>qux</p></td>
</tr>
</tbody>
</table>
<p><br></p>
`;

expect(wwe.getHTML()).toBe(expected);
expect(wwe.getSelection()).toEqual([39, 39]); // in added paragraph
});
});
});

describe('table with list and multiple lines', () => {
Expand Down
9 changes: 7 additions & 2 deletions apps/editor/src/wysiwyg/command/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,15 @@ export function addParagraphBeforeTable(tr: Transaction, map: TableOffsetMap, sc
return tr.setSelection(Selection.near(tableStartPos, -1));
}

export function addParagraphAfterTable(tr: Transaction, map: TableOffsetMap, schema: Schema) {
export function addParagraphAfterTable(
tr: Transaction,
map: TableOffsetMap,
schema: Schema,
forcedAddtion = false
) {
const tableEndPos = tr.doc.resolve(map.tableEndOffset);

if (!tableEndPos.nodeAfter) {
if (forcedAddtion || !tableEndPos.nodeAfter) {
return addParagraph(tr, tableEndPos, schema);
}
return tr.setSelection(Selection.near(tableEndPos, 1));
Expand Down
26 changes: 26 additions & 0 deletions apps/editor/src/wysiwyg/nodes/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,30 @@ export class Table extends NodeSchema {
};
}

private exitTable(): Command {
return (state, dispatch) => {
const { selection, tr, schema } = state;
const { $from } = selection;
const cell = findNodeBy(
$from,
({ type }) => type.name === 'tableHeadCell' || type.name === 'tableBodyCell'
);

if (cell) {
const para = findNodeBy($from, ({ type }) => type.name === 'paragraph');

if (para) {
const { anchor } = getResolvedSelection(selection);
const map = TableOffsetMap.create(anchor)!;

dispatch!(addParagraphAfterTable(tr, map, schema, true));
return true;
}
}
return false;
};
}

commands() {
return {
addTable: this.addTable(),
Expand Down Expand Up @@ -451,6 +475,8 @@ export class Table extends NodeSchema {
'Mod-Backspace': deleteCellContent,
Delete: deleteCellContent,
'Mod-Delete': deleteCellContent,

'Mod-Enter': this.exitTable(),
};
}
}

0 comments on commit 9ecc097

Please sign in to comment.