- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Reactjs 和 draftjs 库创建富文本编辑器。我想自动大写句子的第一个字母。
import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';
class MyEditor extends React.Component {
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.onChange = (editorState) => this.setState({editorState});
}
render() {
return (
<Editor editorState={this.state.editorState} onChange={this.onChange}
autoCapitalize="sentences"/>
);
}
}
ReactDOM.render(
<MyEditor />,
document.getElementById('container')
);
我创建的组件不会在句点后自动大写。为什么自动大写不起作用?有没有更好的方法让它发挥作用?
最佳答案
我只是发现自己在 Draft.js 中遇到了同样的情况:需要实现自动大写,但发现内置的 Draft prop 没有任何作用,所以我自己制定了方法来实现这一点。
这是我创建的一个实用函数,用于查询 Draft.js 中光标前的字符:
const getCharsPrecedingFocus = (
contentState: any,
selectionState: any,
numberOfChars: number,
): string => {
const currentBlock =
contentState.getBlockForKey(selectionState.getAnchorKey());
const currentBlockText = currentBlock.getText();
const endOfText = selectionState.getEndOffset();
return currentBlockText.slice(
endOfText - numberOfChars,
endOfText,
);
};
现在我们可以检查新句子开头是否有一个字符应该大写。将此代码放入作为 Prop 传递的 handleBeforeInput
方法中:
const handleBeforeInput = (chars: string, editorState: any) => {
const NON_CAPITALISED_REGEX = /^(?:\.\s)([a-z])$/;
if (NON_CAPITALISED_REGEX.test(precedingThreeChars)) {
const autoCapitaliseMatch =
precedingThreeChars.match(NON_CAPITALISED_REGEX);
if (autoCapitaliseMatch && autoCapitaliseMatch[1]) {
const capitalisedChar = autoCapitaliseMatch[1].toUpperCase();
const contentStateWithCapitalisedChar = Modifier.replaceText(
contentState,
selectionState.merge({
anchorOffset: focusOffset - 1,
focusOffset: focusOffset,
}),
capitalisedChar + chars,
);
EditorState.push(
editorState,
contentStateWithCapitalisedChar,
'insert-characters'
);
return 'handled';
}
}
}
一些其他注意事项:
我使用 test
RegEx 方法来检查匹配自动大写模式的字符串,而不是 match
,因为这是一个每次都会调用的操作用户按下一个键,test
应该比 match
更快。尽管它可能不会对用户体验产生影响,除非您处理的是非常长的文档。
*编辑:在我在这里发布的原始代码中,我忘记定义precedingThreeChars
。只需像这样添加 const precedingThreeChars =
getCharsPrecedingFocus(contentState, selectionState, 3);
关于javascript - 使用 draft-js 自动大写句子的第一个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47649511/
在 Gmail API 中,drafts.list 经常会返回草稿 ID但是当我将它传递给 drafts.get 时,它会返回 404 错误。这是对于某些草稿 ID 可重复,我可以再次调用 draft
我正在尝试将调用组件中的文本更新到编辑器组件中。我使用 props 来传递来自调用者的文本,但是当文本更改时(在 ContentEditor 中更新了 props),Editor 组件中的文本不是:
我正在使用 MFMailComposeViewController Controller ,如下所示: MFMailComposeViewController *picker1 = [[MFMailC
如果我想处理字符*的输入,我可以使用handleBeforeInput(str): handleBeforeInput(str) { if (str !== '*') { retu
使用下面的代码我得到纯文本但我想要格式化文本点击按钮 {alert(this.state.editorState.getCurrentContent().getPlainText());}} typ
如何添加链接?我知道如何添加链接到 选择 const contentState = editorState.getCurrentContent();
我的draft.js 填充 body带有文本,例如:'{"blocks":[{"key":"3mont","text":"lorem ipsum","type":"unstyled","depth":
我在我的应用程序中使用基于草稿js的react-draft-wysiwyg文本编辑器。我在尝试通过编辑器图像控件上传图像时遇到一些问题。我正在使用自定义 FileUploader 组件。它将图像保存在
我有一个由 Draft.js 提供的编辑器的包装器,我想让 tab/shift-tab 键像它们应该用于 UL 和 OL 一样工作。我定义了以下方法: _onChange(editorState)
我关注了draft-js document创建一个非常简单的 Draft-js 演示。 一切似乎都运行良好,但是当我在 Chrome 中打开网址时,我只能看到一个白色的空白页面(那里有一个隐藏的编辑器
如何测试draftjs的内容编辑器为空? 我现在唯一的想法是与从此函数返回的对象进行对象比较:EditorState.createEmpty().getCurrentContent() 最佳答案 只需
我有一个简单的用例:有一个 DraftEditor 组件,它采用 value作为它的 Prop 并基于value创建一个编辑器状态(空的或有内容的)。 value 可能被父级更改,当它发生时,我希望草
我尝试根据主 draft.js 页面上的示例创建基本草稿 js 编辑器 https://draftjs.org/ . 当我在主页面的编辑器中单击切换了 ol/ul 的选项卡时,我得到了嵌套列表。但是在
我有一个需要多个文本输入的应用程序,为了格式化和自定义,我选择了 draft-js 作为我的编辑器,但是我遇到了一个非常令人困惑的输入问题。 当我在编辑器中输入时,我最近按下的键被打印在编辑器的开头,
问题是:如何将 draft-js 内容保存为 html,然后在页面上呈现内容(此时是 html 字符串)。 我想分享我学到的东西。 请在解决方案中找到一种使用 draft.js 保存和呈现内容的方法。
我正在做一个 React 项目,我们将在其中使用大量表单,包括具有丰富 HTML 的字段。我搜索了一会儿,偶然发现了 draft-js。看起来很好,但是我似乎找不到任何从头到尾如何使用它的好例子。我发
如何确保我的 UI 上唯一的 draft-js 文本框在页面加载时自动聚焦? 有一个记录的方法叫做 focus ,但是我不确定实现所需的语法。 最佳答案 如果您的编辑器是用一些默认值启动的,那么只需执
试图学习如何在我自己的应用程序中使用 DraftJS React 组件,但我遇到了一个大问题。我已经按照位于 here. 的示例进行操作 我使用 create-react-app 获取基础样板文件,我
我在一个项目中将 Draft JS 作为一个简单的编辑器实现了,但是我在设置无序列表的样式时遇到了问题,特别是更改项目符号的颜色以匹配文本颜色。 文档中似乎没有关于如何将样式应用于包装 unorder
我正在尝试将 Draft.js 与图像插件一起使用。这是我的问题。我设法让它工作,但没有加载样式,编辑器占据了整个页面,按钮没有样式。 我从提供的 CSS 加载样式 import './Draft.c
我是一名优秀的程序员,十分优秀!