Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zbeyens committed Dec 19, 2024
1 parent c196f0e commit 1d2fba3
Show file tree
Hide file tree
Showing 63 changed files with 221 additions and 257 deletions.
9 changes: 3 additions & 6 deletions packages/caption/src/react/components/CaptionButton.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
findPath,
useEditorRef,
useElement,
} from '@udecode/plate-common/react';
import { findNodePath } from '@udecode/plate-common';
import { useEditorRef, useElement } from '@udecode/plate-common/react';

import { BaseCaptionPlugin } from '../../lib';

Expand All @@ -20,7 +17,7 @@ export const useCaptionButton = ({
return {
props: {
onClick: () => {
const path = findPath(editor, element);
const path = findNodePath(editor, element);
editor.setOption(BaseCaptionPlugin, 'visibleId', element.id as string);
setTimeout(() => {
path && editor.setOption(BaseCaptionPlugin, 'focusEndPath', path);
Expand Down
68 changes: 30 additions & 38 deletions packages/caption/src/react/components/CaptionTextarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
getNodeString,
getPointAfter,
isHotkey,
setNodes,
} from '@udecode/plate-common';
import {
createPrimitiveComponent,
findPath,
focusEditor,
setNode,
useEditorRef,
useElement,
} from '@udecode/plate-common/react';
Expand Down Expand Up @@ -49,59 +49,51 @@ export const useCaptionTextareaState = () => {
const element = useElement<TCaptionElement>();
const editor = useEditorRef();

const [isComposing, setIsComposing] = useState(false)
const [isComposing, setIsComposing] = useState(false);

const [captionValue, setCaptionValue] = useState<
TextareaAutosizeProps['value']
>(() => {
const nodeCaption =
element.caption ?? ([{ children: [{ text: '' }] }] as [TElement])
return getNodeString(nodeCaption[0])
})
element.caption ?? ([{ children: [{ text: '' }] }] as [TElement]);

return getNodeString(nodeCaption[0]);
});

const updateEditorCaptionValue = useCallback(
(newValue: string) => {
const path = findPath(editor, element)
if (!path) {
return
}

setNodes<TCaptionElement>(
editor,
{ caption: [{ text: newValue }] },
{ at: path },
)
setNode<TCaptionElement>(editor, element, {
caption: [{ text: newValue }],
});
},
[editor, element],
)
[editor, element]
);

const handleChange = useCallback(
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
const newValue = e.target.value
setCaptionValue(newValue)
const newValue = e.target.value;
setCaptionValue(newValue);

if (!isComposing) {
updateEditorCaptionValue(newValue)
updateEditorCaptionValue(newValue);
}
},
[isComposing, updateEditorCaptionValue],
)
[isComposing, updateEditorCaptionValue]
);

const handleCompositionStart = useCallback(() => {
setIsComposing(true)
}, [])
setIsComposing(true);
}, []);

const handleCompositionEnd = useCallback(
(e: React.CompositionEvent<HTMLTextAreaElement>) => {
setIsComposing(false)
const newValue = e.currentTarget.value
setCaptionValue(newValue)
updateEditorCaptionValue(newValue)
setIsComposing(false);
const newValue = e.currentTarget.value;
setCaptionValue(newValue);
updateEditorCaptionValue(newValue);
},
[updateEditorCaptionValue],
)


[updateEditorCaptionValue]
);

const readOnly = useReadOnly();

Expand All @@ -113,21 +105,21 @@ export const useCaptionTextareaState = () => {
captionValue,
element,
readOnly,
textareaRef,
handleChange,
handleCompositionStart,
handleCompositionEnd,
textareaRef,
handleCompositionStart,
};
};

