Skip to content

Commit

Permalink
Smartblock command help (Closes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvargas92495 committed Sep 1, 2021
1 parent be9cd62 commit ebfda1a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 42 deletions.
43 changes: 22 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"roam-client": "^1.86.1",
"roamjs-components": "^0.22.0",
"roamjs-components": "^0.23.8",
"stripe": "^8.167.0",
"uuid": "^8.3.2",
"xregexp": "^5.1.0"
Expand Down
42 changes: 41 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import {
updateBlock,
parseRoamDateUid,
getBasicTreeByParentUid,
getUids,
} from "roam-client";
import {
createConfigObserver,
getSettingValueFromTree,
renderCursorMenu,
renderToast,
setInputSetting,
toFlexRegex,
Expand All @@ -36,6 +38,7 @@ import { render as renderPopover } from "./SmartblockPopover";
import { render as renderBulk } from "./BulkTrigger";
import {
CommandHandler,
COMMANDS,
getCustomWorkflows,
handlerByCommand,
sbBomb,
Expand Down Expand Up @@ -205,6 +208,7 @@ const getLegacy42Setting = (name: string) => {

const ID = "smartblocks";
const CONFIG = toConfig(ID);
const COMMAND_ENTRY_REGEX = /<%$/;
runExtension("smartblocks", () => {
createConfigObserver({
title: CONFIG,
Expand Down Expand Up @@ -290,7 +294,7 @@ runExtension("smartblocks", () => {
defaultValue: "xx",
})
.replace(/"/g, "")
.replace(/\\/, "\\\\")
.replace(/\\/g, "\\\\")
.trim();
const triggerRegex = new RegExp(`${trigger}$`);
const isCustomOnly = tree.some((t) =>
Expand Down Expand Up @@ -338,6 +342,42 @@ runExtension("smartblocks", () => {
isCustomOnly,
dailyConfig,
});
} else if (COMMAND_ENTRY_REGEX.test(valueToCursor)) {
renderCursorMenu({
initialItems: COMMANDS.map(({ text, help }) => ({
text,
id: text,
help,
})),
onItemSelect: (item) => {
const { blockUid } = getUids(textarea);
const suffix = textarea.value.substring(textarea.selectionStart);
const newPrefix = `${valueToCursor.slice(0, -2)}<%${item.text}%>`;
setTimeout(() => {
updateBlock({
uid: blockUid,
text: `${newPrefix}${suffix}`,
});
renderToast({
intent: Intent.PRIMARY,
id: "smartblocks-command-help",
content: `###### ${item.text}\n\n${item.help}`,
position: "bottom-right",
timeout: 10000,
});
setTimeout(() => {
const newTextArea = document.getElementById(
textarea.id
) as HTMLTextAreaElement;
newTextArea.setSelectionRange(
newPrefix.length - 2,
newPrefix.length - 2
);
});
}, 1);
},
textarea,
});
}
}
});
Expand Down
54 changes: 35 additions & 19 deletions src/smartblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { renderPrompt } from "./Prompt";
import { renderToast } from "roamjs-components";
import { ParsingComponents } from "chrono-node/dist/results";
import { ORDINAL_WORD_DICTIONARY } from "./dom";
import { ToasterPosition } from "@blueprintjs/core";

export const PREDEFINED_REGEX = /#\d*-predefined/;
const PAGE_TITLE_REGEX = /^(?:#?\[\[(.*)\]\]|#([^\s]*))$/;
Expand Down Expand Up @@ -428,15 +429,15 @@ const javascriptHandler =
});
};

const COMMANDS: {
export const COMMANDS: {
text: string;
help: string;
args?: true;
handler: CommandHandler;
}[] = [
{
text: "DATE",
help: "Returns a Roam formatted dated page reference.\n\n1: NLP expression\n2: optional: format for returned date, example: YYYY-MM-DD",
help: "Returns a Roam formatted dated page reference.\n\n1: NLP expression\n\n2: optional: format for returned date, example: YYYY-MM-DD",
args: true,
handler: (nlp, ...format) => {
if (!nlp) {
Expand Down Expand Up @@ -539,7 +540,7 @@ const COMMANDS: {
},
{
text: "TODOTODAY",
help: "Returns a list of block refs of TODOs for today\n\n1. Max # blocks\n2. Format of output.\n3. optional filter values",
help: "Returns a list of block refs of TODOs for today\n\n1. Max # blocks\n\n2. Format of output.\n\n3. optional filter values",
handler: (...args) => {
const today = toRoamDate(
customDateNlp.parseDate("today", getDateBasisDate())
Expand All @@ -563,7 +564,7 @@ const COMMANDS: {
},
{
text: "TODOOVERDUE",
help: "Returns a list of block refs of TODOs that are Overdue\n\n1. Max # blocks\n2. Format of output.\n3. optional filter values",
help: "Returns a list of block refs of TODOs that are Overdue\n\n1. Max # blocks\n\n2. Format of output.\n\n3. optional filter values",
handler: (...args) => {
const blocks = getBlockUidsAndTextsReferencingPage("TODO");
const yesterday = subDays(
Expand All @@ -584,7 +585,7 @@ const COMMANDS: {
},
{
text: "TODOOVERDUEDNP",
help: "Returns a list of block refs of TODOs that are Overdue including DNP TODOs\n\n1. Max # blocks\n2. Format of output.\n3. optional filter values",
help: "Returns a list of block refs of TODOs that are Overdue including DNP TODOs\n\n1. Max # blocks\n\n2. Format of output.\n\n3. optional filter values",
handler: (...args) => {
const blocks = getBlockUidsAndTextsReferencingPage("TODO");
const yesterday = subDays(
Expand Down Expand Up @@ -612,7 +613,7 @@ const COMMANDS: {
},
{
text: "TODOFUTURE",
help: "Returns a list of block refs of TODOs that are due in the future\n\n1. Max # blocks\n2. Format of output.\n3. optional filter values",
help: "Returns a list of block refs of TODOs that are due in the future\n\n1. Max # blocks\n\n2. Format of output.\n\n3. optional filter values",
handler: (...args) => {
const blocks = getBlockUidsAndTextsReferencingPage("TODO");
const today = customDateNlp.parseDate("today", getDateBasisDate());
Expand All @@ -630,7 +631,7 @@ const COMMANDS: {
},
{
text: "TODOFUTUREDNP",
help: "Returns a list of block refs of TODOs that are due in the future including DNP TODOs\n\n1. Max # blocks\n2. Format of output.\n3. optional filter values",
help: "Returns a list of block refs of TODOs that are due in the future including DNP TODOs\n\n1. Max # blocks\n\n2. Format of output.\n\n3. optional filter values",
handler: (...args) => {
const blocks = getBlockUidsAndTextsReferencingPage("TODO");
const today = customDateNlp.parseDate("today", getDateBasisDate());
Expand All @@ -655,7 +656,7 @@ const COMMANDS: {
},
{
text: "TODOUNDATED",
help: "Returns a list of block refs of TODOs with no date\n\n1. Max # blocks\n2. Format of output.\n3. optional filter values",
help: "Returns a list of block refs of TODOs with no date\n\n1. Max # blocks\n\n2. Format of output.\n\n3. optional filter values",
handler: (...args) => {
const blocks = getBlockUidsAndTextsReferencingPage("TODO");
const todos = blocks
Expand Down Expand Up @@ -703,7 +704,7 @@ const COMMANDS: {
},
{
text: "BREADCRUMBS",
help: "Returns a list of parent block refs to a given block ref\n\n1: Block reference\n2: Separator used between block references",
help: "Returns a list of parent block refs to a given block ref\n\n1: Block reference\n\n2: Separator used between block references",
handler: (uidArg = "", ...delim) => {
const separator = delim.join(",") || " > ";
const uid = uidArg.replace(/^(\+|-)?\(\(/, "").replace(/\)\)$/, "");
Expand Down Expand Up @@ -772,7 +773,7 @@ const COMMANDS: {
},
{
text: "BLOCKMENTIONS",
help: "Returns list of blocks mentioned\n\n1: Max blocks to return\n2: Page or Tag Name\n3:Format of output.\n4: (opt) filtering",
help: "Returns list of blocks mentioned\n\n1: Max blocks to return\n\n2: Page or Tag Name\n\n3:Format of output.\n\n4: (opt) filtering",
handler: (
limitArg = "20",
titleArg = "",
Expand All @@ -798,7 +799,7 @@ const COMMANDS: {
},
{
text: "BLOCKMENTIONSDATED",
help: "Returns list of blocks mentioned based on date range\n1: Max blocks to return\n2: Page or Tag Name\n3: Start Date\n4. End Date\n5: Sort (ASC,DESC,NONE)\n6:Format of Output\n7: (opt) filtering ",
help: "Returns list of blocks mentioned based on date range\n1: Max blocks to return\n\n2: Page or Tag Name\n\n3: Start Date\n\n4. End Date\n\n5: Sort (ASC,DESC,NONE)\n\n6:Format of Output\n\n7: (opt) filtering ",
handler: (
limitArg = "20",
titleArg = "",
Expand Down Expand Up @@ -858,7 +859,7 @@ const COMMANDS: {
},
{
text: "IF",
help: "Evaluates a condition for true. Use with THEN & ELSE.\n\n1: Logic to be evaluated\n2: (Optional) Value if true\n3: (Optional) Value if false",
help: "Evaluates a condition for true. Use with THEN & ELSE.\n\n1: Logic to be evaluated\n\n2: (Optional) Value if true\n\n3: (Optional) Value if false",
handler: (condition = "false", then, els) => {
try {
const evaluated = eval(condition);
Expand Down Expand Up @@ -980,7 +981,7 @@ const COMMANDS: {
},
{
text: "SET",
help: "Create a variable in memory\n\n1. Variable name\n2: Value of variable",
help: "Create a variable in memory\n\n1. Variable name\n\n2: Value of variable",
handler: (name = "", ...value) => {
smartBlocksContext.variables[name] = value.join(",");
return "";
Expand Down Expand Up @@ -1056,10 +1057,25 @@ const COMMANDS: {
},
{
text: "NOTIFICATION",
help: "Displays notification window\n\n1: Seconds\n2: Message",
handler: (timeoutArg, content) => {
help: "Displays notification window\n\n1: Seconds\n\n2: Message\n\n3: Position",
handler: (timeoutArg, ...contentArgs) => {
const positionArg = contentArgs.slice(-1)[0];
const isValid = ["top", "bottom"]
.flatMap((t) => ["", " left", " right"].map((o) => `${t}${o}`))
.includes(positionArg);
const position = isValid
? (positionArg.replace(" ", "-") as ToasterPosition)
: "top";
const content = (isValid ? contentArgs.slice(0, -1) : contentArgs).join(
","
);
const timeout = (Math.max(Number(timeoutArg), 0) || 1) * 1000;
renderToast({ id: "smartblocks-notification", timeout, content });
renderToast({
id: "smartblocks-notification",
timeout,
content,
position,
});
return "";
},
},
Expand Down Expand Up @@ -1198,7 +1214,7 @@ const COMMANDS: {
},
{
text: "OPENPAGE",
help: "Opens or creates a page or block ref\n\n1. Page name or block ref\n2. A behavior to perform after navigating.",
help: "Opens or creates a page or block ref\n\n1. Page name or block ref\n\n2. A behavior to perform after navigating.",
handler: (...args) => {
const blockNumberArg =
args.length > 1 && args[args.length - 1].includes("GOTOBLOCK")
Expand Down Expand Up @@ -1298,7 +1314,7 @@ const COMMANDS: {
},
{
text: "SIDEBARSTATE",
help: "Toggles state of sidebars\n\nValue of 1 to 4. \n1 - open left sidebar \n2 - close left side bar \n3 - open right side bar \n4 - close right side bar.",
help: "Toggles state of sidebars\n\nValue of 1 to 4. \n1 - open left sidebar \n\n2 - close left side bar \n\n3 - open right side bar \n\n4 - close right side bar.",
handler: (stateArg = "1") => {
const state = Number(stateArg) || 1;
const leftButton = document.querySelector(".rm-open-left-sidebar-btn");
Expand Down Expand Up @@ -1342,7 +1358,7 @@ const COMMANDS: {
},
{
text: "REPLACE",
help: "Returns the text in the first argument after replacing one sub text with another.\n\n1. Source text\n2.Text to replace\n3.Text to replace with",
help: "Returns the text in the first argument after replacing one sub text with another.\n\n1. Source text\n\n2.Text to replace\n\n3.Text to replace with",
handler: (text = "", reg = "", out = "") => {
return text.replace(new RegExp(reg), out);
},
Expand Down

0 comments on commit ebfda1a

Please sign in to comment.