Skip to content

Commit

Permalink
Allow removing last unit typed before select and insert action. (#307)
Browse files Browse the repository at this point in the history
* Allow removing last unit typed before select and insert action.

* keep existing API
  • Loading branch information
jperedadnr authored Apr 3, 2024
1 parent 7b754ac commit d4c02f0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ public Action insertEmoji(Emoji emoji) {
}

public Action selectAndInsertEmoji(Selection selection, Emoji emoji) {
return new BasicAction(control, action -> ACTION_CMD_FACTORY.selectAndInsertEmoji(selection, emoji));
return selectAndInsertEmoji(selection, emoji, false);
}

public Action selectAndInsertEmoji(Selection selection, Emoji emoji, boolean undoLast) {
return new BasicAction(control, action -> ACTION_CMD_FACTORY.selectAndInsertEmoji(selection, emoji, undoLast));
}

public Action insertBlock(Block block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public ActionCmd insertText(String text) {
}

public ActionCmd insertEmoji(Emoji emoji) {
return new ActionCmdInsertEmoji(emoji, null);
return new ActionCmdInsertEmoji(emoji, null, false);
}

public ActionCmd selectAndInsertEmoji(Selection selection, Emoji emoji) {
return new ActionCmdInsertEmoji(emoji, selection);
public ActionCmd selectAndInsertEmoji(Selection selection, Emoji emoji, boolean undoLast) {
return new ActionCmdInsertEmoji(emoji, selection, undoLast);
}

public ActionCmd insertBlock(Block block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ class ActionCmdInsertEmoji implements ActionCmd {

private final Emoji content;
private final Selection selection;
private final boolean undoLast;

public ActionCmdInsertEmoji(Emoji content, Selection selection) {
public ActionCmdInsertEmoji(Emoji content, Selection selection, boolean undoLast) {
this.content = content;
this.selection = selection;
this.undoLast = undoLast;
}

@Override
public void apply(RichTextAreaViewModel viewModel) {
if (Objects.requireNonNull(viewModel).isEditable() && content != null) {
if (selection != null) {
viewModel.getCommandManager().execute(new SelectAndReplaceCmd(
viewModel.getTextBuffer().getInternalSelection(selection), content));
viewModel.getTextBuffer().getInternalSelection(selection), content, undoLast));
} else {
viewModel.getCommandManager().execute(new InsertCmd(content));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ class SelectAndReplaceCmd extends AbstractEditCmd {

private final UnitBuffer content;
private final Selection selection;
private boolean undoLast;

public SelectAndReplaceCmd(Selection selection, String content) {
this.selection = selection;
this.content = UnitBuffer.convertTextToUnits(content);
}

public SelectAndReplaceCmd(Selection selection, Emoji content) {
public SelectAndReplaceCmd(Selection selection, Emoji content, boolean undoLast) {
this.selection = selection;
this.content = new UnitBuffer(new EmojiUnit(content));
this.undoLast = undoLast;
}

public SelectAndReplaceCmd(Selection selection, Block content) {
Expand All @@ -60,6 +62,13 @@ public SelectAndReplaceCmd(Selection selection, Block content) {
public void doRedo(RichTextAreaViewModel viewModel) {
Objects.requireNonNull(viewModel);

if (undoLast) {
// 0. undo adding last unit
// This is needed to prevent an infinite loop if doUndo is called
// (as inserting again that last unit triggers the call to doRedo)
viewModel.undo();
}

// 1. select
if (selection != null) {
viewModel.setSelection(selection);
Expand Down

0 comments on commit d4c02f0

Please sign in to comment.