Skip to content

Commit 9dcd7eb

Browse files
authored
[ENG-644] Improvements for the node menu popover (#310)
* improvements * address PR comments * pr comments
1 parent dd43a84 commit 9dcd7eb

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

apps/roam/src/utils/initializeObserversAndListeners.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ import {
4141
findBlockElementFromSelection,
4242
} from "~/utils/renderTextSelectionPopup";
4343

44+
const debounce = (fn: () => void, delay = 250) => {
45+
let timeout: number;
46+
return () => {
47+
clearTimeout(timeout);
48+
timeout = window.setTimeout(fn, delay);
49+
};
50+
};
51+
4452
export const initObservers = async ({
4553
onloadArgs,
4654
}: {
@@ -247,14 +255,11 @@ export const initObservers = async ({
247255
}
248256
};
249257

250-
const nodeCreationPopoverListener = () => {
258+
const nodeCreationPopoverListener = debounce(() => {
251259
const isTextSelectionPopupEnabled =
252260
onloadArgs.extensionAPI.settings.get("text-selection-popup") !== false;
253261

254-
if (!isTextSelectionPopupEnabled) {
255-
removeTextSelectionPopup();
256-
return;
257-
}
262+
if (!isTextSelectionPopupEnabled) return;
258263

259264
const selection = window.getSelection();
260265

@@ -275,6 +280,7 @@ export const initObservers = async ({
275280
if (blockElement) {
276281
const textarea = blockElement.querySelector("textarea");
277282
if (!textarea) return;
283+
278284
renderTextSelectionPopup({
279285
extensionAPI: onloadArgs.extensionAPI,
280286
blockElement,
@@ -283,7 +289,7 @@ export const initObservers = async ({
283289
} else {
284290
removeTextSelectionPopup();
285291
}
286-
};
292+
}, 150);
287293

288294
return {
289295
observers: [

apps/roam/src/utils/renderTextSelectionPopup.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import { OnloadArgs } from "roamjs-components/types";
66

77
let currentPopupContainer: HTMLDivElement | null = null;
88

9+
export const getCurrentPopupContainer = () => currentPopupContainer;
10+
export const setCurrentPopupContainer = (container: HTMLDivElement | null) => {
11+
currentPopupContainer = container;
12+
};
13+
914
export const findBlockElementFromSelection = (): Element | null => {
1015
const selection = window.getSelection();
1116
if (!selection || selection.rangeCount === 0) return null;
@@ -65,9 +70,10 @@ export const renderTextSelectionPopup = ({
6570
};
6671

6772
export const removeTextSelectionPopup = () => {
68-
const container = document.getElementById("discourse-text-selection-popup");
73+
const container = getCurrentPopupContainer();
6974
if (container) {
7075
ReactDOM.unmountComponentAtNode(container);
7176
container.remove();
77+
setCurrentPopupContainer(null);
7278
}
7379
};

0 commit comments

Comments
 (0)