|
| 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 "~/components/settings/utils/zodSchema"; |
| 9 | +import { setDiscourseNodeSetting } from "~/components/settings/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.heading && { heading: child.heading as 0 | 1 | 2 | 3 }), |
| 24 | + ...(child.open === false && { open: false }), |
| 25 | + ...(child.children.length > 0 && { |
| 26 | + children: serializeBlockTree(child.children), |
| 27 | + }), |
| 28 | + })); |
| 29 | + |
| 30 | +const DualWriteBlocksPanel = ({ |
| 31 | + nodeType, |
| 32 | + settingKeys, |
| 33 | + title, |
| 34 | + description, |
| 35 | + uid, |
| 36 | +}: DualWriteBlocksPanelProps) => { |
| 37 | + const containerRef = useRef<HTMLDivElement>(null); |
| 38 | + const debounceRef = useRef(0); |
| 39 | + const pullWatchArgsRef = useRef< |
| 40 | + [string, string, (before: unknown, after: unknown) => void] | null |
| 41 | + >(null); |
| 42 | + |
| 43 | + const handleChange = useCallback(() => { |
| 44 | + window.clearTimeout(debounceRef.current); |
| 45 | + debounceRef.current = window.setTimeout(() => { |
| 46 | + const tree = getFullTreeByParentUid(uid); |
| 47 | + const serialized = serializeBlockTree(tree.children); |
| 48 | + setDiscourseNodeSetting(nodeType, settingKeys, serialized); |
| 49 | + }, DEBOUNCE_MS); |
| 50 | + }, [uid, nodeType, settingKeys]); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + const el = containerRef.current; |
| 54 | + if (!el || !uid) return; |
| 55 | + |
| 56 | + const pattern = "[:block/string :block/order {:block/children ...}]"; |
| 57 | + const entityId = `[:block/uid "${uid}"]`; |
| 58 | + const callback = () => handleChange(); |
| 59 | + |
| 60 | + const registerPullWatch = () => { |
| 61 | + pullWatchArgsRef.current = [pattern, entityId, callback]; |
| 62 | + window.roamAlphaAPI.data.addPullWatch(pattern, entityId, callback); |
| 63 | + }; |
| 64 | + |
| 65 | + if (!getFirstChildUidByBlockUid(uid)) { |
| 66 | + void createBlock({ node: { text: " " }, parentUid: uid }).then(() => { |
| 67 | + el.innerHTML = ""; |
| 68 | + void window.roamAlphaAPI.ui.components.renderBlock({ uid, el }); |
| 69 | + registerPullWatch(); |
| 70 | + }); |
| 71 | + } else { |
| 72 | + el.innerHTML = ""; |
| 73 | + void window.roamAlphaAPI.ui.components.renderBlock({ uid, el }); |
| 74 | + registerPullWatch(); |
| 75 | + } |
| 76 | + |
| 77 | + return () => { |
| 78 | + window.clearTimeout(debounceRef.current); |
| 79 | + if (pullWatchArgsRef.current) { |
| 80 | + window.roamAlphaAPI.data.removePullWatch(...pullWatchArgsRef.current); |
| 81 | + pullWatchArgsRef.current = null; |
| 82 | + } |
| 83 | + }; |
| 84 | + }, [uid, handleChange]); |
| 85 | + |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <Label> |
| 89 | + {title} |
| 90 | + <Description description={description} /> |
| 91 | + </Label> |
| 92 | + <style>{`.dg-dualwrite-blocks > div > .rm-block-main { |
| 93 | + display: none; |
| 94 | + } |
| 95 | + .dg-dualwrite-blocks > div > .rm-block-children > .rm-multibar { |
| 96 | + display: none; |
| 97 | + } |
| 98 | + .dg-dualwrite-blocks > div > .rm-block-children { |
| 99 | + margin-left: -4px; |
| 100 | + }`}</style> |
| 101 | + <div |
| 102 | + ref={containerRef} |
| 103 | + className="dg-dualwrite-blocks rounded border border-gray-200 py-2" |
| 104 | + /> |
| 105 | + </> |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +export default DualWriteBlocksPanel; |
0 commit comments