Skip to content

Commit

Permalink
Make commands statically typed
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerpena committed May 2, 2022
1 parent 8f34e7b commit de51e1c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 47 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-mde",
"version": "11.5.0",
"version": "12.0.0",
"description": "React Markdown Editor",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand All @@ -10,7 +10,7 @@
"dev": "vite",
"lint": "pretty-quick",
"tsc": "tsc",
"rollup": "rollup -c",
"build": "rollup -c",
"typecheck": "tsc --noEmit"
},
"repository": {
Expand Down
10 changes: 5 additions & 5 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export default [
{
input: "src/index.ts",
output: [
// {
// file: packageJson.main,
// format: "cjs",
// sourcemap: true,
// },
{
file: packageJson.main,
format: "cjs",
sourcemap: true
},
{
file: packageJson.module,
format: "esm",
Expand Down
28 changes: 9 additions & 19 deletions src/commands/command-controller.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
import { extractKeyActivatedCommands } from "./command-utils";
import { TextController } from "../types/CommandOptions";
import { Command, CommandContext, CommandMap } from "./command";
import { CommandContext, CommandMap } from "./command";

export class CommandController {
export class CommandController<CommandName extends string> {
private readonly textController: TextController;
private readonly commandMap: CommandMap;
/**
* Names of commands that can be activated by the keyboard
*/
keyActivatedCommands: string[];
private readonly commandMap: CommandMap<CommandName>;

/**
* Indicates whether there is a command currently executing
*/
isExecuting: boolean = false;

constructor(textController: TextController, commandMap: CommandMap) {
constructor(
textController: TextController,
commandMap: CommandMap<CommandName>
) {
this.textController = textController;
this.commandMap = commandMap;
this.keyActivatedCommands = extractKeyActivatedCommands(commandMap);
}

getCommand = (name: string): Command => {
const command = this.commandMap[name];
if (!command) {
throw new Error(`Cannot execute command. Command not found: ${name}`);
}
return command;
};

async executeCommand(
commandName: string,
commandName: CommandName,
context?: CommandContext
): Promise<void> {
if (this.isExecuting) {
Expand Down
18 changes: 0 additions & 18 deletions src/commands/command-utils.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/hooks/use-markdown-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { TextAreaTextController } from "../text/textarea-text-controller";
import { TextController } from "../types/CommandOptions";
import { CommandMap } from "../commands/command";

export type UseTextAreaMarkdownEditorResult = {
export type UseTextAreaMarkdownEditorResult<CommandName extends string> = {
ref: React.RefObject<HTMLTextAreaElement>;
textController: TextController;
commandController: CommandController;
commandController: CommandController<CommandName>;
};

export type UseTextAreaMarkdownEditorOptions<CommandName extends string> = {
Expand All @@ -16,7 +16,7 @@ export type UseTextAreaMarkdownEditorOptions<CommandName extends string> = {

export function useTextAreaMarkdownEditor<CommandName extends string>(
options: UseTextAreaMarkdownEditorOptions<CommandName>
): UseTextAreaMarkdownEditorResult {
): UseTextAreaMarkdownEditorResult<CommandName> {
const textAreaRef = useRef<HTMLTextAreaElement>(null);

const textController = useMemo(() => {
Expand Down

0 comments on commit de51e1c

Please sign in to comment.