Spaces:
Sleeping
Sleeping
File size: 4,921 Bytes
60f878e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import { createEditor, type Descendant, Range, Transforms } from 'slate';
import { isKeyHotkey } from 'is-hotkey';
import {
Editable,
RenderElementProps,
RenderLeafProps,
Slate,
withReact,
} from 'slate-react';
import {
type CustomElement,
type CustomText,
type LinkElement,
type ParagraphElement,
type TextProps,
} from '@plait/common';
import React, { useMemo, useCallback, useEffect, CSSProperties } from 'react';
import { withHistory } from 'slate-history';
import { isUrl, LinkEditor } from '@plait/text-plugins';
import { withText } from './plugins/with-text';
import { CustomEditor, RenderElementPropsFor } from './custom-types';
import './styles/index.scss';
import { LinkComponent, withInlineLink } from './plugins/with-link';
export type TextComponentProps = TextProps;
export const Text: React.FC<TextComponentProps> = (
props: TextComponentProps
) => {
const { text, readonly, onChange, onComposition, afterInit } = props;
const renderLeaf = useCallback(
(props: RenderLeafProps) => <Leaf {...props} />,
[]
);
const initialValue: Descendant[] = [text];
const editor = useMemo(() => {
const editor = withInlineLink(
withText(withHistory(withReact(createEditor())))
);
afterInit && afterInit(editor);
return editor;
}, []);
useEffect(() => {
if (text === editor.children[0]) {
return;
}
editor.children = [text];
editor.onChange();
}, [text, editor]);
const onKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (event) => {
const { selection } = editor;
// Default left/right behavior is unit:'character'.
// This fails to distinguish between two cursor positions, such as
// <inline>foo<cursor/></inline> vs <inline>foo</inline><cursor/>.
// Here we modify the behavior to unit:'offset'.
// This lets the user step into and out of the inline without stepping over characters.
// You may wish to customize this further to only use unit:'offset' in specific cases.
if (selection && Range.isCollapsed(selection)) {
const { nativeEvent } = event;
if (isKeyHotkey('left', nativeEvent)) {
event.preventDefault();
Transforms.move(editor, { unit: 'offset', reverse: true });
return;
}
if (isKeyHotkey('right', nativeEvent)) {
event.preventDefault();
Transforms.move(editor, { unit: 'offset' });
return;
}
}
};
return (
<Slate
editor={editor}
initialValue={initialValue}
onChange={(value: Descendant[]) => {
onChange &&
onChange({
newText: editor.children[0] as ParagraphElement,
operations: editor.operations,
});
}}
>
<Editable
className="slate-editable-container plait-text-container"
renderElement={(props) => <Element {...props} />}
renderLeaf={renderLeaf}
readOnly={readonly === undefined ? true : readonly}
onCompositionStart={(event) => {
if (onComposition) {
onComposition(event as unknown as CompositionEvent);
}
}}
onCompositionUpdate={(event) => {
if (onComposition) {
onComposition(event as unknown as CompositionEvent);
}
}}
onCompositionEnd={(event) => {
if (onComposition) {
onComposition(event as unknown as CompositionEvent);
}
}}
onKeyDown={onKeyDown}
/>
</Slate>
);
};
const Element = (props: RenderElementProps) => {
const { attributes, children, element } = props as RenderElementPropsFor<
CustomElement & { type: string }
>;
switch (element.type) {
case 'link':
return (
<LinkComponent {...(props as RenderElementPropsFor<LinkElement>)} />
);
default:
return (
<ParagraphComponent
{...(props as RenderElementPropsFor<ParagraphElement>)}
/>
);
}
};
const ParagraphComponent = ({
attributes,
children,
element,
}: RenderElementPropsFor<ParagraphElement>) => {
const style = { textAlign: element.align } as CSSProperties;
return (
<div style={style} {...attributes}>
{children}
</div>
);
};
const Leaf: React.FC<RenderLeafProps> = ({ children, leaf, attributes }) => {
if ((leaf as CustomText).bold) {
children = <strong>{children}</strong>;
}
if ((leaf as CustomText).code) {
children = <code>{children}</code>;
}
if ((leaf as CustomText).italic) {
children = <em>{children}</em>;
}
if ((leaf as CustomText).underlined) {
children = <u>{children}</u>;
}
return (
<span style={{ color: (leaf as CustomText).color }} {...attributes}>
{children}
</span>
);
};
|