Skip to content

Commit

Permalink
added example with focus/blur
Browse files Browse the repository at this point in the history
  • Loading branch information
Darginec05 committed May 25, 2024
1 parent dd16c7f commit c4cd786
Show file tree
Hide file tree
Showing 7 changed files with 1,737 additions and 92 deletions.
5 changes: 3 additions & 2 deletions packages/core/editor/src/YooptaEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ const YooptaEditor = ({
className={className}
selectionBoxRoot={selectionBoxRoot}
width={width}
/>
{children}
>
{children}
</Editor>
</ToolsProvider>
</YooptaContextProvider>
</NoSSR>
Expand Down
6 changes: 4 additions & 2 deletions packages/core/editor/src/components/Editor/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSSProperties, useEffect, useRef } from 'react';
import { CSSProperties, ReactNode, useEffect, useRef } from 'react';
import { useYooptaEditor, useYooptaReadOnly } from '../../contexts/YooptaContext/YooptaContext';
import { RenderBlocks } from './RenderBlocks';
import { YooptaMark } from '../../marks';
Expand All @@ -21,6 +21,7 @@ type Props = {
className?: string;
placeholder?: string;
width?: number | string;
children: ReactNode;
};

const getEditorStyles = (styles: CSSProperties) => ({
Expand All @@ -40,7 +41,7 @@ const DEFAULT_STATE: State = {
startedIndexToSelect: null,
};

const Editor = ({ placeholder, marks, className, selectionBoxRoot, width, autoFocus = true }: Props) => {
const Editor = ({ placeholder, marks, className, selectionBoxRoot, width, children, autoFocus = true }: Props) => {
const editor = useYooptaEditor();
const isReadOnly = useYooptaReadOnly();
const yooptaEditorRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -346,6 +347,7 @@ const Editor = ({ placeholder, marks, className, selectionBoxRoot, width, autoFo
isOpen={selectionBox.selection && !isReadOnly}
/>
)}
{children}
</div>
);
};
Expand Down
30 changes: 15 additions & 15 deletions web/next-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
"@floating-ui/react": "^0.26.11",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-icons": "^1.3.0",
"@yoopta/action-menu-list": "^4.3.1",
"@yoopta/blockquote": "^4.3.1",
"@yoopta/callout": "^4.3.1",
"@yoopta/code": "^4.3.1",
"@yoopta/editor": "^4.3.1",
"@yoopta/embed": "^4.3.1",
"@yoopta/file": "^4.3.1",
"@yoopta/headings": "^4.3.1",
"@yoopta/image": "^4.3.1",
"@yoopta/link": "^4.3.1",
"@yoopta/link-tool": "^4.3.1",
"@yoopta/lists": "^4.3.1",
"@yoopta/action-menu-list": "^4.4.0",
"@yoopta/blockquote": "^4.4.0",
"@yoopta/callout": "^4.4.0",
"@yoopta/code": "^4.4.0",
"@yoopta/editor": "^4.4.0",
"@yoopta/embed": "^4.4.0",
"@yoopta/file": "^4.4.0",
"@yoopta/headings": "^4.4.0",
"@yoopta/image": "^4.4.0",
"@yoopta/link": "^4.4.0",
"@yoopta/link-tool": "^4.4.0",
"@yoopta/lists": "^4.4.0",
"@yoopta/marks": "^4.3.0",
"@yoopta/paragraph": "^4.3.1",
"@yoopta/toolbar": "^4.3.1",
"@yoopta/video": "^4.3.1",
"@yoopta/paragraph": "^4.4.0",
"@yoopta/toolbar": "^4.4.0",
"@yoopta/video": "^4.4.0",
"class-variance-authority": "^0.7.0",
"classnames": "^2.5.1",
"clsx": "^2.1.0",
Expand Down
169 changes: 169 additions & 0 deletions web/next-example/src/components/examples/withEditorFocusBlur/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import YooptaEditor, { createYooptaEditor, useYooptaEditor, useYooptaFocused } from '@yoopta/editor';
import { useMemo, useRef } from 'react';

import Paragraph from '@yoopta/paragraph';
import Blockquote from '@yoopta/blockquote';
import Embed from '@yoopta/embed';
import Image from '@yoopta/image';
import Link from '@yoopta/link';
import Callout from '@yoopta/callout';
import Video from '@yoopta/video';
import File from '@yoopta/file';
import { NumberedList, BulletedList, TodoList } from '@yoopta/lists';
import { Bold, Italic, CodeMark, Underline, Strike, Highlight } from '@yoopta/marks';
import { HeadingOne, HeadingThree, HeadingTwo } from '@yoopta/headings';
import Code from '@yoopta/code';
import ActionMenuList, { DefaultActionMenuRender } from '@yoopta/action-menu-list';
import Toolbar, { DefaultToolbarRender } from '@yoopta/toolbar';
import LinkTool, { DefaultLinkToolRender } from '@yoopta/link-tool';

import { WITH_CUSTOM_STYLES_VALUE } from './initValue';
import { uploadToCloudinary } from '@/utils/cloudinary';

const plugins = [
Paragraph,
HeadingOne,
HeadingTwo.extend({
options: {
HTMLAttributes: {
style: {
color: 'green',
},
},
},
}),
HeadingThree,
Blockquote,
Callout,
NumberedList,
BulletedList,
TodoList,
Code,
Link.extend({
options: {
HTMLAttributes: {
style: {
color: 'green',
},
},
},
}),
Embed,
Image.extend({
options: {
async onUpload(file) {
const data = await uploadToCloudinary(file, 'image');

return {
src: data.secure_url,
alt: 'cloudinary',
sizes: {
width: data.width,
height: data.height,
},
};
},
},
}),
Video.extend({
options: {
onUpload: async (file) => {
const data = await uploadToCloudinary(file, 'video');
return {
src: data.secure_url,
alt: 'cloudinary',
sizes: {
width: data.width,
height: data.height,
},
};
},
},
}),
File.extend({
options: {
onUpload: async (file) => {
const response = await uploadToCloudinary(file, 'auto');
return { src: response.url };
},
},
}),
];

const TOOLS = {
ActionMenu: {
render: DefaultActionMenuRender,
tool: ActionMenuList,
},
Toolbar: {
render: DefaultToolbarRender,
tool: Toolbar,
},
LinkTool: {
render: DefaultLinkToolRender,
tool: LinkTool,
},
};

const MARKS = [Bold, Italic, CodeMark, Underline, Strike, Highlight];

function WithEditorFocusBlur() {
const editor = useMemo(() => createYooptaEditor(), []);
const selectionRef = useRef(null);

return (
<div
className="md:py-[100px] md:pl-[200px] md:pr-[80px] px-[20px] pt-[80px] pb-[40px] flex justify-center flex-col items-center"
ref={selectionRef}
>
<YooptaEditor
editor={editor}
plugins={plugins}
tools={TOOLS}
marks={MARKS}
autoFocus={false}
selectionBoxRoot={selectionRef}
>
<FocusButtons />
<Placeholder />
</YooptaEditor>
</div>
);
}

const FocusButtons = () => {
const editor = useYooptaEditor();

return (
<div className="md:top-2 md:right-2 top-[14.5px] right-[18px] flex z-50">
<button
type="button"
className="relative mx-2 px-3 py-1 text-[14px] rounded text-white flex items-center bg-[#000]"
onClick={() => editor.focus()}
>
Focus
</button>
<button
type="button"
className="relative mx-2 px-3 py-1 text-[14px] rounded text-white flex items-center bg-[#000]"
onClick={() => editor.blur()}
>
Blur
</button>
</div>
);
};

const Placeholder = () => {
const isFocused = useYooptaFocused();

if (!isFocused) {
return (
<div className="fixed md:py-[100px] md:pl-[200px] md:pr-[80px] px-[20px] pt-[80px] pb-[40px]">Placeholder</div>
);
}

return null;
};

export default WithEditorFocusBlur;
Loading

0 comments on commit c4cd786

Please sign in to comment.