Skip to content

Commit

Permalink
Support Command Registration (Closes #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvargas92495 committed Jul 14, 2021
1 parent 353ad62 commit 1352f23
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import differenceInMilliseconds from "date-fns/differenceInMilliseconds";
import { render } from "./SmartblocksMenu";
import { render as renderStore } from "./SmartblocksStore";
import lego from "./img/lego3blocks.png";
import { getCustomWorkflows, sbBomb } from "./smartblocks";
import {
CommandHandler,
getCustomWorkflows,
handlerByCommand,
sbBomb,
} from "./smartblocks";

addStyle(`.rm-page-ref--tag[data-tag="42SmartBlock"]:before, .rm-page-ref--tag[data-tag="SmartBlock"]:before {
display:inline-block;
Expand Down Expand Up @@ -135,7 +140,16 @@ runExtension("smartblocks", () => {
const isCustomOnly = tree.some((t) =>
toFlexRegex("custom only").test(t.text)
);

window.roamjs.extension.smartblocks = {};
window.roamjs.extension.smartblocks.registerCommand = ({
text,
handler,
}: {
text: string;
handler: CommandHandler;
}) => {
handlerByCommand[text] = handler;
};
document.addEventListener("input", (e) => {
const target = e.target as HTMLElement;
if (
Expand Down
7 changes: 5 additions & 2 deletions src/smartblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ const outputTodoBlocks = (
}));

type CommandOutput = string | string[] | InputTextNode[];
export type CommandHandler = (
...args: string[]
) => CommandOutput | Promise<CommandOutput>;
const COMMAND_REGEX = /<%([A-Z0-9]*)(?::(.*?))?%>/g;
const COMMANDS: {
text: string;
help: string;
args?: true;
handler: (...args: string[]) => CommandOutput | Promise<CommandOutput>;
handler: CommandHandler;
}[] = [
{
text: "DATE",
Expand Down Expand Up @@ -367,7 +370,7 @@ const COMMANDS: {
},
},
];
const handlerByCommand = Object.fromEntries(
export const handlerByCommand = Object.fromEntries(
COMMANDS.map((c) => [c.text, c.handler])
);

Expand Down

0 comments on commit 1352f23

Please sign in to comment.