Skip to content

Commit

Permalink
feat: saves cursor position when relationship element is added to ric…
Browse files Browse the repository at this point in the history
…hText
  • Loading branch information
jmikrut committed Mar 25, 2021
1 parent 39000bd commit d24b3f7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
21 changes: 13 additions & 8 deletions demo/client/components/richText/elements/Button/Button/index.tsx
@@ -1,8 +1,7 @@
import React, { Fragment, useCallback } from 'react';
import PropTypes from 'prop-types';
import { Modal, useModal } from '@faceless-ui/modal';
import { Transforms } from 'slate';
import { useSlate } from 'slate-react';
import { useSlate, ReactEditor } from 'slate-react';
import MinimalTemplate from '../../../../../../../src/admin/components/templates/Minimal';
import { ElementButton } from '../../../../../../../components/rich-text';
import X from '../../../../../../../src/admin/components/icons/X';
Expand All @@ -17,7 +16,7 @@ const initialFormData = {
style: 'primary',
};

const insertButton = (editor, { href, label, style, newTab = false }) => {
const insertButton = (editor, { href, label, style, newTab = false }: any) => {
const text = { text: ' ' };
const button = {
type: 'button',
Expand All @@ -32,10 +31,20 @@ const insertButton = (editor, { href, label, style, newTab = false }) => {

const nodes = [button, { children: [{ text: '' }] }];

if (editor.blurSelection) {
Transforms.select(editor, editor.blurSelection);
}

Transforms.insertNodes(editor, nodes);

const currentPath = editor.selection.anchor.path[0];
const newSelection = { anchor: { path: [currentPath + 1, 0], offset: 0 }, focus: { path: [currentPath + 1, 0], offset: 0 } };

Transforms.select(editor, newSelection);
ReactEditor.focus(editor);
};

const ToolbarButton: React.FC = ({ path }) => {
const ToolbarButton: React.FC<{path: string}> = ({ path }) => {
const { open, closeAll } = useModal();
const editor = useSlate();

Expand Down Expand Up @@ -112,8 +121,4 @@ const ToolbarButton: React.FC = ({ path }) => {
);
};

ToolbarButton.propTypes = {
path: PropTypes.string.isRequired,
};

export default ToolbarButton;
5 changes: 5 additions & 0 deletions src/admin/components/forms/field-types/RichText/RichText.tsx
Expand Up @@ -126,6 +126,10 @@ const RichText: React.FC<Props> = (props) => {
return CreatedEditor;
}, [elements, leaves]);

const onBlur = useCallback(() => {
editor.blurSelection = editor.selection;
}, [editor]);

useEffect(() => {
if (!loaded) {
const mergedElements = mergeCustomFunctions(elements, elementTypes);
Expand Down Expand Up @@ -222,6 +226,7 @@ const RichText: React.FC<Props> = (props) => {
placeholder={placeholder}
spellCheck
readOnly={readOnly}
onBlur={onBlur}
onKeyDown={(event) => {
Object.keys(hotkeys).forEach((hotkey) => {
if (isHotkey(hotkey, event as any)) {
Expand Down
@@ -1,7 +1,7 @@
import React, { Fragment, useCallback, useState } from 'react';
import { Modal, useModal } from '@faceless-ui/modal';
import { Transforms } from 'slate';
import { useSlate } from 'slate-react';
import { ReactEditor, useSlate } from 'slate-react';
import { useConfig } from '@payloadcms/config-provider';
import ElementButton from '../../Button';
import RelationshipIcon from '../../../../../../icons/Relationship';
Expand Down Expand Up @@ -36,7 +36,17 @@ const insertRelationship = (editor, { value, relationTo, depth }) => {

const nodes = [relationship, { children: [{ text: '' }] }];

if (editor.blurSelection) {
Transforms.select(editor, editor.blurSelection);
}

Transforms.insertNodes(editor, nodes);

const currentPath = editor.selection.anchor.path[0];
const newSelection = { anchor: { path: [currentPath + 1, 0], offset: 0 }, focus: { path: [currentPath + 1, 0], offset: 0 } };

Transforms.select(editor, newSelection);
ReactEditor.focus(editor);
};

const RelationshipButton = ({ path }) => {
Expand Down

0 comments on commit d24b3f7

Please sign in to comment.