简体   繁体   English

如何在 draft.js 中设置默认字体系列和大小

[英]How to set default font family and size in draft.js

I am learning draft.js editor and can't find how to configure default font-family and font size.我正在学习 draft.js 编辑器,找不到如何配置默认字体系列和字体大小。

I tried this way:我试过这种方式:

let editorState = EditorState.createEmpty();

let newState = RichUtils.toggleBlockType(
    editorState,
    'aligncenter'
);

newState = RichUtils.toggleInlineStyle(
    newState,
    'FONT_SIZE_36'
);
newState = RichUtils.toggleInlineStyle(
    newState,
    'TIMES_NEW_ROMAN'
);

What is weird, aligncenter style works fine, but font size and family disappears when component gets focus.奇怪的是, aligncenter样式工作正常,但是当组件获得焦点时字体大小系列消失。

Can you please suggest correct way how to do it?你能建议正确的方法吗?

Using RichUtils.toggleInlineStyle() is for modifying the currently selected range of text (or setting the inline style for text that will be entered at the current cursor position). 使用RichUtils.toggleInlineStyle()可以修改当前选择的文本范围(或为将在当前光标位置输入的文本设置行内样式)。 There is not a way to use this to set the default inline styles for the entire document (nor is this recommended). 无法使用此方法为整个文档设置默认的内联样式(也不建议这样做)。

To get default styles, you should use CSS and set the styles you want for the entire editor. 要获取默认样式,应使用CSS并为整个编辑器设置所需的样式。 Then you should override those styles for specific text ranges using toggleInlineStyle when the user wants a non-default style (for instance selecting font-size from a dropdown). 然后,当用户需要非默认样式(例如,从下拉列表中选择字体大小)时,应使用toggleInlineStyle覆盖特定文本范围的样式。

Here's the catch. 抓住了。 Currently you can pre-define these inline styles using styleMap but you can't really create them on-the-fly as a user chooses an arbitrary font-family (or size or color). 当前,您可以使用styleMap预先定义这些内联样式,但是当用户选择任意字体系列(或大小或颜色)时,您实际上无法即时创建它们。

I have been struggling with this also while trying to implement a color-picker for react-rte.org . 在尝试为react-rte.org实现颜色选择器时,我也一直在为此作斗争

(Technically, you can add styles on the fly, but it won't trigger a re-render, so that's not really usable.) (从技术上讲,您可以即时添加样式,但不会触发重新渲染,因此实际上并不可用。)

There is an open issue for this here: https://github.com/facebook/draft-js/issues/52 这里有一个开放的问题: https : //github.com/facebook/draft-js/issues/52

I expect this will be resolved within a week or so and I can edit this answer with example code to accomplish what you're after. 我希望这将在一周左右的时间内得到解决,并且我可以使用示例代码来编辑此答案,以完成您的工作。

if your trying to set the default font size in draft.js Editor just set up your component like this below如果您尝试在 draft.js 编辑器中设置默认字体大小,只需像下面这样设置您的组件

notice the div that is wrapped around the Editor, EmojiSuggestions, and mentionSuggestion components.请注意环绕在 Editor、EmojiSuggestions 和 mentionSuggestion 组件周围的 div。 Just set the className for the editor to your font size.只需将编辑器的类名设置为您的字体大小。 in my case it is fs-1.在我的例子中是 fs-1。 Note I have added an editorStyles.editor class that is coming from an attached scss file.请注意,我添加了一个来自附加 scss 文件的 editorStyles.editor class。 This contains some scss for the editor.这包含一些编辑器的 scss。

Here is what the scss looks like, no need to add this though if you are just trying to edit the default font style这是 scss 的样子,如果您只是想编辑默认字体样式,则无需添加它

Just showing this, but not needed to set default font size.只是显示这个,但不需要设置默认字体大小。 That is done in div wrapper这是在 div 包装器中完成的

.editor {
  box-sizing: border-box;
  border: 1px solid #ddd;
  cursor: text;
  padding: 16px;
  border-radius: 2px;
  margin-bottom: 9px;
  box-shadow: inset 0px 1px 8px -3px #ABABAB;
  background: #fefefe;
}

.editor :global(.public-DraftEditor-content) {
  min-height: 140px;
}

.mention {
  color: #2c7be5
}



 <div 
              style={{minHeight: "7em", maxHeight: "10em", overflow: "auto"}}
              className={`border border-2x border-300 bg-light rounded-soft fs-1 ${editorStyles.editor}` }
              onClick={() => { messageFieldRef.current.focus(); }}
            >
            <Editor
              editorKey={'editor'}
              currentContent={ContentState}
              editorState={tempEditorState ? tempEditorState : editorState}
              onChange={setEditorState}
              plugins={plugins}
              ref={messageFieldRef}
            />
            <EmojiSuggestions />
            <MentionSuggestions
              open={open}
              onOpenChange={onOpenChange}
              suggestions={suggestions}
              onSearchChange={onSearchChange}
              onAddMention={(e) => {// get the mention object selected
              }}
              entryComponent={Entry}
              />
            </div>
            <div>
                <EmojiSelect closeOnEmojiSelect />
              <span color="light" className="px-3 py-1 bg-soft-info rounded-capsule shadow-none fs--1 ml-3" >
                <FontAwesomeIcon icon="tags" transform="left-3"/>
                 Press <strong>@</strong>  while typing to insert custom fields
              </span>
            </div>


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM