Skip to content

Commit

Permalink
Removed the optimizations introduced to the CodeBlock and Image compo…
Browse files Browse the repository at this point in the history
…nent.
  • Loading branch information
deepakjosp committed Sep 16, 2024
1 parent 0070270 commit 9dcdd24
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 211 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { useEffect, useRef, useState } from "react";
import { useRef, useState } from "react";

import { NodeViewWrapper, NodeViewContent } from "@tiptap/react";
import { noop } from "neetocist";
import { Down } from "neetoicons";
import CopyToClipboardButton from "neetomolecules/CopyToClipboardButton";
import { Dropdown, Input } from "neetoui";
import { useTranslation } from "react-i18next";

import { SORTED_LANGUAGE_LIST } from "./constants";
import { handleCodeBlockVisibility } from "./utils";

const { Menu, MenuItem } = Dropdown;

const CodeBlockComponent = ({ node, editor, updateAttributes, extension }) => {
const [isVisible, setIsVisible] = useState(true);
const CodeBlockComponent = ({ node, editor, updateAttributes }) => {
const [keyword, setKeyword] = useState("");

const ref = useRef();
Expand All @@ -30,29 +27,6 @@ const CodeBlockComponent = ({ node, editor, updateAttributes, extension }) => {
editor?.commands?.focus();
};

useEffect(() => {
const container = ref.current;
const shouldSetObserver =
extension.options?.enableReactNodeViewOptimization;
if (!shouldSetObserver) return noop;

const observer = new IntersectionObserver(
entries => {
handleCodeBlockVisibility({ entries, setIsVisible });
},
{
rootMargin: "-10% 0% -10% 0%",
root: document.getElementById("neeto-editor-wrapper"),
}
);

if (container) setTimeout(() => observer.observe(container), 0);

return () => {
if (container && shouldSetObserver) observer.unobserve(container);
};
}, []);

const handleContentMount = node => {
if (ref.current && node?.offsetHeight) {
ref.current.style.minHeight = `${node.offsetHeight}px`;
Expand All @@ -62,53 +36,51 @@ const CodeBlockComponent = ({ node, editor, updateAttributes, extension }) => {
return (
<NodeViewWrapper data-cy="neeto-editor-code-block">
<div {...{ ref }}>
{isVisible && (
<pre ref={handleContentMount}>
<div
className="neeto-editor-codeblock-options"
contentEditable={false}
<pre ref={handleContentMount}>
<div
className="neeto-editor-codeblock-options"
contentEditable={false}
>
<Dropdown
appendTo={() => document.body}
buttonSize="small"
buttonStyle="secondary"
icon={Down}
label={node.attrs?.language || t("neetoEditor.common.auto")}
strategy="fixed"
zIndex={99999}
>
<Dropdown
appendTo={() => document.body}
buttonSize="small"
buttonStyle="secondary"
icon={Down}
label={node.attrs?.language || t("neetoEditor.common.auto")}
strategy="fixed"
zIndex={99999}
>
<Input
autoFocus
className="neeto-editor-codeblock-options__input"
placeholder={t("neetoEditor.placeholders.searchLanguages")}
size="small"
value={keyword}
onChange={e => setKeyword(e.target.value)}
onClick={e => {
e.preventDefault();
e.stopPropagation();
}}
/>
<Menu className="neeto-editor-codeblock-options__menu">
{filteredAndSortedLanguageList.map(language => (
<MenuItem.Button
key={language}
onClick={() => handleLanguageSelect(language)}
>
{language || t("neetoEditor.common.auto")}
</MenuItem.Button>
))}
</Menu>
</Dropdown>
<CopyToClipboardButton
<Input
autoFocus
className="neeto-editor-codeblock-options__input"
placeholder={t("neetoEditor.placeholders.searchLanguages")}
size="small"
style="secondary"
value={node?.content?.content[0]?.text}
value={keyword}
onChange={e => setKeyword(e.target.value)}
onClick={e => {
e.preventDefault();
e.stopPropagation();
}}
/>
</div>
<NodeViewContent as="code" />
</pre>
)}
<Menu className="neeto-editor-codeblock-options__menu">
{filteredAndSortedLanguageList.map(language => (
<MenuItem.Button
key={language}
onClick={() => handleLanguageSelect(language)}
>
{language || t("neetoEditor.common.auto")}
</MenuItem.Button>
))}
</Menu>
</Dropdown>
<CopyToClipboardButton
size="small"
style="secondary"
value={node?.content?.content[0]?.text}
/>
</div>
<NodeViewContent as="code" />
</pre>
</div>
</NodeViewWrapper>
);
Expand Down
18 changes: 0 additions & 18 deletions src/components/Editor/CustomExtensions/Image/ExtensionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { DIRECT_UPLOAD_ENDPOINT } from "src/common/constants";
import DirectUpload from "utils/DirectUpload";

import ImageComponent from "./ImageComponent";
import { LazyLoadImage } from "./LazyLoadImage";

const upload = async (file, url) => {
if (file.size <= globalProps.endUserUploadedFileSizeLimitInMb * 1024 * 1024) {
Expand Down Expand Up @@ -131,23 +130,6 @@ export default Node.create({
},

addNodeView() {
if (this.options.enableReactNodeViewOptimization) {
return ({ editor, node, getPos }) => {
const view = new LazyLoadImage({
editor,
node,
getPos,
extension: this,
});

return {
dom: view.dom,
update: view.update.bind(view),
destroy: view.destroy.bind(view),
};
};
}

return ReactNodeViewRenderer(ImageComponent);
},

Expand Down
121 changes: 0 additions & 121 deletions src/components/Editor/CustomExtensions/Image/LazyLoadImage.js

This file was deleted.

0 comments on commit 9dcdd24

Please sign in to comment.