Skip to content

Commit

Permalink
Support page input
Browse files Browse the repository at this point in the history
  • Loading branch information
dvargas92495 committed Jul 12, 2021
1 parent 6ec4416 commit 84cfdda
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 deletions.
6 changes: 3 additions & 3 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 @@ -21,6 +21,6 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"roam-client": "^1.79.1",
"roamjs-components": "^0.18.1"
"roamjs-components": "^0.18.2"
}
}
55 changes: 41 additions & 14 deletions src/Prompt.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Alert, Classes, H3, InputGroup, Label } from "@blueprintjs/core";
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { extractTag } from "roam-client";
import {
Alert,
Classes,
Dialog,
H3,
InputGroup,
Label,
} from "@blueprintjs/core";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { createOverlayRender, MenuItemSelect } from "roamjs-components";
createOverlayRender,
MenuItemSelect,
PageInput,
} from "roamjs-components";

type Props = {
display?: string;
Expand All @@ -23,11 +27,27 @@ const Prompt = ({
initialValue = "",
resolve,
}: { onClose: () => void } & Props) => {
const [value, setValue] = useState(initialValue);
const formattedDisplay = useMemo(
() => display.replace("{page}", "").trim(),
[display]
);
const isPageInput = useMemo(
() => display.trim() !== formattedDisplay,
[display, formattedDisplay]
);
const formattedOptions = useMemo(
() => (isPageInput ? options.map(extractTag) : options),
[options, isPageInput]
);
const formattedInitialValue = useMemo(
() => (isPageInput ? extractTag(initialValue) : initialValue),
[initialValue, isPageInput]
);
const [value, setValue] = useState(formattedInitialValue);
const [loaded, setLoaded] = useState(false);
const resolveAndClose = useCallback(
(s: string) => {
resolve(s);
resolve(isPageInput && s ? `[[${s}]]` : s);
onClose();
},
[resolve, onClose]
Expand Down Expand Up @@ -56,13 +76,20 @@ const Prompt = ({
<H3>SmartBlocks Input</H3>
<div className={Classes.ALERT_BODY} ref={contentRef}>
<Label>
{display}
{options.length ? (
{formattedDisplay}
{formattedOptions.length ? (
<MenuItemSelect
activeItem={value}
onItemSelect={(v) => setValue(v)}
items={[initialValue, ...options]}
items={[formattedInitialValue, ...formattedOptions]}
popoverProps={{ portalClassName: "roamjs-prompt-dropdown" }}
ButtonProps={{ autoFocus: true }}
/>
) : isPageInput ? (
<PageInput
value={value}
setValue={setValue}
onConfirm={() => resolveAndClose(value)}
/>
) : (
<InputGroup
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ addStyle(`.rm-page-ref--tag[data-tag="42SmartBlock"]:before, .rm-page-ref--tag[d
background-size: auto 14px;
}
.roamjs-prompt-dropdown {
z-index: 1020;
.bp3-portal {
z-index: 1000;
}
.roamjs-smartblocks-store-item {
Expand Down
12 changes: 11 additions & 1 deletion src/smartblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,17 @@ export const sbBomb = ({
const childNodes = PREDEFINED_REGEX.test(srcUid)
? predefinedChildrenByUid[srcUid]
: getTreeByBlockUid(srcUid).children;
return Promise.all(childNodes.map((n) => proccessBlockWithSmartness(n)))
return childNodes
.reduce(
(prev, cur) =>
prev.then((r) =>
proccessBlockWithSmartness(cur).then((c) => {
r.push(c);
return r;
})
),
Promise.resolve([] as InputTextNode[][])
)
.then((results) => results.flatMap((r) => r))
.then(([firstChild, ...tree]) => {
const startingOrder = getOrderByBlockUid(uid);
Expand Down

0 comments on commit 84cfdda

Please sign in to comment.