Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: use `debounce` instead of `inlineDebounce` for selection updates in editor

Abdullah Atta87ebe1918cb1
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index 26dfd443c..42ab4eccb 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -52,7 +52,7 @@ import { getCurrentPreset } from "../../common/toolbar-config";
import { useIsUserPremium } from "../../hooks/use-is-user-premium";
import { showBuyDialog } from "../../common/dialog-controller";
import { useStore as useSettingsStore } from "../../stores/setting-store";
-import { debounceWithId, inlineDebounce } from "../../utils/debounce";
+import { debounce, debounceWithId } from "../../utils/debounce";
import { store as editorstore } from "../../stores/editor-store";
type TipTapProps = {
@@ -214,38 +214,32 @@ function TipTap(props: TipTapProps) {
canUndo: editor.can().undo()
});
},
- onSelectionUpdate: ({ editor, transaction }) => {
- inlineDebounce(
- "tiptap:onSelectionUpdate",
- () => {
- const isEmptySelection = transaction.selection.empty;
- configure((old) => {
- const oldSelected = old.statistics?.words?.selected;
- const oldWords = old.statistics?.words.total || 0;
- if (isEmptySelection)
- return oldSelected
- ? {
- statistics: { words: { total: oldWords, selected: 0 } }
- }
- : old;
-
- const selectedWords = getSelectedWords(
- editor as Editor,
- transaction.selection
- );
- return {
- statistics: {
- words: {
- total: oldWords,
- selected: selectedWords
- }
+ onSelectionUpdate: debounce(({ editor, transaction }) => {
+ const isEmptySelection = transaction.selection.empty;
+ configure((old) => {
+ const oldSelected = old.statistics?.words?.selected;
+ const oldWords = old.statistics?.words.total || 0;
+ if (isEmptySelection)
+ return oldSelected
+ ? {
+ statistics: { words: { total: oldWords, selected: 0 } }
}
- };
- });
- },
- 500
- );
- },
+ : old;
+
+ const selectedWords = getSelectedWords(
+ editor as Editor,
+ transaction.selection
+ );
+ return {
+ statistics: {
+ words: {
+ total: oldWords,
+ selected: selectedWords
+ }
+ }
+ };
+ });
+ }, 500),
theme,
onOpenAttachmentPicker: (_editor, type) => {
onInsertAttachment?.(type);
diff --git a/apps/web/src/utils/debounce.ts b/apps/web/src/utils/debounce.ts
index 4e8dae7e4..de6bb90d0 100644
--- a/apps/web/src/utils/debounce.ts
+++ b/apps/web/src/utils/debounce.ts
@@ -16,37 +16,46 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+interface DebouncedFunction<
+ Args extends any[],
+ F extends (...args: Args) => any
+> {
+ (this: ThisParameterType<F>, ...args: Args & Parameters<F>): void;
+}
+
+interface DebouncedFunctionWithId<
+ Args extends any[],
+ F extends (...args: Args) => any
+> {
+ (this: ThisParameterType<F>, id: string, ...args: Args & Parameters<F>): void;
+}
-export function debounce<F extends (...args: never[]) => void>(
+export function debounce<Args extends any[], F extends (...args: Args) => void>(
func: F,
waitFor: number
-) {
+): DebouncedFunction<Args, F> {
let timeout: number | null;
- const debounced = (...args: Parameters<F>) => {
+ return (...args: Args) => {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => func(...args), waitFor) as unknown as number;
};
-
- return debounced;
}
-export function debounceWithId<F extends (...args: never[]) => void>(
- func: F,
- waitFor: number
-) {
+export function debounceWithId<
+ Args extends any[],
+ F extends (...args: Args) => void
+>(func: F, waitFor: number): DebouncedFunctionWithId<Args, F> {
let timeout: number | null;
let debounceId: string | null = null;
- const debounced = (id: string, ...args: Parameters<F>) => {
+ return (id: string, ...args: Parameters<F>) => {
if (timeout && id === debounceId) clearTimeout(timeout);
debounceId = id;
timeout = setTimeout(() => {
func(...args);
}, waitFor) as unknown as number;
};
-
- return debounced;
}
const DEBOUNCE_TIMEOUTS: Record<string, NodeJS.Timeout> = {};