描述
仅在 Firefox 中的 Slatejs 编辑器因文本选择和插入符号定位(通过鼠标)而失败。它总是选择第一个字符之前的位置(路径 [0] 偏移 [0])。您仍然可以使用键盘来选择文本和定位插入符号。
记录
期待
它应该像在其他浏览器中一样选择文本和位置插入符号。
环境
- 石板版本:0.63.0
- 操作系统:Windows 10
- 浏览器:Firefox 90.0.1(64 位)
编辑器实现供参考
import React, { useCallback, useMemo, useState } from "react";
import {
BaseEditor,
createEditor,
Descendant,
Editor,
Element as SlateElement,
Node,
Text,
Transforms
} from "slate";
import { Slate, Editable, ReactEditor, withReact } from "slate-react";
import { HistoryEditor, withHistory } from "slate-history";
export type CustomEditor = BaseEditor & ReactEditor & HistoryEditor
export type ParagraphElement = {
type: 'paragraph'
children: CustomText[]
}
export type TitleElement = {
type: "title"
children: CustomText[]
}
type CustomElement = ParagraphElement | TitleElement;
type FormattedText = { text: string, bold?: true };
type CustomText = FormattedText;
declare module "slate" {
interface CustomTypes {
Editor: CustomEditor
Element: CustomElement
Text: FormattedText
}
}
////////////////////////////////////
// Custom helpers
////////////////////////////////////
const customEditor = {
isBoldMarkActive(editor: CustomEditor) {
const [match] = Editor.nodes(editor, {
match: (n: any) => n.bold === true,
universal: true,
})
return !!match
},
isTitleActive(editor: CustomEditor) {
const [match] = Editor.nodes(editor, {
match: (n: any) => n.type === "title",
})
return !!match
},
toggleBoldMark(editor: CustomEditor) {
const isActive = customEditor.isBoldMarkActive(editor)
Transforms.setNodes(
editor,
{ bold: isActive ? undefined : true },
{ match: n => Text.isText(n), split: true }
)
},
toggleTitle(editor: CustomEditor) {
const isActive = customEditor.isTitleActive(editor)
Transforms.setNodes(
editor,
{ type: isActive ? undefined : "title" },
{ match: n => Editor.isBlock(editor, n) }
)
},
}
////////////////////////////////////
// Forced layout setup - title + paragraph
////////////////////////////////////
const withLayout = (editor: CustomEditor) => {
const { normalizeNode } = editor
editor.normalizeNode = ([node, path]) => {
if (path.length === 0) {
if (editor.children.length < 1) {
const title: TitleElement = {
type: "title",
children: [{ text: 'Untitled' }],
}
Transforms.insertNodes(editor, title, { at: path.concat(0) })
}
if (editor.children.length < 2) {
const paragraph: ParagraphElement = {
type: 'paragraph',
children: [{ text: '' }],
}
Transforms.insertNodes(editor, paragraph, { at: path.concat(1) })
}
for (const [child, childPath] of Node.children(editor, path)) {
const type = childPath[0] === 0 ? "title" : 'paragraph'
if (SlateElement.isElement(child) && child.type !== type) {
const newProperties: Partial<SlateElement> = { type }
Transforms.setNodes(editor, newProperties, { at: childPath })
}
}
}
return normalizeNode([node, path]);
}
return editor;
}
////////////////////////////////////
const TextEditor = () => {
const initialValue: Descendant[] = [
{
type: 'title',
children: [{ text: 'Enter a title...' }],
},
{
type: 'paragraph',
children: [{ text: 'Enter your question'}]
}
];
const editor = useMemo(() => withLayout(withHistory(withReact(createEditor()))), []);
Transforms.deselect(editor);
const [value, setValue] = useState<Descendant[]>(initialValue);
const renderElement = useCallback((props) => <Element {...props} />, [])
// Define a leaf rendering function that is memoized with `useCallback`.
const renderLeaf = useCallback((props) => {
return <Leaf {...props} />
}, []);
return (
// Add the editable component inside the context.
<Slate
editor={editor}
value={value}
onChange={(value) => setValue(value)}
>
<div>
<button
onMouseDown={event => {
event.preventDefault()
customEditor.toggleBoldMark(editor)
}}
>
B
</button>
<button
onMouseDown={event => {
event.preventDefault()
customEditor.toggleTitle(editor)
}}
>
H2
</button>
</div>
<Editable
renderElement={renderElement}
autoFocus
renderLeaf={renderLeaf}
/>
</Slate>
)
}
export default TextEditor
// Define a React component to render leaves with bold text.
const Leaf = (props: any) => {
return (
<span
{...props.attributes}
style={{ fontWeight: props.leaf.bold ? 'bold' : 'normal' }}
>
{props.children}
</span>
)
}
const Element = ({ attributes, children, element }: any) => {
switch (element.type) {
case 'title':
return <h2 {...attributes}>{children}</h2>
case 'paragraph':
return <p {...attributes}>{children}</p>
default:
return <p {...attributes}>{children}</p>
}
}
任何想法可能是什么原因造成的?