roamjs-components 0.86.0-alpha → 0.86.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,117 +1,117 @@
1
- import {
2
- Popover,
3
- PopoverPosition,
4
- Menu,
5
- MenuItem,
6
- InputGroup,
7
- } from "@blueprintjs/core";
8
- import React, { useCallback, useMemo, useRef, useState } from "react";
9
- import getAllBlockUidsAndTexts from "../queries/getAllBlockUidsAndTexts";
10
- import useArrowKeyDown from "../hooks/useArrowKeyDown";
11
-
12
- const searchBlocksByString = (
13
- q: string,
14
- blocks: { text: string; uid: string }[]
15
- ) => {
16
- const regex = new RegExp(q, "i");
17
- return blocks.filter((a) => regex.test(a.text)).slice(0, 9);
18
- };
19
-
20
- const BlockInput = ({
21
- value,
22
- setValue,
23
- onBlur,
24
- onConfirm,
25
- getAllBlocks = getAllBlockUidsAndTexts,
26
- autoFocus,
27
- }: {
28
- value: string;
29
- setValue: (q: string, uid?: string) => void;
30
- onBlur?: (v: string) => void;
31
- onConfirm?: () => void;
32
- getAllBlocks?: () => { text: string; uid: string }[];
33
- autoFocus?: boolean;
34
- }): React.ReactElement => {
35
- const [isOpen, setIsOpen] = useState(false);
36
- const open = useCallback(() => setIsOpen(true), [setIsOpen]);
37
- const close = useCallback(() => setIsOpen(false), [setIsOpen]);
38
- const allBlocks = useMemo(getAllBlocks, []);
39
- const items = useMemo(
40
- () => (value && isOpen ? searchBlocksByString(value, allBlocks) : []),
41
- [value, allBlocks]
42
- );
43
- const menuRef = useRef<HTMLUListElement>(null);
44
- const inputRef = useRef<HTMLInputElement>(null);
45
- const { activeIndex, onKeyDown } = useArrowKeyDown({
46
- onEnter: (value) => {
47
- if (isOpen) {
48
- setValue(value.text, value.uid);
49
- close();
50
- } else if (onConfirm) {
51
- onConfirm();
52
- }
53
- },
54
- results: items,
55
- menuRef,
56
- });
57
- return (
58
- <Popover
59
- portalClassName={"roamjs-block-input"}
60
- targetClassName={"roamjs-block-input-target"}
61
- captureDismiss={true}
62
- isOpen={isOpen}
63
- onOpened={open}
64
- minimal={true}
65
- autoFocus={false}
66
- enforceFocus={false}
67
- position={PopoverPosition.BOTTOM_LEFT}
68
- modifiers={{
69
- flip: { enabled: false },
70
- preventOverflow: { enabled: false },
71
- }}
72
- content={
73
- <Menu className={"max-h-64 overflow-auto max-w-md"} ulRef={menuRef}>
74
- {items.map((t, i) => (
75
- <MenuItem
76
- text={t.text}
77
- active={activeIndex === i}
78
- key={t.uid}
79
- multiline
80
- onClick={() => {
81
- setValue(t.text, t.uid);
82
- close();
83
- inputRef.current?.focus();
84
- }}
85
- />
86
- ))}
87
- </Menu>
88
- }
89
- target={
90
- <InputGroup
91
- value={value || ""}
92
- onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
93
- setValue(e.target.value);
94
- setIsOpen(!!e.target.value);
95
- }}
96
- placeholder={"Search for a block"}
97
- onKeyDown={onKeyDown}
98
- onBlur={(e) => {
99
- if (
100
- e.relatedTarget &&
101
- !(e.relatedTarget as HTMLElement).closest?.(".roamjs-block-input")
102
- ) {
103
- close();
104
- }
105
- if (onBlur) {
106
- onBlur(e.target.value);
107
- }
108
- }}
109
- inputRef={inputRef}
110
- autoFocus={autoFocus}
111
- />
112
- }
113
- />
114
- );
115
- };
116
-
117
- export default BlockInput;
1
+ import {
2
+ Popover,
3
+ PopoverPosition,
4
+ Menu,
5
+ MenuItem,
6
+ InputGroup,
7
+ } from "@blueprintjs/core";
8
+ import React, { useCallback, useMemo, useRef, useState } from "react";
9
+ import getAllBlockUidsAndTexts from "../queries/getAllBlockUidsAndTexts";
10
+ import useArrowKeyDown from "../hooks/useArrowKeyDown";
11
+
12
+ const searchBlocksByString = (
13
+ q: string,
14
+ blocks: { text: string; uid: string }[]
15
+ ) => {
16
+ const regex = new RegExp(q, "i");
17
+ return blocks.filter((a) => regex.test(a.text)).slice(0, 9);
18
+ };
19
+
20
+ const BlockInput = ({
21
+ value,
22
+ setValue,
23
+ onBlur,
24
+ onConfirm,
25
+ getAllBlocks = getAllBlockUidsAndTexts,
26
+ autoFocus,
27
+ }: {
28
+ value: string;
29
+ setValue: (q: string, uid?: string) => void;
30
+ onBlur?: (v: string) => void;
31
+ onConfirm?: () => void;
32
+ getAllBlocks?: () => { text: string; uid: string }[];
33
+ autoFocus?: boolean;
34
+ }): React.ReactElement => {
35
+ const [isOpen, setIsOpen] = useState(false);
36
+ const open = useCallback(() => setIsOpen(true), [setIsOpen]);
37
+ const close = useCallback(() => setIsOpen(false), [setIsOpen]);
38
+ const allBlocks = useMemo(getAllBlocks, []);
39
+ const items = useMemo(
40
+ () => (value && isOpen ? searchBlocksByString(value, allBlocks) : []),
41
+ [value, allBlocks]
42
+ );
43
+ const menuRef = useRef<HTMLUListElement>(null);
44
+ const inputRef = useRef<HTMLInputElement>(null);
45
+ const { activeIndex, onKeyDown } = useArrowKeyDown({
46
+ onEnter: (value) => {
47
+ if (isOpen) {
48
+ setValue(value.text, value.uid);
49
+ close();
50
+ } else if (onConfirm) {
51
+ onConfirm();
52
+ }
53
+ },
54
+ results: items,
55
+ menuRef,
56
+ });
57
+ return (
58
+ <Popover
59
+ portalClassName={"roamjs-block-input"}
60
+ targetClassName={"roamjs-block-input-target"}
61
+ captureDismiss={true}
62
+ isOpen={isOpen}
63
+ onOpened={open}
64
+ minimal={true}
65
+ autoFocus={false}
66
+ enforceFocus={false}
67
+ position={PopoverPosition.BOTTOM_LEFT}
68
+ modifiers={{
69
+ flip: { enabled: false },
70
+ preventOverflow: { enabled: false },
71
+ }}
72
+ content={
73
+ <Menu className={"max-h-64 overflow-auto max-w-md"} ulRef={menuRef}>
74
+ {items.map((t, i) => (
75
+ <MenuItem
76
+ text={t.text}
77
+ active={activeIndex === i}
78
+ key={t.uid}
79
+ multiline
80
+ onClick={() => {
81
+ setValue(t.text, t.uid);
82
+ close();
83
+ inputRef.current?.focus();
84
+ }}
85
+ />
86
+ ))}
87
+ </Menu>
88
+ }
89
+ target={
90
+ <InputGroup
91
+ value={value || ""}
92
+ onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
93
+ setValue(e.target.value);
94
+ setIsOpen(!!e.target.value);
95
+ }}
96
+ placeholder={"Search for a block"}
97
+ onKeyDown={onKeyDown}
98
+ onBlur={(e) => {
99
+ if (
100
+ e.relatedTarget &&
101
+ !(e.relatedTarget as HTMLElement).closest?.(".roamjs-block-input")
102
+ ) {
103
+ close();
104
+ }
105
+ if (onBlur) {
106
+ onBlur(e.target.value);
107
+ }
108
+ }}
109
+ inputRef={inputRef}
110
+ autoFocus={autoFocus}
111
+ />
112
+ }
113
+ />
114
+ );
115
+ };
116
+
117
+ export default BlockInput;
@@ -1,69 +1,70 @@
1
- import { Button } from "@blueprintjs/core";
2
- import React, { useCallback, useState } from "react";
3
- import getUidsFromId from "../dom/getUidsFromId";
4
- import getBlockUidFromTarget from "../dom/getBlockUidFromTarget";
5
- import renderWithUnmount from "../util/renderWithUnmount";
6
- import { OnloadArgs } from "../types/native";
7
-
8
- const ComponentContainer: React.FunctionComponent<{
9
- blockId?: string;
10
- className?: string;
11
- }> = ({ blockId, className, children }) => {
12
- const [showIcons, setShowIcons] = useState(false);
13
- const appear = useCallback(() => setShowIcons(true), [setShowIcons]);
14
- const disappear = useCallback(() => setShowIcons(false), [setShowIcons]);
15
-
16
- const { blockUid, windowId } = getUidsFromId(blockId);
17
- return (
18
- <div
19
- className={className}
20
- onMouseOver={appear}
21
- onMouseLeave={disappear}
22
- style={{ position: "relative", width: "fit-content", minWidth: 300 }}
23
- >
24
- {showIcons && (
25
- <div className={"roamjs-edit-component absolute top-2 right-2 z-50"}>
26
- {blockId && (
27
- <Button
28
- icon="edit"
29
- minimal
30
- onClick={() =>
31
- window.roamAlphaAPI.ui.setBlockFocusAndSelection({
32
- location: { "block-uid": blockUid, "window-id": windowId },
33
- })
34
- }
35
- />
36
- )}
37
- </div>
38
- )}
39
- {children}
40
- </div>
41
- );
42
- };
43
-
44
- export const createComponentRender =
45
- (
46
- Fc: (props: { blockUid: string }) => React.ReactElement,
47
- className?: string
48
- ) =>
49
- (b: HTMLButtonElement, args?: OnloadArgs): void => {
50
- if (b.parentElement) {
51
- b.parentElement.onmousedown = (e: MouseEvent) => e.stopPropagation();
52
- const blockUid = getBlockUidFromTarget(b);
53
- const possibleBlockId = b.closest(".roam-block")?.id;
54
- const blockId = possibleBlockId?.endsWith?.(blockUid)
55
- ? possibleBlockId
56
- : undefined;
57
- if (blockUid) {
58
- renderWithUnmount(
59
- <ComponentContainer blockId={blockId} className={className}>
60
- <Fc blockUid={blockUid} />
61
- </ComponentContainer>,
62
- b.parentElement,
63
- args
64
- );
65
- }
66
- }
67
- };
68
-
69
- export default ComponentContainer;
1
+ import { Button } from "@blueprintjs/core";
2
+ import React, { useCallback, useState } from "react";
3
+ import getUidsFromId from "../dom/getUidsFromId";
4
+ import getBlockUidFromTarget from "../dom/getBlockUidFromTarget";
5
+ import renderWithUnmount from "../util/renderWithUnmount";
6
+ import { OnloadArgs } from "../types/native";
7
+
8
+ const ComponentContainer: React.FunctionComponent<{
9
+ blockId?: string;
10
+ className?: string;
11
+ children?: React.ReactNode;
12
+ }> = ({ blockId, className, children }) => {
13
+ const [showIcons, setShowIcons] = useState(false);
14
+ const appear = useCallback(() => setShowIcons(true), [setShowIcons]);
15
+ const disappear = useCallback(() => setShowIcons(false), [setShowIcons]);
16
+
17
+ const { blockUid, windowId } = getUidsFromId(blockId);
18
+ return (
19
+ <div
20
+ className={className}
21
+ onMouseOver={appear}
22
+ onMouseLeave={disappear}
23
+ style={{ position: "relative", width: "fit-content", minWidth: 300 }}
24
+ >
25
+ {showIcons && (
26
+ <div className={"roamjs-edit-component absolute top-2 right-2 z-50"}>
27
+ {blockId && (
28
+ <Button
29
+ icon="edit"
30
+ minimal
31
+ onClick={() =>
32
+ window.roamAlphaAPI.ui.setBlockFocusAndSelection({
33
+ location: { "block-uid": blockUid, "window-id": windowId },
34
+ })
35
+ }
36
+ />
37
+ )}
38
+ </div>
39
+ )}
40
+ {children}
41
+ </div>
42
+ );
43
+ };
44
+
45
+ export const createComponentRender =
46
+ (
47
+ Fc: (props: { blockUid: string }) => React.ReactElement,
48
+ className?: string
49
+ ) =>
50
+ (b: HTMLButtonElement, args?: OnloadArgs): void => {
51
+ if (b.parentElement) {
52
+ b.parentElement.onmousedown = (e: MouseEvent) => e.stopPropagation();
53
+ const blockUid = getBlockUidFromTarget(b);
54
+ const possibleBlockId = b.closest(".roam-block")?.id;
55
+ const blockId = possibleBlockId?.endsWith?.(blockUid)
56
+ ? possibleBlockId
57
+ : undefined;
58
+ if (blockUid) {
59
+ renderWithUnmount(
60
+ <ComponentContainer blockId={blockId} className={className}>
61
+ <Fc blockUid={blockUid} />
62
+ </ComponentContainer>,
63
+ b.parentElement,
64
+ args
65
+ );
66
+ }
67
+ }
68
+ };
69
+
70
+ export default ComponentContainer;