Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: content editable timeout bug #478

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/components/ContentEditable/ContentEditable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ export const ContentEditable: React.FC<

useEffect(
function shouldUpdateInnerHtml() {
console.log("shouldUpdateInnerHtml", {
propValue: props.value,
currentValue: latestHtml.current,
timeout: timeout.current,
});
if ($ref.current) {
if (!props.value && props.readonly) {
$ref.current.innerHTML = "&nbsp;";
} else if (latestHtml.current !== props.value) {
} else if (latestHtml.current !== props.value && !timeout.current) {
const newHtml = DOMPurify.sanitize(
props.value,
DomPurifyOptions.onPropsChangeOptions
Expand All @@ -120,12 +125,15 @@ export const ContentEditable: React.FC<
useEffect(() => {
return () => {
clearTimeout(timeout.current);
timeout.current = undefined;
};
}, []);

function handleOnChange(e: React.FormEvent<HTMLSpanElement>) {
if ($ref.current) {
console.log("handleOnChange");
clearTimeout(timeout.current);
timeout.current = undefined;
const cleanHTML = DOMPurify.sanitize(
$ref.current.innerHTML,
DomPurifyOptions.onInputChangeOptions
Expand All @@ -138,6 +146,7 @@ export const ContentEditable: React.FC<
timeout.current = setTimeout(() => {
latestHtml.current = cleanHTML;
latestProps.current.onChange?.(cleanHTML, e);
timeout.current = undefined;
}, Delays.contentEditable);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/constants/Delays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export const Delays = {
blockHoverLeaveControls: 1500,
cursorStale: 3000,
rollingDelay: 1000,
characterSheetSessionAutosave: 1000,
characterSheetSessionAutosave: 2000,
};
2 changes: 1 addition & 1 deletion lib/routes/Character/hooks/useCharacter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import isEqual from "lodash/isEqual";
import { useContext, useEffect, useMemo, useState } from "react";
import { previewContentEditable } from "../../../components/ContentEditable/ContentEditable";
import { SettingsContext } from "../../../contexts/SettingsContext/SettingsContext";
import { Id } from "../../../domains/Id/Id";
import { CharacterFactory } from "../../../domains/character/CharacterFactory";
import { ICharacterTemplate } from "../../../domains/character/CharacterType";
import {
Expand All @@ -13,7 +14,6 @@ import {
IPage,
} from "../../../domains/character/types";
import { getUnix, getUnixFrom } from "../../../domains/dayjs/getDayJS";
import { Id } from "../../../domains/Id/Id";

export function useCharacter(
characterFromProps?: ICharacter | undefined,
Expand Down
Loading