Webbrother / react-headless-mde

React headless markdown editor
4 stars 2 forks source link

New API with command context #22

Open Webbrother opened 1 year ago

Webbrother commented 1 year ago

At the moment there is no way to pass command context which could be necessary for such a command like attachment to pass Promise to the command.

Current implementation: commandController.executeCommand('myCommandName');

1st Idea

The idea: commandController.executeCommand('myCommandName', commandContext);

To implement such functionality it is necessary

2nd Idea

Current API can be changed this way:

  class CommandWithoutContext extends BaseCommand {...};
  class CommandWithContext extends BaseCommand<number> {...};

  const { ref, commands {
     commandName1,
     commandName2,
  }} = useTextAreaMarkdownEditor({
    commandMap: {
      commandName1: CommandWithoutContext,
      commandName2: CommandWithContext,
    },
  });

  commandName1(); // No error
  commandName1(42); // Error: 0 arguments expected

  commandName2(); // Error: 1 argument expected
  commandName2(42); // No error

Such changes require complex types inference logic wich I didn't implement at the moment. If anyone has code examples of such type inference logic please share them here. Any idea will be highly appreciated.

Webbrother commented 1 year ago

Play with idea 1

Play with idea 2

Patterns that probably can be used