Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cannot exit table in wysiwyg #2250

Merged
merged 3 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
};
}
}