Skip to content

✨ Editor API for AI Text Suggestion #653

Open
@simon-lund

Description

@simon-lund

I would like to implment a feature similar to Github Copilot for text editing.
That is, the AI can make text suggestions at the current cursor position, which can then be added via press on "Tab".

I have looked through the docs and I could do this with insertInlineContent and use "gray" as text color. This, however, inserts the text into the block immediately. So, if the user doesn't want it there, I wold have to remove the text somehow.

Instead it would be nice to have a insertDiscardableContent function, which displays text at the current text but does not add it to the block right away. It could have a callback function, where the develeoper can implement listeners and what not.

This could look something like this (Written with GPT):

/**
 * Inserts a text suggestion at the current cursor position in the editor.
 * This text is not permanently added to the document and can be accepted or discarded by the user.
 * 
 * @param suggestion The text suggestion to be displayed.
 * @param callback A callback function that handles the user's decision to accept or discard the suggestion.
 */
function insertDiscardableContent(suggestion, callback) {
  // Implementation details for displaying the suggestion go here.
  // This could involve manipulating the DOM to visually present the suggestion near the cursor.
  
  // Example callback usage
  callback(submit, discard);
}

// Usage
insertDiscardableContent("Here's a suggestion for your text.", (submit, discard) => {
  // Implement listeners for user actions.
  // "submit" and "discard" are functions that can be called to finalize the user's choice.
  
  // Example: If the user presses "Enter", call submit().
  document.addEventListener('keydown', (event) => {
    if (event.key === "Enter") {
      submit(); // This would finalize the insertion of the suggestion.
    } else if (event.key === "Escape") {
      discard(); // This would remove the suggestion without altering the text.
    }
  });
});

Originally posted by @simon-lund in #122 (comment)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions