|
| 1 | +import React, { useRef, useEffect, useCallback } from "react"; |
| 2 | +import { Label } from "@blueprintjs/core"; |
| 3 | +import Description from "roamjs-components/components/Description"; |
| 4 | +import createBlock from "roamjs-components/writes/createBlock"; |
| 5 | +import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid"; |
| 6 | +import getFirstChildUidByBlockUid from "roamjs-components/queries/getFirstChildUidByBlockUid"; |
| 7 | +import type { TreeNode } from "roamjs-components/types"; |
| 8 | +import type { RoamNodeType } from "../utils/zodSchema"; |
| 9 | +import { setDiscourseNodeSetting } from "../utils/accessors"; |
| 10 | +import type { DiscourseNodeBaseProps } from "./BlockPropSettingPanels"; |
| 11 | + |
| 12 | +const DEBOUNCE_MS = 250; |
| 13 | + |
| 14 | +type DualWriteBlocksPanelProps = DiscourseNodeBaseProps & { |
| 15 | + uid: string; |
| 16 | +}; |
| 17 | + |
| 18 | +const serializeBlockTree = (children: TreeNode[]): RoamNodeType[] => |
| 19 | + children |
| 20 | + .sort((a, b) => a.order - b.order) |
| 21 | + .map((child) => ({ |
| 22 | + text: child.text, |
| 23 | + ...(child.children.length > 0 && { |
| 24 | + children: serializeBlockTree(child.children), |
| 25 | + }), |
| 26 | + })); |
| 27 | + |
| 28 | +const DualWriteBlocksPanel = ({ |
| 29 | + nodeType, |
| 30 | + settingKeys, |
| 31 | + title, |
| 32 | + description, |
| 33 | + uid, |
| 34 | +}: DualWriteBlocksPanelProps) => { |
| 35 | + const containerRef = useRef<HTMLDivElement>(null); |
| 36 | + const debounceRef = useRef(0); |
| 37 | + const pullWatchArgsRef = useRef< |
| 38 | + [string, string, (before: unknown, after: unknown) => void] | null |
| 39 | + >(null); |
| 40 | + |
| 41 | + const handleChange = useCallback(() => { |
| 42 | + window.clearTimeout(debounceRef.current); |
| 43 | + debounceRef.current = window.setTimeout(() => { |
| 44 | + const tree = getFullTreeByParentUid(uid); |
| 45 | + const serialized = serializeBlockTree(tree.children); |
| 46 | + setDiscourseNodeSetting(nodeType, settingKeys, serialized); |
| 47 | + }, DEBOUNCE_MS); |
| 48 | + }, [uid, nodeType, settingKeys]); |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + const el = containerRef.current; |
| 52 | + if (!el) return; |
| 53 | + |
| 54 | + if (!getFirstChildUidByBlockUid(uid)) { |
| 55 | + void createBlock({ node: { text: " " }, parentUid: uid }).then(() => { |
| 56 | + el.innerHTML = ""; |
| 57 | + void window.roamAlphaAPI.ui.components.renderBlock({ uid, el }); |
| 58 | + }); |
| 59 | + } else { |
| 60 | + el.innerHTML = ""; |
| 61 | + void window.roamAlphaAPI.ui.components.renderBlock({ uid, el }); |
| 62 | + } |
| 63 | + |
| 64 | + const pattern = "[:block/string {:block/children ...}]"; |
| 65 | + const entityId = `[:block/uid "${uid}"]`; |
| 66 | + const callback = () => handleChange(); |
| 67 | + pullWatchArgsRef.current = [pattern, entityId, callback]; |
| 68 | + window.roamAlphaAPI.data.addPullWatch(pattern, entityId, callback); |
| 69 | + |
| 70 | + return () => { |
| 71 | + window.clearTimeout(debounceRef.current); |
| 72 | + if (pullWatchArgsRef.current) { |
| 73 | + window.roamAlphaAPI.data.removePullWatch(...pullWatchArgsRef.current); |
| 74 | + pullWatchArgsRef.current = null; |
| 75 | + } |
| 76 | + }; |
| 77 | + }, [uid, handleChange]); |
| 78 | + |
| 79 | + return ( |
| 80 | + <> |
| 81 | + <Label> |
| 82 | + {title} |
| 83 | + <Description description={description} /> |
| 84 | + </Label> |
| 85 | + <style>{`.roamjs-dualwrite-blocks > div > .rm-block-main { |
| 86 | + display: none; |
| 87 | + } |
| 88 | + .roamjs-dualwrite-blocks > div > .rm-block-children > .rm-multibar { |
| 89 | + display: none; |
| 90 | + } |
| 91 | + .roamjs-dualwrite-blocks > div > .rm-block-children { |
| 92 | + margin-left: -4px; |
| 93 | + }`}</style> |
| 94 | + <div |
| 95 | + ref={containerRef} |
| 96 | + style={{ |
| 97 | + border: "1px solid #33333333", |
| 98 | + padding: "8px 0", |
| 99 | + borderRadius: 4, |
| 100 | + }} |
| 101 | + className="roamjs-dualwrite-blocks" |
| 102 | + /> |
| 103 | + </> |
| 104 | + ); |
| 105 | +}; |
| 106 | + |
| 107 | +export default DualWriteBlocksPanel; |
0 commit comments