Skip to content

Commit

Permalink
addCommand "Create Query Block" and "Preview" kb shortcut (#178)
Browse files Browse the repository at this point in the history
* addCommand "Create Query Block"

* add preview keyboard shortcut for QueryEditor

* Create Query Block command fixes

* move preview to addCommand via CustomEvent

* rm unused import

* 1.14.0

* update docs

* update docs again
  • Loading branch information
mdroidian authored Nov 30, 2023
1 parent 2b54dea commit 27134b8
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ These queries are far more powerful than vanilla Roam queries, as it taps into R

The above UI is available as a block component. This allows you to create several on a page, wherever on the page you want.

To create one, simply add `{{query block}}` to any block on the page.
To create one, simply add `{{query block}}` to any block on the page or use the `Create Query Block` from the Roam Command Palette.

## Query Pages

Expand Down
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "query-builder",
"version": "1.13.0",
"version": "1.14.0",
"description": "Introduces new user interfaces for building queries in Roam",
"main": "./build/main.js",
"author": {
Expand Down
19 changes: 19 additions & 0 deletions src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,35 @@ const getConditionByUid = (
type QueryEditorComponent = (props: {
parentUid: string;
onQuery?: () => void;
setHasResults?: () => void;
hideCustomSwitch?: boolean;
showAlias?: boolean;
}) => JSX.Element;

const QueryEditor: QueryEditorComponent = ({
parentUid,
onQuery,
setHasResults,
hideCustomSwitch,
showAlias,
}) => {
useEffect(() => {
const previewQuery = ((e: CustomEvent) => {
if (parentUid !== e.detail) return;
if (setHasResults) setHasResults();
}) as EventListener;
document.body.addEventListener(
"roamjs-query-builder:fire-query",
previewQuery
);

return () => {
document.body.removeEventListener(
"roamjs-query-builder:fire-query",
previewQuery
);
};
}, [setHasResults, parentUid]);
const [conditionLabels, setConditionLabels] = useState(
() => new Set(getConditionLabels())
);
Expand Down
10 changes: 5 additions & 5 deletions src/components/QueryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ type QueryPageComponent = (props: {

type Props = Parameters<QueryPageComponent>[0];

const QueryPage = ({
pageUid,
isEditBlock,
showAlias,
}: Props) => {
const QueryPage = ({ pageUid, isEditBlock, showAlias }: Props) => {
const extensionAPI = useExtensionAPI();
const hideMetadata = useMemo(
() => !!extensionAPI && !!extensionAPI.settings.get("hide-metadata"),
Expand Down Expand Up @@ -159,6 +155,10 @@ const QueryPage = ({
setHasResults(true);
setIsEdit(false);
}}
setHasResults={() => {
setHasResults(true);
onRefresh();
}}
showAlias={showAlias}
/>
</>
Expand Down
79 changes: 79 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import getUids from "roamjs-components/dom/getUids";
import { render as renderMessageBlock } from "./components/MessageBlock";
import getBlockProps, { json } from "./utils/getBlockProps";
import resolveQueryBuilderRef from "./utils/resolveQueryBuilderRef";
import getBlockUidFromTarget from "roamjs-components/dom/getBlockUidFromTarget";
import { render as renderToast } from "roamjs-components/components/Toast";

const loadedElsewhere = document.currentScript
? document.currentScript.getAttribute("data-source") === "discourse-graph"
Expand Down Expand Up @@ -606,6 +608,83 @@ svg.rs-svg-container {
})
),
});
extensionAPI.ui.commandPalette.addCommand({
label: "Create Query Block",
callback: async () => {
const uid = window.roamAlphaAPI.ui.getFocusedBlock()?.["block-uid"];
if (!uid) {
renderToast({
id: "query-builder-create-block",
content: "Must be focused on a block to create a Query Block",
});
return;
}

// setTimeout is needed because sometimes block is left blank
setTimeout(async () => {
await updateBlock({
uid,
text: "{{query block}}",
open: false,
});
}, 200);

await createBlock({
node: {
text: "scratch",
children: [
{
text: "custom",
},
{
text: "selections",
},
{
text: "conditions",
children: [
{
text: "clause",
children: [
{
text: "source",
children: [{ text: "node" }],
},
{
text: "relation",
},
],
},
],
},
],
},
parentUid: uid,
});
document.querySelector("body")?.click();
// TODO replace with document.body.dispatchEvent(new CustomEvent)
setTimeout(() => {
const el = document.querySelector(`.roam-block[id*="${uid}"]`);
const conditionEl = el?.querySelector(
".roamjs-query-condition-relation"
);
const conditionInput = conditionEl?.querySelector(
"input"
) as HTMLInputElement;
conditionInput?.focus();
}, 200);
},
});

extensionAPI.ui.commandPalette.addCommand({
label: "Preview Current Query Builder Results",
callback: () => {
const target = document.activeElement as HTMLElement;
const uid = getBlockUidFromTarget(target);
document.body.dispatchEvent(
new CustomEvent("roamjs-query-builder:fire-query", { detail: uid })
);
},
});

const renderCustomBlockView = ({
view,
Expand Down

0 comments on commit 27134b8

Please sign in to comment.