Skip to content

Commit

Permalink
allow smartblocks for attribute options (#446)
Browse files Browse the repository at this point in the history
* allow smartblocks for attribute options

* use modified triggerSmartblock instead

* processing children instead of single smartblock
  • Loading branch information
mdroidian authored Sep 26, 2023
1 parent a456970 commit 70a43bf
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/features/attributeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import getBlockUidFromTarget from "roamjs-components/dom/getBlockUidFromTarget";
import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByParentUid";
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
import createBlock from "roamjs-components/writes/createBlock";
import { PullBlock } from "roamjs-components/types";
import { InputTextNode, PullBlock } from "roamjs-components/types";
import getSubTree from "roamjs-components/util/getSubTree";
import updateBlock from "roamjs-components/writes/updateBlock";
import deleteBlock from "roamjs-components/writes/deleteBlock";
import createPage from "roamjs-components/writes/createPage";
import MenuItemSelect from "roamjs-components/components/MenuItemSelect";
import addStyle from "roamjs-components/dom/addStyle";
import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid";
import { render as renderToast } from "roamjs-components/components/Toast";

const CONFIG = `roam/js/attribute-select`;

Expand Down Expand Up @@ -99,16 +100,40 @@ const AttributeButton = ({
key: attributeName,
parentUid: attributesNode.uid,
}).uid;
const newOptions = getSubTree({
const optionsNode = getSubTree({
key: "options",
parentUid: attributeUid,
}).children.map((t) => t.text);
});

const useSmartBlocks =
optionsNode.children.filter((obj) => obj.text.includes("<%")).length >
0;

setOptions(newOptions);
if (useSmartBlocks && !window.roamjs?.extension?.smartblocks) {
renderToast({
content:
"This attribute requires SmartBlocks. Enable SmartBlocks in Roam Depot to use this template.",
id: "smartblocks-extension-disabled",
intent: "warning",
});
setOptions(optionsNode.children.map((t) => t.text));
} else if (useSmartBlocks && window.roamjs?.extension?.smartblocks) {
window.roamjs.extension.smartblocks
.triggerSmartblock({
srcUid: optionsNode.uid,
})
.then((r) => {
const results = r as InputTextNode[];
setOptions(results.map((t) => t.text) || []);
});
} else {
setOptions(optionsNode.children.map((t) => t.text));
}
const regex = new RegExp(`^${attributeName}::\\s*`);
setCurrentValue(getTextByBlockUid(uid).replace(regex, "").trim());
}
}, [isOpen]);

return (
<AttributeButtonPopover
setIsOpen={setIsOpen}
Expand Down Expand Up @@ -317,6 +342,7 @@ const AttributeConfigPanel = ({
</div>
);
};

const TabsPanel = ({
attributeName,
attributesUid,
Expand Down Expand Up @@ -492,6 +518,7 @@ const ConfigPage = ({
</Card>
);
};

const renderConfigPage = ({
h,
pageUid,
Expand Down

0 comments on commit 70a43bf

Please sign in to comment.