Skip to content

Commit

Permalink
prevent breaking table after back key events (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
jperedadnr authored Aug 4, 2023
1 parent 24c0927 commit d31fc32
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions rta/src/main/java/com/gluonhq/richtextarea/RichTextAreaSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,42 @@ interface ActionBuilder extends Function<KeyEvent, ActionCmd>{}
} else if (paragraph.getStart() == caret) {
// check backspace at beginning of paragraph:
if (decoration.getGraphicType() != ParagraphDecoration.GraphicType.NONE) {
// remove graphic type decoration
return ACTION_CMD_FACTORY.decorate(ParagraphDecoration.builder().fromDecoration(decoration).graphicType(ParagraphDecoration.GraphicType.NONE).build());
} else if (decoration.getIndentationLevel() > 0) {
// decrease indentation level
return ACTION_CMD_FACTORY.decorate(ParagraphDecoration.builder().fromDecoration(decoration).indentationLevel(decoration.getIndentationLevel() - 1).build());
} else {
// if previous paragraph is a table:
int index = viewModel.getParagraphList().indexOf(paragraph);
if (index > 0) {
if (viewModel.getParagraphList().get(index - 1).getDecoration().hasTableDecoration()) {
// just move to last cell
return ACTION_CMD_FACTORY.caretMove(Direction.BACK, false, false, false);
}
}
}
}
}
return ACTION_CMD_FACTORY.removeText(-1);
}),
entry( new KeyCodeCombination(BACK_SPACE, SHORTCUT_DOWN, SHIFT_ANY), e -> {
int caret = viewModel.getCaretPosition();
Paragraph paragraph = viewModel.getParagraphWithCaret().orElse(null);
ParagraphDecoration decoration = viewModel.getDecorationAtParagraph();
if (paragraph != null && decoration != null && decoration.hasTableDecoration()) {
// TODO: remove cell content, else if empty move to prev cell
return null;
} else if (paragraph != null && paragraph.getStart() == caret) {
// if previous paragraph is a table:
int index = viewModel.getParagraphList().indexOf(paragraph);
if (index > 0) {
if (viewModel.getParagraphList().get(index - 1).getDecoration().hasTableDecoration()) {
// just move to last cell
return ACTION_CMD_FACTORY.caretMove(Direction.BACK, false, false, false);
}
}
}
if (Tools.MAC) {
// CMD + BACKSPACE or CMD + SHIFT + BACKSPACE removes line in Mac
return ACTION_CMD_FACTORY.removeText(0, RichTextAreaViewModel.Remove.LINE);
Expand All @@ -234,6 +261,22 @@ interface ActionBuilder extends Function<KeyEvent, ActionCmd>{}
}),
entry( new KeyCodeCombination(BACK_SPACE, ALT_DOWN), e -> {
if (Tools.MAC) {
int caret = viewModel.getCaretPosition();
Paragraph paragraph = viewModel.getParagraphWithCaret().orElse(null);
ParagraphDecoration decoration = viewModel.getDecorationAtParagraph();
if (paragraph != null && decoration != null && decoration.hasTableDecoration()) {
// TODO: remove prev word from cell if any, else if empty move to prev cell, else nothing
return null;
} else if (paragraph != null && paragraph.getStart() == caret) {
// if previous paragraph is a table:
int index = viewModel.getParagraphList().indexOf(paragraph);
if (index > 0) {
if (viewModel.getParagraphList().get(index - 1).getDecoration().hasTableDecoration()) {
// just move to last cell
return ACTION_CMD_FACTORY.caretMove(Direction.BACK, false, false, false);
}
}
}
return ACTION_CMD_FACTORY.removeText(0, RichTextAreaViewModel.Remove.WORD);
}
return null;
Expand Down

0 comments on commit d31fc32

Please sign in to comment.