export const useCaptionTextarea = ({
captionValue,
element,
readOnly,
textareaRef,
handleChange,
handleCompositionStart,
handleCompositionEnd,
textareaRef,
handleCompositionStart,
}: ReturnType<typeof useCaptionTextareaState>) => {
const editor = useEditorRef();

Expand Down Expand Up @@ -171,10 +163,10 @@ export const useCaptionTextarea = ({
readOnly,
value: captionValue,
onBlur,
onKeyDown,
onChange: handleChange,
onCompositionStart: handleCompositionStart,
onCompositionEnd: handleCompositionEnd,
onCompositionStart: handleCompositionStart,
onKeyDown,
},
ref: textareaRef,
};
Expand Down
11 changes: 2 additions & 9 deletions packages/code-block/src/react/hooks/useCodeBlockCombobox.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import React from 'react';

import { setNodes } from '@udecode/plate-common';
import {
findPath,
useEditorRef,
useElement,
} from '@udecode/plate-common/react';
import { setNode, useEditorRef, useElement } from '@udecode/plate-common/react';
import { useReadOnly } from 'slate-react';

import { type TCodeBlockElement, BaseCodeBlockPlugin } from '../../lib';
Expand Down Expand Up @@ -40,9 +35,7 @@ export const useCodeBlockCombobox = ({
return {
commandItemProps: {
onSelect: (_value: string) => {
const path = findPath(editor, element);
path &&
setNodes<TCodeBlockElement>(editor, { lang: _value }, { at: path });
setNode<TCodeBlockElement>(editor, element, { lang: _value });
setValue(_value);
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const useExcalidrawElement = ({
scrollToContent,
},
// onChange: (elements: readonly ExcalidrawElementType[], state: AppState) => {
// const path = findPath(editor, element);
// const path = findNodePath(editor, element);

// FIXME: setNodes triggers render loop as onChange is triggered on rerender
// in the meantime, the prop can be used to save the data outside slate
Expand Down
4 changes: 2 additions & 2 deletions packages/indent-list/src/react/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* @file Automatically generated by barrelsby.
*/

export * from './someIndentList';
export * from './someIndentTodo';
export * from '../../lib/queries/someIndentList';
export * from '../../lib/queries/someIndentTodo';
export * from './useIndentListToolbarButton';
export * from './useIndentTodoListElement';
export * from './useIndentTodoToolbarButton';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEditorRef, useEditorSelector } from '@udecode/plate-common/react';

import { ListStyleType, toggleIndentList } from '../../index';
import { someIndentList } from './someIndentList';
import { someIndentList } from '../../lib/queries/someIndentList';

export const useIndentListToolbarButtonState = ({
nodeType = ListStyleType.Disc,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type TElement, setNodes } from '@udecode/plate-common';
import { findPath, useEditorRef } from '@udecode/plate-common/react';
import { type TElement, findNodePath, setNodes } from '@udecode/plate-common';
import { useEditorRef } from '@udecode/plate-common/react';
import { useReadOnly } from 'slate-react';

export const useIndentTodoListElementState = ({
Expand Down Expand Up @@ -30,7 +30,7 @@ export const useIndentTodoListElement = (
onCheckedChange: (value: boolean) => {
if (readOnly) return;

const path = findPath(editor, element);
const path = findNodePath(editor, element);

if (!path) return;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEditorRef, useEditorSelector } from '@udecode/plate-common/react';

import { ListStyleType, toggleIndentList } from '../../index';
import { someIndentTodo } from './someIndentTodo';
import { someIndentTodo } from '../../lib/queries/someIndentTodo';

export const useIndentTodoToolBarButtonState = ({
nodeType = ListStyleType.Disc,
Expand Down
15 changes: 2 additions & 13 deletions packages/list/src/react/hooks/useTodoListElement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { setNodes } from '@udecode/plate-common';
import { findPath, useEditorRef } from '@udecode/plate-common/react';
import { setNode, useEditorRef } from '@udecode/plate-common/react';
import { useReadOnly } from 'slate-react';

import type { TTodoListItemElement } from '../../lib';
Expand Down Expand Up @@ -33,17 +32,7 @@ export const useTodoListElement = (
onCheckedChange: (value: boolean) => {
if (readOnly) return;

const path = findPath(editor, element);

if (!path) return;

setNodes<TTodoListItemElement>(
editor,
{ checked: value },
{
at: path,
}
);
setNode<TTodoListItemElement>(editor, element, { checked: value });
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/slate-react/src/react-editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export * from './deselectEditor';
export * from './findEditorDocumentOrShadowRoot';
export * from './findEventRange';
export * from './findNodeKey';
export * from './findNodePath';
export * from './findPath';
export * from './focusEditor';
export * from './getEditorWindow';
export * from './hasEditorDOMNode';
Expand Down
4 changes: 3 additions & 1 deletion packages/slate/src/queries/findNodePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export const findNodePath = <E extends TEditor = TEditor>(
node: TNode,
options: FindNodeOptions<E> = {}
) => {
const { match } = getQueryOptions(editor, options);

const nodeEntry = findNode(editor, {
at: [],
match: (n) => n === node && getQueryOptions(editor, options).match?.(n),
match: (n) => n === node && (!match || match(n)),
...options,
});

Expand Down
1 change: 1 addition & 0 deletions packages/slate/src/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

export * from './addRangeMarks';
export * from './setElements';
export * from './setNode';
export * from './unhangCharacterRange';
24 changes: 24 additions & 0 deletions packages/slate/src/transforms/setNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type {
NodeOf,
SetNodesOptions,
TEditor,
TNodeProps,
} from '../interfaces';

import { findNodePath } from '../queries';

export const setNode = <N extends NodeOf<E>, E extends TEditor = TEditor>(
editor: E,
node: N,
props: Partial<TNodeProps<N>>,
options?: Omit<SetNodesOptions<E>, 'at'>
) => {
const path = findNodePath(editor, node);

if (!path) return;

editor.setNodes(props, {
...options,
at: path,
} as any);
};
2 changes: 1 addition & 1 deletion packages/suggestion/src/lib/withSuggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export const withSuggestion: ExtendEditor<SuggestionConfig> = ({
// return;
// }
//
// const path = findPath(editor, node);
// const path = findNodePath(editor, node);
// if (!path) return;
//
// const id = findSuggestionId(editor, path) ?? nanoid();
Expand Down
7 changes: 7 additions & 0 deletions packages/table/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
export * from './BaseTablePlugin';
export * from './constants';
export * from './types';
export * from './withDeleteTable';
export * from './withGetFragmentTable';
export * from './withInsertFragmentTable';
export * from './withInsertTextTable';
export * from './withMarkTable';
export * from './withNormalizeTable';
export * from './withSelectionTable';
export * from './withSetFragmentDataTable';
export * from './withTable';
export * from './merge/index';
export * from './queries/index';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import {
getCellPath,
getCellTypes,
getColSpan,
} from '../../lib';
import { TablePlugin } from '../TablePlugin';
} from '..';
import { BaseTablePlugin } from '../BaseTablePlugin';
import { deleteColumnWhenExpanded } from './deleteColumnWhenExpanded';

export const deleteTableMergeColumn = (editor: SlateEditor) => {
const { getOptions, type } = getEditorPlugin(editor, TablePlugin);
const { getOptions, type } = getEditorPlugin(editor, BaseTablePlugin);

if (
someNode(editor, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@udecode/plate-common';
import { type PathRef, Node, Range } from 'slate';

import { type TTableCellElement, BaseTableRowPlugin } from '../../lib';
import { type TTableCellElement, BaseTableRowPlugin } from '..';
import { getTableGridAbove } from '../queries';

export const deleteColumnWhenExpanded = (
Expand Down
Loading

0 comments on commit 1d2fba3

Please sign in to comment.